DappLib.sol
Overview
DappLib.sol is a Solidity library designed to facilitate the creation and management of decentralized application (dapp) configurations on the TON blockchain. The library provides utility functions to compose and derive the smart contract state initialization and addresses for DappConfig contracts. It primarily focuses on generating the correct TvmCell state initialization data and computing the unique address for a dapp configuration contract based on a given dapp identifier and contract code.
This file interacts closely with the DappConfig contract, enabling other parts of the system to instantiate or reference dapp configuration contracts deterministically by their unique dapp ID.
Library: DappLib
DappLib is a Solidity library that contains three main public functions:
Properties
versionLib(string constant):
The version of the library, set to"1.0.0".
Functions
calculateDappConfigAddress
function calculateDappConfigAddress(TvmCell code, uint256 dapp_id) public returns(address)
Purpose:
Computes the deterministic blockchain address of aDappConfigcontract instance for a given dapp ID and contract code.Parameters:
code(TvmCell): The smart contract code cell of theDappConfigcontract.dapp_id(uint256): A unique identifier for the dapp.
Returns:
address: The standard address (with workchain ID 0) corresponding to the computed contract state.
Usage Example:
TvmCell dappConfigCode = /* obtained or compiled DappConfig code */; uint256 myDappId = 123; address dappConfigAddr = DappLib.calculateDappConfigAddress(dappConfigCode, myDappId);Details:
Internally, this function invokescomposeDappConfigStateInitto generate the full state initialization cell (TvmCell), then computes its hash to derive the contract address using the standard address format for workchain 0.
composeDappConfigStateInit
function composeDappConfigStateInit(TvmCell code, uint256 dapp_id) public returns(TvmCell)
Purpose:
Composes the full state initialization cell (TvmCell) required to deploy or reference aDappConfigcontract for a specific dapp ID.Parameters:
code(TvmCell): The original smart contract code cell ofDappConfig.dapp_id(uint256): The unique dapp identifier.
Returns:
TvmCell: The encoded state initialization containing the contract code with embedded salt and the contract's ABI.
Usage Example:
TvmCell stateInit = DappLib.composeDappConfigStateInit(dappConfigCode, 123);Details:
This method builds thecodewith a salt by callingbuildDappConfigCode(which encodesdapp_idas a salt), then encodes a state initialization structure using theabi.encodeStateInitfunction. Thecontrfield points to theDappConfigcontract, and no variable initialization parameters are provided (varInit: {}).
buildDappConfigCode
function buildDappConfigCode(TvmCell originalCode, uint256 dapp_id) public returns (TvmCell)
Purpose:
Embeds thedapp_idas a salt into the original contract code cell to create a unique version of the code for the dapp configuration.Parameters:
originalCode(TvmCell): The originalDappConfigcontract's code cell.dapp_id(uint256): The unique identifier to salt the code.
Returns:
TvmCell: The modified contract code cell with the salt applied.
Usage Example:
TvmCell saltedCode = DappLib.buildDappConfigCode(dappConfigCode, 123);Details:
The function usesabi.encodeto encode thedapp_idinto a cell (finalcell) and then applies it as a salt to the original code usingabi.setCodeSalt. This salted code ensures that each dapp configuration contract is uniquely identified by itsdapp_idin its code hash.
Implementation Details
The library uses the TON Virtual Machine (
TvmCell) data structure for handling the smart contract code and state initialization.The use of
abi.setCodeSaltwith the encodeddapp_idas salt ensures that each deployedDappConfigcontract has a unique code hash and therefore a unique address.Addresses are created using
address.makeAddrStdfor workchain 0 with the hash of the state initialization cell.No variable initialization (
varInit) is used, indicating that thedapp_idis embedded purely in the code (via salt), not as a separate contract variable.
Interaction with Other Components
The library imports the
DappConfig.solcontract, which defines the actual dapp configuration contract whose instances are created or referenced via this library.Functions in
DappLibare intended to be used by deployment scripts, factory contracts, or other system components that need to deterministically calculate the address or state init of aDappConfigcontract for a given dapp ID.By providing deterministic address calculation, the library facilitates trustless referencing of dapp configuration contracts without requiring on-chain lookups or storage.
Visual Diagram
classDiagram
class DappLib {
+string versionLib
+calculateDappConfigAddress(code: TvmCell, dapp_id: uint256) address
+composeDappConfigStateInit(code: TvmCell, dapp_id: uint256) TvmCell
+buildDappConfigCode(originalCode: TvmCell, dapp_id: uint256) TvmCell
}
DappLib ..> DappConfig : uses
The diagram shows
DappLibas a library with its three main public functions and a constant version string.The dependency on the
DappConfigcontract is indicated with a dashed arrow labeled "uses."
For further details on smart contract deployment and address derivation on the TON blockchain, refer to the topics on TON Addressing and StateInit and Smart Contract Salted Code.