1
1
/**
2
- * Example showing how to add liquidity to a pool.
2
+ * Example showing how to add liquidity to a pool from a V2 nested pool.
3
+ * User use signature to approve Balancer Relayer to use any existing V2 token approvals.
3
4
* (Runs against a local Anvil fork)
4
5
*
5
6
* Run with:
6
- * pnpm example ./examples/addLiquidity/addLiquidityNested.ts
7
+ * pnpm example ./examples/addLiquidity/addLiquidityNested.V2. ts
7
8
*/
8
9
import {
9
10
createTestClient ,
@@ -21,18 +22,20 @@ import {
21
22
CHAINS ,
22
23
PriceImpact ,
23
24
Relayer ,
24
- replaceWrapped ,
25
25
Slippage ,
26
+ AddLiquidityNested ,
27
+ AddLiquidityNestedInput ,
26
28
} from '../../src' ;
27
- import { ANVIL_NETWORKS , startFork } from '../../test/anvil/anvil-global-setup' ;
29
+ import {
30
+ ANVIL_NETWORKS ,
31
+ NetworkSetup ,
32
+ startFork ,
33
+ } from '../../test/anvil/anvil-global-setup' ;
28
34
import { makeForkTx } from 'examples/lib/makeForkTx' ;
29
35
import { getSlot } from 'examples/lib/getSlot' ;
30
- import { AddLiquidityNestedInput } from '@/entities/addLiquidityNested/addLiquidityNestedV2/types' ;
31
- import { AddLiquidityNested } from '@/entities/addLiquidityNested' ;
32
36
33
- async function runAgainstFork ( ) {
37
+ const addLiquidityNested = async ( ) => {
34
38
// User defined inputs
35
- const { rpcUrl } = await startFork ( ANVIL_NETWORKS . MAINNET ) ;
36
39
const chainId = ChainId . MAINNET ;
37
40
// WETH-3POOL
38
41
const pool = {
@@ -48,61 +51,17 @@ async function runAgainstFork() {
48
51
} ,
49
52
] ;
50
53
const slippage = Slippage . fromPercentage ( '1' ) ; // 1%
51
- // This example requires the account to sign relayer approval
52
- const client = createTestClient ( {
53
- mode : 'anvil' ,
54
- chain : CHAINS [ chainId ] ,
55
- transport : http ( rpcUrl ) ,
56
- } )
57
- . extend ( publicActions )
58
- . extend ( walletActions ) ;
59
- const userAccount = ( await client . getAddresses ( ) ) [ 0 ] ;
60
- const relayerApprovalSignature = await Relayer . signRelayerApproval (
61
- BALANCER_RELAYER [ chainId ] ,
62
- userAccount ,
63
- client ,
64
- ) ;
65
-
66
- const call = await addLiquidityNested ( {
67
- rpcUrl,
68
- chainId,
69
- userAccount,
70
- relayerApprovalSignature,
71
- amountsIn,
72
- poolId : pool . id ,
73
- slippage,
74
- } ) ;
54
+ const wethIsEth = false ;
75
55
76
- await makeForkTx (
77
- call ,
78
- {
79
- rpcUrl,
80
- chainId,
81
- impersonateAccount : userAccount ,
82
- forkTokens : amountsIn . map ( ( a ) => ( {
83
- address : a . address ,
84
- slot : getSlot ( chainId , a . address ) ,
85
- rawBalance : a . rawAmount ,
86
- } ) ) ,
87
- } ,
88
- [ ...amountsIn . map ( ( a ) => a . address ) , pool . address ] ,
89
- 2 , // TODO - Currently only V2 support, update when SC ready
90
- ) ;
91
- }
56
+ // This sets up a local fork with a test client/account
57
+ const { rpcUrl, client } = await setup ( chainId , ANVIL_NETWORKS . MAINNET ) ;
58
+ const userAccount = ( await client . getAddresses ( ) ) [ 0 ] ;
92
59
93
- const addLiquidityNested = async ( {
94
- rpcUrl,
95
- userAccount,
96
- relayerApprovalSignature,
97
- chainId,
98
- poolId,
99
- amountsIn,
100
- slippage,
101
- } ) => {
102
60
// API is used to fetch relevant pool data
103
61
const balancerApi = new BalancerApi ( API_ENDPOINT , chainId ) ;
104
- const nestedPoolState =
105
- await balancerApi . nestedPools . fetchNestedPoolState ( poolId ) ;
62
+ const nestedPoolState = await balancerApi . nestedPools . fetchNestedPoolState (
63
+ pool . id ,
64
+ ) ;
106
65
107
66
// setup add liquidity helper
108
67
const addLiquidityNested = new AddLiquidityNested ( ) ;
@@ -131,24 +90,82 @@ const addLiquidityNested = async ({
131
90
amountsIn : queryOutput . amountsIn . map ( ( a ) => a . amount ) ,
132
91
} ) ;
133
92
console . log ( `BPT Out: ${ queryOutput . bptOut . amount . toString ( ) } ` ) ;
134
- const wethIsEth = false ;
135
93
136
- const call = addLiquidityNested . buildCall ( {
137
- ...queryOutput ,
138
- slippage,
139
- accountAddress : userAccount ,
140
- relayerApprovalSignature,
141
- wethIsEth,
142
- } ) ;
94
+ // Use signature to approve Balancer Relayer to use any existing V2 token approvals
95
+ const relayerApprovalSignature = await Relayer . signRelayerApproval (
96
+ BALANCER_RELAYER [ chainId ] ,
97
+ userAccount ,
98
+ client ,
99
+ ) ;
143
100
144
- let tokensIn = queryOutput . amountsIn . map ( ( a ) => a . token ) ;
145
- if ( wethIsEth ) {
146
- tokensIn = replaceWrapped ( tokensIn , chainId ) ;
147
- }
101
+ const call = addLiquidityNested . buildCall (
102
+ addLiquidityNested . buildAddLiquidityInput ( queryOutput , {
103
+ slippage,
104
+ accountAddress : userAccount ,
105
+ relayerApprovalSignature,
106
+ wethIsEth,
107
+ } ) ,
108
+ ) ;
148
109
149
110
console . log ( '\nWith slippage applied:' ) ;
150
111
console . log ( `Min BPT Out: ${ call . minBptOut . toString ( ) } ` ) ;
151
- return call ;
112
+
113
+ return {
114
+ rpcUrl,
115
+ chainId,
116
+ txInfo : {
117
+ to : call . to ,
118
+ callData : call . callData ,
119
+ } ,
120
+ account : userAccount ,
121
+ bptOut : queryOutput . bptOut ,
122
+ amountsIn : queryOutput . amountsIn ,
123
+ protocolVersion : queryOutput . protocolVersion ,
124
+ } ;
152
125
} ;
153
126
127
+ async function runAgainstFork ( ) {
128
+ const {
129
+ rpcUrl,
130
+ chainId,
131
+ txInfo,
132
+ account,
133
+ bptOut,
134
+ amountsIn,
135
+ protocolVersion,
136
+ } = await addLiquidityNested ( ) ;
137
+
138
+ await makeForkTx (
139
+ txInfo ,
140
+ {
141
+ rpcUrl,
142
+ chainId,
143
+ impersonateAccount : account ,
144
+ forkTokens : amountsIn . map ( ( a ) => ( {
145
+ address : a . token . address ,
146
+ slot : getSlot ( chainId , a . token . address ) ,
147
+ rawBalance : a . amount ,
148
+ } ) ) ,
149
+ } ,
150
+ [ ...amountsIn . map ( ( a ) => a . token . address ) , bptOut . token . address ] ,
151
+ protocolVersion ,
152
+ ) ;
153
+ }
154
+
155
+ async function setup ( chainId : ChainId , network : NetworkSetup ) {
156
+ const { rpcUrl } = await startFork ( network ) ;
157
+
158
+ const client = createTestClient ( {
159
+ mode : 'anvil' ,
160
+ chain : CHAINS [ chainId ] ,
161
+ transport : http ( rpcUrl ) ,
162
+ } )
163
+ . extend ( publicActions )
164
+ . extend ( walletActions ) ;
165
+ return {
166
+ client,
167
+ rpcUrl,
168
+ } ;
169
+ }
170
+
154
171
export default runAgainstFork ;
0 commit comments