You should always monitor the balance of the account that hosts the library. It should be sufficient for at least 10 years.
Fee savings
Library cells help reduce fees in two main ways.- Forwarding fees. For example, in jettons, the StateInit must be forwarded with each transfer, resulting in high forwarding fees. Moving the code into a library cell significantly reduces these fees.
- Storage costs. Because a library must be hosted in the Masterchain, where storage is approximately 1,000 times more expensive than in the Basechain, hosting a copy of the code in the Basechain may be cheaper if there are fewer than 1,000 instances of the contract. The 1,000 factor is not constant and is subject to change. Consult blockchain config parameter 18 for the latest value. However, due to the savings on forwarding fees, you should calculate whether this approach is cost-effective for your specific use case.
code
, it is automatically dereferenced on first access. This allows you to replace part of the contract code—or even the entire code—with a library cell.
Replacing the entire code with a library cell is widely used in TON smart contracts.
Some common examples:
- USDT (and other popular jettons) Jetton wallet contracts
- The Order contract in Multisig v2
- NFT item contracts in popular collections
code
cell in an explorer.
Fragment of USDT jetton-wallet account
SPECIAL
(indicating it is an exotic cell). Following this is the hexadecimal representation of the library cell’s internals. The first byte equals 2, indicating that it is a library cell, and the remaining bytes contain the hash of the referenced cell.
Here you can see that the entire contract code consists of the 8‑bit tag equal to 2 and a 256‑bit representation hash of the referenced cell.
If you don’t want to put the entire code in a library cell, you can make only part of it a library cell. For example, if the same function is used in multiple different contracts, it makes sense to turn it into a library. However, you will likely need to set up the build process for such code yourself.
Using @ton/core
You can construct a library cell entirely in TypeScript using the@ton/core
library. Here’s how to do it in a Blueprint project:
Publishing an ordinary cell in the Masterchain library context
The following example was taken from multisig v2 repository.set_lib_code(lib_to_publish, 2);
. This function call publishes an ordinary cell with the flag set to 2
, which indicates that the library is public and can be used by anyone.
Note that this contract becomes bricked after the cell is published, so no further operations on this contract can be performed.
Testing in Blueprint
When testing smart contracts locally, there are two ways to register libraries in your blockchain environment.Option 1: Automatic Library Deployment
Enable automatic library detection by passing theautoDeployLibs
flag when creating the blockchain:
Option 2: Manual Library Deployment
IfautoDeployLibs
is not enabled, you’ll need to register libraries manually: