near-explorer icon indicating copy to clipboard operation
near-explorer copied to clipboard

"Attached Gas" for a transaction should cover all gas spent on the transaction

Open jakmeier opened this issue 2 years ago • 2 comments

Story

As a user, I want to fully understand the gas costs of a transaction, by looking at the explorer.

Today, when adding up all tokens burnt and all refunds displayed in a transaction that includes function calls, the sum is greater than the display attached gas divided by gas price. This is unintuitive and frustrating. (Nothing is worse than numbers not adding up in a system that I trust my money.)

I appears that today any transaction that includes a contract call just displays the prepaid gas for the contract call and ignores receipt conversion costs entirely. For other transactions, it seems to display the correct value of attached gas. At least as far as I can tell by looking at the rounded numbers displayed for attached gas in the UI.

The solution isn't quite so obvious, though. When adding up all gas costs correctly, different gas prices have to be considered in the formula. I wrote a detailed explanation about this formula in a separate document.

As for this story, which aims to make gas costs easier to understand in the Web UI, I would suggest to add another filed called "Prepaid tokens". That field would contain the sum of all purchased gas times the corresponding price for each purchase. This number would also be equal to the sum of all refunds + the sum off all tokens burnt. The UI hint could be something like "Pessimistically inflated price paid to purchase attached gas".

Acceptance Criteria

  • [ ] The displayed number for gas in a transaction includes "receipt conversion cost", "prepaid receipt execution gas", and "prepaid gas" for contract calls.
  • [ ] The displayed numbers for "refunded tokens" and "burned tokens" are comprehensively related to what is displayed in "attached gas" and/or "prepaid tokens".
  • [ ] Tested that below example is now correct and comprehensible in the explorer

Detailed Example

Take this transaction as an example: https://explorer.near.org/transactions/mGPAZ9oTEMSFcvZxEyZAC8wRGwFAfKfSb5ed8nGEmwk#6iijMhCAydeA2sxvo8Bm6FUNr1y2EtxwxrwkmQZFZr1b

Gas attached to the function call (already displayed):

  • prepaid_gas: 250Tgas
    • purchased at pessimistically inflated price of 451542320 yoctoⓃ / gas = 0.11288558 Ⓝ

Additional gas purchased for transaction (today missing in attached gas):

  • gas_burnt: 2.427972426482 Tgas for creating the new receipt
    • purchased at the real gas price of 100000000 yoctoⓃ / gas = 0.0002427972426482 Ⓝ
  • prepaid_exec_fee: 2.427972426482 Tgas to cover execution of the new receipt
    • purchased at the pessimistically inflated price of 451542320 yoctoⓃ / gas = 0.001096332302349711718 Ⓝ

The sum of these values gives the total tokens a transaction can spend. It is exactly 0.11422471 Ⓝ (no rounding)

On the other side of the balance sheet, there are refunded tokens and burnt tokens (already all displayed in explorer)

  • 0.000242797242648 Ⓝ burnt for tx -> receipt conversion
  • 0.0012051368015055 Ⓝ burnt for applying the first action receipt containing the function call
  • 0.0001175204792914 Ⓝ burnt for applying the second action receipt with delete/add key actions and contract deployment
  • 0.11224612080217670459776 Ⓝ refunded for unspent gas after first receipt
  • 0.00041313421937610712048 Ⓝ refunded for price difference between purchase gas price and real gas price when applying the second action receipt

Again adding all those up gives exactly 0.11422471 Ⓝ

Note that gas_burnt and prepaid_exec_fee are the same amount in this example. This cannot be relied upon. In future releases, we might change some parameters so that execution fee and sender fee are not the same. (For staking actions, this is already the case.)

The detailed breakdown for these costs are summarised in this table:

| action_receipt_creation_config.send.sir | 108059500000 |   | 100000000 | 0.00001080595 | | function_call_cost.send_fee | 2319861500000 |   | 100000000 | 0.00023198615 | | (len(method)+len(args)) * function_call_cost_per_byte.send_fee | 51426482 |   | 100000000 | 0.0000000051426482 | | prepaid_gas for function call |   | 250000000000000 | 451542320 | 0.11288558 | | action_receipt_creation_config.exec_fee |   | 108059500000 | 451542320 | 0.00004879343733 | | function_call_cost.exec_fee |   | 2319861500000 | 451542320 | 0.001047515644 | | (len(method)+len(args)) * function_call_cost_per_byte.exec_fee |   | 51426482 | 451542320 | 0.00000002322123299 |

A correct display would show the sum of all gas values here (254.86 Tgas) and the sum of all token equivalents for prepaid tokens (0.11422471 Ⓝ).

jakmeier avatar Feb 18 '22 17:02 jakmeier

@jakmeier Thanks for the awesome detailed report! There was a concern that we missed some details around gas reporting. We are going through a thorough revamp of the data we report and the terminology we use. We actually missed a lot such a thorough breakdown. I will definitely use your report to improve on things!

@telezhnaya You might be interested in this one (no action items for you)

@AustinBaggio @reefoh @luixo @shelegdmitriy We will need to do some diligence work around the gas reporting and terminology.

Let's get back to it the week after the next one (Feb 28th)

frol avatar Feb 18 '22 18:02 frol

For the context: https://github.com/near/near-analytics/issues/19#issuecomment-1101439663

telezhnaya avatar Apr 18 '22 14:04 telezhnaya