Skip to main content

Withdrawing USDC

Remove USDC from the 7N7D Trading Vault programmatically.

Coming Soon

The SDK is currently in development. This documentation describes the planned API.

Basic Withdrawal​

import { SevenNSevenD } from '@7n7d/sdk';

const client = new SevenNSevenD({
rpcUrl: process.env.RPC_URL,
privateKey: process.env.PRIVATE_KEY,
});

// Withdraw $500 worth of USDC
const result = await client.vault.withdraw('500');

console.log(`Transaction hash: ${result.hash}`);
console.log(`USDC received: ${result.usdcAmount}`);
console.log(`Shares burned: ${result.sharesBurned}`);

Withdrawal Methods​

By USDC Amount​

Withdraw a specific USDC value:

// Withdraw $500 USDC
const result = await client.vault.withdraw('500');
console.log(`Received: ${result.usdcAmount} USDC`);

By Share Amount​

Withdraw a specific number of shares:

// Withdraw 1000 shares
const result = await client.vault.withdrawShares('1000');
console.log(`Received: ${result.usdcAmount} USDC`);
console.log(`Shares burned: ${result.sharesBurned}`);

Withdraw All​

Withdraw entire balance:

// Withdraw everything
const result = await client.vault.withdrawAll();
console.log(`Received: ${result.usdcAmount} USDC`);
console.log(`Shares burned: ${result.sharesBurned}`);

Withdrawal Options​

const result = await client.vault.withdraw('500', {
// Slippage protection
minAmount: '495', // Minimum USDC to receive

// Gas settings
gasLimit: 300000,
maxFeePerGas: '0.1', // gwei
maxPriorityFeePerGas: '0.01',

// Deadline
deadline: Date.now() + 300000, // 5 minutes
});

Estimate Withdrawal​

Before withdrawing, check what you'll receive:

// Estimate by USDC amount
const estimate = await client.vault.estimateWithdraw('500');
console.log(`To withdraw: $500 USDC`);
console.log(`Shares to burn: ${estimate.shares}`);
console.log(`Current share price: ${estimate.sharePrice}`);
console.log(`Estimated gas: ${estimate.gasEstimate}`);

// Estimate by shares
const shareEstimate = await client.vault.estimateWithdrawShares('1000');
console.log(`Burning: 1000 shares`);
console.log(`USDC to receive: ${shareEstimate.usdcAmount}`);

Transaction Results​

WithdrawResult Object​

interface WithdrawResult {
// Transaction info
hash: string; // Transaction hash
blockNumber: number; // Block number

// Withdrawal details
usdcAmount: string; // USDC received
sharesBurned: string; // Shares burned
sharePrice: string; // Price per share

// Fees (if any)
feeAmount: string; // Fees deducted
netAmount: string; // Net USDC received

// Gas info
gasUsed: string;
gasCost: string;
}

Access Results​

const result = await client.vault.withdraw('500');

console.log(`TX: ${result.hash}`);
console.log(`Received: ${result.usdcAmount} USDC`);
console.log(`Shares burned: ${result.sharesBurned}`);
console.log(`Share price: $${result.sharePrice}`);
console.log(`Gas cost: ${result.gasCost} ETH`);

Check Withdrawable Amount​

// Get current vault position
const balance = await client.vault.getBalance();

console.log(`Shares owned: ${balance.shares}`);
console.log(`Current value: $${balance.usdcValue}`);
console.log(`Max withdrawable: $${balance.withdrawable}`);

Partial vs Full Withdrawal​

Partial Withdrawal​

const balance = await client.vault.getBalance();
console.log(`Total value: $${balance.usdcValue}`);

// Withdraw half
const halfValue = parseFloat(balance.usdcValue) / 2;
const result = await client.vault.withdraw(halfValue.toString());

// Check remaining
const newBalance = await client.vault.getBalance();
console.log(`Remaining: $${newBalance.usdcValue}`);

Full Withdrawal​

// Method 1: withdrawAll()
const result = await client.vault.withdrawAll();

// Method 2: Withdraw all shares
const balance = await client.vault.getBalance();
const result = await client.vault.withdrawShares(balance.shares);

// Verify empty
const newBalance = await client.vault.getBalance();
console.log(`Remaining shares: ${newBalance.shares}`); // Should be 0

Error Handling​

import { SDKError } from '@7n7d/sdk';

try {
await client.vault.withdraw('500');
} catch (error) {
if (error instanceof SDKError) {
switch (error.code) {
case 'INSUFFICIENT_SHARES':
console.log('Not enough shares to withdraw this amount');
const balance = await client.vault.getBalance();
console.log(`Max withdrawable: $${balance.usdcValue}`);
break;

case 'MIN_AMOUNT_NOT_MET':
console.log('Received less USDC than minimum specified');
break;

case 'WITHDRAWAL_PAUSED':
console.log('Withdrawals temporarily paused');
break;

case 'DEADLINE_EXCEEDED':
console.log('Transaction took too long');
break;

default:
console.log('Error:', error.message);
}
}
}

Full Example​

Complete withdrawal workflow:

import { SevenNSevenD } from '@7n7d/sdk';

async function withdrawFromVault(amount: string) {
const client = new SevenNSevenD({
rpcUrl: process.env.RPC_URL!,
privateKey: process.env.PRIVATE_KEY!,
});

// 1. Check current balance
const balance = await client.vault.getBalance();
console.log(`Current position: ${balance.shares} shares ($${balance.usdcValue})`);

// 2. Validate withdrawal amount
if (parseFloat(amount) > parseFloat(balance.usdcValue)) {
throw new Error(`Cannot withdraw $${amount}. Max: $${balance.usdcValue}`);
}

// 3. Estimate withdrawal
const estimate = await client.vault.estimateWithdraw(amount);
console.log(`Will burn: ${estimate.shares} shares`);
console.log(`Will receive: ~$${estimate.usdcAmount} USDC`);
console.log(`Gas estimate: ${estimate.gasEstimate} ETH`);

// 4. Execute withdrawal with slippage protection
const minAmount = (parseFloat(amount) * 0.99).toString(); // 1% slippage

console.log(`Withdrawing $${amount} USDC (min: $${minAmount})...`);
const result = await client.vault.withdraw(amount, { minAmount });

console.log('Withdrawal successful!');
console.log(` TX Hash: ${result.hash}`);
console.log(` USDC Received: ${result.usdcAmount}`);
console.log(` Shares Burned: ${result.sharesBurned}`);

// 5. Verify USDC received
const usdcBalance = await client.getUSDCBalance();
console.log(`New USDC balance: ${usdcBalance}`);

// 6. Check remaining vault position
const newVaultBalance = await client.vault.getBalance();
console.log(`Remaining in vault: ${newVaultBalance.shares} shares ($${newVaultBalance.usdcValue})`);

return result;
}

// Execute
withdrawFromVault('500')
.then(result => console.log('Done!', result))
.catch(error => console.error('Failed:', error));

Emergency Withdrawal​

If the vault has issues:

// Emergency withdrawal bypasses some checks
// May have higher slippage
const result = await client.vault.emergencyWithdraw();
console.log(`Emergency withdrawal: ${result.usdcAmount} USDC`);
warning

Emergency withdrawals should only be used when normal withdrawals fail. They may result in receiving less USDC than expected.


Next: Learn how to Check Balances.