Skip to content

Commit

Permalink
fix: return zero price impact when user wantsProportional
Browse files Browse the repository at this point in the history
  • Loading branch information
agualis committed Nov 28, 2024
1 parent e9332c4 commit a356b72
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/lib/config/networks/sepolia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const networkConfig: NetworkConfig = {
allowNestedActions: [
'0x965f7d7387d81056ebf0edaf4a869dc46471a676',
'0xc9233cc69435591b193b50f702ac31e404a08b10',
'0x42de4fa875126fdbaf590b2fc3802adbca58acee',
],
}),
layerZeroChainId: 10161,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export function _useAddLiquidity(urlTxHash?: Hash) {
const [acceptPoolRisks, setAcceptPoolRisks] = useState(false)
const [wethIsEth, setWethIsEth] = useState(false)
const [totalUSDValue, setTotalUSDValue] = useState('0')
// Used when the user explicitly choses proportional input mode
const [wantsProportional, setWantsProportional] = useState(false)
const [proportionalSlippage, setProportionalSlippage] = useState<string>('0')

const { pool, refetch: refetchPool, isLoading } = usePool()
Expand Down Expand Up @@ -110,6 +112,7 @@ export function _useAddLiquidity(urlTxHash?: Hash) {
handler,
humanAmountsIn,
enabled: !urlTxHash,
wantsProportional,
})

/**
Expand Down Expand Up @@ -196,6 +199,8 @@ export function _useAddLiquidity(urlTxHash?: Hash) {
slippage,
proportionalSlippage,
isForcedProportionalAdd,
wantsProportional,
setWantsProportional,
setProportionalSlippage,
refetchQuote,
setHumanAmountIn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useUserAccount } from '@repo/lib/modules/web3/UserAccountProvider'
import { WalletIcon } from '@repo/lib/shared/components/icons/WalletIcon'
import { useCurrency } from '@repo/lib/shared/hooks/useCurrency'
import { Card, HStack, Spacer, VStack, Text, Box, Tooltip } from '@chakra-ui/react'
import { useEffect, useState } from 'react'
import { useEffect } from 'react'
import { XOctagon } from 'react-feather'
import { useAddLiquidity } from '../AddLiquidityProvider'
import { TokenInputs } from './TokenInputs'
Expand All @@ -26,8 +26,7 @@ export function TokenInputsWithAddable({
}: Props) {
const { isConnected } = useUserAccount()
const { toCurrency } = useCurrency()
const { setHumanAmountIn } = useAddLiquidity()
const [wantsProportional, setWantsProportional] = useState(false)
const { setHumanAmountIn, wantsProportional, setWantsProportional } = useAddLiquidity()

const {
handleProportionalHumanInputChange,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export interface AddLiquidityHandler {
): Promise<QueryAddLiquidityOutput>

// Calculate the price impact of adding liquidity
getPriceImpact(humanAmountsIn: HumanTokenAmountWithAddress[]): Promise<number>
getPriceImpact(
humanAmountsIn: HumanTokenAmountWithAddress[],
wantsProportional?: boolean // Used when the user is explicitly using the proportional add liquidity feature
): Promise<number>
/*
Build tx callData payload for adding liquidity
It is responsibility of the UI to avoid calling buildAddLiquidityCallData before the last queryAddLiquidity was finished
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ export abstract class BaseUnbalancedAddLiquidityHandler implements AddLiquidityH
return { bptOut: sdkQueryOutput.bptOut, to: sdkQueryOutput.to, sdkQueryOutput }
}

public async getPriceImpact(humanAmountsIn: HumanTokenAmountWithAddress[]): Promise<number> {
public async getPriceImpact(
humanAmountsIn: HumanTokenAmountWithAddress[],
wantsProportional = false
): Promise<number> {
if (areEmptyAmounts(humanAmountsIn)) {
// Avoid price impact calculation when there are no amounts in
return 0
}

// When the user is explicitly using the proportional add liquidity feature
if (wantsProportional) return 0

const addLiquidityInput = this.constructSdkInput(humanAmountsIn)

const priceImpactABA: PriceImpactAmount = await PriceImpact.addLiquidityUnbalanced(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ type Params = {
handler: AddLiquidityHandler
humanAmountsIn: HumanTokenAmountWithAddress[]
enabled: boolean
wantsProportional?: boolean
}

export function useAddLiquidityPriceImpactQuery({ handler, humanAmountsIn, enabled }: Params) {
export function useAddLiquidityPriceImpactQuery({
handler,
humanAmountsIn,
enabled,
wantsProportional = false,
}: Params) {
const { pool, chainId } = usePool()
const { userAddress } = useUserAccount()
const { slippage } = useUserSettings()
Expand All @@ -36,7 +42,7 @@ export function useAddLiquidityPriceImpactQuery({ handler, humanAmountsIn, enabl

const queryKey = addLiquidityKeys.priceImpact(params)

const queryFn = async () => handler.getPriceImpact(humanAmountsIn)
const queryFn = async () => handler.getPriceImpact(humanAmountsIn, wantsProportional)

return useQuery({
queryKey,
Expand Down
6 changes: 6 additions & 0 deletions packages/lib/modules/price-impact/price-impact.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ export function cannotCalculatePriceImpactError(error: Error | null): boolean {
// All ContractFunctionExecutionErrors are shown as unknown price impact
if (error.name === 'ContractFunctionExecutionError') return true
// All Swap PI errors are shown as unknown price impact
if (
error.message.startsWith('Unexpected error while calculating') &&
error.message.includes('PI')
) {
return true
}
if (
error.message.startsWith(
'Unexpected error while calculating addLiquidityUnbalanced PI at Swap step'
Expand Down

0 comments on commit a356b72

Please sign in to comment.