Reading from network
Introduction
This guide will walk you through reading data from TON Blockchain. You'll learn how to:
- Fetch account information
- Call
get methods
- Retrieve account transactions
By the end, you'll understand how to interact with TON HTTP-based APIs. In this guide, TON Center is used—a fast and reliable HTTP API for TON.
Set up environment
First, visit the installation pages and install Node.js and npm for your OS. Check that the installation is correct by running the following commands:
node -v
npm -v
Versions of node
and npm
should be at least v20
and v10
correspondingly.
Project setup
Let’s set up your project structure:
- Create a new directory for your project and navigate into it.
- Initialize a Node.js project.
- Install the required dependencies.
- Initialize TypeScript configuration.
Run these commands in your terminal:
mkdir reading-from-ton && cd reading-from-ton
npm init -y
npm install typescript ts-node @ton/ton @ton/core @ton/crypto
npx tsc --init
To run a TypeScript script saved as script.ts
in your current directory, run:
npx ts-node script.ts
Reading account information
Account information includes the balance
, state
, code
, and data
.
balance
: The amount of TON the account holds.state
: Can be one of:- Nonexist: The address has no data.
- Uninit: The address has a balance but no smart contract code.
- Active: The address is live with code and balance.
- Frozen: The address is locked due to insufficient balance for storage costs.
code
: The contract's code in raw format.data
: Serialized contract data stored in a Cell.
Account state may be obtained using the getContractState
method.
Implementation
Create a new file 1-get-account-state.ts
:
import { Address, TonClient } from "@ton/ton";
async function main() {
// Initializaing TON HTTP API Client
const tonClient = new TonClient({
endpoint: 'https://testnet.toncenter.com/api/v2/jsonRPC',
});
const accountAddress = Address.parse('0QD-SuoCHsCL2pIZfE8IAKsjc0aDpDUQAoo-ALHl2mje04A-'); // Replace with any address
// Calling method on http api
const state = await tonClient.getContractState(accountAddress);
console.log('State: ', state.state);
console.log('Balance: ', state.balance);
console.log('Data: ', state.data?.toString('hex'));
console.log('Code: ', state.code?.toString('hex'));
}
main();
Run this example using the following command:
npx ts-node 1-get-account-state.ts
Expected result
State: active
Balance: 3722511000883n
Data: b5ee9c7241010101002...fd1e976824402aa67b98
Code: b5ee9c7241021401000...c9ed54696225e5
Calling get methods
Get methods are special functions in smart contracts that allow you to observe the current state of a smart contract. Their execution doesn't cost any fees and can't change the smart contract's storage.
The result of calling a get method from the TON HTTP API comes in stack format and may be deserialized one by one using readNumber()
or a similar function.
Implementation
Create a new file 2-call-get-method.ts
:
import { Address, TonClient, TupleBuilder } from "@ton/ton";
async function main() {
// Initializaing TON HTTP API Client
const tonClient = new TonClient({
endpoint: 'https://testnet.toncenter.com/api/v2/jsonRPC',
});
// Building optional get method parameters list
const builder = new TupleBuilder();
builder.writeAddress(Address.parse('0QD-SuoCHsCL2pIZfE8IAKsjc0aDpDUQAoo-ALHl2mje04A-'));
const accountAddress = Address.parse('kQD0GKBM8ZbryVk2aESmzfU6b9b_8era_IkvBSELujFZPsyy')
// Calling http api to run get method on specific contract
const result = await tonClient.runMethod(
accountAddress, // address to call get method on
'get_wallet_address', // method name
builder.build(), // optional params list
);
// Deserializing get method result
const address = result.stack.readAddress();
console.log(address.toRawString());
}
main();