Adding Liquidity

This guide shows how to add liquidity into a LBPair using the SDKs and viem. In this example, we will be adding 20 USDC and 20 USDC.e into a LBPair of USDC/USDC.e/1bps

1. Required imports and constants for this guide

imports

import { ChainId, Token, TokenAmount } from '@traderjoe-xyz/sdk-core'
import { PairV2, LB_ROUTER_V22_ADDRESS, jsonAbis, LiquidityDistribution, getLiquidityConfig, getUniformDistributionFromBinRange } from '@traderjoe-xyz/sdk-v2'
import { createPublicClient, createWalletClient, http, parseUnits, BaseError, ContractFunctionRevertedError } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { avalanche } from 'viem/chains'
import JSBI from 'jsbi'
import { config } from 'dotenv';

Constants: chain and wallet

config();
const privateKey = process.env.PRIVATE_KEY;
const { LBRouterV22ABI } = jsonAbis
const CHAIN_ID = ChainId.AVALANCHE
const router = LB_ROUTER_V22_ADDRESS[CHAIN_ID]
const account = privateKeyToAccount(`0x${privateKey}`)

Note that in your project, you most likely will not hardcode the private key at any time. You would be using libraries like web3reactarrow-up-right or wagmiarrow-up-right to connect to a wallet, sign messages, interact with contracts, and get the values for PROVIDER, SIGNER and ACCOUNT

Constants: tokens and LBPair bin step

2. Create Viem clients

3. Initialize user inputs

4. Get the LBPair's active bin

5. Get addLiquidity parameters

For additional details about the parameters, refer to this linkarrow-up-right.

6. Execute addLiquidity contract call

Last updated