Skip to content

Commit

Permalink
fix: approval issues with removing V2 liquidity
Browse files Browse the repository at this point in the history
  • Loading branch information
0xMasayoshi committed Mar 19, 2024
1 parent 2eb8177 commit 8c9d71a
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions apps/evm/src/ui/pool/RemoveSectionLegacy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,29 @@ export const RemoveSectionLegacy: FC<RemoveSectionLegacyProps> =
const currencyAToRemove = useMemo(
() =>
token0
? percentToRemove?.greaterThan('0') && underlying0
? percentToRemoveDebounced?.greaterThan('0') && underlying0
? Amount.fromRawAmount(
token0,
percentToRemove.multiply(underlying0.quotient).quotient || '0',
percentToRemoveDebounced.multiply(underlying0.quotient)
.quotient || '0',
)
: Amount.fromRawAmount(token0, '0')
: undefined,
[percentToRemove, token0, underlying0],
[percentToRemoveDebounced, token0, underlying0],
)

const currencyBToRemove = useMemo(
() =>
token1
? percentToRemove?.greaterThan('0') && underlying1
? percentToRemoveDebounced?.greaterThan('0') && underlying1
? Amount.fromRawAmount(
token1,
percentToRemove.multiply(underlying1.quotient).quotient || '0',
percentToRemoveDebounced.multiply(underlying1.quotient)
.quotient || '0',
)
: Amount.fromRawAmount(token1, '0')
: undefined,
[percentToRemove, token1, underlying1],
[percentToRemoveDebounced, token1, underlying1],
)

const [minAmount0, minAmount1] = useMemo(() => {
Expand All @@ -136,7 +138,15 @@ export const RemoveSectionLegacy: FC<RemoveSectionLegacyProps> =
const debouncedMinAmount1 = useDebounce(minAmount1, 500)

const amountToRemove = useMemo(
() => balance?.multiply(percentToRemove),
() =>
balance &&
percentToRemoveDebounced &&
percentToRemoveDebounced.greaterThan('0')
? Amount.fromRawAmount(
balance.currency,
percentToRemoveDebounced.multiply(balance.quotient).quotient,
)
: undefined,
[balance, percentToRemove],
)

Expand Down Expand Up @@ -169,6 +179,7 @@ export const RemoveSectionLegacy: FC<RemoveSectionLegacyProps> =
useEffect(() => {
const prep = async (): Promise<UsePrepareSendTransactionConfig> => {
if (
!approved ||
!token0 ||
!token1 ||
!chain?.id ||
Expand All @@ -177,7 +188,7 @@ export const RemoveSectionLegacy: FC<RemoveSectionLegacyProps> =
!underlying1 ||
!address ||
!pool ||
!balance ||
!amountToRemove ||
!debouncedMinAmount0 ||
!debouncedMinAmount1 ||
!deadline ||
Expand Down Expand Up @@ -206,7 +217,7 @@ export const RemoveSectionLegacy: FC<RemoveSectionLegacyProps> =
token1IsNative
? (pool.token0.wrapped.address as Address)
: (pool.token1.wrapped.address as Address),
balance.multiply(percentToRemoveDebounced).quotient,
amountToRemove.quotient,
token1IsNative
? debouncedMinAmount0.quotient
: debouncedMinAmount1.quotient,
Expand All @@ -224,7 +235,7 @@ export const RemoveSectionLegacy: FC<RemoveSectionLegacyProps> =
args: [
pool.token0.wrapped.address as Address,
pool.token1.wrapped.address as Address,
balance.multiply(percentToRemoveDebounced).quotient,
amountToRemove.quotient,
debouncedMinAmount0.quotient,
debouncedMinAmount1.quotient,
address,
Expand Down Expand Up @@ -273,6 +284,7 @@ export const RemoveSectionLegacy: FC<RemoveSectionLegacyProps> =
console.error('remove prepare error', e)
})
}, [
approved,
token0,
token1,
chain?.id,
Expand All @@ -281,12 +293,11 @@ export const RemoveSectionLegacy: FC<RemoveSectionLegacyProps> =
underlying1,
address,
pool,
balance,
amountToRemove,
debouncedMinAmount0,
debouncedMinAmount1,
deadline,
_pool.chainId,
percentToRemoveDebounced,
percentage,
])

Expand Down

0 comments on commit 8c9d71a

Please sign in to comment.