Quickstart
Start building with Onton Finance in under 5 minutes
Setup your development environment
Learn how to set up your development environment and start building with Onton Finance.
Install and configure
To get started with Onton Finance, you’ll need to install the Onton SDK. Run the following command in your terminal:
npm install onton-sdk
This will install the Onton SDK and all its dependencies, allowing you to interact with the Onton Finance platform.
Before you can start using the Onton SDK, you need to configure your environment with your private key and other necessary details. Follow the steps below to set up your environment:
import dotenv from "dotenv";
import TonWeb from "tonweb";
import { TonClient, toNano } from "@ton/ton";
import { mnemonicToKeyPair } from "tonweb-mnemonic";
import { Factory } from "onton-sdk";
import { WalletV4ContractR2 } from "tonweb/dist/types/contract/wallet/v4/wallet-v4-contract-r2";
dotenv.config();
Next, define a helper function to wait for the sequence number to change:
async function awaitSeqno(wallet: WalletV4ContractR2, seqno: number): Promise<boolean> {
let currentSeqno = seqno;
while (currentSeqno == seqno) {
await new Promise((resolve) => setTimeout(resolve, 1500));
seqno = (await wallet.methods.seqno().call()) ?? 0;
}
return Promise.resolve(true);
}
Then, create the main function to initialize the Onton SDK and perform the operations:
const main = async () => {
const apiKey = process.env.API_KEY!;
const walletAddress = process.env.WALLET_ADDRESS!;
const token0JettonAddress = process.env.TOKEN0_JETTON_ADDRESS!;
const token1JettonAddress = process.env.TOKEN1_JETTON_ADDRESS!;
const token0Amount = toNano(process.env.TOKEN0_AMOUNT!);
const token1Amount = toNano(process.env.TOKEN1_AMOUNT!);
const mnemonic = process.env.MNEMONIC!.split(" ");
const referralAddress = process.env.REFERRAL_ADDRESS!;
const tonClient = new TonClient({
endpoint: "https://toncenter.com/api/v2/jsonRPC",
apiKey: apiKey
});
const ontonSDK = tonClient.open(new Factory());
const depositLiquidityTransactionMessages = await ontonSDK.getDepositLiquidity({
walletAddress: walletAddress,
token0JettonAddress: token0JettonAddress,
token0Amount: token0Amount,
token1JettonAddress: token1JettonAddress,
token1Amount: token1Amount
});
const provider = new TonWeb.HttpProvider("https://toncenter.com/api/v2/jsonRPC", { apiKey: apiKey });
const tonweb = new TonWeb(provider);
const keyPair = await mnemonicToKeyPair(mnemonic);
const wallet = new tonweb.wallet.all["v4R2"](tonweb.provider, {
publicKey: keyPair.publicKey,
wc: 0
});
const response0 = await wallet.methods
.transfer({
secretKey: keyPair.secretKey,
toAddress: depositLiquidityTransactionMessages[0].to.toString(),
amount: new tonweb.utils.BN(depositLiquidityTransactionMessages[0].value),
payload: TonWeb.boc.Cell.oneFromBoc(
TonWeb.utils.base64ToBytes(depositLiquidityTransactionMessages[0].body!.toBoc().toString("base64"))
),
sendMode: 3,
seqno: (await wallet.methods.seqno().call()) ?? 0
})
.send();
let seqno = (await wallet.methods.seqno().call()) ?? 0;
await awaitSeqno(wallet, seqno);
const response1 = await wallet.methods
.transfer({
secretKey: keyPair.secretKey,
toAddress: depositLiquidityTransactionMessages[1].to.toString(),
amount: new tonweb.utils.BN(depositLiquidityTransactionMessages[1].value),
payload: TonWeb.boc.Cell.oneFromBoc(
TonWeb.utils.base64ToBytes(depositLiquidityTransactionMessages[1].body!.toBoc().toString("base64"))
),
sendMode: 3,
seqno: (await wallet.methods.seqno().call()) ?? 0
})
.send();
console.log(response0);
console.log(response1);
};
main();
Replace the environment variables with your actual values:
API_KEY
WALLET_ADDRESS
TOKEN0_JETTON_ADDRESS
TOKEN1_JETTON_ADDRESS
TOKEN0_AMOUNT
TOKEN1_AMOUNT
MNEMONIC
REFERRAL_ADDRESS
Start building
The Onton SDK provides a wide range of functionality for interacting with the Onton Finance platform. Some key features include:
- Swapping tokens
- Providing liquidity
- Managing liquidity pools
- Retrieving pool and account data
Refer to the API Reference for detailed information on available endpoints and their usage.
To ensure your integration with Onton Finance is working as expected, you can test your code using the Onton SDK. Here’s an example of how to perform a token swap:
async function swapTokens() {
try {
const swapTransactionMessage = await ontonSDK.getSwap({
walletAddress: process.env.WALLET_ADDRESS!,
sendJettonAddress: "YOUR_SEND_JETTON_ADDRESS",
receiveJettonAddress: "YOUR_RECEIVE_JETTON_ADDRESS",
referralAddress: process.env.REFERRAL_ADDRESS!,
tokenAmount: toNano(30)
});
const response = await wallet.methods
.transfer({
secretKey: keyPair.secretKey,
toAddress: swapTransactionMessage.to.toString(),
amount: new tonweb.utils.BN(swapTransactionMessage.value),
payload: TonWeb.boc.Cell.oneFromBoc(
TonWeb.utils.base64ToBytes(swapTransactionMessage.body!.toBoc().toString("base64"))
),
sendMode: 3,
seqno: (await wallet.methods.seqno().call()) ?? 0
})
.send();
let seqno = (await wallet.methods.seqno().call()) ?? 0;
await awaitSeqno(wallet, seqno);
console.log(response);
} catch (error) {
console.error('Swap error:', error);
}
}
swapTokens();
This code snippet swaps 30 of a specified jetton for another using the swap
method provided by the Onton SDK. Test various scenarios to ensure your integration handles different cases correctly.
Explore the API
Dive into the Onton Finance API and discover the available endpoints for building powerful decentralized finance applications.
Liquidity Pool API
Interact with liquidity pools using the Onton Finance API.
Swap Tokens
Create token swaps and manage liquidity using the API endpoints.
Liquidity Management
Provide and manage liquidity in the Onton Finance pools.
Pool Data
Retrieve data and information about liquidity pools and accounts.
API Reference
- Introduction
- Get Supported Coins
- Make Liquidity Pool Configuration
- Burn Liquidity
- Create Liquidity Pool
- Create Swap
- Get Expected Liquidity
- Get Expected Outputs
- Get Expected Tokens
- Get LP Account Address
- Get LP Account Data
- Get Pool Address
- Get Pool Data
- Get Router Data
- Provide Liquidity
- Refund Liquidity