The best way to get all the pools and general information about them is to use our indexer.
// Some code
import { ApolloClient, InMemoryCache, gql} from "@apollo/client/core";
import { Command, OptionValues } from "commander";
export const POOLS_QUERY = gql`
query PoolsQuery {
pools {
name
address
jetton0 {
address
symbol
decimals
}
jetton1 {
address
symbol
decimals
}
}
}
`;
async function queryPools(options: OptionValues) {
const appoloClient = new ApolloClient({
uri: "https://indexer.tonco.io/",
credentials: 'same-origin',
cache: new InMemoryCache(),
});
const response = await appoloClient.query({ query: POOLS_QUERY });
const appoloPoolList = response.data.pools
console.log(appoloPoolList);
}
Indexer is critical for our system, and we keep it highly available, however, we encourage caching the pool list.
Getting pool APR
For information about the APR of the pool in farming, you need to refer to the URL:
GET api-farming.tonco.io/apr?pool=<pool address>
The answer has “apr” as the base apr and an array of farmings. Farming is considered active if rewardsLeft is not equal to zero. Farming has a property - multiplier, it is a coefficient that denotes how many times farming increases the base apr.
APRfinal=APR⋅i∑farming_multiplieri
In most cases, a pool has only one active farming, but it is possible that it will have several in the future.
This question depends on your requirements, limitations, and infrastructure of choice. Here are 4 options
If you have ever implemented integration with other Algebra EVM projects written in your language of choice - then the output simulation can be bit-precise
a) Estimate Swap 1 TON -> USDT Example (on-chain)
import { Address, toNano, TonClient4 } from "@ton/ton";
import { getHttpV4Endpoint } from "@orbs-network/ton-access";
import { getSwapEstimate, PoolV3Contract, pTON_MINTER } from "@toncodex/sdk";
const POOL_ADDRESS = "EQD25vStEwc-h1QT1qlsYPQwqU5IiOhox5II0C_xsDNpMVo7"; // TON - USDT
const endpoint = await getHttpV4Endpoint();
const client = new TonClient4({ endpoint });
const poolV3Contract = client.open(new PoolV3Contract(Address.parse(POOL_ADDRESS)));
const inputJettonAddress = Address.parse(pTON_MINTER); // TON
const amountIn = toNano(1); // 1 TON
/* pool.jetton0_minter and pool.jetton1_minter from poolState are always sorted, so jetton0 is always first */
const { jetton0_minter } = await poolV3Contract.getPoolStateAndConfiguration(); // TON
const zeroToOne = inputJettonAddress.equals(jetton0_minter); // true
/* estimate 1 TON to USDT swap on-chain */
const result = await getSwapEstimate(amountIn, POOL_ADDRESS, zeroToOne, client);
return result;
b) Estimate Swap 1 TON -> USDT Example (off-chain)
import { Address, toNano, TonClient4 } from "@ton/ton";
import { getHttpV4Endpoint } from "@orbs-network/ton-access";
import { PoolV3Contract, pTON_MINTER, SwapSimulator, TickConstructorArgs } from "@toncodex/sdk";
const POOL_ADDRESS = "EQD25vStEwc-h1QT1qlsYPQwqU5IiOhox5II0C_xsDNpMVo7"; // TON - USDT
const endpoint = await getHttpV4Endpoint();
const client = new TonClient4({ endpoint });
const poolV3Contract = client.open(new PoolV3Contract(Address.parse(POOL_ADDRESS)));
const inputJettonAddress = Address.parse(pTON_MINTER); // TON
const amountIn = toNano(1); // 1 TON
const { jetton0_minter, price_sqrt, tick, tick_spacing, lp_fee_current, liquidity } =
await poolV3Contract.getPoolStateAndConfiguration();
/* pool.jetton0_minter and pool.jetton1_minter from poolState are always sorted, so jetton0 is always first */
const zeroToOne = inputJettonAddress.equals(jetton0_minter); // true
const poolTicks = await poolV3Contract.getTickInfosAll();
const tickList: TickConstructorArgs[] = poolTicks.map((tick) => ({
index: tick.tickNum,
liquidityGross: tick.liquidityGross.toString(),
liquidityNet: tick.liquidityNet.toString(),
}));
const swapSimulator = new SwapSimulator(price_sqrt, tick, tick_spacing, liquidity, lp_fee_current, tickList);
/* estimate 1 TON to USDT swap off-chain */
const result = await swapSimulator.swapExactIn(zeroToOne, amountIn);
return result;
You can use our Kotlin implementation of user-side swap simulation.
# Usage example
# Expecting you have JVM and kotlinc
git clone [email protected]:cryptoalgebra/tonco-demo.git
cd tonco-demo
cd swap_kotlin
./run.sh
Marking transactions for referral tracking
With TONCO v1 It is possible to mark transactions to be able to index them for needs of referral tracking
You have 64 bits of query_id at your disposal. We don't alter it and copy it in the outgoing messages
Forming Messages for Mint
Example: Minting a Position in the TON/USDT Pool Below is an example of mint preparation for the TON/USDT pool within the price range [3.1, 6.5], based on an input amount of 1 TON.
⚠️ Important ⚠️: The validation step inside the example is crucial. Skipping it may result in a loss of funds.
Explorer page:
GraphQL endpoint:
Farming Backend:
Explorer page:
GraphQL endpoint:
Alternatively, if you don’t want to depend on our infrastructure, you can rescan the blockchain in search of messages sent by the router
If you want to manually scan and enumerate all the positions for a particular pool you can first use the method and get "Number of active NFT positions" from it. Then iterate from 0 as the index of NFT and call
Position NFT is a real NFT so you can use and to get NFT address info and metadata. For position parameters, however, you would need to call the NFT get-method -
Please address the GraphQL schema documents for more details -
Getting collected fees -
Here is a small snippet that uses our indexer\
Below is an example of how to retrieve data for a specific liquidity position using the Position entity from the . The Position instance calculates the current token amounts based on liquidity and price range, while accumulated fees can be fetched directly from the smart contract via the method.
Sending swap can be done with our SDK -
An example of swap preparation can be found here -
A swap request is created as a payload in the jetton transfer and is sent to the router. The general logic of the input parameters can be derived from the SDK example above and the doc of the pool swap message
The simplest and most precise way is to call the pool contract get method -
You can use our Typescript implementation of user-side swap simulation using ). It includes 2 ways to estimate a swap: and . Before actual blockchain execution please check that the estimates you made with TS match the contract call.
A swap request is created as a payload for . The current version of the swap request - uses at most 850bits and 1ref. In the current version, all remaining cell part is ignored. However, to be future-proof proof we recommend to occupy second maybe_ref (1 ref and 1 bit). This cell starts with 4 byte of your service id and any content you need as remaining data.
Messages can be constructed using our SDK () The source code for message construction can be found here: ().