Skip to content

balancer/b-sdk

Folders and files

NameName
Last commit message
Last commit date
Feb 7, 2024
Jan 30, 2024
Jan 17, 2024
Jan 4, 2024
Feb 7, 2024
Feb 7, 2024
Feb 7, 2024
Sep 21, 2023
Feb 7, 2024
Mar 2, 2023
Feb 2, 2024
Jan 4, 2024
Feb 7, 2024
Jan 19, 2024
Dec 20, 2023
Jan 4, 2024
Apr 5, 2023
Feb 1, 2024

Repository files navigation

SDK

WIP upgrade of the SDK. Not meant for production usage yet and interfaces may have frequent breaking changes until a stable release.

Setup

pnpm install

Requirements

  • fetch

Polyfill

If your platform does not support one of the required features, it is also possible to import a polyfill.

Testing

Testing requires access to an archive node for onchain quote comparisons. This can be done using Infura.

pnpm test

Balancer Api Provider

The Balancer API Provider is a provider that facilitates data fetching from the Balancer API, it can be used for:

  • Fetch Pool State for AddLiquidity;
  • Fetch Pool State for RemoveLiquidity.

Usage for adding liquidity to a Pool

  import { BalancerApi, AddLiquidity } from "@balancer/sdk";
    ...
    const addLiquidityInput: AddLiquidityProportionalInput = {
      bptOut,
      chainId,
      rpcUrl,
      kind: AddLiquidityKind.Proportional,
    };

    const balancerApi = new BalancerApi('https://backend-v3-canary.beets-ftm-node.com/graphql', 1);
    const poolState = await balancerApi.pools.fetchPoolState('0x5f1d6874cb1e7156e79a7563d2b61c6cbce03150000200000000000000000586');
    const addLiquidity = new AddLiquidity();
    const queryOutput = await addLiquidity.query(addLiquidityInput, poolState);
    const { call, to, value, maxAmountsIn, minBptOut } =
        addLiquidity.buildCall({
            ...queryOutput,
            slippage,
            sender: signerAddress,
            recipient: signerAddress,
        });
    const client = createClient({
      ...
    })
    
    await client.sendTransaction({
      account: signerAddress,
      chain: client.chain,
      data: call,
      to,
      value,
    });

Full working add liquidity example: examples/addLiquidity.ts

Usage for removing liquidity from a Pool

import { BalancerApi, RemoveLiquidity } from "@balancer/sdk";
...
const removeLiquidityInput: RemoveLiquiditySingleTokenExactInInput = {
  chainId,
  rpcUrl,
  bptIn,
  tokenOut,
  kind: RemoveLiquidityKind.SingleTokenExactIn,
};

const balancerApi = new BalancerApi('https://backend-v3-canary.beets-ftm-node.com/graphql', 1);
const poolState = await balancerApi.pools.fetchPoolState('0x5f1d6874cb1e7156e79a7563d2b61c6cbce03150000200000000000000000586');
const removeLiquidity = new RemoveLiquidity();
const queryOutput = await removeLiquidity.query(removeLiquidityInput, poolState);
const { call, to, value, maxAmountsIn, minBptOut } =
  removeLiquidity.buildCall({
    ...queryOutput,
    slippage,
    sender: signerAddress,
    recipient: signerAddress,
  });
const client = createClient({
  ...
})

await client.sendTransaction({
  account: signerAddress,
  chain: client.chain,
  data: call,
  to,
  value,
});

Full working remove liquidity example: examples/removeLiquidity.ts

Pool Creation And Initialization

Pool Creation functionality is available for the latest versions of Weighted Pools (V4) and Composable Stable Pools (V5). For usage of Pool Creation check our examples:

For usage of Pool Initialization check our example:

Anvil client

To download and install the anvil client, run the following commands (MacOS):

  • curl -L https://foundry.paradigm.xyz | bash
  • brew install libusb
  • source /Users/$(whoami)/.zshenv
  • foundryup

For other SO's check https://book.getfoundry.sh/getting-started/installation