How expensive are loops in solidity? (in terms of gas usage). I need to loop through ~1000 wallet address, and send each of them some tokens.

my code:

struct User {
    uint32 frozenAt ; 
    uint earnedTokens;
    bool isValid;

mapping(address => User) users;
address[] public usersAddresses;

uint reciversLength = usersAddresses.length;
for (uint i = 0; i < reciversLength; i++) {
    User storage theUser = users[usersAddresses[i]];

    // transfer(usersAddresses[i], theUser.earnedTokens);

should i be worry about gas usage? anything i can do to optimize this loop?

maybe instead of a truct for each user i should do couple of mappings? (i read somewhere that this is more efficient)

I was wondering if the better way would be grabbing the data from a node.js server, and send the token for each user using tronweb. would doing this worth the trouble?

any help would be appreciated

  • It's gonna cost more if you do this one wallet at a time. Your problem is not the gas-cost, but the potential of exceeding the block gas-limit, in which case, the transaction (your function-call) will not be executed. What you can do is to send the wallet-array as input to the function. Then, if the transaction fails due to the block gas-limit, you can simply retry with a smaller array. Or even better, before executing the transaction, call estimateGas on it and make sure that it doesn't exceed the block gas-limit. Nov 5, 2019 at 12:29
  • Just bare in mind to restrict the permission of calling this function (assuming that you wouldn't want others to call it with their input). Nov 5, 2019 at 12:32

1 Answer 1


Loops themselves are not as expensive as sending some tokens to number of addresses.

Usually, in order to send some tokens to an address, you need to update at least two storage slots: sender's balance and recipient's balance. While updating sender's balance may be optimized in case the same sender does several transfers in one transaction, recipient balance update will cost 20K gas in case original valance was zero.

In case you call transfer from a contract other that token smart contract itself, the call will also consume significant gas.

In case there is a way to calculate transfer amount from recipient address, it could be cheaper to implement transfers in lazy way, so tokens will be deposited to recipient address only when the recipient will try to use them at the first time.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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