0

I know about gas limits per block, but I can't wrap my head around this problem right now.

If I have a token contract that needs to update the balance of every address, is it possible to have a function create a new balance map with all of the new balances? This seems like a ton of data to manipulate in a single transaction...

If I have a balance map with say 1,000,000 addresses, how much data would this be represented in kB? How do you calculate it? If you can't replace the entire balance map because of gas limits, how do you go about this?

1

Storing a word (SSTORE opcode) costs 5000 gas if changing a non-zero to another non-zero. (It's more if the old value was a zero.)

5000 gas * 1,000,000 = 5,000,000,000 gas

ETH Gas Station says 5 gwei is a reasonable price per gas.

5,000,000,000 * 5 gwei = 25,000,000,000 gwei = 25 ether

At ~$1300 per ether, this would cost $32,500. You can't actually do a transaction that costs this much gas because there's a limit, but even if you spread it out across many transactions (potentially from many users), this is extraordinarily expensive to do.

Depending on what your goal is, there may be a better way. For example, if you want to periodically increase everyone's balance, you could just store a value extra and compute balances on the fly as balances[address] + extra. It's hard to suggest a specific scheme without knowing what you're trying to do.

  • Yea that's really exactly what i'm trying to do but do it daily...This is an interesting approach, but this wouldn't work with balance checking services like MEW and Etherscan, right? – Elliott McNary Jan 15 '18 at 3:48
  • It should. Presumably those tools call balanceOf to get the current balance for an account, so as long as you make sure that function returns the right thing, you should be good. (It could be that right now you just have a public mapping, but you should instead use a private mapping and implement something like function balanceOf(address who) public view returns (uint256);) – user19510 Jan 15 '18 at 3:49
0

Instead of having a single function update all of the balances, it's probably better to make each token holder responsible for making the transaction themselves to update their own balance and redeem their tokens or do whatever logic it's required by the contract. Gas limits will prohibit running such large number of computations like what you described.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.