This guide explains how to withdraw assets from a Glider portfolio.

Overview

Withdrawing assets from your portfolio allows you to access your funds when needed. The withdrawal process involves:

  1. Getting withdrawal instructions from the Glider API
  2. Creating a transaction with these instructions
  3. Having the user sign and send the transaction

Withdrawal Process

1. Get Withdrawal Instructions

First, request withdrawal instructions from the API:

const withdrawResponse = await fetch(`https://api.glider.fi/v1/portfolio/${portfolioId}/withdraw`, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-API-KEY': 'your_api_key_here',
  },
  body: JSON.stringify({
    assets: [
      {
        chainId: 8453, // Base chain
        address: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE', // ETH on Base
        amount: '0.05' // 0.05 ETH
      }
    ]
  }),
});

const withdrawData = await withdrawResponse.json();

For multiple assets or ERC-20 tokens, include them in the assets array:

body: JSON.stringify({
  assets: [
    {
      chainId: 8453,
      address: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE', // ETH
      amount: '0.05'
    },
    {
      chainId: 8453,
      address: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', // USDC
      amount: '10000000' // 10 USDC (6 decimals)
    }
  ]
})

2. Create and Send the Transaction

The response will include transaction data that you can use to create a transaction for the user to sign:

// Example response
{
  "success": true,
  "data": {
    "to": "0xPortfolioVaultAddress",
    "data": "0x...", // Transaction calldata
    "chainId": 8453
  }
}

Use this data to create and send a transaction:

// Using ethers.js
const tx = await signer.sendTransaction({
  to: withdrawData.data.to,
  data: withdrawData.data.data,
  chainId: withdrawData.data.chainId
});

// Wait for confirmation
const receipt = await tx.wait();
console.log("Withdrawal confirmed:", receipt.transactionHash);

3. Verify the Withdrawal

After the transaction is confirmed, you can verify the withdrawal by retrieving the portfolio details:

const portfolioResponse = await fetch(`https://api.glider.fi/v1/portfolio/${portfolioId}`, {
  method: 'GET',
  headers: {
    'X-API-KEY': 'your_api_key_here',
  },
});

const portfolio = await portfolioResponse.json();
console.log("Portfolio assets after withdrawal:", portfolio.data.vaults[0].assets);

Withdraw as ETH Option

Glider also provides a convenient endpoint to withdraw all assets as ETH, which automatically converts all tokens to ETH before withdrawal:

const withdrawAsEthResponse = await fetch(`https://api.glider.fi/v1/portfolios/${portfolioId}/withdraw-as-eth`, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-API-KEY': 'your_api_key_here',
  },
  body: JSON.stringify({
    assets: [
      {
        chainId: 8453,
        address: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', // USDC
        amount: '10000000' // 10 USDC
      }
    ]
  }),
});

const withdrawAsEthData = await withdrawAsEthResponse.json();

Partial vs Full Withdrawals

You can withdraw a portion of each asset or the full amount:

  • Partial withdrawal: Specify the exact amount for each asset
  • Full withdrawal: Use the special value “max” for the amount
// Full withdrawal of ETH
body: JSON.stringify({
  assets: [
    {
      chainId: 8453,
      address: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
      amount: 'max'
    }
  ]
})

Best Practices

  1. Check available balances: Verify that the portfolio has sufficient assets before initiating a withdrawal
  2. Handle transaction failures: Implement proper error handling for rejected transactions
  3. Inform users: Display a loading state during transaction confirmation
  4. Gas considerations: Be aware that withdrawals may require more gas than simple transfers, especially for multiple assets
  5. Automation impact: Consider pausing portfolio automation before large withdrawals to prevent concurrent rebalancing

Next Steps

After withdrawing assets, you may want to:

  1. Update the portfolio to adjust its configuration for the new balance
  2. Pause automation if you’ve withdrawn a significant amount
  3. Archive the portfolio if you’ve withdrawn all assets and no longer need it