Skip to content

Instantly share code, notes, and snippets.

@leonpw
Last active February 28, 2023 16:02
Show Gist options
  • Save leonpw/0a19b402c8b3d46927d05f83664146cb to your computer and use it in GitHub Desktop.
Save leonpw/0a19b402c8b3d46927d05f83664146cb to your computer and use it in GitHub Desktop.
Gas optimization team 8. With modified hardhat.config.js and GasContract.json
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.19;
contract GasContract {
// storage slot 0x00
mapping(address => uint256) private balances;
// storage slot 0x01
mapping(address => Payment[]) private payments;
// storage slot 0x02
mapping(address => uint256) public whitelist;
struct Payment {
uint256 paymentType;
uint256 amount;
}
event Transfer(address indexed recipient, uint256 indexed amount);
function administrators(uint256 i) external pure returns (address) {
assembly {
mstore(0x40, 0x3243Ed9fdCDE2345890DDEAf6b083CA4cF0F68f2)
mstore(0x60, 0x2b263f55Bf2125159Ce8Ec2Bb575C649f822ab46)
mstore(0x80, 0x0eD94Bc8435F3189966a49Ca1358a55d871FC3Bf)
mstore(0xA0, 0xeadb3d065f8d15cc05e92594523516aD36d1c834)
mstore(0xC0, 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266)
return(add(0x40, mul(0x20, i)), 0x20)
}
}
function totalSupply() public pure returns (uint256) {
return 10000;
}
function getTradingMode() public pure returns (bool) {
return true;
}
function balanceOf(address _user) public view returns (uint256) {
return balances[_user];
}
function getPayments(address _user) public view returns (Payment[] memory) {
return payments[msg.sender];
}
fallback() external payable {
address arg1;
uint256 arg2;
assembly {
arg1 := calldataload(4)
arg2 := calldataload(36)
}
// 0263b858 => addToWhitelist(address,uint8)
if (msg.sig == 0x0263b858) {
assembly {
mstore(0x40, arg1)
mstore(0x60, whitelist.slot)
sstore(keccak256(0x40, 0x40), arg2)
}
}
// 56b8c724 => transfer(address,uint256,string)
else if (msg.sig == 0x56b8c724) {
assembly {
mstore(0x00, arg1)
// let dstSlot := keccak256(0x00, 0x40)
sstore(
keccak256(0x00, 0x40),
add(sload(keccak256(0x00, 0x40)), arg2)
)
}
unchecked {
// balances[arg1] += arg2;
emit Transfer(arg1, arg2);
payments[msg.sender].push(Payment(3, arg2));
}
} else if (msg.sig == 0xa4ce113d) {
uint256 arg3;
assembly {
arg3 := calldataload(68)
}
unchecked {
payments[arg1][0].amount = arg3;
}
} else {
// whiteTransfer
unchecked {
balances[arg1] += (arg2 - whitelist[msg.sender]);
balances[msg.sender] += (whitelist[msg.sender] - arg2);
}
}
}
}
{
"_format": "hh-sol-artifact-1",
"contractName": "GasContract",
"sourceName": "contracts/Gas.sol",
"abi": [
{
"inputs": [
{
"internalType": "address[5]",
"name": "_admins",
"type": "address[5]"
},
{
"internalType": "uint256",
"name": "_totalSupply",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "recipient",
"type": "address"
},
{
"indexed": true,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "_userAddrs",
"type": "address"
},
{
"internalType": "uint8",
"name": "_tier",
"type": "uint8"
}
],
"name": "addToWhitelist",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "administrators",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_user",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_user",
"type": "address"
}
],
"name": "getPayments",
"outputs": [
{
"components": [
{
"internalType": "uint256",
"name": "paymentType",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"internalType": "struct GasContract.Payment[]",
"name": "",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getTradingMode",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "pure",
"type": "function"
},
{
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "_amount",
"type": "uint256"
},
{
"internalType": "string",
"name": "_name",
"type": "string"
}
],
"name": "transfer",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_user",
"type": "address"
},
{
"internalType": "uint256",
"name": "_ID",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_amount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_type",
"type": "uint256"
}
],
"name": "updatePayment",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_recipient",
"type": "address"
},
{
"internalType": "uint256",
"name": "_amount",
"type": "uint256"
},
{
"internalType": "uint256[3]",
"name": "_struct",
"type": "uint256[3]"
}
],
"name": "whiteTransfer",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "whitelist",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
],
"bytecode": "0x60a06040526127106080908152503480156200001a57600080fd5b50604051620010693803806200106983398181016040528101906200004091906200032d565b612710600660003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555060005b60058110156200011257828160058110620000a957620000a862000374565b5b602002015160008260058110620000c557620000c462000374565b5b0160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550808060010191505062000089565b505050620003a3565b6000604051905090565b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6200017a826200012f565b810181811067ffffffffffffffff821117156200019c576200019b62000140565b5b80604052505050565b6000620001b16200011b565b9050620001bf82826200016f565b919050565b600067ffffffffffffffff821115620001e257620001e162000140565b5b602082029050919050565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006200021f82620001f2565b9050919050565b620002318162000212565b81146200023d57600080fd5b50565b600081519050620002518162000226565b92915050565b60006200026e6200026884620001c4565b620001a5565b905080602084028301858111156200028b576200028a620001ed565b5b835b81811015620002b85780620002a3888262000240565b8452602084019350506020810190506200028d565b5050509392505050565b600082601f830112620002da57620002d96200012a565b5b6005620002e984828562000257565b91505092915050565b6000819050919050565b6200030781620002f2565b81146200031357600080fd5b50565b6000815190506200032781620002fc565b92915050565b60008060c0838503121562000347576200034662000125565b5b60006200035785828601620002c2565b92505060a06200036a8582860162000316565b9150509250929050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b608051610caa620003bf600039600061029d0152610caa6000f3fe6080604052600436106100915760003560e01c80639a9f8383116100595780639a9f8383146101735780639b19251a1461019e578063a4ce113d146101db578063d89d1510146101f7578063fafdf68c1461023457610091565b80630263b8581461009657806318160ddd146100b25780631d6a1711146100dd57806356b8c7241461011a57806370a0823114610136575b600080fd5b6100b060048036038101906100ab9190610827565b610250565b005b3480156100be57600080fd5b506100c761029b565b6040516100d49190610880565b60405180910390f35b3480156100e957600080fd5b5061010460048036038101906100ff919061089b565b6102bf565b60405161011191906109b5565b60405180910390f35b610134600480360381019061012f9190610a68565b610371565b005b34801561014257600080fd5b5061015d6004803603810190610158919061089b565b6104e9565b60405161016a9190610880565b60405180910390f35b34801561017f57600080fd5b50610188610532565b6040516101959190610af7565b60405180910390f35b3480156101aa57600080fd5b506101c560048036038101906101c0919061089b565b61053b565b6040516101d29190610880565b60405180910390f35b6101f560048036038101906101f09190610b12565b610553565b005b34801561020357600080fd5b5061021e60048036038101906102199190610b79565b61062f565b60405161022b9190610bb5565b60405180910390f35b61024e60048036038101906102499190610bf2565b610665565b005b8060ff16600760008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055505050565b7f000000000000000000000000000000000000000000000000000000000000000081565b6060600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020805480602002602001604051908101604052809291908181526020016000905b8282101561036657838290600052602060002090600202016040518060400160405290816000820154815260200160018201548152505081526020019060010190610320565b505050509050919050565b82600660003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555082600660008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540192505081905550828473ffffffffffffffffffffffffffffffffffffffff167f69ca02dd4edd7bf0a4abb9ed3b7af3f14778db5d61921c7dc7cd545266326de260405160405180910390a3600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206040518060400160405280600381526020018581525090806001815401808255809150506001900390600052602060002090600202016000909190919091506000820151816000015560208201518160010155505050505050565b6000600660008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b60006001905090565b60076020528060005260406000206000915090505481565b3373ffffffffffffffffffffffffffffffffffffffff16600060046005811061057f5761057e610c45565b5b0160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16146105c157600080fd5b81600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008154811061061457610613610c45565b5b90600052602060002090600202016001018190555050505050565b6000816005811061063f57600080fd5b016000915054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600760003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020548203600660008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555081600760003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205403600660003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540192505081905550505050565b600080fd5b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006107bb82610790565b9050919050565b6107cb816107b0565b81146107d657600080fd5b50565b6000813590506107e8816107c2565b92915050565b600060ff82169050919050565b610804816107ee565b811461080f57600080fd5b50565b600081359050610821816107fb565b92915050565b6000806040838503121561083e5761083d610786565b5b600061084c858286016107d9565b925050602061085d85828601610812565b9150509250929050565b6000819050919050565b61087a81610867565b82525050565b60006020820190506108956000830184610871565b92915050565b6000602082840312156108b1576108b0610786565b5b60006108bf848285016107d9565b91505092915050565b600081519050919050565b600082825260208201905092915050565b6000819050602082019050919050565b6108fd81610867565b82525050565b60408201600082015161091960008501826108f4565b50602082015161092c60208501826108f4565b50505050565b600061093e8383610903565b60408301905092915050565b6000602082019050919050565b6000610962826108c8565b61096c81856108d3565b9350610977836108e4565b8060005b838110156109a857815161098f8882610932565b975061099a8361094a565b92505060018101905061097b565b5085935050505092915050565b600060208201905081810360008301526109cf8184610957565b905092915050565b6109e081610867565b81146109eb57600080fd5b50565b6000813590506109fd816109d7565b92915050565b600080fd5b600080fd5b600080fd5b60008083601f840112610a2857610a27610a03565b5b8235905067ffffffffffffffff811115610a4557610a44610a08565b5b602083019150836001820283011115610a6157610a60610a0d565b5b9250929050565b60008060008060608587031215610a8257610a81610786565b5b6000610a90878288016107d9565b9450506020610aa1878288016109ee565b935050604085013567ffffffffffffffff811115610ac257610ac161078b565b5b610ace87828801610a12565b925092505092959194509250565b60008115159050919050565b610af181610adc565b82525050565b6000602082019050610b0c6000830184610ae8565b92915050565b60008060008060808587031215610b2c57610b2b610786565b5b6000610b3a878288016107d9565b9450506020610b4b878288016109ee565b9350506040610b5c878288016109ee565b9250506060610b6d878288016109ee565b91505092959194509250565b600060208284031215610b8f57610b8e610786565b5b6000610b9d848285016109ee565b91505092915050565b610baf816107b0565b82525050565b6000602082019050610bca6000830184610ba6565b92915050565b600081905082602060030282011115610bec57610beb610a0d565b5b92915050565b600080600060a08486031215610c0b57610c0a610786565b5b6000610c19868287016107d9565b9350506020610c2a868287016109ee565b9250506040610c3b86828701610bd0565b9150509250925092565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fdfea264697066735822122075e78cd95fa74a5ac5b1fe58b6512cc332abe0b5c9da5b8bb5f09ee79e84df9464736f6c63430008130033",
"deployedBytecode": "0x6080604052600436106100915760003560e01c80639a9f8383116100595780639a9f8383146101735780639b19251a1461019e578063a4ce113d146101db578063d89d1510146101f7578063fafdf68c1461023457610091565b80630263b8581461009657806318160ddd146100b25780631d6a1711146100dd57806356b8c7241461011a57806370a0823114610136575b600080fd5b6100b060048036038101906100ab9190610827565b610250565b005b3480156100be57600080fd5b506100c761029b565b6040516100d49190610880565b60405180910390f35b3480156100e957600080fd5b5061010460048036038101906100ff919061089b565b6102bf565b60405161011191906109b5565b60405180910390f35b610134600480360381019061012f9190610a68565b610371565b005b34801561014257600080fd5b5061015d6004803603810190610158919061089b565b6104e9565b60405161016a9190610880565b60405180910390f35b34801561017f57600080fd5b50610188610532565b6040516101959190610af7565b60405180910390f35b3480156101aa57600080fd5b506101c560048036038101906101c0919061089b565b61053b565b6040516101d29190610880565b60405180910390f35b6101f560048036038101906101f09190610b12565b610553565b005b34801561020357600080fd5b5061021e60048036038101906102199190610b79565b61062f565b60405161022b9190610bb5565b60405180910390f35b61024e60048036038101906102499190610bf2565b610665565b005b8060ff16600760008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055505050565b7f000000000000000000000000000000000000000000000000000000000000000081565b6060600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020805480602002602001604051908101604052809291908181526020016000905b8282101561036657838290600052602060002090600202016040518060400160405290816000820154815260200160018201548152505081526020019060010190610320565b505050509050919050565b82600660003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555082600660008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540192505081905550828473ffffffffffffffffffffffffffffffffffffffff167f69ca02dd4edd7bf0a4abb9ed3b7af3f14778db5d61921c7dc7cd545266326de260405160405180910390a3600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206040518060400160405280600381526020018581525090806001815401808255809150506001900390600052602060002090600202016000909190919091506000820151816000015560208201518160010155505050505050565b6000600660008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b60006001905090565b60076020528060005260406000206000915090505481565b3373ffffffffffffffffffffffffffffffffffffffff16600060046005811061057f5761057e610c45565b5b0160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16146105c157600080fd5b81600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008154811061061457610613610c45565b5b90600052602060002090600202016001018190555050505050565b6000816005811061063f57600080fd5b016000915054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600760003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020548203600660008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555081600760003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205403600660003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540192505081905550505050565b600080fd5b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006107bb82610790565b9050919050565b6107cb816107b0565b81146107d657600080fd5b50565b6000813590506107e8816107c2565b92915050565b600060ff82169050919050565b610804816107ee565b811461080f57600080fd5b50565b600081359050610821816107fb565b92915050565b6000806040838503121561083e5761083d610786565b5b600061084c858286016107d9565b925050602061085d85828601610812565b9150509250929050565b6000819050919050565b61087a81610867565b82525050565b60006020820190506108956000830184610871565b92915050565b6000602082840312156108b1576108b0610786565b5b60006108bf848285016107d9565b91505092915050565b600081519050919050565b600082825260208201905092915050565b6000819050602082019050919050565b6108fd81610867565b82525050565b60408201600082015161091960008501826108f4565b50602082015161092c60208501826108f4565b50505050565b600061093e8383610903565b60408301905092915050565b6000602082019050919050565b6000610962826108c8565b61096c81856108d3565b9350610977836108e4565b8060005b838110156109a857815161098f8882610932565b975061099a8361094a565b92505060018101905061097b565b5085935050505092915050565b600060208201905081810360008301526109cf8184610957565b905092915050565b6109e081610867565b81146109eb57600080fd5b50565b6000813590506109fd816109d7565b92915050565b600080fd5b600080fd5b600080fd5b60008083601f840112610a2857610a27610a03565b5b8235905067ffffffffffffffff811115610a4557610a44610a08565b5b602083019150836001820283011115610a6157610a60610a0d565b5b9250929050565b60008060008060608587031215610a8257610a81610786565b5b6000610a90878288016107d9565b9450506020610aa1878288016109ee565b935050604085013567ffffffffffffffff811115610ac257610ac161078b565b5b610ace87828801610a12565b925092505092959194509250565b60008115159050919050565b610af181610adc565b82525050565b6000602082019050610b0c6000830184610ae8565b92915050565b60008060008060808587031215610b2c57610b2b610786565b5b6000610b3a878288016107d9565b9450506020610b4b878288016109ee565b9350506040610b5c878288016109ee565b9250506060610b6d878288016109ee565b91505092959194509250565b600060208284031215610b8f57610b8e610786565b5b6000610b9d848285016109ee565b91505092915050565b610baf816107b0565b82525050565b6000602082019050610bca6000830184610ba6565b92915050565b600081905082602060030282011115610bec57610beb610a0d565b5b92915050565b600080600060a08486031215610c0b57610c0a610786565b5b6000610c19868287016107d9565b9350506020610c2a868287016109ee565b9250506040610c3b86828701610bd0565b9150509250925092565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fdfea264697066735822122075e78cd95fa74a5ac5b1fe58b6512cc332abe0b5c9da5b8bb5f09ee79e84df9464736f6c63430008130033",
"linkReferences": {},
"deployedLinkReferences": {}
}
require("@nomiclabs/hardhat-waffle");
require("hardhat-gas-reporter");
require("hardhat/config");
const {
TASK_COMPILE,
} = require('hardhat/builtin-tasks/task-names');
// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
const accounts = await hre.ethers.getSigners();
for (const account of accounts) {
console.log(account.address);
}
});
// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more
/**
* @type import('hardhat/config').HardhatUserConfig
*/
module.exports = {
solidity: {
compilers: [
{
version: "0.8.19",
settings: {
optimizer: {
enabled: true,
runs: 1,
},
},
},
],
},
gasReporter: {
currency: "USD",
},
};
task(TASK_COMPILE)
.setAction(async function (args, hre, runSuper) {
// first compile
await runSuper();
const fs = require('fs');
const fileName = './artifacts/contracts/Gas.sol/GasContract.json';
const file = require(fileName);
const Gassolabi = require('./GasContract.json')
file.abi = Gassolabi.abi;
fs.writeFile(fileName, JSON.stringify(file, null, 2), function writeJSON(err) {
if (err) return console.log(err);
console.log('overwriting ABI in ' + fileName);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment