Operation - Withdraw

Key Steps

  1. Parse Withdrawal Amount:

    • Use the parseEther function to convert the desired amount of collateral to be withdrawn into Wei. This function ensures that the amount is accurately converted to the smallest unit of ether, allowing precise handling in blockchain transactions.

  2. Execute Withdrawal Transaction:

    • Call the doWithdraw function, passing the necessary parameters such as the public and wallet clients, protocol configuration, collateral information, and the amount of collateral to withdraw. This function handles the transaction on the blockchain and adjusts the user's trove accordingly.

Usage

This function is typically used when a user wants to:

  • Access Liquidity: Withdraw a portion of their collateral for immediate liquidity needs, such as covering expenses or reinvesting in other opportunities.

  • Adjust Collateral Levels: Decrease their collateral exposure due to changes in market conditions or personal risk preferences.

Example

import { parseEther } from 'viem';
import { publicClient, walletClient, protocolConfig, collateral } from 'satoshi-sdk';

async function withdrawCollateral() {
  // Step 1: Define the amount to withdraw
  const withdrawCollAmt = parseEther('0.01');  // Withdrawing 0.01 ETH worth of collateral

  // Step 2: Execute the withdrawal transaction
  const receipt = await satoshiClient.Postition.doWithdraw({
    publicClient,
    walletClient,
    protocolConfig,
    collateral,
    withdrawCollAmt,
  });

  console.log('Withdrawal successful:', receipt);
}

withdrawCollateral();

This example function, withdrawCollateral, demonstrates the step-by-step process to withdraw a small amount of collateral from a trove. It includes the precise conversion of the desired withdrawal amount and the execution of the withdrawal transaction, ensuring the trove remains compliant with the protocol's requirements.

Last updated