Gas – Advanced

To Share and +4 nLEARNs

Costs of complex actions

Let’s cover some more complex gas calculations: deploying contracts and function calls.

Deploying Contracts

The basic action cost includes two different values for deploying contracts. Simplified, these are:

deploy_contract_cost: 184765750000,
deploy_contract_cost_per_byte: 64572944,

These values can be queried by using the protocol_config RPC endpoint.

The first is a baseline cost, no matter the contract size. Keeping in mind that each need to be multiplied by two, for both send and execute costs, and will also require sending & executing a receipt, the gas units comes to:

2 * 184765750000 +
2 * contract_size_in_bytes * 64572944 +
2 * 108059500000

(Divide the resulting number by 10¹² to get to TGas!)

Note that this covers the cost of uploading and writing bytes to storage, but does not cover the cost of holding these bytes in storage. Long-term storage is compensated via storage staking, a recoverable cost-per-byte amount that will also be deducted from your account during contract deployment.

Deploying a 16kb contract requires 2.65 TGas (and thus 0.265mN at minimum gas price) for the transaction fee, while 1.5N will be locked up for storage staking.

Function calls

Given the general-purpose nature of NEAR, function calls win the award for most complex gas calculations. A given function call will use a hard-to-predict amount of CPU, network, and IO, and the amount of each can even change based on the amount of data already stored in the contract!

With this level of complexity, it’s no longer useful to walk through an example, enumerating each (see ext_costs under wasm_config using the protocol_config RPC endpoint) of the gas calculations as we go (you can research this yourself, if you want). Instead, let’s approach this from two other angles: ballpark comparisons to Ethereum, and getting accurate estimates with automated tests.

**How much of the gas fee goes as a 30% reward to the smart contract account?**

The NEAR Whitepaper mentions that [30% of all gas fees](https://near.org/papers/the-official-near-white-paper/) go to smart contract accounts on which the fees are expensed.

This amount can be calculated for function calls in two ways:
1. Summing all values in the gas profile
2. Taking the total gas burnt for the transaction and subtract the static execution gas (which is equal to the amount of gas spent on sending the receipt(s)) from it. Both these numbers are available on the [NEAR Explorer](https://explorer.near.org/) overview page for a transaction.

The second approach is shorter, and quite possibly easier to remember. So here’s an example:

– An account calls the method `submit` on `aurora`
– Converting the transaction to receipt burned a total of ~0.00024Ⓝ
– Executing the receipt burned a total of ~0.00376Ⓝ

The 30% reward for the smart contract owner (in this case aurora) would be: (0.00376Ⓝ – 0.00024Ⓝ) * 0.3 = 0.001056Ⓝ

This transaction can also be found [here](https://explorer.near.org/transactions/GzRn9yhDaQ8f3ReJguCBGxdi4iJEeBguJ5MWufMcu1JP) on NEAR Explorer, feel free to have a look around!

For calls involving multiple contracts, calculating the reward for each contract with this method would not be possible with the data shown on NEAR Explorer (June 2022) as the explorer does not show the conversion cost for the second (and other) receipt(s).

Ballpark Comparisons to Ethereum

Like NEAR, Ethereum uses gas units to model computational complexity of an operation. Unlike NEAR, rather than using a predictable gas price, Ethereum uses a dynamic, auction-based marketplace. This makes a comparison to Ethereum’s gas prices a little tricky, but we’ll do our best.

Etherscan gives a historic Ethereum gas price chart. These prices are given in "Gwei", or Gigawei, where a wei is the smallest possible amount of ETH, 10⁻¹⁸. From November 2017 through July 2020, average gas price was 21Gwei. Let’s call this the "average" gas price. In July 2020, average gas price went up to 57Gwei. Let’s use this as a "high" Ethereum gas fee.

Multiplying Ethereum’s gas units by gas price usually results in an amount that’s easy to show in milliETH (mE), the same way we’ve been converting NEAR’s TGas to milliNEAR. Let’s look at some common operations side-by-side, comparing ETH’s gas units to NEAR’s, as well as converting to both the above "average" & "high" gas prices.

Operation ETH gas units avg mE high mE NEAR TGas mN
Transfer native token (ETH or NEAR) 21k 0.441 1.197 0.45 0.045
Deploy & initialize a [fungible token] contract [1.1M] 23.3 63.1 [9] 0.9 (plus 1.5Ⓝ in [storage staking])
Transfer a fungible token [~45k] 0.945 2.565 [14] 1.4
Setting an escrow for a fungible token [44k] 0.926 2.51 [8] 0.8
Checking a balance for a fungible token 0 0 0 0 0

Generate comment with AI 2 nL
Scroll to Top
Report a bug👀