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

Commit 78365e6

Browse files
authored
fix: USDT approval edge case (#1152)
1 parent 45975a3 commit 78365e6

File tree

4 files changed

+169
-88
lines changed

4 files changed

+169
-88
lines changed

app/(app)/debug/page.tsx

+6-84
Original file line numberDiff line numberDiff line change
@@ -9,89 +9,8 @@ export default function Debug() {
99
<FadeInOnView>
1010
<VStack padding="lg" margin="lg">
1111
<Heading size="md">Demos</Heading>
12-
<Link as={NextLink} href="pools/sepolia/v3/0x7cf221fa36584f59a4f7fd7b946b8571c78e3692">
13-
Sepolia V3 pool (Balancer 50 BAL 50 WETH)
14-
</Link>
15-
<Link
16-
as={NextLink}
17-
href="pools/ethereum/v2/0x68e3266c9c8bbd44ad9dca5afbfe629022aee9fe000200000000000000000512/add-liquidity"
18-
>
19-
Add liquidity in WEIGHTED (wjAura-weth)
20-
</Link>
21-
<Link
22-
as={NextLink}
23-
href="pools/ethereum/v2/0x32296969ef14eb0c6d29669c550d4a0449130230000200000000000000000080/add-liquidity"
24-
>
25-
Add liquidity in META_STABLE (wstETH_wETH)
26-
</Link>
27-
<Link
28-
as={NextLink}
29-
href="pools/ethereum/v2/0x1e19cf2d73a72ef1332c882f20534b6519be0276000200000000000000000112/add-liquidity"
30-
>
31-
Add liquidity in STABLE (B-rETH-STABLE in Mainnet)
32-
</Link>
33-
<Link
34-
as={NextLink}
35-
href="pools/ethereum/v2/0x3dd0843a028c86e0b760b1a76929d1c5ef93a2dd000200000000000000000249/add-liquidity"
36-
>
37-
Add liquidity in STABLE with BPT tokens (AuraBal 80/20 pool in Mainnet)
38-
</Link>
39-
<Link
40-
as={NextLink}
41-
href="pools/optimism/v2/0x3dc09db8e571da76dd04e9176afc7feee0b89106000000000000000000000019/add-liquidity"
42-
>
43-
Add liquidity in STABLE (FRAX_USDC_MAI in Optimism)
44-
</Link>
45-
<Link
46-
as={NextLink}
47-
href="pools/ethereum/v2/0x08775ccb6674d6bdceb0797c364c2653ed84f3840002000000000000000004f0/add-liquidity"
48-
>
49-
Add liquidity in nested pool (50WETH-50-3pool)
50-
</Link>
51-
<Link
52-
as={NextLink}
53-
href="pools/ethereum/v2/0xc6853f0539f7d4926c719326d60bd84a752bbb8f00020000000000000000065e/add-liquidity"
54-
>
55-
Add liquidity in Gyro pool (2CLP-WSTETH-WETH in Mainnet)
56-
</Link>
57-
<Link
58-
as={NextLink}
59-
href="pools/polygon/v2/0xee278d943584dd8640eaf4cc6c7a5c80c0073e85000200000000000000000bc7/add-liquidity"
60-
>
61-
Add liquidity in Gyro pool (2CLP_WMATIC/MATICX in Polygon)
62-
</Link>
63-
<Link
64-
as={NextLink}
65-
href="pools/ethereum/v2/0x0da692ac0611397027c91e559cfd482c4197e4030002000000000000000005c9"
66-
>
67-
Weighted Pool in recovery mode (not paused)
68-
</Link>
69-
<Link
70-
as={NextLink}
71-
href="pools/ethereum/v2/0x156c02f3f7fef64a3a9d80ccf7085f23cce91d76000000000000000000000570"
72-
>
73-
Composable Pool in recovery mode (not paused)
74-
</Link>
75-
<Link
76-
as={NextLink}
77-
href="pools/fraxtal/v2/0x33251abecb0364df98a27a8d5d7b5ccddc774c42000000000000000000000008"
78-
>
79-
Pool with Merkl APR items (Fraxtal)
80-
</Link>
81-
<Link as={NextLink} href="pools/sepolia/v2/0xd1bdc51decb61ee0c98e47fe17217c58be525180">
82-
CoW AMM Pool (Sepolia)
83-
</Link>
84-
<Link
85-
as={NextLink}
86-
href="pools/ethereum/v2/0xae8535c23afedda9304b03c68a3563b75fc8f92b0000000000000000000005a0"
87-
>
88-
Bricked Composable Stable Pool in recovery mode and paused
89-
</Link>
90-
<Link
91-
as={NextLink}
92-
href="pools/ethereum/v2/0x7b50775383d3d6f0215a8f290f2c9e2eebbeceb20000000000000000000000fe"
93-
>
94-
Old boosted pool with issues
12+
<Link as={NextLink} href="/debug/pools">
13+
Pools
9514
</Link>
9615
<Link as={NextLink} href="/debug/token-select">
9716
Token select
@@ -112,7 +31,10 @@ export default function Debug() {
11231
Modal animation
11332
</Link>
11433
<Link as={NextLink} href="/debug/remove-allowance">
115-
Remove allowance
34+
Remove token allowance
35+
</Link>
36+
<Link as={NextLink} href="/debug/revoke-relayer-approval">
37+
Revoke relayer approval
11638
</Link>
11739
</VStack>
11840
</FadeInOnView>

app/(app)/debug/pools/page.tsx

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
'use client'
2+
import { Heading, VStack } from '@chakra-ui/react'
3+
import NextLink from 'next/link'
4+
import { Link } from '@chakra-ui/react'
5+
import FadeInOnView from '@/lib/shared/components/containers/FadeInOnView'
6+
7+
export default function DebugPools() {
8+
return (
9+
<FadeInOnView>
10+
<VStack padding="lg" margin="lg">
11+
<Heading size="md">Debug pools</Heading>
12+
<Link as={NextLink} href="/pools/sepolia/v3/0x7cf221fa36584f59a4f7fd7b946b8571c78e3692">
13+
Sepolia V3 pool (Balancer 50 BAL 50 WETH)
14+
</Link>
15+
<Link
16+
as={NextLink}
17+
href="pools/ethereum/v2/0x68e3266c9c8bbd44ad9dca5afbfe629022aee9fe000200000000000000000512/add-liquidity"
18+
>
19+
Add liquidity in WEIGHTED (wjAura-weth)
20+
</Link>
21+
<Link
22+
as={NextLink}
23+
href="/pools/ethereum/v2/0x32296969ef14eb0c6d29669c550d4a0449130230000200000000000000000080/add-liquidity"
24+
>
25+
Add liquidity in META_STABLE (wstETH_wETH)
26+
</Link>
27+
<Link
28+
as={NextLink}
29+
href="pools/ethereum/v2/0x1e19cf2d73a72ef1332c882f20534b6519be0276000200000000000000000112/add-liquidity"
30+
>
31+
Add liquidity in STABLE (B-rETH-STABLE in Mainnet)
32+
</Link>
33+
<Link
34+
as={NextLink}
35+
href="pools/ethereum/v2/0x3dd0843a028c86e0b760b1a76929d1c5ef93a2dd000200000000000000000249/add-liquidity"
36+
>
37+
Add liquidity in STABLE with BPT tokens (AuraBal 80/20 pool in Mainnet)
38+
</Link>
39+
<Link
40+
as={NextLink}
41+
href="pools/optimism/v2/0x3dc09db8e571da76dd04e9176afc7feee0b89106000000000000000000000019/add-liquidity"
42+
>
43+
Add liquidity in STABLE (FRAX_USDC_MAI in Optimism)
44+
</Link>
45+
<Link
46+
as={NextLink}
47+
href="pools/ethereum/v2/0x08775ccb6674d6bdceb0797c364c2653ed84f3840002000000000000000004f0/add-liquidity"
48+
>
49+
Add liquidity in nested pool (50WETH-50-3pool)
50+
</Link>
51+
<Link
52+
as={NextLink}
53+
href="pools/ethereum/v2/0xc6853f0539f7d4926c719326d60bd84a752bbb8f00020000000000000000065e/add-liquidity"
54+
>
55+
Add liquidity in Gyro pool (2CLP-WSTETH-WETH in Mainnet)
56+
</Link>
57+
<Link
58+
as={NextLink}
59+
href="pools/polygon/v2/0xee278d943584dd8640eaf4cc6c7a5c80c0073e85000200000000000000000bc7/add-liquidity"
60+
>
61+
Add liquidity in Gyro pool (2CLP_WMATIC/MATICX in Polygon)
62+
</Link>
63+
<Link
64+
as={NextLink}
65+
href="pools/ethereum/v2/0x0da692ac0611397027c91e559cfd482c4197e4030002000000000000000005c9"
66+
>
67+
Weighted Pool in recovery mode (not paused)
68+
</Link>
69+
<Link
70+
as={NextLink}
71+
href="pools/ethereum/v2/0x156c02f3f7fef64a3a9d80ccf7085f23cce91d76000000000000000000000570"
72+
>
73+
Composable Pool in recovery mode (not paused)
74+
</Link>
75+
<Link
76+
as={NextLink}
77+
href="pools/fraxtal/v2/0x33251abecb0364df98a27a8d5d7b5ccddc774c42000000000000000000000008"
78+
>
79+
Pool with Merkl APR items (Fraxtal)
80+
</Link>
81+
<Link as={NextLink} href="pools/sepolia/v2/0xd1bdc51decb61ee0c98e47fe17217c58be525180">
82+
CoW AMM Pool (Sepolia)
83+
</Link>
84+
<Link
85+
as={NextLink}
86+
href="pools/ethereum/v2/0xae8535c23afedda9304b03c68a3563b75fc8f92b0000000000000000000005a0"
87+
>
88+
Bricked Composable Stable Pool in recovery mode and paused
89+
</Link>
90+
<Link
91+
as={NextLink}
92+
href="pools/ethereum/v2/0x7b50775383d3d6f0215a8f290f2c9e2eebbeceb20000000000000000000000fe"
93+
>
94+
Old boosted pool with issues
95+
</Link>
96+
</VStack>
97+
</FadeInOnView>
98+
)
99+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use client'
2+
3+
import { getNetworkConfig } from '@/lib/config/app.config'
4+
import { TransactionStepButton } from '@/lib/modules/transactions/transaction-steps/TransactionStepButton'
5+
import { TransactionLabels } from '@/lib/modules/transactions/transaction-steps/lib'
6+
import { useUserAccount } from '@/lib/modules/web3/UserAccountProvider'
7+
import {
8+
ManagedTransactionInput,
9+
useManagedTransaction,
10+
} from '@/lib/modules/web3/contracts/useManagedTransaction'
11+
import { Center, VStack } from '@chakra-ui/react'
12+
13+
export default function Page() {
14+
const labels: TransactionLabels = {
15+
title: 'Revoke relayer approval',
16+
description: 'Revoke Balancer relayer approval',
17+
init: 'Revoke relayer approval',
18+
confirming: 'Confirming relayer approval revoke...',
19+
confirmed: 'Relayer revoked!',
20+
tooltip: '',
21+
}
22+
const { chain, userAddress } = useUserAccount()
23+
const chainId = chain?.id || 1
24+
const config = getNetworkConfig(chainId)
25+
26+
const relayerAddress = config.contracts.balancer.relayerV6
27+
const vaultAddress = config.contracts.balancer.vaultV2
28+
29+
const props: ManagedTransactionInput = {
30+
contractAddress: vaultAddress,
31+
contractId: 'balancer.vaultV2',
32+
functionName: 'setRelayerApproval',
33+
labels,
34+
chainId,
35+
args: [userAddress, relayerAddress, false],
36+
enabled: !!userAddress,
37+
txSimulationMeta: {},
38+
}
39+
40+
const transaction = useManagedTransaction(props)
41+
42+
return (
43+
<Center>
44+
<VStack w="50%">
45+
<TransactionStepButton step={{ labels: props.labels, ...transaction }} />
46+
</VStack>
47+
</Center>
48+
)
49+
}

lib/modules/tokens/approvals/useTokenApprovalSteps.tsx

+15-4
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,28 @@ export function useTokenApprovalSteps({
6464
})
6565

6666
const steps = useMemo(() => {
67-
return tokenAmountsToApprove.map(tokenAmountToApprove => {
67+
return tokenAmountsToApprove.map((tokenAmountToApprove, index) => {
6868
const { tokenAddress, requiredRawAmount, requestedRawAmount } = tokenAmountToApprove
69+
// USDT edge-case: requires setting approval to 0n before adjusting the value up again
70+
const isApprovingZeroForDoubleApproval =
71+
requiresDoubleApproval(chain, tokenAddress) && requiredRawAmount === 0n
72+
const id = isApprovingZeroForDoubleApproval ? `${tokenAddress}-0` : tokenAddress
6973
const token = getToken(tokenAddress, chain)
7074
const symbol = bptSymbol ?? (token && token?.symbol) ?? 'Unknown'
7175
const labels = buildTokenApprovalLabels({ actionType, symbol })
72-
const id = tokenAddress
7376

7477
const isComplete = () => {
7578
const isAllowed = tokenAllowances.allowanceFor(tokenAddress) >= requiredRawAmount
76-
// USDT edge-case: requires setting approval to 0n before adjusting the value up again
77-
if (requiresDoubleApproval(chain, tokenAddress)) return isAllowed
79+
if (isApprovingZeroForDoubleApproval) {
80+
// Edge case USDT case is completed if:
81+
// - The allowance is 0n
82+
// - The allowance is greater than the required amount (of the next step)
83+
return (
84+
tokenAllowances.allowanceFor(tokenAddress) === 0n ||
85+
tokenAllowances.allowanceFor(tokenAddress) >=
86+
tokenAmountsToApprove[index + 1].requiredRawAmount
87+
)
88+
}
7889
return requiredRawAmount > 0n && isAllowed
7990
}
8091

0 commit comments

Comments
 (0)