synapsecns / sanguine

Synapse Monorepo
MIT License
35 stars 27 forks source link

[SDK] Expose better wrappers for pool liquidity operations #1878

Open ChiTimesChi opened 6 months ago

ChiTimesChi commented 6 months ago

TODO

greptile-apps[bot] commented 1 month ago

Implementation Steps

Slippage Adjustment

  1. Adding Liquidity to the Pool:

    • Modify 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 }
      }
  2. Removing Liquidity from the Pool in a Balanced Way:

    • Modify 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 }
      }
  3. Removing Liquidity from the Pool in a Form of a Single Token:

    • Modify 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 }
      }

API for Populating the Transaction

  1. Adding Liquidity to the Pool:

    • Create a new function 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])
      }
      }
  2. Removing Liquidity from the Pool in a Balanced Way:

    • Create a new function 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)])
      }
      }
  3. Removing Liquidity from the Pool in a Form of a Single Token:

    • Create a new function 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])
      }
      }

API for Quotes

  1. Update the existing API for quotes:
    • Modify the existing functions to provide clearer and more consistent return values.
    • Ensure the functions calculateAddLiquidity, calculateRemoveLiquidity, and calculateRemoveLiquidityOne return structured and well-documented data.

References

/packages/sdk-router/src/operations/pools.ts /packages/sdk-router/src/operations/pools.ts

Ask Greptile