Skip to content

Withdraw Action

Function: withdraw()

Withdraws tokens from a lending position. Supports both ERC20 and native tokens.

  1. ERC20 Tokens & Native Tokens:

    • If withdraw max amount: Calls redeem function

    • If withdraw any other amount: Calls withdraw function

    • Transaction data:

      typescript
      // For max amount (redeem)
      {
        to: fTokenAddress,
        data: encodeFunctionData({
          abi: ERC20Abi,
          functionName: 'redeem',
          args: [shares, receiver.address, owner.address]
        })
      }
      
      // For specific amount (withdraw)
      {
        to: fTokenAddress,
        data: encodeFunctionData({
          abi: ERC20Abi,
          functionName: 'withdraw',
          args: [BigInt(withdrawAmountInWei), receiver.address, owner.address]
        })
      }
    • ERC20 ABI:

      typescript
       const ERC20Abi = 
       // Withdraw function  
      {
      inputs: [
       {
         internalType: 'uint256',
         name: 'assets_',
         type: 'uint256',
       },
       {
         internalType: 'address',
         name: 'receiver_',
         type: 'address',
       },
       {
         internalType: 'address',
         name: 'owner_',
         type: 'address',
       },
      ],
      name: 'withdraw',
      outputs: [
       {
         internalType: 'uint256',
         name: 'shares_',
         type: 'uint256',
       },
      ],
      stateMutability: 'nonpayable',
      type: 'function',
      },
      
      // Redeem Function
      {
      inputs: [
       {
         internalType: 'uint256',
         name: 'shares_',
         type: 'uint256',
       },
       {
         internalType: 'address',
         name: 'receiver_',
         type: 'address',
       },
       {
         internalType: 'address',
         name: 'owner_',
         type: 'address',
       },
      ],
      name: 'redeem',
      outputs: [
       {
         internalType: 'uint256',
         name: 'assets_',
         type: 'uint256',
       },
      ],
      stateMutability: 'nonpayable',
      type: 'function',
      }

Flow:


User → withdraw() / redeem() → transaction sent

Full lend withdraw example

ts
/**
 * Example: Withdraw underlying USDC from Fluid lending (fUSDC on Polygon)
 *
 * Flow: withdraw(amountWei, receiver, owner) on the fToken.
 * Set PRIVATE_KEY and RPC_URL in .env.
 */

import fTokenAbi from "../shared/abis/fTokenAbi.js";
import { encodeFunctionData, parseUnits } from "viem";
import { getFTokenAddress } from "../shared/utils/fTokens.js";
import {
  account,
  chain,
  publicClient,
  walletClient,
} from "../shared/config.js";

async function main() {
  // fToken symbol for the underlying asset we want to withdraw
  const tokenSymbol = "fUSDC";

  const fTokenAddress = getFTokenAddress(chain.id, tokenSymbol);
  if (!fTokenAddress) {
    console.error(`Unknown token: ${tokenSymbol}`);
    process.exit(1);
  }
  const owner = account.address;
  const receiver = owner;

  const decimals = 6; // USDC
  const withdrawAmountWei = parseUnits("1", decimals);

  const data = encodeFunctionData({
    abi: fTokenAbi,
    functionName: "withdraw",
    args: [withdrawAmountWei, receiver, owner],
  });

  // Send the withdrawal transaction to the fToken contract
  const hash = await walletClient.sendTransaction({
    to: fTokenAddress,
    data,
    account,
  });

  console.log("Tx hash:", hash);
  console.log("Explorer:", `${chain.blockExplorers?.default?.url}/tx/${hash}`);

  const receipt = await publicClient.waitForTransactionReceipt({ hash });
  console.log("Status:", receipt.status === "success" ? "success" : "reverted");
}

main().catch((e) => {
  console.error(e);
  process.exit(1);
});