1
1
/* eslint-disable @typescript-eslint/no-non-null-assertion */
2
- import { HumanTokenAmountWithAddress } from '@/lib/modules/tokens/token.types'
3
2
import { TransactionConfig } from '@/lib/modules/web3/contracts/contract.types'
4
- import { getRpcUrl } from '@/lib/modules/web3/transports'
5
- import {
6
- AddLiquidity ,
7
- AddLiquidityBaseBuildCallInput ,
8
- AddLiquidityBaseQueryOutput ,
9
- AddLiquidityKind ,
10
- AddLiquidityUnbalancedInput ,
11
- Permit2 ,
12
- Permit2Helper ,
13
- PriceImpact ,
14
- PriceImpactAmount ,
15
- PublicWalletClient ,
16
- Slippage ,
17
- } from '@balancer/sdk'
18
- import { Pool } from '../../../PoolProvider'
19
- import {
20
- LiquidityActionHelpers ,
21
- areEmptyAmounts ,
22
- formatBuildCallParams ,
23
- } from '../../LiquidityActionHelpers'
24
- import { SdkBuildAddLiquidityInput , SdkQueryAddLiquidityOutput } from '../add-liquidity.types'
25
- import { AddLiquidityHandler , Permit2AddLiquidityInput } from './AddLiquidity.handler'
26
- import { NoncesByTokenAddress } from '@/lib/modules/tokens/approvals/permit2/usePermit2Nonces'
3
+ import { AddLiquidity } from '@balancer/sdk'
4
+ import { formatBuildCallParams } from '../../LiquidityActionHelpers'
5
+ import { SdkBuildAddLiquidityInput } from '../add-liquidity.types'
6
+ import { BaseUnbalancedAddLiquidityHandler } from './BaseUnbalancedAddLiquidity.handler'
7
+ import { constructBaseBuildCallInput } from './v3Helpers'
27
8
28
9
/**
29
10
* UnbalancedAddLiquidityHandler is a handler that implements the
30
- * AddLiquidityHandler interface for unbalanced adds, e.g. where the user
11
+ * AddLiquidityHandler interface for unbalanced adds in v1(cowAMM) and v2 pools , e.g. where the user
31
12
* specifies the token amounts in. It uses the Balancer SDK to implement it's
32
13
* methods. It also handles the case where one of the input tokens is the native
33
14
* asset instead of the wrapped native asset.
34
15
*/
35
- export class UnbalancedAddLiquidityHandler implements AddLiquidityHandler {
36
- helpers : LiquidityActionHelpers
37
-
38
- constructor ( pool : Pool ) {
39
- this . helpers = new LiquidityActionHelpers ( pool )
40
- }
41
-
42
- public async simulate (
43
- humanAmountsIn : HumanTokenAmountWithAddress [ ]
44
- ) : Promise < SdkQueryAddLiquidityOutput > {
45
- const addLiquidity = new AddLiquidity ( )
46
- const addLiquidityInput = this . constructSdkInput ( humanAmountsIn )
47
-
48
- const sdkQueryOutput = await addLiquidity . query ( addLiquidityInput , this . helpers . poolState )
49
-
50
- return { bptOut : sdkQueryOutput . bptOut , sdkQueryOutput }
51
- }
52
-
53
- public async getPriceImpact ( humanAmountsIn : HumanTokenAmountWithAddress [ ] ) : Promise < number > {
54
- if ( areEmptyAmounts ( humanAmountsIn ) ) {
55
- // Avoid price impact calculation when there are no amounts in
56
- return 0
57
- }
58
-
59
- const addLiquidityInput = this . constructSdkInput ( humanAmountsIn )
60
-
61
- const priceImpactABA : PriceImpactAmount = await PriceImpact . addLiquidityUnbalanced (
62
- addLiquidityInput ,
63
- this . helpers . poolState
64
- )
65
-
66
- return priceImpactABA . decimal
67
- }
68
-
16
+ export class UnbalancedAddLiquidityHandler extends BaseUnbalancedAddLiquidityHandler {
69
17
public async buildCallData ( {
18
+ humanAmountsIn,
70
19
slippagePercent,
71
20
queryOutput,
72
21
account,
73
- permit2,
74
22
} : SdkBuildAddLiquidityInput ) : Promise < TransactionConfig > {
75
23
const addLiquidity = new AddLiquidity ( )
76
24
77
25
const buildCallParams = formatBuildCallParams (
78
- this . constructBaseBuildCallInput ( {
26
+ constructBaseBuildCallInput ( {
27
+ humanAmountsIn,
79
28
sdkQueryOutput : queryOutput . sdkQueryOutput ,
80
29
slippagePercent : slippagePercent ,
30
+ pool : this . helpers . pool ,
81
31
} ) ,
82
- this . helpers . isV3Pool ( ) ,
83
32
account
84
33
)
85
34
86
- if ( this . helpers . isV3Pool ( ) && ! permit2 ) {
87
- throw new Error ( 'Permit2 signature is required for V3 pools' )
88
- }
35
+ console . log ( { buildCallParams } )
89
36
90
- const { callData, to, value } =
91
- this . helpers . isV3Pool ( ) && permit2
92
- ? addLiquidity . buildCallWithPermit2 ( buildCallParams , permit2 )
93
- : addLiquidity . buildCall ( buildCallParams )
37
+ const { callData, to, value } = addLiquidity . buildCall ( buildCallParams )
94
38
95
39
return {
96
40
account,
@@ -100,56 +44,4 @@ export class UnbalancedAddLiquidityHandler implements AddLiquidityHandler {
100
44
value,
101
45
}
102
46
}
103
-
104
- public async signPermit2 (
105
- input : Permit2AddLiquidityInput ,
106
- sdkClient : PublicWalletClient ,
107
- nonces : NoncesByTokenAddress
108
- ) : Promise < Permit2 > {
109
- const baseInput = this . constructBaseBuildCallInput ( {
110
- slippagePercent : input . slippagePercent ,
111
- sdkQueryOutput : input . sdkQueryOutput as AddLiquidityBaseQueryOutput ,
112
- } )
113
- const signature = await Permit2Helper . signAddLiquidityApproval ( {
114
- ...baseInput ,
115
- client : sdkClient ,
116
- owner : input . account ,
117
- nonces : baseInput . amountsIn . map ( a => nonces [ a . token . address ] ) ,
118
- } )
119
- return signature
120
- }
121
-
122
- /**
123
- * PRIVATE METHODS
124
- */
125
- private constructSdkInput (
126
- humanAmountsIn : HumanTokenAmountWithAddress [ ]
127
- ) : AddLiquidityUnbalancedInput {
128
- const amountsIn = this . helpers . toSdkInputAmounts ( humanAmountsIn )
129
-
130
- return {
131
- chainId : this . helpers . chainId ,
132
- rpcUrl : getRpcUrl ( this . helpers . chainId ) ,
133
- amountsIn,
134
- kind : AddLiquidityKind . Unbalanced ,
135
- }
136
- }
137
-
138
- public constructBaseBuildCallInput ( {
139
- slippagePercent,
140
- sdkQueryOutput,
141
- } : {
142
- slippagePercent : string
143
- sdkQueryOutput : AddLiquidityBaseQueryOutput
144
- } ) : AddLiquidityBaseBuildCallInput {
145
- const baseBuildCallParams = {
146
- ...( sdkQueryOutput as AddLiquidityBaseQueryOutput ) ,
147
- slippage : Slippage . fromPercentage ( `${ Number ( slippagePercent ) } ` ) ,
148
- wethIsEth : this . helpers . includesNativeAsset ( sdkQueryOutput . amountsIn ) ,
149
- }
150
- // baseBuildCallParams.amountsIn = baseBuildCallParams.amountsIn.filter(
151
- // amountIn => amountIn.amount > 0n
152
- // )
153
- return baseBuildCallParams
154
- }
155
47
}
0 commit comments