Learn how to add and remove liquidity using the Saros SDK with this complete example.
import {
getPoolInfo,
depositAllTokenTypes,
withdrawAllTokenTypes,
getTokenMintInfo,
getTokenAccountInfo,
getInfoTokenByMint,
genConnectionSolana,
convertBalanceToWei
} from '@saros-finance/sdk';
import { PublicKey } from '@solana/web3.js';
import BN from 'bn.js';
const connection = genConnectionSolana();
const accountSol = 'YOUR_WALLET_PUBLIC_KEY';
const SLIPPAGE = 0.5;
// Pool example on saros C98 to USDC
const USDC_TOKEN = {
mintAddress: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
addressSPL: 'FXRiEosEvHnpc3XZY1NS7an2PB1SunnYW1f5zppYhXb3',
decimals: '6'
};
const C98_TOKEN = {
mintAddress: 'C98A4nkJXhpVZNAZdHUA95RpTF3T4whtQubL3YobiUX9',
addressSPL: 'EKCdCBjfQ6t5FBfDC2zvmr27PgfVVZU37C8LUE4UenKb',
decimals: '6'
};
const poolParams = {
address: '2wUvdZA8ZsY714Y5wUL9fkFmupJGGwzui2N74zqJWgty'
};
async function addLiquidity() {
try {
const poolAccountInfo = await getPoolInfo(
connection,
new PublicKey(poolParams.address)
);
const newPoolLpMintInfo = await getTokenMintInfo(
connection,
poolAccountInfo.lpTokenMint
);
const lpTokenSupply = newPoolLpMintInfo.supply
? newPoolLpMintInfo.supply.toNumber()
: 0;
const convertFromAmount = convertBalanceToWei(1, USDC_TOKEN.decimals);
const newPoolToken0AccountInfo = await getTokenAccountInfo(
connection,
poolAccountInfo.token0Account
);
const lpTokenAmount =
(parseFloat(convertFromAmount) * lpTokenSupply) /
newPoolToken0AccountInfo.amount.toNumber();
const result = await depositAllTokenTypes(
connection,
accountSol,
new PublicKey(accountSol),
new PublicKey(C98_TOKEN.addressSPL),
new PublicKey(USDC_TOKEN.addressSPL),
lpTokenAmount,
new PublicKey(poolParams.address),
new PublicKey('SSwapUtytfBdBn1b9NUGG6foMVPtcWgpRU32HToDUZr'), // Program address
C98_TOKEN.mintAddress,
USDC_TOKEN.mintAddress,
SLIPPAGE
);
if (result.isError) {
console.error('Add liquidity failed:', result.mess);
return { success: false, message: result.mess };
} else {
console.log('Liquidity added successfully! Hash:', result.hash);
return { success: true, hash: result.hash };
}
} catch (error) {
console.error('Error:', error);
return { success: false, message: error.message };
}
}
async function removeLiquidity() {
try {
const poolAccountInfo = await getPoolInfo(
connection,
new PublicKey(poolParams.address)
);
const lpTokenSupply = poolAccountInfo.supply
? poolAccountInfo.supply.toNumber()
: 0;
const lpTokenMint = poolAccountInfo.lpTokenMint.toString();
const newPoolToken0AccountInfo = await getTokenAccountInfo(
connection,
poolAccountInfo.token0Account
);
const lpTokenAmount =
(parseFloat(1) * lpTokenSupply) /
newPoolToken0AccountInfo.amount.toNumber();
const infoLpUser = await getInfoTokenByMint(lpTokenMint, accountSol);
const result = await withdrawAllTokenTypes(
connection,
accountSol,
infoLpUser.pubkey,
C98_TOKEN.addressSPL,
USDC_TOKEN.addressSPL,
lpTokenAmount,
new PublicKey(poolParams.address),
new PublicKey('SSwapUtytfBdBn1b9NUGG6foMVPtcWgpRU32HToDUZr'), // Program address
C98_TOKEN.mintAddress,
USDC_TOKEN.mintAddress,
SLIPPAGE
);
if (result.isError) {
console.error('Remove liquidity failed:', result.mess);
return { success: false, message: result.mess };
} else {
console.log('Liquidity removed successfully! Hash:', result.hash);
return { success: true, hash: result.hash };
}
} catch (error) {
console.error('Error:', error);
return { success: false, message: error.message };
}
}
// Execute the operations
addLiquidity().then(result => {
if (result.success) {
console.log("Liquidity added with hash:", result.hash);
} else {
console.log("Add liquidity failed:", result.message);
}
});
removeLiquidity().then(result => {
if (result.success) {
console.log("Liquidity removed with hash:", result.hash);
} else {
console.log("Remove liquidity failed:", result.message);
}
});
This simulation demonstrates the expected behavior of adding and removing liquidity. In a real application, this would connect to the Solana network.
Click 'Run Liquidity Simulation' to see the output here...
getPoolInfo
retrieves information about the liquidity pool.
getTokenMintInfo
gets details about the LP token mint.
depositAllTokenTypes
adds liquidity to the pool.
withdrawAllTokenTypes
removes liquidity from the pool.