In erc20 contracts, conventionally balances mapping saves the current balance the users are holding.

mapping (address => uint256) private balances;

If this contract has a lot of users( which means that the balances variable is holding a lot of information) will copying the balances variable to another variable bad for gas?

If it is bad, will it go over the limit of 'block gas limit'?

How bad is it?

Or is it okay to program it with this kind of design?

I am writing a contract that might have to copy balances mapping frequently and put it into an array.

like this

mapping (address => uint256) private balances; // this is the variable for token balance 
                                               // which holds a lot of information

struct RoyaltyInfo {
    mapping (address => uint256) private balances_copy;
    uint256 private royalty;

RoyaltyInfo[] private royaltyInfomation;

function sendRoyalty() public payable {
    royaltyInformation.push(RoyaltyInfo(balances, msg.value)); // pushing current balances state into array

If I push balanes mapping into royaltyInfo array. Am I copying the variable and putting it in the array or am I just putting the pointer of variable balances into the array?

Thanks in advance

1 Answer 1


Yes it's quite expansive. From yellow paper page 25 : https://ethereum.github.io/yellowpaper/paper.pdf

Gsset 20000 Paid for an SSTORE operation when the storage value is set to non-zero from zero.

So 20k gas for each entrie.

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.