Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
This section contains information about SDK for STON.fi protocol
Welcome to the STON.fi SDK docs section.
The SDK is written in TypeScript and designed to be a thin wrapper on top of the STON.fi contracts, which will help STON.fi protocol to be used more easily in JS/TS projects
The SDK reflects the structure of contracts so that access to contract methods is possible through the use of the corresponding class from the SDK.
SDK is built based on the @ton package, which provides primitives for working with data on the TON blockchain, as well as bare contract instances that our SDK expands with unique contract methods
The @ton library allows you to make on-chain requests and for that uses toncenter API with a rate limit of 1 request per second. You can obtain higher limits by:
requesting an API key from toncenter with a 10 request per minute limit
use open TON API instance, for example, from ORBS
found a paid instance of TON API with suitable rate limits for your case
selfhost an instance of TON API
Firstly install the @ton/ton package following their installation guide
Then, add SDK package using the package manager of your choice.
Sometimes, we publish a release candidate version of the SDK package to give you early access to the next version. To install this version, you need to expressly specify that you are interested in the next version during the installation.
We recommend you check the documentation sections dedicated to the architecture of contracts and their methods to get more context about STON.fi protocol in general before starting to interact with the contracts via the SDK
The remaining sections of the documentation will demonstrate specific examples of the SDK usage:
We are providing a simple but fully functional demo app with the SDK usage in the next.js app to demonstrate the SDK functionality. The source code is open-sourced and can be found here. Try this app at sdk-demo-app.ston.fi
This section contains SDK example for refunding undeposited liquidity in DEX
Refund tokens that were sent to LP account but weren't added to a liquidity pool yet
This section contains SDK example for performing swap in DEX
In this section, to illustrate all three possible types of a swap, we will do following exchange chain
swap 1 TON to STON (ton to jetton swap)
swap STON to GEMSTON (jetton to jetton swap)
swap GEMSTON back to TON (jetton to ton swap)
Below are recommended values for TON sent and forward gas for each type of the swap:
pTON
-> Jetton
Jetton
-> Jetton
Jetton
-> pTON
This section contains SDK reference for DEX
The remaining sections of the documentation will demonstrate specific examples of the DEX usage:
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our with examples for different libraries.
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our with examples for different libraries.
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our with examples for different libraries.
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our with examples for different libraries.
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our with examples for different libraries.
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our with examples for different libraries.
pTON
-> Jetton
swap_amount + 0.185
0.185
Jetton
-> Jetton
0.22
0.175
Jetton
-> pTON
0.17
0.125
This section contains SDK example for withdrawing liquidity from DEX
Burn all liquidity tokens to free liquidity from a pool
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our doc section about transaction sending guide with examples for different libraries.
This section contains SDK example for refunding undeposited liquidity in DEX
Refund tokens that were sent to LP account but weren't added to a liquidity pool yet
This section contains SDK example for performing swap in DEX
This section contains SDK reference for DEX
The remaining sections of the documentation will demonstrate specific examples of the DEX usage:
This section contains SDK example for performing fee withdrawal from the vault contract
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our with examples for different libraries.
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our with examples for different libraries.
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our with examples for different libraries.
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our with examples for different libraries.
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our with examples for different libraries.
This section contains SDK example for providing liquidity in DEX
Provide liquidity for a pool
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our doc section about transaction sending guide with examples for different libraries.
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our doc section about transaction sending guide with examples for different libraries.
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our doc section about transaction sending guide with examples for different libraries.
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our doc section about transaction sending guide with examples for different libraries.
This section contains SDK example for withdrawing liquidity from DEX
Burn all liquidity tokens to free liquidity from a pool
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our doc section about transaction sending guide with examples for different libraries.
This section contains sdk methods reference
address
Address of the Router contract
gasConstants
getPoolAddress
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
token0
Address | string
The address of the router's wallet of first Jetton
token1
Address | string
The address of the router's wallet of second Jetton
Result
Returns an address of a pool for a specified pair of assets.
getPoolAddressByJettonMinters
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
token0
Address | string
The address of the first pool Jetton Minter
wallet
token1
Address | string
The address of the second pool Jetton Minter
wallet
Result
Returns an address of a pool for a specified pair of assets.
getPool
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
token0
Address | string
The address of the first pool Jetton Minter
wallet
token1
Address | string
The address of the second pool Jetton Minter
wallet
Result
Returns an instance of PoolV1
with specified Jetton token addresses.
getRouterData
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
Result
Returns structure containing current state of the router.
isLocked
boolean
true if transfer_notification
operations are locked (swap
, provide_lp
)
adminAddress
Address
Address of contract's admin account
tempUpgrade
Cell
A structure describing state of contract's code & admin upgrade; zero values indicate that no upgrade is pending
poolCode
Cell
Code of the router's liquidity pool contract
jettonLpWalletCode
Cell
Code of lp wallet contract
lpAccountCode
Cell
Code of lp account contract
createSwapBody
userWalletAddress
Address | string
User's address
minAskAmount
bigint | number | string
Minimum amount of tokens received (in basic token units)
askJettonWalletAddress
Address | string
Jetton router's wallet address of tokens to be received
referralAddress
Address | string | undefined
referral address
getSwapJettonToJettonTxParams
Build all data required to execute a jetton to jetton swap
transaction
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
userWalletAddress
Address | string
User's address
offerJettonAddress
Address | string
Jetton Minter
address of a token to be swapped
askJettonAddress
Address | string
Jetton Minter
address of a token to be received
offerAmount
bigint | number | string
Amount of tokens to be swapped (in basic token units)
minAskAmount
bigint | number | string
Minimum amount of tokens to be received (in basic token units)
referralAddress
Address | string | undefined
Referral address
gasAmount
bigint | number | string | undefined
Transaction gas
forwardGasAmount
bigint | number | string | undefined
Forward amount of gas for the next transaction (in nanoTons)
queryId
bigint | number | undefined
Query id
Result
to
Address
Address of the router's Jetton wallet for the swapped token
value
bigint
Recommended amount of TON (in nanoTons) sent as gas
body
Cell | null | undefined
Optional Cell
with data to be send
sendSwapJettonToJetton
getSwapJettonToTonTxParams
Build all data required to execute a jetton to ton swap
transaction
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
userWalletAddress
Address | string
User's address
offerJettonAddress
Address | string
Jetton Minter
address of a token to be swapped
proxyTon
PtonV1
instance of pTON contract
offerAmount
bigint | number | string
Amount of tokens to be swapped (in basic token units)
minAskAmount
bigint | number | string
Minimum amount of tokens to be received (in basic token units)
referralAddress
Address | string | undefined
Referral address
gasAmount
bigint | number | string | undefined
Transaction gas
forwardGasAmount
bigint | number | string | undefined
Forward amount of gas for the next transaction (in nanoTons)
queryId
bigint | number | undefined
Query id
Result
to
Address
Address of the router's Jetton wallet for the swapped token
value
bigint
Recommended amount of TON (in nanoTons) sent as gas
body
Cell | null | undefined
Optional Cell
with data to be send
sendSwapJettonToTon
getSwapTonToJettonTxParams
Build all data required to execute a ton to jetton swap
transaction
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
userWalletAddress
Address | string
User's address
proxyTon
PtonV1
instance of pTON contract
askJettonAddress
Address | string
Jetton Minter
address of a token to be received
offerAmount
bigint | number | string
Amount of tokens to be swapped (in basic token units)
minAskAmount
bigint | number | string
Minimum amount of tokens to be received (in basic token units)
referralAddress
Address | string | undefined
Referral address
forwardGasAmount
bigint | number | string | undefined
Forward amount of gas for the next transaction (in nanoTons)
queryId
bigint | number | undefined
Query id
Result
to
Address
Address of the router's Jetton wallet for the swapped token
value
bigint
Recommended amount of TON (in nanoTons) sent as gas
body
Cell | null | undefined
Optional Cell
with data to be send
sendSwapTonToJetton
createProvideLiquidityBody
routerWalletAddress
Address | string
Address of the router's Jetton token wallet
minLpOut
bigint | number | string
Minimum amount of created liquidity tokens (in basic token units)
getProvideLiquidityJettonTxParams
Collect all data required to execute a jetton provide_lp
transaction
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
userWalletAddress
Address | string
User's address
sendTokenAddress
Address | string
Address of the first Jetton token or a proxy ton contract
otherTokenAddress
Address | string
Address of the second Jetton token
sendAmount
bigint | number | string
Amount of the first/second tokens deposited as liquidity (in basic token units)
minLpOut
bigint | number | string
Minimum amount of created liquidity tokens (in basic token units)
gasAmount
bigint | number | string | undefined
Transaction gas
forwardGasAmount
bigint | number | string | undefined
Forward amount of gas for the next transaction (in nanoTons)
queryId
bigint | number | undefined
Query id
Result
to
Address
Address of the router's Jetton wallet for the swapped token
value
bigint
Recommended amount of TON (in nanoTons) sent as gas
body
Cell | null | undefined
Optional Cell
with data to be send
sendProvideLiquidityJetton
getProvideLiquidityTonTxParams
Collect all data required to execute a proxy ton provide_lp
transaction
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
userWalletAddress
Address | string
User's address
proxyTon
PtonV1
instance of pTON contract
otherTokenAddress
Address | string
Address of the second Jetton token
sendAmount
bigint | number | string
Amount of the first/second tokens deposited as liquidity (in basic token units)
minLpOut
bigint | number | string
Minimum amount of created liquidity tokens (in basic token units)
forwardGasAmount
bigint | number | string | undefined
Forward amount of gas for the next transaction (in nanoTons)
queryId
bigint | number | undefined
Query id
Result
to
Address
Address of the router's Jetton wallet for the swapped token
value
bigint
Recommended amount of TON (in nanoTons) sent as gas
body
Cell | null | undefined
Optional Cell
with data to be send
sendProvideLiquidityTon
address
Address of the Pool contract
gasConstants
getPoolData
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
Result
Returns structure containing current state of the pool.
reserve0
bigint
Amount of the first token (in basic token units)
reserve1
bigint
Amount of the second token (in basic token units)
token0WalletAddress
Address
Address of the first Jetton token
token1WalletAddress
Address
Address of the second Jetton token
lpFee
bigint
Liquidity pool fee value
protocolFee
bigint
Protocol fee
refFee
bigint
Referral fee
protocolFeeAddress
Address
Address for receiving protocol fees
collectedToken0ProtocolFee
bigint
Amount of collected protocol fees of the first token (in basic token units)
collectedToken1ProtocolFee
bigint
Amount of collected protocol fees of the second token (in basic token units)
Notes:
fee ratio is the value of fee divided by FEE_DIVIDER
(10000); so a fee of 1% has a value of 100
getExpectedOutputs
Estimate expected result of the amount of jettonWallet tokens swapped to the other type of tokens of the pool
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
amount
bigint | number | string
Amount of tokens to swap (in basic token units)
jettonWallet
Address | string
Token Jetton address (must be equal to one of the Jetton addresses of the pool)
Result
Returns structure with expected result of a token swap
jettonToReceive
bigint
Amount of tokens received (in basic token units)
protocolFeePaid
bigint
Amount tokens paid for protocol fees (in basic token units)
refFeePaid
bigint
Amount tokens paid for referral fees (in basic token units)
getExpectedTokens
Estimate an expected amount of lp tokens minted when providing liquidity.
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
amount0
bigint | number | string
Amount of tokens for the first Jetton (in basic token units)
amount1
bigint | number | string
Amount of tokens for the second Jetton (in basic token units)
Result
Returns an estimated amount of liquidity tokens to be minted
getExpectedLiquidity
Estimate expected liquidity freed upon burning liquidity tokens.
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
jettonAmount
bigint | number | string
Amount of liquidity tokens (in basic token units)
Result
Returns structure with expected freed liquidity
amount0
bigint
Amount of tokens for the first Jetton (in basic token units)
amount1
bigint
Amount of tokens for the second Jetton (in basic token units)
getLpAccountAddress
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
ownerAddress
Address | string
Address of a user
Result
Function getLpAccountAddress
returns the lp account address of a user
getLpAccount
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
ownerAddress
Address | string
Address of a user
Result
returns an instance of LpAccountV1
getJettonData
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
Result
Returns a structure with Jetton data
totalSupply
bigint
Total token supply (in basic token units)
isMintable
boolean
If mintable
adminAddress
Address
Admin address
jettonContentUri
string
Offchain uri with Jetton data
jettonWalletCode
Cell
Code of the lp Jetton wallet
getWalletAddress
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
ownerAddress
Address | string
Address of a user
Result
returns a calculated lp wallet address of a user
getJettonWallet
Arguments
ownerAddress
Address | string
Address of a user
ownerAddress
Address | string
Address of a user
Result
Function getJettonWallet
returns a JettonWallet
instance for an address returned by getWalletAddress
createCollectFeesBody
getCollectFeeTxParams
Build all data required to execute a collect_fees
transaction.
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
gasAmount
bigint | number | string | undefined
Transaction gas
queryId
bigint | number | undefined
Query id
Result
to
Address
Address of the router's Jetton wallet for the swapped token
value
bigint
Recommended amount of TON (in nanoTons) sent as gas
body
Cell | null | undefined
Optional Cell
with data to be send
Notes:
param
is entirely optional
the default value for gasAmount
is equal to 1.1 TON
sendCollectFees
createBurnBody
getBurnTxParams
Build all data required to execute a burn
transaction.
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
amount
bigint | number | string
Lp tokens to burn
responseAddress
Address | string
Excesses address
gasAmount
bigint | number | string | undefined
Transaction gas
queryId
bigint | number | undefined
Query id
sendBurn
address
Address of the LpAccount contract
gasConstants
getLpAccountData
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
Result
Returns structure containing current state of the lp account.
userAddress
Address
Owner's address
poolAddress
Address
Pool's address
amount0
bigint
Balance of the first Jetton token (in basic token units)
amount1
bigint
Balance of the second Jetton token (in basic token units)
createRefundBody
getRefundTxParams
Build all data required to execute a refund_me
transaction.
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
gasAmount
bigint | number | string | undefined
Transaction gas
queryId
bigint | number | undefined
Query id
Result
to
Address
Address of the router's Jetton wallet for the swapped token
value
bigint
Recommended amount of TON (in nanoTons) sent as gas
body
Cell | null | undefined
Optional Cell
with data to be send
sendRefund
createDirectAddLiquidityBody
getDirectAddLiquidityTxParams
Build all data required to execute a direct_add_liquidity
transaction.
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
amount0
bigint | number | string
Amount of the first Jetton tokens (in basic token units)
amount1
bigint | number | string
Amount of the second Jetton tokens (in basic token units)
minimumLpToMint
bigint | number | string | undefined
Minimum amount of received liquidity tokens (in basic token units)
gasAmount
bigint | number | string | undefined
Transaction gas
queryId
bigint | number | undefined
Query id
Result
to
Address
Address of the router's Jetton wallet for the swapped token
value
bigint
Recommended amount of TON (in nanoTons) sent as gas
body
Cell | null | undefined
Optional Cell
with data to be send
Notes:
addition of liquidity will fail if a user should receive less than minimumLpToMint
of lp tokens as a result
the default value for gasAmount
is equal to 0.3 TON
sendDirectAddLiquidity
createResetGasBody
getResetGasTxParams
Build all data required to execute a reset_gas
transaction.
Arguments
provider
ContractProvider
Instance of @ton/core
contract provider
gasAmount
bigint | number | string | undefined
Transaction gas
queryId
bigint | number | undefined
Query id
Result
to
Address
Address of the router's Jetton wallet for the swapped token
value
bigint
Recommended amount of TON (in nanoTons) sent as gas
body
Cell | null | undefined
Optional Cell
with data to be send
Notes:
params
is entirely optional
the default value for gasAmount
is equal to 0.3 TON
sendResetGas
This section contains a guide for sending transactions in TON blockchain
As a result of the SDK get*TxParams
methods calls, you will get the object with the following structure
This object describes to what address the transaction should be sent and what amount of TON to pay for the gas. And body
contains information for the contract of what action you want to perform.
Now, to actually send the transaction to the blockchain we need to use some king of sendTransaction method provided by a TON SDK you are using in your project. There is an extensive , and everyone has a slightly different syntax for sending transactions. Usually, you can find the information about how to send the tx on each TON SDK docs, but for a few of them, we got examples right here:
This section contains SDK example for unstaking tokens in Farm
Unstake funds from Farm NFT
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our with examples for different libraries.
This section contains SDK example for claiming rewards in Farm
Claim rewards from Farm NFT
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our with examples for different libraries.
This section contains SDK example for destroying unstaked farm NFT
Destroy of the Farm NFT is a transfer of the Farm NFT from the user wallet to the zero address.
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our with examples for different libraries.
This section contains SDK example for staking tokens in Farm
Staking of the LP tokens in Farm
To execute the transaction, you need to send a transaction with these parameters to the blockchain. This code will be different based on the wallet you are using to send the tx from, so please check our with examples for different libraries.
This section contains SDK reference for Farm contracts
The remaining sections of the documentation will demonstrate specific examples of the Farm usage:
This section contains migration guide for SDK v0.5.* -> v1.0.0
In the SDK v1.0.0
release, we introduce some breaking changes. You can find the complete list of the release changes in the CHANGELOG.md
file, but in this docs section, we will be focused on some real code snippet migration from the SDK v0.5.*
to v1.0.0
Let's migrate the v0.5.*
jetton to TON swap example code to the v1.0.0
:
The main source of this release's breaking changes is the migration from the tonweb to the @ton/ton package. So, the first step is to add the @ton/ton package to your project. To do that, follow the installation step from their docs.
As a result of this step, we need to be able to create an instance of TonClient that @ton-ton package uses to make on-chain requests.
Since the @ton package makes an on-chain request by injecting the provider into open contract methods, SDK contracts no longer need to require a tonApiClient in constructor parameters. So, the contract's constructor interface has been changed to accept the address as a first parameter and the configuration object as a second parameter.
That means router contract creation in the code we are migrating needed to be changed to the following:
Router.v1
has a known default address, so the first constructor parameter, in this case, is optional. For other contracts, there is no default address, so it should be specified
If you specified custom gasConstants
for a contract, it is still possible to do so. Only instead of passing them as part of the first constructor parameter object, you need to pass them in a second object
The @ton/ton package needs to be opened for use in making on-chain requests from the contract.
build*TxParams
methods renamingAll contracts build*TxParams
methods were renamed to get*TxParams.
This was needed to math @ton/ton package convention for methods that could make on-chain request
In the previous version of the SDK package, to represent the amount, we used BN.js, which came as a part of the TonWeb package and was accessible via TonWeb.utils.BN
. @ton/ton package uses native javascript BigInt
instead of BN.js.
And the migration is peaty easy - since the constructors of BN and BigInt are comparable. We only need to replace Class that represents the amount
Previously, every operation that worked with a native TON requested a proxyTonAddress
. But since we are developing the next version of the proxyTon contract with some differences in the ton sending transaction instead of only the address, we need to know more about the proxyTon.
So now operations with native ton require the instance of pTON contracts.
txParams
shape change and MessageData
type dropTo better match @ton/ton package interfaces, we slightly changed the object's shape that returns from the get*TxParams
methods. The change is only in names; the purpose of this field stayed unchanged. It describes the transaction that, if sent, will cause the requested action on the DEX.
This change lets us use the get*TxParams
methods output directly in the @ton/ton send functionality.
Before
After
Because we switched from our MessageData
interface to @ton/ton SenderArguments
type, MessageData
export from the SDK package was removed
In v1.0.0
, some of the contract methods were renamed:
v0.5.*
v1.0.0
RouterV1.getData
RouterV1.getRouterData
PoolV1.getData
PoolV1.getPoolData
LpAccountV1.getData
LpAccountV1.getLpAccountData
And in the end this is a code we have migrated to
This section contains a guide for sending transactions in TON blockchain using @ton/ton
Ton package uses the sendTransfer
method to send a transaction to the blockchain. An example of the usage is on their README.
This section contains SDK reference for DEX
The remaining sections of the documentation will demonstrate specific examples of the DEX usage:
This section contains a guide for sending transactions in TON blockchain using @tonconnect
Tonconnect package uses the sendTransaction
method to send a transaction to the blockchain. An example of the usage is on their DOCs.
This section contains a guide for sending transactions in TON blockchain using tonweb
Tonweb package uses the transfer
method to send a transaction to the blockchain. An example of the usage is on their README.
This section contains information about SDK for STON.fi protocol
v0.5
version of the SDK is deprecated. Please use latest SDK version instead
Welcome to the STON.fi SDK docs section.
The SDK is written in TypeScript and designed to be a thin wrapper on top of the STON.fi contracts, which will help STON.fi protocol to be used more easily in JS/TS projects
The SDK reflects the structure of contracts so that access to contract methods is possible through the use of the corresponding class from the SDK.
SDK is built based on the TonWeb library, which provides primitives for working with data on the TON blockchain, as well as bare contract instances that our SDK expands with unique contract methods
The TonWeb library allows you to make on-chain requests and for that uses toncenter API with a rate limit of 1 request per second. You can obtain higher limits by:
requesting an API key from toncenter with a 10 request per minute limit
use open TON API instance, for example, from ORBS
found a paid instance of TON API with suitable rate limits for your case
selfhost an instance of TON API
To install SDK via npm
use:
To install SDK with yarn
use:
To install SDK with pnpm
use:
We recommend you check the documentation sections dedicated to the architecture of contracts and their methods to get more context about STON.fi protocol in general before starting to interact with the contracts via the SDK
The remaining sections of the documentation will demonstrate specific examples of the SDK usage:
This section contains SDK example for withdrawing liquidity from DEX
Burn all liquidity tokens to free liquidity from a pool
This section contains SDK example for refunding undeposited liquidity in DEX
Refund tokens that were sent to LP account but weren't added to a liquidity pool yet
This section contains SDK example for staking tokens in Farm
Staking of the LP tokens in Farm
This section contains SDK example for performing swap in DEX
In this section, to illustrate all three possible types of a swap, we will do following exchange chain
swap 1 TON to STON (ton to jetton swap)
swap STON to GEMSTON (jetton to jetton swap)
swap GEMSTON back to TON (jetton to ton swap)
Below are recommended values for TON sent and forward gas for each type of the swap:
pTON
-> Jetton
Jetton
-> Jetton
Jetton
-> pTON
This section contains SDK reference for Farm contracts
The remaining sections of the documentation will demonstrate specific examples of the Farm usage:
pTON
-> Jetton
swap_amount + 0.215
0.215
Jetton
-> Jetton
0.265
0.205
Jetton
-> pTON
0.185
0.125
This section contains a guide for sending transactions in TON blockchain
As a result of the SDK build*TxParams
methods calls, you will get the object with the following structure
This object describes to what address the transaction should be sent and what amount of TON to pay for the gas. And payload
contains information for the contract of what action you want to perform.
Now, to actually send the transaction to the blockchain we need to use some king of sendTransaction method provided by a TON SDK you are using in your project. There is an extensive list of TON SDKs, and everyone has a slightly different syntax for sending transactions. Usually, you can find the information about how to send the tx on each TON SDK docs, but for a few of them, we got examples right here:
This section contains SDK example for claiming rewards in Farm
Claim rewards from Farm NFT
This section contains SDK example for destroying unstaked farm NFT
Destroy of the Farm NFT is a transfer of the Farm NFT from the user wallet to the zero address.
This section contains a guide for sending transactions in TON blockchain using @ton/ton
Ton package uses the sendTransfer
method to send a transaction to the blockchain. An example of the usage is on their README.
This section contains SDK example for unstaking tokens in Farm
Unstake funds from Farm NFT
This section contains a guide for sending transactions in TON blockchain using tonweb
Tonweb package uses the transfer
method to send a transaction to the blockchain. An example of the usage is on their README.
This section contains a guide for sending transactions in TON blockchain using @tonconnect
Tonconnect package uses the sendTransaction
method to send a transaction to the blockchain. An example of the usage is on their DOCs.
This section contains migration guide for SDK v0.4.* -> v0.5.*
In the SDK v0.5
release, we introduce some breaking changes. You can find the full list of the release changes in the CHANGELOG.md
file, but in this docs section, we will be focused on some real code snippet migration from the SDK v0.4
to v0.5
Let's migrate the v0.4
jetton to TON swap example code to the v0.5
:
The first thing you will face after the SDK update is that Module '"@ston-fi/sdk"' has no exported member Router
.
This is because contracts (such as Router
, Pool
, LpAccount
, etc) now no longer exported by their names. We start to use the export of domain related objects that will contain all the contracts.
At the current state SDK exports 3 domain object DEX
, FARM
, pTON
.
So the first required change to our code is to replace the import of Router
contract with the DEX
object.
Because multiple versions of the contracts appeared with uncomparable interfaces, we were forced to drop the architecture with revisions and the root class that obtained multiple revisions.
before:
after:
If you use extends or overrides contract revisions from the SDK anywhere in your code, you will now need to override the contract class instead.
If you use the override of the revision to define custom gas constants, it's now possible to pass gas constants as constructor parameters.
Since we dropped revisions but still need to be able to separate different contracts, every contract now provides the statice field version
. And to math this, *_REVISION
enums was renamed to *_VERSION
.
Now you can use it like this:
*_VERSION
enum values is in lover case (v1
) to match the STON.fi API responses
Back to our code - you can just remove import and usage of the ROUTER_REVISION
completely and instead, to determine witch version of the DEX contract you would like to use, use version key of the domain object (DEX
in this case)
For contracts with known addresses (for example, a router), the address is available as a static field on the class and is no longer required to be specified in the constructor.
The following changes need to be made on this step
All contract constructors now require 1 object with tonApiClient
field to be passed. tonApiClient
is a name for what in TonWeb is named provider
.
Optionally, you can pass an instance of stonApiClient
. This is a class from @ston-fi/api
package. If it is passed, STON.fi public REST API will be used to fetch data instead of using direct on-chain methods called via TON API.
At the current state of the package development, this client is optional, but we expect it to be required in the future when it no longer is possible to get some data required for the SDK to work from on-chain and
In v0.5
, some of the contract methods were renamed:
v0.4
v0.5
buildSwapJettonTxParams
buildSwapJettonToJettonTxParams
& buildSwapJettonToTonTxParams
buildSwapProxyTonTxParams
buildSwapTonToJettonTxParams
buildProvideLiquidityProxyTonTxParams
buildProvideLiquidityTonTxParams
Our code snippet using renamed fn buildSwapJettonTxParams
that was split in two: buildSwapJettonToJettonTxParams
& buildSwapJettonToTonTxParams
. Since we are swapping a jetton to TON, we will use a new fn buildSwapJettonToTonTxParams
buildSwapJettonToTonTxParams
fn now explicitly requires us to pass the address of the pTON contract to be used in the swap. Previously, it required the same, but under the askJettonAddress
name, that was confusing.
Because pTON class is now exported from the SDK we can use address from the pTON contract instead of a hardcoded string value in our code
And in the end this is a code we have migrated to
This section contains SDK reference for DEX
v0.4
version of the SDK is deprecated. Please use latest SDK version instead
The remaining sections of the documentation will demonstrate specific examples of the SDK usage:
This section contains SDK example for refunding undeposited liquidity in DEX
Refund tokens deposited on liquidity pool account but not added to a liquidity pool yet.
This section describes how to create a custom router revision
Create a custom router revision.
This section contains SDK example for withdrawing liquidity from DEX
Free liquidity by buring liquidity pool tokens.
This section describes the usage of get methods
Example of usage of various get methods of the SDK.
This section contains SDK example for performing swap in DEX
Perform a swap operation.
Perform a swap operation using proxy-ton.
Below are recommended values for TON sent and forward gas for each type of the swap:
Jetton
-> Jetton
0.265
0.205
Jetton
-> pTON
0.185
0.125
pTON
-> Jetton
swap_amount + 0.215
0.215
Jetton
-> Jetton
Jetton
-> pTON
pTON
-> Jetton