Hello, can someone explain to me how people manage to make their ERC-1155 contract's minting function consume only 4,000 gas per transaction, while my contract consumes 25,000 gas per transaction? I rewrote the entire contract in YUL, thinking that it would help, but unfortunately, I didn't achieve the desired gas efficiency.

Oct 23, 2023, 12:34 AM
If you really want help, show us code+ txn that uses 4k and then the other code + txn which consumes 25k, then we can easily compare and diagnose.
Oct 23, 2023, 2:47 AM
mine contract:
object "ERC1155Yul" {
code {
/*
* slot0: owner
* slot1: uriLen
* slot (keccak256(urlLen) + i): uri value
* slot keccak256(id,account) : balances[id][account]
* slot keccak256(owner,operator) : operatorApproval[owner][operator]
*/

// slot0: owner
sstore(0, caller())

// Deploy the contract
datacopy(0, dataoffset("runtime"), datasize("runtime"))
return(0, datasize("runtime"))
}
object "runtime" {
code {
mstore(0x40, 0x80)

// Protection against sending Ether
if require(iszero(callvalue())) {
revert(0, 0)
}

function uriPos() -> pos {
pos := 0x20
}
switch selector()
case 0x729ad39e /* "airdrop(address[])" */ {
airdrop(decodeAsUint(0))
}
default {
revert(0, 0)
}
/* ---------- dispatcher functions ---------- */
function airdrop(accountsOffset) {
let toLen := decodeAsArrayLen(accountsOffset)
let accountsStartOffset := add(accountsOffset, 0x24)

let operator := caller()
for { let i := 0 } lt(i, toLen) { i:= add(i, 1)}
{
let to := calldataload(add(accountsStartOffset, mul(0x20, i)))
_addBalance(to, 1, 1)
emitTransferSingle(operator, 0, to, 1, 1)
}
}
function _addBalance(to, id, amount) {
let offset := balanceStorageOffset(id, to)
let prev := sload(offset)
sstore(offset, safeAdd(prev, amount))
}
/* -------- storage layout ---------- */
function ownerPos() -> p { p := 0 }

function uriLenPos() -> p { p := 1 }

function balanceStorageOffset(id, account) -> offset {
mstore(0, id)
mstore(0x20, account)
offset := keccak256(0, 0x40)
}
/* ---------- events ---------- */
function emitTransferSingle(operator, from, to, id, value) {
/* TransferSingle(address,address,address,uint256) */
let signatureHash := 0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62
mstore(0x00, id)
mstore(0x20, value)
log4(0x00, 0x40, signatureHash, operator, from, to)
}
}
}
}
my transaction: https://polygonscan.com/tx/0x4dfeb0141fcfe4a4c0caa63e9d978a797e85a7680694d6469d63e67af195dfe3
which you can see takes 50,160 gas which is 25000 per each

for theirs contract I don't know, I just sawed some guy sending vouchers and their gas usage was much lower
https://polygonscan.com/tx/0x7442e9b90837d726abe35050d81b3a7bb572e0bbb20c77dd824f07657bf9504b
Oct 23, 2023, 8:21 AM
This is really odd code. I guess you said it's YUL, which I've never seen. But according to the 2 transactions, the first one is 50k, airdrop is called with 2 addresses. Then it looks like another txn is called with 160 addresses or so, and gas is 600k so yes that's much cheaper (per address) so if it's the same code, then the bulk of the overhead probably has little to do with the number of iterations and mostly with the call itself. According to the line intersection of the 2 equations: x+2y=50k and x+160y = 600k, it would appear the base overhead is ~43k while each iteration is only ~3.5k gas. This may be quite reasonable. You have 1 read and 1 write it appears, so I think maybe it is possible that the iterations cost only around 3.5k gas. I think the 43k is probably a standard fee to simply process any txn. This could be ENTIRELY WRONG as I haven't really looked into it in depth, but this may be a possible explanation.
Oct 23, 2023, 8:46 AM
that's the thing that I was thinking, but when I set 500 token the estimation is around 1~ matic which if we take the gas price let's say 75gwei it will be
1 / 0.000000075 = 13,333,333 use by txn which if we divide by 500 will be around ~25k as well. So the problem probably is that i'm paying each time by the call?
Oct 23, 2023, 9:20 AM
I'm a bit lost. Do you mean 500 addresses and the txn fee ends up 13,333k gas? 13,333,333 seems like a lot of gas. Again, I think a txn link would be most helpful.
Oct 23, 2023, 9:26 AM
That's because its a scam contract. They don't do any state change or whatnot, just emits an event to make it show up on wallets
And probably set a single state variable which is shown as balance for all the users
Oct 23, 2023, 9:27 AM
oh, so there's nothing wrong with my contract?
I was so confused
Oct 23, 2023, 9:29 AM
Essentially it'll look like


function airdrop(address[] calldata users) external {
for(uint256 i =0; i < users.length; i++) {
emit Transfer(address(0), users[i], 2000000000000000000000);
}
}
Oct 23, 2023, 9:29 AM
cuz I wasted lot of time watching yul thinking it will be cheper
Oct 23, 2023, 9:29 AM
Think about it, 1 SSTORE is atleast 5000 gas
Oct 23, 2023, 9:30 AM
how can you add them to metamask after? because they are shown in metamask which I think they call ownerOf?
Oct 23, 2023, 9:30 AM
Maybe metamask also tracks it with events?
Oct 23, 2023, 9:30 AM
no surely no
they make the call to the contract, but im not sure about the method they call
Oct 23, 2023, 9:31 AM
https://polygonscan.com/token/0x293ec93eac0c7ebd1f8bf23ffedaee148296b584
Oct 23, 2023, 9:33 AM
anyways thx @WilliamOney @grimreaper619
Oct 23, 2023, 9:33 AM
Check the totalsupply
nothing is minted
Oct 23, 2023, 9:33 AM
yeah
i seen ow
did notice that thing before
Oct 23, 2023, 9:34 AM

© 2024 Draquery.com All rights reserved.