Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Withdraw

Withdraw liquidity from vaults. Users can withdraw their LP tokens and receive the underlying assets.

/**
 * @param {Object} params - The parameters for the calculateWithdrawAmount function.
 * @param {string} params.vault_id - The ID of the vault.
 * @param {boolean} params.fix_amount_a - Whether to fix the amount of token A. If true, the input_amount represents token A amount; if false, it represents token B amount.
 * @param {string} params.input_amount - The input amount. If is_ft_input is true, this is the LP token amount; if false, this is the token amount (either A or B based on fix_amount_a).
 * @param {number} params.slippage - The slippage percentage (eg: 0.01 = 1%)
 * @param {boolean} params.is_ft_input - Whether the input is LP token. If true, input_amount is LP token amount; if false, input_amount is token amount.
 * @param {InputType} params.side - The withdrawal type. Both for withdrawing both tokens, OneSide for withdrawing a single token.
 * @param {string} params.max_ft_amount - The amount of LP tokens held by the user. In OneSide mode, this value is used to balance the withdrawal amount.
 */
const result = await sdk.Vaults.calculateWithdrawAmount({
  vault_id,
  fix_amount_a: true,
  input_amount: '1000000000',
  slippage: 0.01,
  is_ft_input: false,
  side: InputType.Both,
  max_ft_amount: '',
})

/**
 * @param {Object} params - The parameters for the withdraw function.
 * @param {string} params.vault_id - The ID of the vault.
 * @param {number} params.slippage - The slippage percentage (0-1).
 * @param {string} params.ft_amount - The amount of LP tokens to burn.
 * @param {string} params.return_coin - Optional. If set to true, returns the coin object. The user needs to handle it.
 */
const tx = new Transaction()
const { return_coin_a, return_coin_b } = await sdk.Vaults.withdraw(
  {
    vault_id,
    slippage: 0.01,
    ft_amount: result.burn_ft_amount,
    return_coin: true,
  },
  tx
)
if (return_coin_a) {
  tx.transferObjects([return_coin_a], sdk.senderAddress)
}
if (return_coin_b) {
  tx.transferObjects([return_coin_b], sdk.senderAddress)
}

const send_key_pair = 'THE_KEY_PAIR_GENERATED_BY_YOUR_PRIVATE_KEY'

const tx_result = await sdk.fullClient.sendTransaction(send_key_pair, payload)

Last updated