This repository was archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathProportionalRemoveLiquidity.handler.ts
93 lines (80 loc) · 2.61 KB
/
ProportionalRemoveLiquidity.handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { TransactionConfig } from '@/lib/modules/web3/contracts/contract.types'
import {
HumanAmount,
InputAmount,
RemoveLiquidity,
RemoveLiquidityKind,
RemoveLiquidityProportionalInput,
Slippage,
} from '@balancer/sdk'
import { Address, parseEther } from 'viem'
import { BPT_DECIMALS } from '../../../pool.constants'
import { Pool } from '../../../PoolProvider'
import { LiquidityActionHelpers, adaptBuildCallParams } from '../../LiquidityActionHelpers'
import {
QueryRemoveLiquidityInput,
SdkBuildRemoveLiquidityInput,
SdkQueryRemoveLiquidityOutput,
} from '../remove-liquidity.types'
import { RemoveLiquidityHandler } from './RemoveLiquidity.handler'
import { getRpcUrl } from '@/lib/modules/web3/transports'
export class ProportionalRemoveLiquidityHandler implements RemoveLiquidityHandler {
helpers: LiquidityActionHelpers
constructor(pool: Pool) {
this.helpers = new LiquidityActionHelpers(pool)
}
public async simulate({
humanBptIn: bptIn,
}: QueryRemoveLiquidityInput): Promise<SdkQueryRemoveLiquidityOutput> {
const removeLiquidity = new RemoveLiquidity()
const removeLiquidityInput = this.constructSdkInput(bptIn)
const sdkQueryOutput = await removeLiquidity.query(removeLiquidityInput, this.helpers.poolState)
return { amountsOut: sdkQueryOutput.amountsOut.filter(a => a.amount > 0n), sdkQueryOutput }
}
public async getPriceImpact(): Promise<number> {
// proportional remove liquidity does not have price impact
return 0
}
public async buildCallData({
account,
slippagePercent,
queryOutput,
wethIsEth,
}: SdkBuildRemoveLiquidityInput): Promise<TransactionConfig> {
const removeLiquidity = new RemoveLiquidity()
const baseBuildCallParams = {
...queryOutput.sdkQueryOutput,
slippage: Slippage.fromPercentage(`${Number(slippagePercent)}`),
wethIsEth,
}
const buildCallParams = adaptBuildCallParams(
baseBuildCallParams,
this.helpers.isV3Pool(),
account
)
const { callData, to, value } = removeLiquidity.buildCall(buildCallParams)
return {
account,
chainId: this.helpers.chainId,
data: callData,
to,
value,
}
}
/**
* PRIVATE METHODS
*/
protected constructSdkInput(humanBptIn: HumanAmount): RemoveLiquidityProportionalInput {
const bptIn: InputAmount = {
rawAmount: parseEther(humanBptIn),
decimals: BPT_DECIMALS,
address: this.helpers.pool.address as Address,
}
return {
chainId: this.helpers.chainId,
rpcUrl: getRpcUrl(this.helpers.chainId),
bptIn,
kind: RemoveLiquidityKind.Proportional,
}
}
}