-4

I send transactions programmatically and I need to know exactly how much the fee is going to be. I managed to figure out how to calculate fees for ordinary transaction ((transfer cost + receipt creation cost) * 2), but now I'm struggling with a case where I need all my funds out of the account without deleting it. As I understand, in this case there must be a storage rent left on the account. However, I can't really figure out how to calculate that rent. There is a value returned from 'EXPERIMENTAL_protocol_config' method that seem to be connected to rent - 'storage_amount_per_byte', which implies that each byte costs 10000000000000000000 yocto, and also I can get 'storage_usage' from 'query' method with request type 'view_account', which is supposedly indicated how many bytes my account uses (which is 182). But whenever I try to send a transaction, I get a 'NotEnoughBalance' error that states that transaction cost is higher than the balance, but just by 669547687500000000 yocto. Whatever I do, I can't understand where this number comes from. No combination of fees from aforementioned 'EXPERIMENTAL_protocol_config' method yields this number. There seems to be little to no decent documentation on transaction fee calculation, except for some 'fixed' values for most used actions. If you have any info on fee/storage rent calculation - I'll be thankful for it.

Old Man
  • 11
  • 3
  • Through trial and error, I managed to figure out the quite obvious price of bytes-based storage price - that would be 182 * 10000000000000000000 = 1820000000000000000000. However, the part with 669547687500000000 is still a mystery. I tried leaving 1820669547687**4**00000000 on the account, just 100000000 less than supposedly needed, and the transaction failed. So the 1820669547687**5**00000000 is the bare minimum in my case. If you find this number familiar or know where it comes from - please share. – Old Man Jun 22 '22 at 10:27

1 Answers1

1

Through a random chance, I managed to find out the name that the number '6695476875' is referred to as, 'Reserved for transactions', (in gas, not tokens) as in the official wallet (wallet.near.org). God knows why it is reserved, neither docs.near.org, nomicon.io nor wiki.near.org have any info regarding this 'reservation' and this number is never mentioned in any RPC API method. This number is also never mentioned in 'near-api-js' lib, so I really have no idea if devs are even aware of it. Anyway, since the title of this problem is 'How to calculate storage rent', the answer is something like this:

  1. You get account info from 'query' method of RPC API (here's the doc) and take the "storage_usage" value (this is the amount of bytes that your account takes up on the blockchain).
  2. You get protocol info from 'EXPERIMENTAL_protocol_config' method of RPC API (here's the doc) and take the "storage_amount_per_byte" value.
  3. You multiply the amount of bytes by the storage_amount_per_byte and add the magic 669547687500000000 number to it.

And the resulting number is the least amount of tokens that you must have at your account at any time.

I don't know why it is a common practice to make lives of developers harder in blockchain industry, but this is a good example of such practice.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Old Man
  • 11
  • 3