Is that the only storage you have? I mean, it is theoretically possible, but it would all depend on what else is in the contract. Have you tried deploying it in a test net and seeing what the estimated gas usage is?

Mar 11, 2021, 2:47 AM
Also, that would be one long-ass constructor to have 1000 ids to map
:)
I wouldn't know on that.
But you could try it. If you have time, you can always put it together and try test deploying to see estimated gas
You might be able to push everything into an array to hold it but have a mapping that uses the ID to recall the data.
Mar 11, 2021, 2:52 AM
Not sure what you mean here. Can you please elaborate?
Mar 11, 2021, 3:04 AM
You can have an array and a mapping that uses a variable in that array. i.e. Have an array with the IDs and name, info, etc. Then have your mapping use that ID number as the key. I'm not sure if it is cheaper or easier to get everything into an array at the deployment, but if it is, it might work. Would be stuck and ready to code into what you already have and give it a test. Might be worse, never know. But you could try.
I haven't done a lot of array or mapping initializing at deployment so not sure what gas implications are, would need to test it.
Mar 11, 2021, 3:10 AM
Thanks, Ben, really appreciate it. For a simple contract like this:
contract SandwichFactory {

mapping(bytes32 => uint) test;
bytes32 idx;

constructor () {
for (uint i = 0; i < 100; i++) {
idx = keccak256(abi.encode(i));
test[idx] = i;
}
}
}
It used 2348348 Gas.
it's only 100 iterations.
More than 20M Gas would be consumed for the goal I was trying to achieve, which is 2 ETHs...
Mar 11, 2021, 3:17 AM
And block gas limit is 12.5M, so you could only do half, maybe, then have to push in the rest
Mar 11, 2021, 3:22 AM
I cannot believe I am the first one who's trying to do this and I'm sure I will not be the last one. Just hope to find a clever answer.
Mar 11, 2021, 3:24 AM
Loops always cost a lot. This might not be bad because you aren't searching, just assigning. However, I would try removing the for loop constructor, put in an add() function, and try deploying without any initializing, and then try calling to add each one at a time. Do 10, and see what it costs. Might be cheaper than using the loop
Or do 10 direct mapping assignments in the code and see what that costs.
If you just initialize 100 directly rather than the loop, might be cheaper
And I am sure after all this, someone will come on and say "super easy, just do this" lolol.
Mar 11, 2021, 3:29 AM
viable ideas
yet the numbers say otherwise. Execution cost might be cheaper for 20000, but the overall transaction cost is a 500000 higher due to overheads.
Mar 11, 2021, 4:39 AM

© 2024 Draquery.com All rights reserved.