-
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.
- Loading branch information
Showing
16 changed files
with
355 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use client' | ||
|
||
import { useUserAccount } from '@/lib/modules/web3/UserAccountProvider' | ||
import { Center, Input, Text, VStack } from '@chakra-ui/react' | ||
import { useState } from 'react' | ||
import { Address } from 'viem' | ||
import { sepolia } from 'viem/chains' | ||
import { useReadContract } from 'wagmi' | ||
import { fNum } from '@/lib/shared/utils/numbers' | ||
import { getGqlChain, getNetworkConfig } from '@/lib/config/app.config' | ||
import { permit2Abi } from '@balancer/sdk' | ||
|
||
export default function Page() { | ||
const [tokenAddress, setTokenAddress] = useState<Address>('' as Address) | ||
|
||
const { chain, userAddress } = useUserAccount() | ||
|
||
const chainId = chain?.id || sepolia.id | ||
|
||
const { data } = usePermit2Allowance({ chainId, tokenAddress, owner: userAddress }) | ||
|
||
return ( | ||
<Center> | ||
<VStack w="50%"> | ||
<Text> | ||
Enter address of token to check permit2 allowance in the current chain:{' '} | ||
{chain ? chain.name : 'None'} | ||
</Text> | ||
<Input type="text" onChange={e => setTokenAddress(e.target.value as Address)} /> | ||
|
||
{data && ( | ||
<div> | ||
<div>Amount: {fNum('integer', data[0])}</div> | ||
<div>Expires: {data[1]}</div> | ||
<div>Nonce: {data[2]}</div> | ||
</div> | ||
)} | ||
</VStack> | ||
</Center> | ||
) | ||
} | ||
|
||
type Params = { | ||
chainId: number | ||
tokenAddress: Address | ||
owner: Address | ||
} | ||
function usePermit2Allowance({ chainId, tokenAddress, owner }: Params) { | ||
const permit2Address = '0x000000000022D473030F116dDEE9F6B43aC78BA3' | ||
const balancerRouter = getNetworkConfig(getGqlChain(chainId)).contracts.balancer.router! | ||
const spender = balancerRouter | ||
|
||
return useReadContract({ | ||
chainId, | ||
address: permit2Address, | ||
abi: permit2Abi, | ||
functionName: 'allowance', | ||
args: [owner, tokenAddress, spender], | ||
query: { | ||
enabled: !!tokenAddress && !!owner, | ||
}, | ||
}) | ||
} |
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
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
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
157 changes: 157 additions & 0 deletions
157
lib/modules/pool/actions/add-liquidity/handlers/UnbalancedAddLiquidity.handlerLOCURA.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,157 @@ | ||
/* 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, | ||
AddLiquidityBaseBuildCallInput, | ||
AddLiquidityBaseQueryOutput, | ||
AddLiquidityKind, | ||
AddLiquidityUnbalancedInput, | ||
Permit2, | ||
Permit2Helper, | ||
PriceImpact, | ||
PriceImpactAmount, | ||
PublicWalletClient, | ||
Slippage, | ||
} from '@balancer/sdk' | ||
import { Pool } from '../../../PoolProvider' | ||
import { | ||
LiquidityActionHelpers, | ||
areEmptyAmounts, | ||
formatBuildCallParams, | ||
} from '../../LiquidityActionHelpers' | ||
import { SdkBuildAddLiquidityInput, SdkQueryAddLiquidityOutput } from '../add-liquidity.types' | ||
import { AddLiquidityHandler, Permit2AddLiquidityInput } from './AddLiquidity.handler' | ||
import { NoncesByTokenAddress } from '@/lib/modules/tokens/approvals/permit2/usePermit2Nonces' | ||
|
||
/** | ||
* 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 class UnbalancedAddLiquidityHandler implements AddLiquidityHandler { | ||
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 async buildCallData({ | ||
slippagePercent, | ||
queryOutput, | ||
account, | ||
permit2, | ||
}: SdkBuildAddLiquidityInput): Promise<TransactionConfig> { | ||
const addLiquidity = new AddLiquidity() | ||
|
||
const buildCallParams = formatBuildCallParams( | ||
this.constructBaseBuildCallInput({ | ||
sdkQueryOutput: queryOutput.sdkQueryOutput, | ||
slippagePercent: slippagePercent, | ||
}), | ||
this.helpers.isV3Pool(), | ||
account | ||
) | ||
|
||
if (this.helpers.isV3Pool() && !permit2) { | ||
throw new Error('Permit2 signature is required for V3 pools') | ||
} | ||
|
||
const { callData, to, value } = | ||
this.helpers.isV3Pool() && permit2 | ||
? addLiquidity.buildCallWithPermit2(buildCallParams, permit2) | ||
: addLiquidity.buildCall(buildCallParams) | ||
|
||
return { | ||
account, | ||
chainId: this.helpers.chainId, | ||
data: callData, | ||
to, | ||
value, | ||
} | ||
} | ||
|
||
public async signPermit2( | ||
input: Permit2AddLiquidityInput, | ||
sdkClient: PublicWalletClient, | ||
nonces?: NoncesByTokenAddress | ||
): Promise<Permit2> { | ||
// console.log('tengo los nonces super ricos') | ||
const baseInput = this.constructBaseBuildCallInput({ | ||
slippagePercent: input.slippagePercent, | ||
sdkQueryOutput: input.sdkQueryOutput as AddLiquidityBaseQueryOutput, | ||
}) | ||
const signature = await Permit2Helper.signAddLiquidityApproval({ | ||
...baseInput, | ||
client: sdkClient, | ||
owner: input.account, | ||
nonces: baseInput.amountsIn.map(a => (a.amount === 0n ? 3 : 1)), | ||
}) | ||
|
||
return signature | ||
} | ||
|
||
/** | ||
* PRIVATE METHODS | ||
*/ | ||
private constructSdkInput( | ||
humanAmountsIn: HumanTokenAmountWithAddress[] | ||
): AddLiquidityUnbalancedInput { | ||
const amountsIn = this.helpers.toSdkInputAmounts(humanAmountsIn) | ||
|
||
return { | ||
chainId: this.helpers.chainId, | ||
rpcUrl: getRpcUrl(this.helpers.chainId), | ||
amountsIn, | ||
kind: AddLiquidityKind.Unbalanced, | ||
} | ||
} | ||
|
||
public constructBaseBuildCallInput({ | ||
slippagePercent, | ||
sdkQueryOutput, | ||
}: { | ||
slippagePercent: string | ||
sdkQueryOutput: AddLiquidityBaseQueryOutput | ||
}): AddLiquidityBaseBuildCallInput { | ||
const baseBuildCallParams = { | ||
...(sdkQueryOutput as AddLiquidityBaseQueryOutput), | ||
slippage: Slippage.fromPercentage(`${Number(slippagePercent)}`), | ||
wethIsEth: this.helpers.includesNativeAsset(sdkQueryOutput.amountsIn), | ||
} | ||
baseBuildCallParams.amountsIn = baseBuildCallParams.amountsIn.filter( | ||
amountIn => amountIn.amount > 0n | ||
) | ||
return baseBuildCallParams | ||
} | ||
} |
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
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
Oops, something went wrong.