Skip to main content
In TON, a contract’s performance is defined by its gas consumption, so it’s important to design your logic efficiently. Unlike many other blockchains, TON also requires you to pay for storing contract data and forwarding messages between contracts.

Gas consumption

As you develop and iterate on a contract, even small changes to its logic can affect both gas usage and data size. Monitoring these changes helps ensure that your contract remains efficient and cost-effective.

Gas metrics reporting

To simplify tracking changes in gas usage and data size, we’ve introduced a reporting system that lets you collect and compare metrics across different versions of a contract. To enable this, write test scenarios that cover the contract’s primary usage patterns and verify expected behavior. This approach is sufficient to gather relevant metrics, which you can later use to compare performance changes after updating the implementation. Before running the tests, a store is created to collect metrics from all transactions generated during the tests. After test execution, the collected metrics are supplemented with ABI information from the snapshot, and a report is generated based on this data. While more metrics are collected, the current report format includes gasUsed, cells, and bits, which correspond to the internal metrics compute.phase, state.code, and state.data.

Metrics comparison example

To see how gas metrics can be collected and compared in practice, let’s walk through a complete example. Start by creating a new project using npm create ton@latest:
Note:
  • The -y flag skips prompts and accepts defaults.
  • --type specifies the template (e.g., func-counter).
  • --contractName sets the contract name.
Alternatively, you can run:
This command scaffolds a project with a basic counter contract at contracts/sample.fc. It defines a simple stateful contract that stores an id and a counter and supports an increase operation.
sample.fc

Generate a gas report

Let’s now generate a gas usage report for the contract. Run the following command:
This runs your tests with gas tracking enabled and outputs a gas-report.json with transaction metrics.

Storage fee calculation

You can use the cells and bits values from the report to estimate the storage fee for your contract. Here’s the formula:
To try this in practice, use the calculator example.

Regenerate the gas report

Note that the op::increase method appears in the report as the raw opcode 0x7e8764ef. To display a human-readable name in the report, update the generated contract.abi.json by replacing the raw opcode with the name increase in both the messages and types sections:
Once you’ve updated the contract.abi.json file, rerun the command to regenerate the gas report:
Now the method name appears in the report as increase, making it easier to read:

Save a snapshot for future comparison

To track how gas usage evolves, you can create a named snapshot of the current metrics. This allows you to compare future versions of the contract against this baseline:
This creates a snapshot file in .snapshot/:

Optimize the contract and compare the metrics

Let’s try a simple optimization — adding the inline specifier to some functions. Update your contract like this:
Now regenerate the gas report. Since we already created a snapshot labeled v1, this report will include a comparison with the previous version:
You see a side-by-side comparison of gas usage before and after the change:

Project setup instructions

If your project already exists, you need to configure jest to collect gas metrics. You can do this in one of two ways:

Option 1: update the existing jest.config.ts

Add the necessary environment and reporter settings:
jest.config.ts

Option 2: create a separate config gas-report.config.ts

If you prefer not to modify your main jest.config.ts, you can create a dedicated config file:
gas-report.config.ts
When using this separate config, pass it using the --config option:

Collect metrics manually

You can collect metrics manually using the low-level API from @ton/sandbox.
collect-metrics.ts
For more details, see the Collect Metric API documentation.