A friend and I were thinking about transaction costs, is there any attached cost to send a transfer to a new address that has not been stored on chain before? So we started digging.
First off we called transfer to a new address and another one that has been used before, as expected the cost was 21000 for both of them.
New address: https://ropsten.etherscan.io/tx/0xec05af75ec938aa1cc78612b13d8d2e3c8f4212f60173161450297c9a25ea5a3
Miner (used before) address: https://ropsten.etherscan.io/tx/0x7fe1c8c699901923d89e07a9b66e7d952c67064bcc9d86bf84bad07cac2391b1
Then we tried to made the transfer from an SC, this is the function we are using:
function test (address receiver) public payable {
receiver.transfer(msg.value);
}
Simple enough, we started testing, called the function to a new address:
https://ropsten.etherscan.io/tx/0x38dabd569350fa985e11f892f7175ad09a32a0c067b993f68152f34d7ca4dcb3
First surprising thing was the cost to execute this function, was 55356 gas, much higher then the 21000 we saw before, we called it again:
https://ropsten.etherscan.io/tx/0xa2d23ea51102cc98f92b6ef89ed452a5be0e0ebbc26c136fc43f9a5a67a99a52
To our surprise, now gas costs were 30356 much less than before, why??
We tried to call the function on a used address (that should be already stored on chain) to see if sending to a new address was the differential factor, we did it twice:
https://ropsten.etherscan.io/tx/0xf9eb2e6536db2ec933ac21fb5bd8c422b6ae3b811f4bd72be4a55e02a6aec227
https://ropsten.etherscan.io/tx/0x4ef30764af81df4dc0dcc98bd4db5424c0c2bea99257df829b1c347a8764048f
And it costed 30356 in both.
Why does this happen, why sending ether to a new address from a SC costs over 50% more than sending to a used address?
transfer
call shouldn't depend on whether the recipient has been the recipient of earlier transfers, but it's hard to give you an explanation without seeing the code.