Provide Liquidity (v2)
Provide liquidity on STON.fi v2 - single-sided deposits with automatic vault management
Mainnet-first workflow
import { Client, dexFactory, toUnits } from "@ston-fi/sdk";
import { StonApiClient } from "@ston-fi/api";
const tonClient = new Client({
endpoint: "https://toncenter.com/api/v2/jsonRPC",
});
const apiClient = new StonApiClient();
const userWalletAddress = "<your wallet address>";
const tokenA = { address: "<asset A address or 'ton'>", decimals: 9 };
const tokenB = { address: "<asset B address>", decimals: 9 };
const amountA = "10"; // human-readable amount (e.g. "10")
const amountB = "5"; // human-readable amount for the second token
const slippageTolerance = "0.001";
// Discover existing pools for the pair. If none are found we are creating a new pool.
const [poolInfo] = await apiClient.getPoolsByAssetPair({
asset0Address: tokenA.address,
asset1Address: tokenB.address,
});
const provisionType = poolInfo ? "Balanced" : "Initial"; // or "Arbitrary" for manual ratios
const simulationInput: Parameters<StonApiClient["simulateLiquidityProvision"]>[0] = {
tokenA: tokenA.address,
tokenB: tokenB.address,
provisionType,
slippageTolerance,
walletAddress: userWalletAddress,
...(poolInfo ? { poolAddress: poolInfo.address } : {}),
};
if (provisionType === "Initial") {
simulationInput.tokenAUnits = toUnits(amountA, tokenA.decimals).toString();
simulationInput.tokenBUnits = toUnits(amountB, tokenB.decimals).toString();
} else if (provisionType === "Balanced") {
// Supply **one** side; the API calculates the matching amount for the other token.
simulationInput.tokenAUnits = toUnits(amountA, tokenA.decimals).toString();
} else {
// "Arbitrary" – provide any ratio. To go single-sided, set one of the amounts to "0".
simulationInput.tokenAUnits = toUnits(amountA, tokenA.decimals).toString();
simulationInput.tokenBUnits = toUnits(amountB, tokenB.decimals).toString();
}
const simulationResult = await apiClient.simulateLiquidityProvision(simulationInput);
// 2. Router metadata ships with the simulation result
const { router: routerInfo } = simulationResult;
const dexContracts = dexFactory(routerInfo);
// 3. Open the router contract
const router = tonClient.open(
dexContracts.Router.create(routerInfo.address)
);
// Optional helper when TON is part of the provision
const proxyTon = dexContracts.pTON.create(routerInfo.ptonMasterAddress);Interpreting simulation results
Generating transaction messages
Testnet provisions (manual setup)
Last updated