Skip to main content

Method Signature

agent.withdraw(params: WithdrawParams): Promise<WithdrawResponse>

Description

Withdraws funds from an agent. Supports both partial and full withdrawal:
  • Partial withdrawal: Specify an amount to withdraw specific funds while keeping the agent active
  • Full withdrawal: Omit amount to withdraw all funds and deactivate the agent

Parameters

wallet
Address
required
Smart account wallet address to withdraw from.
amount
string
Amount to withdraw (in token’s smallest unit, e.g., 6 decimals for USDC).
  • If specified: Partial withdrawal, agent remains active
  • If omitted: Full withdrawal, agent is deactivated
Example: "1000000000" for 1000 USDC
transfer
boolean
default:"true"
Whether to transfer withdrawn funds to the origin wallet. Only applicable for full withdrawal (when amount is not specified).

Return Value

For partial withdrawal:
date
string
Withdrawal date.
total_value
number
Total value withdrawn.
total_value_in_usd
number
Total value in USD.
withdraw_details
WithdrawDetail[]
Details for each token withdrawn:
  • token - Token address
  • amount - Amount withdrawn
  • value - Value in token
  • value_in_usd - Value in USD
For full withdrawal:
message
string
Confirmation message.

Example

import { GizaAgent, Chain } from '@giza/agent-sdk';

const giza = new GizaAgent({ chainId: Chain.BASE });

// Partial withdrawal - withdraw 1000 USDC, agent stays active
const partialResult = await giza.agent.withdraw({
  wallet: smartAccountAddress,
  amount: '1000000000', // 1000 USDC (6 decimals)
});

console.log('Withdrew:', partialResult.total_value_in_usd, 'USD');
partialResult.withdraw_details.forEach((detail) => {
  console.log(`  ${detail.token}: ${detail.amount}`);
});

// Full withdrawal - withdraw everything and deactivate
const fullResult = await giza.agent.withdraw({
  wallet: smartAccountAddress,
  transfer: true, // Transfer to origin wallet
});

console.log(fullResult.message);

// Poll for deactivation completion
const finalStatus = await giza.agent.pollWithdrawalStatus(
  smartAccountAddress,
  { interval: 5000, timeout: 300000 }
);

console.log('Final status:', finalStatus.status);

Output

Partial withdrawal:
{
  date: '2024-01-20T15:30:00Z',
  total_value: 1000,
  total_value_in_usd: 1000,
  withdraw_details: [
    {
      token: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
      amount: 1000000000,
      value: 1000,
      value_in_usd: 1000
    }
  ]
}
Full withdrawal:
{
  message: 'Deactivation initiated'
}

Error Handling

import { ValidationError, GizaAPIError } from '@giza/agent-sdk';

try {
  const result = await giza.agent.withdraw({
    wallet: smartAccountAddress,
    amount: '1000000000',
  });
} catch (error) {
  if (error instanceof ValidationError) {
    console.error('Invalid parameters:', error.message);
  } else if (error instanceof GizaAPIError) {
    console.error('Withdrawal failed:', error.message);
  }
}

Common Errors

ErrorCauseSolution
ValidationError: wallet address is requiredMissing wallet parameterProvide a valid wallet address
GizaAPIError: Insufficient balanceRequested amount exceeds available balanceRequest a smaller amount
GizaAPIError: Agent not activeAgent is not in active stateOnly active agents can be withdrawn from

Notes

  • Partial withdrawal is useful for taking profits while continuing to earn yield
  • Full withdrawal stops the agent completely and transfers all funds
Withdrawals may take time to process as the agent needs to withdraw funds from protocols. Use pollWithdrawalStatus() to monitor completion.

Next Steps