-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract base abstract class
- Loading branch information
Showing
13 changed files
with
245 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
lib/modules/pool/actions/add-liquidity/handlers/BaseUnbalancedAddLiquidity.handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import { HumanTokenAmountWithAddress } from '@/lib/modules/tokens/token.types' | ||
import { TransactionConfig } from '@/lib/modules/web3/contracts/contract.types' | ||
import { getRpcUrl } from '@/lib/modules/web3/transports' | ||
import { | ||
AddLiquidity, | ||
AddLiquidityKind, | ||
AddLiquidityUnbalancedInput, | ||
PriceImpact, | ||
PriceImpactAmount, | ||
} from '@balancer/sdk' | ||
import { Pool } from '../../../PoolProvider' | ||
import { LiquidityActionHelpers, areEmptyAmounts } from '../../LiquidityActionHelpers' | ||
import { SdkBuildAddLiquidityInput, SdkQueryAddLiquidityOutput } from '../add-liquidity.types' | ||
import { AddLiquidityHandler } from './AddLiquidity.handler' | ||
|
||
/** | ||
* UnbalancedAddLiquidityHandler is a handler that implements the | ||
* AddLiquidityHandler interface for unbalanced adds, e.g. where the user | ||
* specifies the token amounts in. It uses the Balancer SDK to implement it's | ||
* methods. It also handles the case where one of the input tokens is the native | ||
* asset instead of the wrapped native asset. | ||
*/ | ||
export abstract class BaseUnbalancedAddLiquidityHandler implements AddLiquidityHandler { | ||
protected helpers: LiquidityActionHelpers | ||
|
||
constructor(pool: Pool) { | ||
this.helpers = new LiquidityActionHelpers(pool) | ||
} | ||
|
||
public async simulate( | ||
humanAmountsIn: HumanTokenAmountWithAddress[] | ||
): Promise<SdkQueryAddLiquidityOutput> { | ||
const addLiquidity = new AddLiquidity() | ||
const addLiquidityInput = this.constructSdkInput(humanAmountsIn) | ||
|
||
const sdkQueryOutput = await addLiquidity.query(addLiquidityInput, this.helpers.poolState) | ||
|
||
return { bptOut: sdkQueryOutput.bptOut, sdkQueryOutput } | ||
} | ||
|
||
public async getPriceImpact(humanAmountsIn: HumanTokenAmountWithAddress[]): Promise<number> { | ||
if (areEmptyAmounts(humanAmountsIn)) { | ||
// Avoid price impact calculation when there are no amounts in | ||
return 0 | ||
} | ||
|
||
const addLiquidityInput = this.constructSdkInput(humanAmountsIn) | ||
|
||
const priceImpactABA: PriceImpactAmount = await PriceImpact.addLiquidityUnbalanced( | ||
addLiquidityInput, | ||
this.helpers.poolState | ||
) | ||
|
||
return priceImpactABA.decimal | ||
} | ||
|
||
public abstract buildCallData(input: SdkBuildAddLiquidityInput): Promise<TransactionConfig> | ||
|
||
/** | ||
* PRIVATE METHODS | ||
*/ | ||
protected constructSdkInput( | ||
humanAmountsIn: HumanTokenAmountWithAddress[] | ||
): AddLiquidityUnbalancedInput { | ||
const amountsIn = this.helpers.toSdkInputAmounts(humanAmountsIn) | ||
|
||
return { | ||
chainId: this.helpers.chainId, | ||
rpcUrl: getRpcUrl(this.helpers.chainId), | ||
amountsIn, | ||
kind: AddLiquidityKind.Unbalanced, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
lib/modules/pool/actions/add-liquidity/handlers/UnbalancedAddLiquidityV3.handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
import { TransactionConfig } from '@/lib/modules/web3/contracts/contract.types' | ||
import { AddLiquidity } from '@balancer/sdk' | ||
import { SdkBuildAddLiquidityInput } from '../add-liquidity.types' | ||
import { BaseUnbalancedAddLiquidityHandler } from './BaseUnbalancedAddLiquidity.handler' | ||
import { constructBaseBuildCallInput } from './v3Helpers' | ||
|
||
/** | ||
* UnbalancedAddLiquidityHandlerV3 is a handler that implements the | ||
* AddLiquidityHandler interface for unbalanced V3 adds, e.g. where the user | ||
* specifies the token amounts in. It uses the Balancer SDK to implement it's | ||
* methods. It also handles the case where one of the input tokens is the native | ||
* asset instead of the wrapped native asset. | ||
*/ | ||
export class UnbalancedAddLiquidityHandlerV3 extends BaseUnbalancedAddLiquidityHandler { | ||
public async buildCallData({ | ||
humanAmountsIn, | ||
slippagePercent, | ||
queryOutput, | ||
account, | ||
permit2, | ||
}: SdkBuildAddLiquidityInput): Promise<TransactionConfig> { | ||
const addLiquidity = new AddLiquidity() | ||
|
||
const buildCallParams = constructBaseBuildCallInput({ | ||
humanAmountsIn, | ||
sdkQueryOutput: queryOutput.sdkQueryOutput, | ||
slippagePercent: slippagePercent, | ||
pool: this.helpers.pool, | ||
}) | ||
|
||
if (!permit2) { | ||
throw new Error('Permit2 signature is required for V3 pools') | ||
} | ||
|
||
const { callData, to, value } = addLiquidity.buildCallWithPermit2(buildCallParams, permit2) | ||
|
||
return { | ||
account, | ||
chainId: this.helpers.chainId, | ||
data: callData, | ||
to, | ||
value, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
lib/modules/pool/actions/add-liquidity/handlers/v3Helpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//TODO: move all this file logic to a better place | ||
|
||
import { | ||
AddLiquidityBaseBuildCallInput, | ||
AddLiquidityBaseQueryOutput, | ||
Slippage, | ||
} from '@balancer/sdk' | ||
import { Pool } from '../../../PoolProvider' | ||
import { LiquidityActionHelpers } from '../../LiquidityActionHelpers' | ||
import { HumanTokenAmountWithAddress } from '@/lib/modules/tokens/token.types' | ||
|
||
// For now only valid for unbalanced adds | ||
export function constructBaseBuildCallInput({ | ||
humanAmountsIn, | ||
slippagePercent, | ||
sdkQueryOutput, | ||
pool, | ||
}: { | ||
humanAmountsIn: HumanTokenAmountWithAddress[] | ||
slippagePercent: string | ||
sdkQueryOutput: AddLiquidityBaseQueryOutput | ||
pool: Pool | ||
}): AddLiquidityBaseBuildCallInput { | ||
const helpers = new LiquidityActionHelpers(pool) | ||
|
||
console.log({ includesNativeAsset: helpers.isNativeAssetIn(humanAmountsIn) }) | ||
|
||
const baseBuildCallParams = { | ||
...(sdkQueryOutput as AddLiquidityBaseQueryOutput), | ||
slippage: Slippage.fromPercentage(`${Number(slippagePercent)}`), | ||
wethIsEth: helpers.isNativeAssetIn(humanAmountsIn), | ||
} | ||
// baseBuildCallParams.amountsIn = baseBuildCallParams.amountsIn.filter( | ||
// amountIn => amountIn.amount > 0n | ||
// ) | ||
return baseBuildCallParams | ||
} |
Oops, something went wrong.