Open ChiTimesChi opened 6 months ago
Adding Liquidity to the Pool:
calculateAddLiquidity
to accept a slippage parameter and adjust the returned amount accordingly.
export async function calculateAddLiquidity(
this: SynapseSDK,
chainId: number,
poolAddress: string,
amounts: Record<string, BigNumber>,
slippage: number
): Promise<{ amount: BigNumber; routerAddress: string }> {
// existing code...
const amount = amountsArray.every((value) => value.isZero())
? Zero
: await router.calculateAddLiquidity(poolAddress, amountsArray)
const adjustedAmount = amount.mul(100 - slippage).div(100)
return { amount: adjustedAmount, routerAddress }
}
Removing Liquidity from the Pool in a Balanced Way:
calculateRemoveLiquidity
to accept a slippage parameter and adjust the returned amounts accordingly.
export async function calculateRemoveLiquidity(
this: SynapseSDK,
chainId: number,
poolAddress: string,
amount: BigNumber,
slippage: number
): Promise<{
amounts: Array<{ value: BigNumber; index: number }>
routerAddress: string
}> {
// existing code...
const adjustedAmounts = amounts.map(({ value, index }) => ({
value: value.mul(100 - slippage).div(100),
index
}))
return { amounts: adjustedAmounts, routerAddress }
}
Removing Liquidity from the Pool in a Form of a Single Token:
calculateRemoveLiquidityOne
to accept a slippage parameter and adjust the returned amount accordingly.
export async function calculateRemoveLiquidityOne(
this: SynapseSDK,
chainId: number,
poolAddress: string,
amount: BigNumber,
poolIndex: number,
slippage: number
): Promise<{
amount: { value: BigNumber; index: number }
routerAddress: string
}> {
// existing code...
const adjustedAmount = {
value: amountOut.value.mul(100 - slippage).div(100),
index: amountOut.index
}
return { amount: adjustedAmount, routerAddress }
}
Adding Liquidity to the Pool:
populateAddLiquidityTransaction
to return the transaction data.
export async function populateAddLiquidityTransaction(
this: SynapseSDK,
chainId: number,
poolAddress: string,
amounts: Record<string, BigNumber>,
slippage: number
): Promise<TransactionRequest> {
const { amount, routerAddress } = await this.calculateAddLiquidity(chainId, poolAddress, amounts, slippage)
return {
to: routerAddress,
data: router.interface.encodeFunctionData('addLiquidity', [poolAddress, amountsArray, amount])
}
}
Removing Liquidity from the Pool in a Balanced Way:
populateRemoveLiquidityTransaction
to return the transaction data.
export async function populateRemoveLiquidityTransaction(
this: SynapseSDK,
chainId: number,
poolAddress: string,
amount: BigNumber,
slippage: number
): Promise<TransactionRequest> {
const { amounts, routerAddress } = await this.calculateRemoveLiquidity(chainId, poolAddress, amount, slippage)
return {
to: routerAddress,
data: router.interface.encodeFunctionData('removeLiquidity', [poolAddress, amount, amounts.map(a => a.value)])
}
}
Removing Liquidity from the Pool in a Form of a Single Token:
populateRemoveLiquidityOneTransaction
to return the transaction data.
export async function populateRemoveLiquidityOneTransaction(
this: SynapseSDK,
chainId: number,
poolAddress: string,
amount: BigNumber,
poolIndex: number,
slippage: number
): Promise<TransactionRequest> {
const { amount: adjustedAmount, routerAddress } = await this.calculateRemoveLiquidityOne(chainId, poolAddress, amount, poolIndex, slippage)
return {
to: routerAddress,
data: router.interface.encodeFunctionData('removeLiquidityOneToken', [poolAddress, amount, poolIndex, adjustedAmount.value])
}
}
calculateAddLiquidity
, calculateRemoveLiquidity
, and calculateRemoveLiquidityOne
return structured and well-documented data./packages/sdk-router/src/operations/pools.ts /packages/sdk-router/src/operations/pools.ts
TODO