Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
12 tasks
ChiTimesChi opened this issue Jan 17, 2024 · 1 comment
Open
12 tasks

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

ChiTimesChi opened this issue Jan 17, 2024 · 1 comment

Comments

@ChiTimesChi
Copy link
Collaborator

TODO

  • Slippage adjustment for:
    • Adding liquidity to the pool
    • Removing liquidity from the pool in a balanced way
    • Removing liquidity from the pool in a form of a single token
  • API for populating the transaction:
    • Adding liquidity to the pool
    • Removing liquidity from the pool in a balanced way
    • Removing liquidity from the pool in a form of a single token
  • The existing API for quotes is very confusing, needs to be changed
Copy link

greptile-apps bot commented Jun 24, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants