-
Notifications
You must be signed in to change notification settings - Fork 21
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 #262 from balancer/223-v3-pool-init
V3 Initialize Pool
- Loading branch information
Showing
22 changed files
with
477 additions
and
107 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 | ||
--- | ||
|
||
Adds Initialize Pool functionality for V3 pools; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,57 @@ | ||
import { balancerRouterAbi } from '@/abi'; | ||
import { PoolState } from '../types'; | ||
import { InitPoolBase, InitPoolBuildOutput, InitPoolInput } from './types'; | ||
import { InitPoolBase, InitPoolBuildOutput, InitPoolInputV3 } from './types'; | ||
import { BALANCER_ROUTER, NATIVE_ASSETS, isSameAddress } from '@/utils'; | ||
import { encodeFunctionData, Address } from 'viem'; | ||
import { getSortedTokens, parseInitializeArgs, getAmounts } from '../utils'; | ||
import { Token } from '../token'; | ||
|
||
export class InitPoolV3 implements InitPoolBase { | ||
buildCall(input: InitPoolInput, poolState: PoolState): InitPoolBuildOutput { | ||
console.log(input, poolState); | ||
throw new Error('Method not implemented.'); | ||
buildCall( | ||
input: InitPoolInputV3, | ||
poolState: PoolState, | ||
): InitPoolBuildOutput { | ||
const sortedTokens = getSortedTokens(poolState.tokens, input.chainId); | ||
const { exactAmountsIn } = this.getAmounts(input, sortedTokens); | ||
const { args } = parseInitializeArgs({ | ||
...input, | ||
exactAmountsIn, | ||
poolAddress: poolState.address, | ||
sortedTokens, | ||
}); | ||
|
||
const call = encodeFunctionData({ | ||
abi: balancerRouterAbi, | ||
functionName: 'initialize', | ||
args, | ||
}); | ||
|
||
const value = this.value(input); | ||
|
||
return { | ||
call, | ||
to: BALANCER_ROUTER[input.chainId] as Address, | ||
value, | ||
}; | ||
} | ||
|
||
private getAmounts( | ||
input: InitPoolInputV3, | ||
tokens: Token[], | ||
): { exactAmountsIn: bigint[] } { | ||
return { | ||
exactAmountsIn: getAmounts(tokens, input.amountsIn), | ||
}; | ||
} | ||
|
||
private value(input: InitPoolInputV3) { | ||
return input.wethIsEth | ||
? (input.amountsIn.find((a) => | ||
isSameAddress( | ||
a.address, | ||
NATIVE_ASSETS[input.chainId].wrapped, | ||
), | ||
)?.rawAmount as bigint) | ||
: 0n; | ||
} | ||
} |
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.