Search code examples
ton

TON supports multiple workchains - which one should I use in my code?


According to TON whitepaper, the TON blockchain network supports multiple chains.

What's the differences between these and which workchain should I use when deploying contracts or reading data from contracts?

When deploying my contract I must to specify which workchain I’m working on, I’m not sure which value to put there:

import { contractAddress } from "ton";

const workchain = ?;
const newContractAddress = contractAddress({ workchain, initialData: initDataCell, initialCode: initCodeCell });

Solution

  • TLDR

    For regular user work, always use workchain 0 - which is the workchain with workchain_id = 0

    What are the different chains in TON?

    • One master chain - the special unique workchain with workchain_id = -1

      Mostly used by network validators for running the PoS elections contracts, regular users don't normally send transactions on this chain.

    • Up to 2^32 workchains - today there's only one with workchain_id = 0 but possibly more in the future

      99.9% of user transactions on TON take place on workchain 0, this is where you should work unless you know exactly what you're doing.

    • Up to 2^60 shardchains per each workchain (they all have the same workchain_id)

      This is an internal implementation detail of TON's infinite sharding (autoscaling). If any of the workchains is under heavy load, it will be split automatically to two shardchains and when the load is reduced, it will be merged back. You would normally not care about this, it happens under the hood. When you deploy contracts or send transactions you don't need to specify the shardchain you're working on, it's calculated by the system automatically.