1

I want to create contract, which will be able to store data like array of:

struct Funder {
        address addr_first;
        address addr_second;
    }

How can I calculate gas fee for transactions which inserts additional Funder to my array ?

Should I pay more and gas for each insertion or I should always pay a fix gas price for all insertions to my storage ?

For example, if I have to pay 10 Gas for first insertion, how many gas should I pay when I'll have thousands of Funders and want to insert another one?

0

1 Answer 1

2

The cost depends on the actual function that does the writing, as you pay for every computational step. If you had a simple storage variable of your struct and a function

Funder fdr;

function setFunder(address a1, address a2) {
  fdr.addr_first = a1;
  fdr.addr_second = a2;
}

then you can estimate the gas costs for every call via estimateGas as discussed here.

On lower level, the yellow paper tells us more on page 20: Writing one word (32 bytes for Ethereum) to storage (SSTORE opcode) costs you either 20'000 gas for an SSTORE operation when the storage value is set to non-zero from zero or 5'000 gas for an SSTORE operation when the storage value’s zeroness remains unchanged or is set to zero. I.e. the first time you call the function it will be more expensive than all following times that you reset it.

1
  • when working . with . arrays what does this mean? If I replace an array, does it cost me to replace the array for every element in the array or is it one SSTORE operation for the whole batch? Commented Sep 14, 2019 at 22:02

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