Skip to content

Deposit Action

Function: deposit()

Deposits tokens into a lending position. Supports both ERC20 and native tokens.

  1. ERC20 Tokens:

    • If approval is needed, the user must call approve() separately first
    • Then sends a single deposit transaction to the position token contract
    • Transaction data:
      typescript
      {
        to: fTokenAddress,
        data: encodeFunctionData({
          abi: ERC20Abi,
          functionName: 'deposit',
          args: [BigInt(depositAmountInWei), userAddress]
        })
      }
    • ERC20 ABI:
      typescript
       const ERC20Abi = {
             inputs: [
                 {
                     internalType: 'uint256',
                     name: 'assets_',
                     type: 'uint256',
                 },
                 {
                     internalType: 'address',
                     name: 'receiver_',
                     type: 'address',
                 },
             ],
             name: 'deposit',
             outputs: [
                 {
                     internalType: 'uint256',
                     name: 'shares_',
                     type: 'uint256',
                 },
             ],
             stateMutability: 'nonpayable',
             type: 'function',
         }
  2. Native Tokens:

    • Sends a single depositNative transaction with native value
    • Transaction data:
      typescript
      {
        to: fTokenAddress,
        data: encodeFunctionData({
          abi: nativeAbi,
          functionName: 'depositNative',
          args: [userAddress]
        }),
        value: BigInt(depositAmountInWei)
      }
    • Native ABI:
      typescript
         const nativeAbi = {
             inputs: [
                 {
                     internalType: "address",
                     name: "receiver_",
                     type: "address",
                 },
             ],
             name: "depositNative",
             outputs: [
                 {
                     internalType: "uint256",
                     name: "shares_",
                     type: "uint256",
                 },
             ],
             stateMutability: "payable",
             type: "function",
         };

Flow:


User → approve() [if needed] → deposit() → Transaction sent

Full lend deposit example

ts
/**
 * Example: Deposit USDC into Fluid lending (mint fUSDC on Polygon)
 *
 * Flow: (1) approve the fToken to spend your USDC, (2) call deposit(amountWei, receiver) on the fToken.
 * Set PRIVATE_KEY and RPC_URL in .env..
 */

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

// USDC contract address on Polygon
const POLYGON_USDC = "0x3c499c542cef5e3811e1192ce70d8cc03d5c3359";

async function main() {
  // Fluid fToken symbol
  const tokenSymbol = "fUSDC";

  // Lookup fToken contract address for this chain
  const fTokenAddress = getFTokenAddress(chain.id, tokenSymbol);
  if (!fTokenAddress) {
    console.error(`Unknown token: ${tokenSymbol}`);
    process.exit(1);
  }

  const decimals = 6; // USDC
  const amountWei = parseUnits("1", decimals); // 1 USDC
  const receiver = account.address;

  const approveData = encodeFunctionData({
    abi: erc20Abi,
    functionName: "approve",
    args: [fTokenAddress, amountWei],
  });

  // Send approve transaction from the user to USDC contract
  const approveHash = await walletClient.sendTransaction({
    to: POLYGON_USDC,
    data: approveData,
    account,
  });

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

  const approveReceipt = await publicClient.waitForTransactionReceipt({
    hash: approveHash,
  });
  if (approveReceipt.status !== "success") {
    console.error("Approve tx reverted");
    process.exit(1);
  }

  const depositData = encodeFunctionData({
    abi: fTokenAbi,
    functionName: "deposit",
    args: [amountWei, receiver],
  });

  // Send deposit transaction to fToken contract
  const depositHash = await walletClient.sendTransaction({
    to: fTokenAddress,
    data: depositData,
    account,
  });

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

  const receipt = await publicClient.waitForTransactionReceipt({
    hash: depositHash,
  });

  if (receipt.status !== "success") {
    console.error("Deposit tx reverted");
    process.exit(1);
  }
  console.log("Deposit success.");
}

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