get_collection_data get-method on an NFT collection), and for any logic that is a part of a smart contract but is needed off-chain (get_nft_address_by_index get-method on an NFT collection). Under the hood of APIs, this happens by fetching the actual state of the smart contract from the blockchain and executing TVM to get the result. This process is purely read-only and does not modify the blockchain state in any way.
Defining
Get methods are processed by the function selector. By convention, the ID of a get method is calculated ascrc16("name") | 0x10000 where name is set by the developer. This simplifies practical usage because a human-readable name has to be used instead of some arbitrary number.
The algorithm used for hashing is CRC‑16/XMODEM (
poly=0x1021, init=0x0000, refin=false, refout=false, xorout=0x0000).PROGRAM{ from the Fift assembler that handles function selector logic.
Executing
In order to execute a get method, the actual state of the smart contract has to be fetched, and TVM has to be executed with c7 initialized and the desired parameters pushed on the stack.Local way
With all the required values known, it is possible to execute a get method completely locally. A minimal example that uses a placeholder c7 for simplicity, as it is only necessary when the get method uses data from it during execution:NOW or MYCODE, the c7 tuple should be populated according to its structure.
Decentralized way
The process of fetching the actual contract state and initializing c7 can be handled by liteserver for easier execution. To execute a get method via liteserver, the request follows theliteServer.runSmcMethod TL schema.
In that request, params:bytes is a BoC of a serialized VmStack object containing the stack with arguments.
The response follows the liteServer.runMethodResult TL schema. Apart from the values used for initialization and proofs, the result is included as result:mode.2?bytes, which is a BoC of a serialized VmStack object, similarly to the request.
An example execution via liteclient that handles serialization:
High-level API
The easiest path is using a high-level API, such as TON Center. It has a/api/v3/runGetMethod endpoint that takes a smart contract address, a get method name, and arguments and returns the resulting stack. Example usage: