Liquidity Provisioning Implementation

To add liquidity to a pool we will use the addLiquidity function of the sdk.

Syntax

async function addLiquidity(
  xfai: Xfai,
  ethAmount: BigNumber,
  supplementary: LiquidityInAmount,
  options: TransactionOptions,
): Promise<PopulatedTransaction>

Parameters

  • xfai: An instance of the Xfai class.

  • ethAmount: The amount of ETH to add to the pool.

  • target: An object containing the token and amount of the target token to add to the pool

  • options: This parameter allows you to specify additional transaction options such as deadline, slippage and more. Review the available options and set them according to your needs.

Return Value

A promise that return the PopulatedTransaction object.

Examples

Double sided

import { addLiquidity, sendPopulatedTx, getPoolState, getPoolFromToken, quote, Token, Xfai } from '@xfai-labs/sdk';

const xfai = new Xfai(...);
const ethAmount = ethers.utils.parseEther('1');

const targetToken =  new Token('0x123...');

const targetState =  await getPoolState(xfai, getPoolFromToken(xfai, targetToken!));

const ethAmount = BigNumber.from(10).pow(18);

// calculate token amount needed for given ethAmount
const tokenAmount = quote(
    type: 'ETH', // we use ETH, as we want to calculate Token amount in terms of ETH
    token: {
        token: targetToken,
        state: targetPoolState,
    },
    amount: ethAmount,
  );

const target: LiquidityInAmount = {
  token: targetToken
  amount: tokenAmount
};

const options = {
  deadline: Math.floor(Date.now() / 1000) + 60 * 20,
};

const populatedTx = await addLiquidity(xfai, ethAmount, target, options);

// sign and send the transaction
sendPopulatedTx(xfai, populatedTx);

Last updated