Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

feat: adapt V2 and V3 buildCall parameters in handlers #1094

Merged
merged 8 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/config/networks/sepolia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const networkConfig: NetworkConfig = {
multicall2: '0xca11bde05977b3631167028862be2a173976ca11',
balancer: {
vaultV2: '0xBA12222222228d8Ba445958a75a0704d566BF2C8',
vaultV3: '0xD5584b37D1845fFeD958C2d94bC675603DdCce68',
vaultV3: '0x0EF1c156a7986F394d90eD1bEeA6483Cc435F542',
relayerV6: '0x7852fB9d0895e6e8b3EedA553c03F6e2F9124dF9',
minter: '0x1783Cd84b3d01854A96B4eD5843753C2CcbD574A',
},
Expand Down
2 changes: 2 additions & 0 deletions lib/debug-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export const vaultV3Address = sepoliaNetworkConfig.contracts.balancer.vaultV3 as

export const poolId = '0x68e3266c9c8bbd44ad9dca5afbfe629022aee9fe000200000000000000000512' as const // Balancer Weighted wjAura and WETH

export const sepoliaRouter = '0x1c58cc548a23956469c7C528Bb3a846c842dfaF9'

/*
Used to pretty print objects when debugging
*/
Expand Down
11 changes: 10 additions & 1 deletion lib/modules/pool/actions/LiquidityActionHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export function toPoolStateWithBalances(pool: Pool): PoolStateWithBalances {
* - is native and the wrapped native token is already in the array and
* - is wrapped native and the native token is already in the array
*
* @param {HumanAmoHumanTokenAmountWithAddressuntIn[]} humanAmountsIn - The array of human amounts to filter.
* @param {HumanTokenAmountWithAddress[]} humanAmountsIn - The array of human amounts to filter.
* @param {Address} tokenAddress - The token address to compare against.
* @param {GqlChain} chain - The chain type for comparison.
* @return {HumanTokenAmountWithAddress[]} The filtered array of human amounts.
Expand Down Expand Up @@ -309,3 +309,12 @@ export function injectNativeAsset(
export function hasNoLiquidity(pool: Pool): boolean {
return isZero(pool.dynamicData.totalShares)
}

// When the pool has version < v3, it adds extra buildCall params (sender and recipient) that must be present only in V1/V2
export function formatBuildCallParams<T>(buildCallParams: T, isV3Pool: boolean, account: Address) {
// sender must be undefined for v3 pools
if (isV3Pool) return buildCallParams

// sender and recipient must be defined only for v1 and v2 pools
return { ...buildCallParams, sender: account, recipient: account }
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import {
Slippage,
} from '@balancer/sdk'
import { Pool } from '../../../PoolProvider'
import { LiquidityActionHelpers, areEmptyAmounts } from '../../LiquidityActionHelpers'
import { SdkBuildAddLiquidityInput, SdkQueryAddLiquidityOutput } from '../add-liquidity.types'
import {
LiquidityActionHelpers,
formatBuildCallParams,
areEmptyAmounts,
} from '../../LiquidityActionHelpers'
import { AddLiquidityHandler } from './AddLiquidity.handler'
import { SdkBuildAddLiquidityInput, SdkQueryAddLiquidityOutput } from '../add-liquidity.types'

/**
* UnbalancedAddLiquidityHandler is a handler that implements the
Expand Down Expand Up @@ -64,13 +68,19 @@ export class UnbalancedAddLiquidityHandler implements AddLiquidityHandler {
}: SdkBuildAddLiquidityInput): Promise<TransactionConfig> {
const addLiquidity = new AddLiquidity()

const { callData, to, value } = addLiquidity.buildCall({
const baseBuildCallParams = {
...queryOutput.sdkQueryOutput,
slippage: Slippage.fromPercentage(`${Number(slippagePercent)}`),
sender: account,
recipient: account,
wethIsEth: this.helpers.isNativeAssetIn(humanAmountsIn),
})
}

const buildCallParams = formatBuildCallParams(
baseBuildCallParams,
this.helpers.isV3Pool(),
account
)

const { callData, to, value } = addLiquidity.buildCall(buildCallParams)

return {
account,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { useShouldSignRelayerApproval } from '@/lib/modules/relayer/signRelayerA
import { useApproveRelayerStep } from '@/lib/modules/relayer/useApproveRelayerStep'
import { useRelayerMode } from '@/lib/modules/relayer/useRelayerMode'
import { useTokenApprovalSteps } from '@/lib/modules/tokens/approvals/useTokenApprovalSteps'
import { useContractAddress } from '@/lib/modules/web3/contracts/useContractAddress'
import { useMemo } from 'react'
import { usePool } from '../../PoolProvider'
import { LiquidityActionHelpers } from '../LiquidityActionHelpers'
import { AddLiquidityStepParams, useAddLiquidityStep } from './useAddLiquidityStep'
import { getVaultConfig } from '../../pool.helpers'
import { useSignRelayerStep } from '@/lib/modules/transactions/transaction-steps/useSignRelayerStep'
import { Address } from 'viem'
import { isCowAmmPool } from '../../pool.helpers'
Expand All @@ -21,10 +21,11 @@ export function useAddLiquiditySteps({
humanAmountsIn,
simulationQuery,
}: AddLiquidityStepsParams) {
const vaultAddress = useContractAddress('balancer.vaultV2')
const { pool, chainId, chain } = usePool()
const { vaultAddress } = getVaultConfig(pool)
const relayerMode = useRelayerMode(pool)
const shouldSignRelayerApproval = useShouldSignRelayerApproval(chainId, relayerMode)

const { step: approveRelayerStep, isLoading: isLoadingRelayerApproval } =
useApproveRelayerStep(chainId)
const signRelayerStep = useSignRelayerStep(chain)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { Address, parseEther } from 'viem'
import { BPT_DECIMALS } from '../../../pool.constants'
import { Pool } from '../../../PoolProvider'
import { LiquidityActionHelpers } from '../../LiquidityActionHelpers'
import { LiquidityActionHelpers, formatBuildCallParams } from '../../LiquidityActionHelpers'
import {
QueryRemoveLiquidityInput,
SdkBuildRemoveLiquidityInput,
Expand Down Expand Up @@ -50,13 +50,19 @@ export class ProportionalRemoveLiquidityHandler implements RemoveLiquidityHandle
}: SdkBuildRemoveLiquidityInput): Promise<TransactionConfig> {
const removeLiquidity = new RemoveLiquidity()

const { callData, to, value } = removeLiquidity.buildCall({
const baseBuildCallParams = {
...queryOutput.sdkQueryOutput,
slippage: Slippage.fromPercentage(`${Number(slippagePercent)}`),
sender: account,
recipient: account,
wethIsEth,
})
}

const buildCallParams = formatBuildCallParams(
baseBuildCallParams,
this.helpers.isV3Pool(),
account
)

const { callData, to, value } = removeLiquidity.buildCall(buildCallParams)

return {
account,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import {
import { Address, parseEther } from 'viem'
import { BPT_DECIMALS } from '../../../pool.constants'
import { Pool } from '../../../PoolProvider'
import { LiquidityActionHelpers, isEmptyHumanAmount } from '../../LiquidityActionHelpers'
import {
LiquidityActionHelpers,
formatBuildCallParams,
isEmptyHumanAmount,
} from '../../LiquidityActionHelpers'
import {
SdkBuildRemoveLiquidityInput,
SdkQueryRemoveLiquidityOutput,
Expand Down Expand Up @@ -72,13 +76,19 @@ export class SingleTokenRemoveLiquidityHandler implements RemoveLiquidityHandler
}: SdkBuildRemoveLiquidityInput): Promise<TransactionConfig> {
const removeLiquidity = new RemoveLiquidity()

const { callData, to, value } = removeLiquidity.buildCall({
const baseBuildCallParams = {
...queryOutput.sdkQueryOutput,
slippage: Slippage.fromPercentage(`${Number(slippagePercent)}`),
sender: account,
recipient: account,
wethIsEth,
})
}

const buildCallParams = formatBuildCallParams(
baseBuildCallParams,
this.helpers.isV3Pool(),
account
)

const { callData, to, value } = removeLiquidity.buildCall(buildCallParams)

return {
account,
Expand Down
Loading