API initialization
TheBalancerApi class provides helper functions for interacting with the Berancer API.
Constructor
| Name | Type | Description | Mainnet Value |
|---|---|---|---|
| apiUrl | string | Url of API | https://chgbtcc9ffu7rbdw2kmu4urwy.stellate.sh/ |
| chainId | number | Chain that will be queried | 80094 |
Properties
TheBalancerApi class initializes the following properties:
- Pools - Methods for interacting with individual pools
- NestedPools - Methods for working with nested pools
- SorSwapPaths - Methods for optimizing swap paths
Pools
pools.fetchPoolState
Finds state of given pool.| Name | Type | Description |
|---|---|---|
| id | string | ID of pool, v2=poolId, v3=address |
pools.fetchPoolStateWithBalances
Finds state of given pool including token balances and pool shares.| Name | Type | Description |
|---|---|---|
| id | string | poolId |
Nested pools
nestedPools.fetchPoolState
Finds state of a set of nested pools.| Name | Type | Description |
|---|---|---|
| id | string | ID of pool, v2=poolId, v3=address |
SorSwapPaths
sorSwapPaths.fetchSorSwapPaths
Finds optimized swap paths for a given swap config.| Name | Type | Description |
|---|---|---|
| sorInput | SorInput | Swap configs |
AddLiquidity
This class provides functionality to:- Perform on-chain queries to see the result of an addLiquidity operation
- Build an addLiquidity transaction, with slippage, for a consumer to submit
- Supported add types: SingleToken, Unbalanced, Proportional
Constructor
Methods
query
Simulate addLiquidity operation by using an onchain call.| Name | Type | Description |
|---|---|---|
| input | AddLiquidityInput | User defined inputs |
| poolState | PoolState | Current state of pool that liquidity is being added to |
buildCall. Includes updated bptOut amount.
buildCall
Builds the addLiquidity transaction using user defined slippage.| Name | Type | Description |
|---|---|---|
| input | AddLiquidityBuildCallInput | Parameters required to build the call including user defined slippage |
buildCallWithPermit2
Builds the addLiquidity transaction using user defined slippage and Permit2 signature for token approval.Check out the Permit2 Helper section on how to generate a Permit2 signature.
| Name | Type | Description |
|---|---|---|
| input | AddLiquidityBuildCallInput | Parameters required to build the call including user defined slippage |
| permit2 | Permit2 | Permit2 object with metadata and encoded signature |
RemoveLiquidity
This class provides functionality to:- Perform on-chain queries to see the result of a removeLiquidity operation
- Build a removeLiquidity transaction, with slippage, for a consumer to submit
- Supported remove types: Unbalanced, SingleTokenExactOutInput, SingleTokenExactInInput, Proportional
Constructor
Methods
query
Simulate removeLiquidity operation by using an onchain call.| Name | Type | Description |
|---|---|---|
| input | RemoveLiquidityInput | User defined inputs |
| poolState | PoolState | Current state of pool that liquidity is being removed from |
buildCall. Includes updated amountsOut amount.
queryRemoveLiquidityRecovery
Calculates proportional exit using pool state. Note - this does not do an onchain query.| Name | Type | Description |
|---|---|---|
| input | RemoveLiquidityRecoveryInput | User defined inputs |
| poolState | PoolState | Current state of pool that liquidity is being removed from |
buildCall. Includes updated amountsOut amount.
buildCall
Builds the removeLiquidity transaction using user defined slippage.| Name | Type | Description |
|---|---|---|
| input | RemoveLiquidityBuildCallInput | Input with user defined slippage |
buildCallWithPermit
Builds the removeLiquidity transaction using user defined slippage and Permit signature for token approval.Check out the Permit Helper section on how to generate a Permit signature.
| Name | Type | Description |
|---|---|---|
| input | RemoveLiquidityBuildCallInput | Input with user defined slippage |
| permit | Permit | Permit object with metadata and encoded signature |
Swap
This class provides functionality to:- Perform on-chain queries to see the result of a Swap operation
- Build a Swap transaction, with slippage, for a consumer to submit
Constructor
| Name | Type | Description |
|---|---|---|
| swapInput | SwapInput | Swap input including path information. |
SwapInput data is normally returned from an API SOR query but may be constructed manually.
Methods
query
Gets up to date swap result by querying onchain.| Name | Type | Description |
|---|---|---|
| rpcUrl (optional) | string | RPC URL, e.g. Infura/Alchemy |
| block (optional) | bigint | Block no to perform the query |
expectedAmountOut or expectedAmountIn depending on swap kind.
buildCall
Builds the swap transaction using user defined slippage.| Name | Type | Description |
|---|---|---|
| input | SwapBuildCallInput | Input with user defined slippage |
minAmountOut or maxAmountIn depending on swap kind.
buildCallWithPermit2
Builds the swap transaction using user defined slippage and Permit2 signature for token approval.Check out the Permit2 Helper section on how to generate a Permit2 signature.
| Name | Type | Description |
|---|---|---|
| input | SwapBuildCallInput | Input with user defined slippage |
| permit2 | Permit2 | Permit2 object with metadata and encoded signature |
minAmountOut or maxAmountIn depending on swap kind.
quote
Gives the combined return amount for all paths. Note - this always uses the original path amounts provided in constructor and does not get updated.inputAmount
outputAmount
queryCallData
PriceImpact
This class provides helper functions to calculate Price Impact for add/remove/swap actions.Methods
addLiquiditySingleToken
Calculate price impact on add liquidity single token operations.| Name | Type | Description |
|---|---|---|
| input | AddLiquiditySingleTokenInput | Same input used in the corresponding add liquidity operation |
| poolState | PoolState | Current state of pool that liquidity is being added to |
addLiquidityUnbalanced
Calculate price impact on add liquidity unbalanced operations.| Name | Type | Description |
|---|---|---|
| input | AddLiquidityUnbalancedInput | Same input used in the corresponding add liquidity operation |
| poolState | PoolState | Current state of pool that liquidity is being added to |
addLiquidityNested
Calculate price impact on add liquidity nested token operations.| Name | Type | Description |
|---|---|---|
| input | AddLiquidityNestedInput | Same input used in the corresponding add liquidity operation |
| nestedPoolState | NestedPoolState | Current state of nested pools |
removeLiquidity
Calculate price impact on remove liquidity operations.| Name | Type | Description |
|---|---|---|
| input | RemoveLiquiditySingleTokenExactInInput | Same input used in the corresponding remove liquidity operation |
| input | RemoveLiquidityUnbalancedInput | Same input used in the corresponding remove liquidity operation |
| poolState | PoolState | Current state of pool that liquidity is being removed from |
removeLiquidityNested
Calculate price impact on remove liquidity single token nested operations.| Name | Type | Description |
|---|---|---|
| input | RemoveLiquidityNestedSingleTokenInput | Same input used in the corresponding remove liquidity operation |
| nestedPoolState | NestedPoolState | Current state of nested pools |
swap
Calculate price impact on swap operations.| Name | Type | Description |
|---|---|---|
| swapInput | SwapInput | Swap input including path information. |
| rpcUrl (optional) | string | RPC URL, e.g. Infura/Alchemy |
| block (optional) | bigint | Block no to perform the query |
SwapInput data is normally returned from an API SOR query but may be constructed manually.
Returns
Utils
Helper functions.calculateProportionalAmounts
Given pool balances (including BPT) and a reference token amount, it calculates all other amounts proportional to the reference amount.| Name | Type | Description |
|---|---|---|
| pool | See above | Pool state |
| referenceAmount | InputAmount | Ref token amount |
Permit2 helper
Balancer v3 handles token approval through Permit2 and this helper facilitates Permit2 signature generation. Each operation (i.e.addLiquidity, addLiquidityNested, addLiquidityBoosted and swap) has its own method that leverages the same input type of the operation itself in order to simplify signature generation.
Function
| Name | Type | Description |
|---|---|---|
| input | AddLiquidityBaseBuildCallInput | Add Liquidity Input |
| client | PublicWalletClient | Viem’s wallet client with public actions |
| owner | Address | User address |
| nonces (optional) | number[] | Nonces for each token |
| expirations (optional) | number[] | Expirations for each token |
Permit helper
Balancer v3 conforms with EIP-2612 and this helper facilitates Permit signature generation. Each operation (i.e.removeLiquidity, removeLiquidityNested and removeLiquidityBoosted) has its own method that leverages the same input type of the operation itself in order to simplify signature generation.
Function
| Name | Type | Description |
|---|---|---|
| input | RemoveLiquidityBaseBuildCallInput | Remove Liquidity Input |
| client | PublicWalletClient | Viem’s wallet client with public actions |
| owner | Address | User address |
| nonces (optional) | number[] | Nonces for each token |
| expirations (optional) | number[] | Expirations for each token |