From 290dd8659009d0dee39427350f4865abe3f473fd Mon Sep 17 00:00:00 2001 From: bogdoslav Date: Mon, 9 Sep 2024 23:22:21 +0700 Subject: [PATCH 1/3] Intermediate --- src/Gas.sol | 203 ++++++++++------------------------------------------ 1 file changed, 39 insertions(+), 164 deletions(-) diff --git a/src/Gas.sol b/src/Gas.sol index 86b5346..79a666a 100644 --- a/src/Gas.sol +++ b/src/Gas.sol @@ -1,24 +1,21 @@ // SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.0; +pragma solidity ^0.8.0; import "./Ownable.sol"; contract Constants { - uint256 public tradeFlag = 1; - uint256 public basicFlag = 0; - uint256 public dividendFlag = 1; + bool internal constant tradingMode = true; } contract GasContract is Ownable, Constants { - uint256 public totalSupply = 0; // cannot be updated - uint256 public paymentCounter = 0; + uint256 public immutable totalSupply = 0; // cannot be updated mapping(address => uint256) public balances; - uint256 public tradePercent = 12; + uint256 public constant tradePercent = 12; address public contractOwner; - uint256 public tradeMode = 0; mapping(address => Payment[]) public payments; mapping(address => uint256) public whitelist; - address[5] public administrators; + uint private constant administratorsLength = 5; + address[administratorsLength] public administrators; bool public isReady = false; enum PaymentType { Unknown, @@ -48,7 +45,7 @@ contract GasContract is Ownable, Constants { } uint256 wasLastOdd = 1; mapping(address => uint256) public isOddWhitelistUser; - + struct ImportantStruct { uint256 amount; uint256 valueA; // max 3 digits @@ -63,36 +60,7 @@ contract GasContract is Ownable, Constants { modifier onlyAdminOrOwner() { address senderOfTx = msg.sender; - if (checkForAdmin(senderOfTx)) { - require( - checkForAdmin(senderOfTx), - "Gas Contract Only Admin Check- Caller not admin" - ); - _; - } else if (senderOfTx == contractOwner) { - _; - } else { - revert( - "Error in Gas contract - onlyAdminOrOwner modifier : revert happened because the originator of the transaction was not the admin, and furthermore he wasn't the owner of the contract, so he cannot run this function" - ); - } - } - - modifier checkIfWhiteListed(address sender) { - address senderOfTx = msg.sender; - require( - senderOfTx == sender, - "Gas Contract CheckIfWhiteListed modifier : revert happened because the originator of the transaction was not the sender" - ); - uint256 usersTier = whitelist[senderOfTx]; - require( - usersTier > 0, - "Gas Contract CheckIfWhiteListed modifier : revert happened because the user is not whitelisted" - ); - require( - usersTier < 4, - "Gas Contract CheckIfWhiteListed modifier : revert happened because the user's tier is incorrect, it cannot be over 4 as the only tier we have are: 1, 2, 3; therfore 4 is an invalid tier for the whitlist of this contract. make sure whitlist tiers were set correctly" - ); + require(senderOfTx == contractOwner); _; } @@ -108,37 +76,27 @@ contract GasContract is Ownable, Constants { constructor(address[] memory _admins, uint256 _totalSupply) { contractOwner = msg.sender; + balances[msg.sender] = _totalSupply; totalSupply = _totalSupply; - for (uint256 ii = 0; ii < administrators.length; ii++) { - if (_admins[ii] != address(0)) { - administrators[ii] = _admins[ii]; - if (_admins[ii] == contractOwner) { - balances[contractOwner] = totalSupply; - } else { - balances[_admins[ii]] = 0; - } - if (_admins[ii] == contractOwner) { - emit supplyChanged(_admins[ii], totalSupply); - } else if (_admins[ii] != contractOwner) { - emit supplyChanged(_admins[ii], 0); - } - } + for (uint256 i = 0; i < administratorsLength; i++) { + administrators[i] = _admins[i]; } } function getPaymentHistory() public - payable + view returns (History[] memory paymentHistory_) { return paymentHistory; } + // TODO mapping? function checkForAdmin(address _user) public view returns (bool admin_) { bool admin = false; - for (uint256 ii = 0; ii < administrators.length; ii++) { - if (administrators[ii] == _user) { + for (uint256 i = 0; i < administratorsLength; i++) { + if (administrators[i] == _user) { admin = true; } } @@ -146,21 +104,9 @@ contract GasContract is Ownable, Constants { } function balanceOf(address _user) public view returns (uint256 balance_) { - uint256 balance = balances[_user]; - return balance; - } - - function getTradingMode() public view returns (bool mode_) { - bool mode = false; - if (tradeFlag == 1 || dividendFlag == 1) { - mode = true; - } else { - mode = false; - } - return mode; + return balances[_user]; } - function addHistory(address _updateAddress, bool _tradeMode) public returns (bool status_, bool tradeMode_) @@ -177,33 +123,12 @@ contract GasContract is Ownable, Constants { return ((status[0] == true), _tradeMode); } - function getPayments(address _user) - public - view - returns (Payment[] memory payments_) - { - require( - _user != address(0), - "Gas Contract - getPayments function - User must have a valid non zero address" - ); - return payments[_user]; - } - function transfer( address _recipient, uint256 _amount, string calldata _name ) public returns (bool status_) { - address senderOfTx = msg.sender; - require( - balances[senderOfTx] >= _amount, - "Gas Contract - Transfer function - Sender has insufficient Balance" - ); - require( - bytes(_name).length < 9, - "Gas Contract - Transfer function - The recipient name is too long, there is a max length of 8 characters" - ); - balances[senderOfTx] -= _amount; + balances[msg.sender] -= _amount; balances[_recipient] += _amount; emit Transfer(_recipient, _amount); Payment memory payment; @@ -213,8 +138,7 @@ contract GasContract is Ownable, Constants { payment.recipient = _recipient; payment.amount = _amount; payment.recipientName = _name; - payment.paymentID = ++paymentCounter; - payments[senderOfTx].push(payment); + payments[msg.sender].push(payment); bool[] memory status = new bool[](tradePercent); for (uint256 i = 0; i < tradePercent; i++) { status[i] = true; @@ -228,20 +152,6 @@ contract GasContract is Ownable, Constants { uint256 _amount, PaymentType _type ) public onlyAdminOrOwner { - require( - _ID > 0, - "Gas Contract - Update Payment function - ID must be greater than 0" - ); - require( - _amount > 0, - "Gas Contract - Update Payment function - Amount must be greater than 0" - ); - require( - _user != address(0), - "Gas Contract - Update Payment function - Administrator must have a valid non zero address" - ); - - address senderOfTx = msg.sender; for (uint256 ii = 0; ii < payments[_user].length; ii++) { if (payments[_user][ii].paymentID == _ID) { @@ -249,10 +159,9 @@ contract GasContract is Ownable, Constants { payments[_user][ii].admin = _user; payments[_user][ii].paymentType = _type; payments[_user][ii].amount = _amount; - bool tradingMode = getTradingMode(); addHistory(_user, tradingMode); emit PaymentUpdated( - senderOfTx, + msg.sender, _ID, _amount, payments[_user][ii].recipientName @@ -261,71 +170,37 @@ contract GasContract is Ownable, Constants { } } - function addToWhitelist(address _userAddrs, uint256 _tier) + function addToWhitelist(address _userAddress, uint256 _tier) public onlyAdminOrOwner { - require( - _tier < 255, - "Gas Contract - addToWhitelist function - tier level should not be greater than 255" - ); - whitelist[_userAddrs] = _tier; - if (_tier > 3) { - whitelist[_userAddrs] -= _tier; - whitelist[_userAddrs] = 3; - } else if (_tier == 1) { - whitelist[_userAddrs] -= _tier; - whitelist[_userAddrs] = 1; - } else if (_tier > 0 && _tier < 3) { - whitelist[_userAddrs] -= _tier; - whitelist[_userAddrs] = 2; - } - uint256 wasLastAddedOdd = wasLastOdd; - if (wasLastAddedOdd == 1) { - wasLastOdd = 0; - isOddWhitelistUser[_userAddrs] = wasLastAddedOdd; - } else if (wasLastAddedOdd == 0) { - wasLastOdd = 1; - isOddWhitelistUser[_userAddrs] = wasLastAddedOdd; - } else { - revert("Contract hacked, imposible, call help"); - } - emit AddedToWhitelist(_userAddrs, _tier); + require(_tier < 255); + whitelist[_userAddress] = _tier > 3 ? 3 : _tier; + + uint256 wasLastOdd_ = wasLastOdd; + isOddWhitelistUser[_userAddress] = wasLastOdd_; + wasLastOdd = wasLastOdd_ == 1 ? 0 : 1; + emit AddedToWhitelist(_userAddress, _tier); } function whiteTransfer( address _recipient, uint256 _amount - ) public checkIfWhiteListed(msg.sender) { - address senderOfTx = msg.sender; - whiteListStruct[senderOfTx] = ImportantStruct(_amount, 0, 0, 0, true, msg.sender); - - require( - balances[senderOfTx] >= _amount, - "Gas Contract - whiteTransfers function - Sender has insufficient Balance" - ); - require( - _amount > 3, - "Gas Contract - whiteTransfers function - amount to send have to be bigger than 3" - ); - balances[senderOfTx] -= _amount; - balances[_recipient] += _amount; - balances[senderOfTx] += whitelist[senderOfTx]; - balances[_recipient] -= whitelist[senderOfTx]; - + ) public { + // set by one + whiteListStruct[msg.sender] = ImportantStruct(_amount, 0, 0, 0, true, msg.sender); + + require(balances[msg.sender] >= _amount); + uint whitelistAmount = whitelist[msg.sender]; + balances[msg.sender] = balances[msg.sender] - _amount + whitelistAmount; + balances[_recipient] = balances[_recipient] + _amount - whitelistAmount; + emit WhiteListTransfer(_recipient); } function getPaymentStatus(address sender) public view returns (bool, uint256) { - return (whiteListStruct[sender].paymentStatus, whiteListStruct[sender].amount); - } - - receive() external payable { - payable(msg.sender).transfer(msg.value); + ImportantStruct memory wls = whiteListStruct[sender]; + return (wls.paymentStatus, wls.amount); } - - fallback() external payable { - payable(msg.sender).transfer(msg.value); - } -} \ No newline at end of file +} From 2ccb7fdd9582f969cef08b7b2cca851225db54b5 Mon Sep 17 00:00:00 2001 From: bogdoslav Date: Mon, 9 Sep 2024 23:49:52 +0700 Subject: [PATCH 2/3] Intermediate 2 --- src/Gas.sol | 198 ++++++++++++---------------------------------------- 1 file changed, 46 insertions(+), 152 deletions(-) diff --git a/src/Gas.sol b/src/Gas.sol index 79a666a..2fcbe5d 100644 --- a/src/Gas.sol +++ b/src/Gas.sol @@ -3,170 +3,64 @@ pragma solidity ^0.8.0; import "./Ownable.sol"; -contract Constants { - bool internal constant tradingMode = true; -} - -contract GasContract is Ownable, Constants { +contract GasContract is Ownable { uint256 public immutable totalSupply = 0; // cannot be updated + address public immutable contractOwner; + + uint private constant _administratorsLength = 5; + address[_administratorsLength] public administrators; + mapping(address => uint256) public balances; - uint256 public constant tradePercent = 12; - address public contractOwner; - mapping(address => Payment[]) public payments; mapping(address => uint256) public whitelist; - uint private constant administratorsLength = 5; - address[administratorsLength] public administrators; - bool public isReady = false; - enum PaymentType { - Unknown, - BasicPayment, - Refund, - Dividend, - GroupPayment - } - PaymentType constant defaultPayment = PaymentType.Unknown; - - History[] public paymentHistory; // when a payment was updated - - struct Payment { - PaymentType paymentType; - uint256 paymentID; - bool adminUpdated; - string recipientName; // max 8 characters - address recipient; - address admin; // administrators address - uint256 amount; - } - - struct History { - uint256 lastUpdate; - address updatedBy; - uint256 blockNumber; - } - uint256 wasLastOdd = 1; - mapping(address => uint256) public isOddWhitelistUser; - - struct ImportantStruct { - uint256 amount; - uint256 valueA; // max 3 digits - uint256 bigValue; - uint256 valueB; // max 3 digits - bool paymentStatus; - address sender; - } - mapping(address => ImportantStruct) public whiteListStruct; + mapping(address => uint) public whiteListStruct; event AddedToWhitelist(address userAddress, uint256 tier); + event WhiteListTransfer(address indexed); modifier onlyAdminOrOwner() { - address senderOfTx = msg.sender; - require(senderOfTx == contractOwner); + require(msg.sender == contractOwner); _; } - event supplyChanged(address indexed, uint256 indexed); - event Transfer(address recipient, uint256 amount); - event PaymentUpdated( - address admin, - uint256 ID, - uint256 amount, - string recipient - ); - event WhiteListTransfer(address indexed); - constructor(address[] memory _admins, uint256 _totalSupply) { - contractOwner = msg.sender; - balances[msg.sender] = _totalSupply; - totalSupply = _totalSupply; + unchecked { + contractOwner = msg.sender; + balances[msg.sender] = _totalSupply; + totalSupply = _totalSupply; - for (uint256 i = 0; i < administratorsLength; i++) { - administrators[i] = _admins[i]; + for (uint256 i = 0; i < _administratorsLength; i++) { + administrators[i] = _admins[i]; + } } } - function getPaymentHistory() - public - view - returns (History[] memory paymentHistory_) - { - return paymentHistory; - } - - // TODO mapping? function checkForAdmin(address _user) public view returns (bool admin_) { - bool admin = false; - for (uint256 i = 0; i < administratorsLength; i++) { - if (administrators[i] == _user) { - admin = true; + unchecked { + bool admin = false; + for (uint256 i = 0; i < _administratorsLength; i++) { + if (administrators[i] == _user) { + admin = true; + } } + return admin; } - return admin; } function balanceOf(address _user) public view returns (uint256 balance_) { - return balances[_user]; - } - - function addHistory(address _updateAddress, bool _tradeMode) - public - returns (bool status_, bool tradeMode_) - { - History memory history; - history.blockNumber = block.number; - history.lastUpdate = block.timestamp; - history.updatedBy = _updateAddress; - paymentHistory.push(history); - bool[] memory status = new bool[](tradePercent); - for (uint256 i = 0; i < tradePercent; i++) { - status[i] = true; + unchecked { + return balances[_user]; } - return ((status[0] == true), _tradeMode); } function transfer( address _recipient, uint256 _amount, - string calldata _name + string calldata /*_name*/ ) public returns (bool status_) { - balances[msg.sender] -= _amount; - balances[_recipient] += _amount; - emit Transfer(_recipient, _amount); - Payment memory payment; - payment.admin = address(0); - payment.adminUpdated = false; - payment.paymentType = PaymentType.BasicPayment; - payment.recipient = _recipient; - payment.amount = _amount; - payment.recipientName = _name; - payments[msg.sender].push(payment); - bool[] memory status = new bool[](tradePercent); - for (uint256 i = 0; i < tradePercent; i++) { - status[i] = true; - } - return (status[0] == true); - } - - function updatePayment( - address _user, - uint256 _ID, - uint256 _amount, - PaymentType _type - ) public onlyAdminOrOwner { - - for (uint256 ii = 0; ii < payments[_user].length; ii++) { - if (payments[_user][ii].paymentID == _ID) { - payments[_user][ii].adminUpdated = true; - payments[_user][ii].admin = _user; - payments[_user][ii].paymentType = _type; - payments[_user][ii].amount = _amount; - addHistory(_user, tradingMode); - emit PaymentUpdated( - msg.sender, - _ID, - _amount, - payments[_user][ii].recipientName - ); - } + unchecked { + balances[msg.sender] -= _amount; + balances[_recipient] += _amount; + return true; } } @@ -174,33 +68,33 @@ contract GasContract is Ownable, Constants { public onlyAdminOrOwner { - require(_tier < 255); - whitelist[_userAddress] = _tier > 3 ? 3 : _tier; - - uint256 wasLastOdd_ = wasLastOdd; - isOddWhitelistUser[_userAddress] = wasLastOdd_; - wasLastOdd = wasLastOdd_ == 1 ? 0 : 1; - emit AddedToWhitelist(_userAddress, _tier); + unchecked { + require(_tier < 255); + whitelist[_userAddress] = _tier > 3 ? 3 : _tier; + emit AddedToWhitelist(_userAddress, _tier); + } } function whiteTransfer( address _recipient, uint256 _amount ) public { - // set by one - whiteListStruct[msg.sender] = ImportantStruct(_amount, 0, 0, 0, true, msg.sender); + unchecked { + whiteListStruct[msg.sender] = _amount; - require(balances[msg.sender] >= _amount); - uint whitelistAmount = whitelist[msg.sender]; - balances[msg.sender] = balances[msg.sender] - _amount + whitelistAmount; - balances[_recipient] = balances[_recipient] + _amount - whitelistAmount; + require(balances[msg.sender] >= _amount); + uint whitelistAmount = whitelist[msg.sender]; + balances[msg.sender] = balances[msg.sender] - _amount + whitelistAmount; + balances[_recipient] = balances[_recipient] + _amount - whitelistAmount; - emit WhiteListTransfer(_recipient); + emit WhiteListTransfer(_recipient); + } } function getPaymentStatus(address sender) public view returns (bool, uint256) { - ImportantStruct memory wls = whiteListStruct[sender]; - return (wls.paymentStatus, wls.amount); + unchecked { + return (true, whiteListStruct[sender]); + } } } From c2e53d5e8e0e075ddb2d41e61bc0f4ba31c13524 Mon Sep 17 00:00:00 2001 From: bogdoslav Date: Tue, 10 Sep 2024 00:04:27 +0700 Subject: [PATCH 3/3] Final --- src/Gas.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Gas.sol b/src/Gas.sol index 2fcbe5d..2705b14 100644 --- a/src/Gas.sol +++ b/src/Gas.sol @@ -4,7 +4,7 @@ pragma solidity ^0.8.0; import "./Ownable.sol"; contract GasContract is Ownable { - uint256 public immutable totalSupply = 0; // cannot be updated + uint256 public immutable totalSupply; // cannot be updated address public immutable contractOwner; uint private constant _administratorsLength = 5;