Documentation
Chevron icon
Neo Blockchain Concepts
Chevron icon

Neo Blockchain Fees

Jun 17, 2024 11:39:01 PM

Neo N3 Fee Calculation

Neo Blockchain divides fees into two main categories: network fees and system fees. Network fees prioritize transactions, while system fees cover script executions. Fees include Invocation Fees and Verification Fees, based on CPU Fees and Storage Fees. The network committee, a node group maintaining the network, can adjust fees through the Policy native contract.

SDKs and Wallets will automatically calculate the fees before sending a transaction. If you are a developer, the following sections provide more details on fee calculation.

Using SDKs to Calculate Fees

All transactions must include fees. Failing to calculate fees correctly can result in transaction failure. The Neo SDKs provide methods to calculate fees, such as testInvoke. Note that this calculation is done outside the smart contracts, using an RPC API call.

The following example demonstrates how to calculate the fees of a NEO transfer using the Neo SDKs. The calculate fee method won’t send the transaction but will return the fees required to execute it.

The first signer in the signers field is the account that will pay for the transaction fees. Setting the signers field is necessary when the transaction requires a signature. Not providing a signer may result in incorrect fee calculation, due to differences in the execution steps between signed and unsigned transactions.

It’s not necessary to have access to the user’s private key. Setting a valid account object is enough to calculate the fees. In this example, we are loading the account using the address, without the private key.

Setting Fees Manually

In some cases, the SDKs may not calculate the fees correctly. In such cases, developers can override the fees or add extra fees to ensure the transaction is processed.

This can be achieved by adding an extra fee amount or by setting the network fees directly in the transaction object.

Calculating Fees using Contract Signers

When a transaction requires a smart contract signature, the signers field must include the contract’s script hash and an empty verification and invocation script. This will trigger the contract’s verify method.

The following example demonstrates how to calculate fees for a transaction requiring a smart contract signature.

Use a testInvoke to validate the transaction outcome to ensure the fees will be calculated using a valid execution path.

Fee Calculation Formulas

Internally, system fees and network fees are calculated using different formulas. The following sections provide more details on each fee type.

System Fees

Linked to computational costs, system fees are required for a transaction’s execution, including CPU and storage. These fees are burned. System fees depend on transaction steps, each with a specific cost, and encompass verification and invocation fees.

  • Formula: SystemFee = VerificationFees + InvocationFees

Invocation Fees

Charged for CPU and storage usage, these fees are configurable through network votes.

  • Formula: Invocation Fees = CPU Fees(Transaction Script) + Storage Fees

Network Fees

Based on transaction size, network fees prioritize transaction processing, especially under high network loads.

  • Formula: NetworkFee = VerificationFees + (TransactionSize * FeePerByte)

Verification Fees

For verifying user accounts or smart-contract ownership. Costs are calculated from the account Verification Script or the execution of the verify method in a smart contract. Verification fees are charged per execution and are capped at 0.5 GAS.

  • Formula: VerificationFees = CPU Fees(Witnesses Script) * 30

Note: Due to limitations, the SDKs may not calculate the network fees correctly when the signer is a smart contract. In such cases, run a few tests and set the network fees manually.

CPU Fees

Calculated from verification and invocation script executions combined in the Witness field.

Storage Fees

Applied for storage updates during transactions, charged per byte added or replaced, with discounts for data replacement.

  • Inserted Bytes Fee: Charged for new storage data.
    • Formula: InsertedBytesFee = (Key Size + Value Size) * Fee per Byte
  • Replaced Bytes Fee: Charges for data replacement, with a discount.
    • Formula: ReplacedBytesFee = FeePerByte *1 + ((ReplacedBytesSize - 1) * FeePerByte * Discount)