-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #530 from balancer/init-buffer
Add support for InitBuffer
- Loading branch information
Showing
16 changed files
with
490 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@balancer/sdk": minor | ||
--- | ||
|
||
Add support for InitBuffer |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { createPublicClient, http } from 'viem'; | ||
import { BALANCER_BUFFER_ROUTER, ChainId, CHAINS } from '@/utils'; | ||
import { Address } from '@/types'; | ||
import { | ||
balancerBufferRouterAbi, | ||
permit2Abi, | ||
vaultExtensionAbi_V3, | ||
vaultV3Abi, | ||
} from '@/abi'; | ||
|
||
export const doInitBufferQuery = async ( | ||
rpcUrl: string, | ||
chainId: ChainId, | ||
wrappedToken: Address, | ||
exactAmountUnderlyingIn: bigint, | ||
exactAmountWrappedIn: bigint, | ||
): Promise<{ issuedShares: bigint }> => { | ||
const client = createPublicClient({ | ||
transport: http(rpcUrl), | ||
chain: CHAINS[chainId], | ||
}); | ||
|
||
const { result: issuedShares } = await client.simulateContract({ | ||
address: BALANCER_BUFFER_ROUTER[chainId], | ||
abi: [ | ||
...balancerBufferRouterAbi, | ||
...vaultV3Abi, | ||
...vaultExtensionAbi_V3, | ||
...permit2Abi, | ||
], | ||
functionName: 'queryInitializeBuffer', | ||
args: [wrappedToken, exactAmountUnderlyingIn, exactAmountWrappedIn], | ||
}); | ||
return { issuedShares }; | ||
}; |
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,94 @@ | ||
import { encodeFunctionData } from 'viem'; | ||
import { TokenAmount } from '@/entities/tokenAmount'; | ||
|
||
import { Permit2 } from '@/entities/permit2Helper'; | ||
|
||
import { doInitBufferQuery } from './doInitBufferQuery'; | ||
import { BALANCER_BUFFER_ROUTER } from '@/utils'; | ||
import { balancerBufferRouterAbi, balancerRouterAbi } from '@/abi'; | ||
|
||
import { | ||
InitBufferBuildCallInput, | ||
InitBufferBuildCallOutput, | ||
InitBufferInput, | ||
InitBufferQueryOutput, | ||
} from './types'; | ||
|
||
export class InitBufferV3 { | ||
async query(input: InitBufferInput): Promise<InitBufferQueryOutput> { | ||
const { issuedShares } = await doInitBufferQuery( | ||
input.rpcUrl, | ||
input.chainId, | ||
input.wrappedAmountIn.address, | ||
input.underlyingAmountIn.rawAmount, | ||
input.wrappedAmountIn.rawAmount, | ||
); | ||
const underlyingAmountIn = TokenAmount.fromInputAmount( | ||
input.underlyingAmountIn, | ||
input.chainId, | ||
); | ||
const wrappedAmountIn = TokenAmount.fromInputAmount( | ||
input.wrappedAmountIn, | ||
input.chainId, | ||
); | ||
|
||
const output: InitBufferQueryOutput = { | ||
issuedShares, | ||
underlyingAmountIn, | ||
wrappedAmountIn, | ||
chainId: input.chainId, | ||
protocolVersion: 3, | ||
to: BALANCER_BUFFER_ROUTER[input.chainId], | ||
}; | ||
|
||
return output; | ||
} | ||
|
||
buildCall(input: InitBufferBuildCallInput): InitBufferBuildCallOutput { | ||
const minIssuedShares = input.slippage.applyTo(input.issuedShares, -1); | ||
|
||
const callData = encodeFunctionData({ | ||
abi: balancerBufferRouterAbi, | ||
functionName: 'initializeBuffer', | ||
args: [ | ||
input.wrappedAmountIn.token.address, | ||
input.underlyingAmountIn.amount, | ||
input.wrappedAmountIn.amount, | ||
minIssuedShares, | ||
] as const, | ||
}); | ||
return { | ||
callData, | ||
to: BALANCER_BUFFER_ROUTER[input.chainId], | ||
value: 0n, // Default to 0 as native not supported | ||
minIssuedShares, | ||
}; | ||
} | ||
|
||
public buildCallWithPermit2( | ||
input: InitBufferBuildCallInput, | ||
permit2: Permit2, | ||
): InitBufferBuildCallOutput { | ||
// generate same calldata as buildCall | ||
const buildCallOutput = this.buildCall(input); | ||
|
||
const args = [ | ||
[], | ||
[], | ||
permit2.batch, | ||
permit2.signature, | ||
[buildCallOutput.callData], | ||
] as const; | ||
|
||
const callData = encodeFunctionData({ | ||
abi: balancerRouterAbi, | ||
functionName: 'permitBatchAndCall', | ||
args, | ||
}); | ||
|
||
return { | ||
...buildCallOutput, | ||
callData, | ||
}; | ||
} | ||
} |
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,31 @@ | ||
import { Address, Hex } from 'viem'; | ||
import { Slippage } from '../slippage'; | ||
import { TokenAmount } from '../tokenAmount'; | ||
import { InputAmount } from '@/types'; | ||
|
||
export type InitBufferInput = { | ||
chainId: number; | ||
rpcUrl: string; | ||
wrappedAmountIn: InputAmount; | ||
underlyingAmountIn: InputAmount; | ||
}; | ||
|
||
export type InitBufferQueryOutput = { | ||
issuedShares: bigint; | ||
wrappedAmountIn: TokenAmount; | ||
underlyingAmountIn: TokenAmount; | ||
chainId: number; | ||
protocolVersion: 3; | ||
to: Address; | ||
}; | ||
|
||
export type InitBufferBuildCallInput = { | ||
slippage: Slippage; | ||
} & InitBufferQueryOutput; | ||
|
||
export type InitBufferBuildCallOutput = { | ||
callData: Hex; | ||
to: Address; | ||
value: bigint; | ||
minIssuedShares: bigint; | ||
}; |
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
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.