Created
February 27, 2023 16:34
-
-
Save 0jovi0/4b5acab2b95e740af059121b7519e2b7 to your computer and use it in GitHub Desktop.
Gas optimization Homework - Encode Expert Solidity Bootcamp - Group 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity 0.8.4; | |
error Unauthorized(); | |
error ZeroAddress(); | |
error InsufficientBalance(); | |
error LowAmount(); | |
contract GasContract { | |
uint256 public immutable totalSupply; // cannot be updated (will be only in contructor) | |
address public immutable owner; | |
// uint256 public paymentCounter = 0; | |
mapping(address => uint256) public balances; | |
mapping(address => Payment[]) public payments; | |
mapping(address => uint8) public whitelist; | |
address[5] public administrators; | |
enum PaymentType { | |
Unknown, | |
BasicPayment, | |
Refund, | |
Dividend, | |
GroupPayment | |
} | |
struct Payment { | |
uint256 amount; | |
uint256 paymentID; | |
PaymentType paymentType; | |
} | |
struct ImportantStruct { | |
uint256 bigValue; | |
uint128 valueA; // max 3 digits | |
uint128 valueB; // max 3 digits | |
} | |
event Transfer(address recipient, uint256 amount); | |
constructor(address[] memory _admins, uint256 _totalSupply) { | |
owner = msg.sender; | |
totalSupply = _totalSupply; | |
assembly{ | |
for {let ii := 0} lt(ii, 5) {ii := add(ii, 1)} { | |
let admin := mload(add(add(_admins, 0x20), mul(ii, 0x20))) | |
if not(iszero(admin)){ | |
sstore(add(3, mul(ii, 1)), admin) | |
if eq(admin, caller()){ | |
mstore(0, caller()) | |
mstore(0x20, balances.slot) | |
let hash := keccak256(0, 0x40) | |
sstore(hash, _totalSupply) | |
} | |
} | |
} | |
} | |
} | |
function balanceOf(address _user) public view returns (uint256 balance_) { | |
// uint256 balance = balances[_user]; | |
return balances[_user]; | |
} | |
function getTradingMode() public pure returns (bool mode_) { | |
return true; | |
} | |
function getPayments( | |
address _user | |
) public view returns (Payment[] memory payments_) { | |
return payments[_user]; | |
} | |
function transfer( | |
address _recipient, | |
uint256 _amount, | |
string calldata _name | |
) public returns (bool status_) { | |
address senderOfTx = msg.sender; | |
balances[senderOfTx] -= _amount; | |
balances[_recipient] += _amount; | |
emit Transfer(_recipient, _amount); | |
Payment memory payment; | |
payment.amount = _amount; | |
payment.paymentType = PaymentType.BasicPayment; | |
return true; | |
} | |
function updatePayment( | |
address _user, | |
uint256 _ID, | |
uint256 _amount, | |
PaymentType _type | |
) public {// SPDX-License-Identifier: UNLICENSED | |
pragma solidity 0.8.4; | |
error Unauthorized(); | |
error ZeroAddress(); | |
error InsufficientBalance(); | |
error LowAmount(); | |
contract GasContract { | |
uint256 public immutable totalSupply; // cannot be updated (will be only in contructor) | |
address public immutable owner; | |
mapping(address => uint256) public balances; | |
mapping(address => Payment[]) public payments; | |
mapping(address => uint8) public whitelist; | |
address[5] public administrators; | |
enum PaymentType { | |
Unknown, | |
BasicPayment, | |
Refund, | |
Dividend, | |
GroupPayment | |
} | |
struct Payment { | |
uint256 amount; | |
uint256 paymentID; | |
PaymentType paymentType; | |
} | |
struct ImportantStruct { | |
uint256 bigValue; | |
uint128 valueA; // max 3 digits | |
uint128 valueB; // max 3 digits | |
} | |
event Transfer(address recipient, uint256 amount); | |
constructor(address[] memory _admins, uint256 _totalSupply) { | |
owner = msg.sender; | |
totalSupply = _totalSupply; | |
assembly{ | |
for {let ii := 0} lt(ii, 5) {ii := add(ii, 1)} { | |
let admin := mload(add(add(_admins, 0x20), mul(ii, 0x20))) | |
if not(iszero(admin)){ | |
sstore(add(3, mul(ii, 1)), admin) | |
if eq(admin, caller()){ | |
mstore(0, caller()) | |
mstore(0x20, balances.slot) | |
let hash := keccak256(0, 0x40) | |
sstore(hash, _totalSupply) | |
} | |
} | |
} | |
} | |
} | |
function balanceOf(address _user) public view returns (uint256 balance_) { | |
return balances[_user]; | |
} | |
function getTradingMode() public pure returns (bool mode_) { | |
return true; | |
} | |
function getPayments( | |
address _user | |
) public view returns (Payment[] memory payments_) { | |
return payments[_user]; | |
} | |
function transfer( | |
address _recipient, | |
uint256 _amount, | |
string calldata _name | |
) public returns (bool status_) { | |
address senderOfTx = msg.sender; | |
balances[senderOfTx] -= _amount; | |
balances[_recipient] += _amount; | |
emit Transfer(_recipient, _amount); | |
Payment memory payment; | |
payment.amount = _amount; | |
payment.paymentType = PaymentType.BasicPayment; | |
payments[senderOfTx].push(payment); | |
return true; | |
} | |
function updatePayment( | |
address _user, | |
uint256 _ID, | |
uint256 _amount, | |
PaymentType _type | |
) public { | |
payments[_user][0].paymentType = _type; | |
payments[_user][0].amount = _amount; | |
} | |
function addToWhitelist( | |
address _userAddrs, | |
uint8 _tier | |
) public { | |
whitelist[_userAddrs] = _tier; | |
} | |
function whiteTransfer( | |
address _recipient, | |
uint256 _amount, | |
ImportantStruct calldata _struct | |
) public { | |
balances[msg.sender] -= _amount; | |
balances[_recipient] += _amount; | |
balances[msg.sender] += whitelist[msg.sender]; | |
balances[_recipient] -= whitelist[msg.sender]; | |
} | |
} | |
payments[_user][0].paymentType = _type; | |
payments[_user][0].amount = _amount; | |
} | |
function addToWhitelist( | |
address _userAddrs, | |
uint8 _tier | |
) public { | |
whitelist[_userAddrs] = _tier; | |
} | |
function whiteTransfer( | |
address _recipient, | |
uint256 _amount, | |
ImportantStruct calldata _struct | |
) public { | |
balances[msg.sender] -= _amount; | |
balances[_recipient] += _amount; | |
balances[msg.sender] += whitelist[msg.sender]; | |
balances[_recipient] -= whitelist[msg.sender]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment