diff --git a/app/api/aave/route.tsx b/app/api/aave/route.tsx index 9b70de9..cc80818 100644 --- a/app/api/aave/route.tsx +++ b/app/api/aave/route.tsx @@ -273,6 +273,44 @@ export async function POST(request: Request) { } } + if (action === 'withdraw') { + try { + const amountToWithdraw = parseUnits(amount, 6); // Assuming USDC has 6 decimals + + console.log('Attempting to withdraw:', { + asset: USDC_ADDRESS, + amount: amountToWithdraw.toString(), + to: address.getId() + }); + + const withdrawContract = await wallet.invokeContract({ + contractAddress: AAVE_POOL_ADDRESS, + method: "withdraw", + args: { + asset: USDC_ADDRESS, + amount: amountToWithdraw.toString(), + to: address.getId() + }, + abi: aaveAbi, + }); + + const withdrawTx = await withdrawContract.wait(); + if (!withdrawTx) { + throw new Error('Failed to withdraw USDC from Aave'); + } + + console.log('Withdraw transaction sent, hash:', withdrawTx.getTransactionHash()); + + return NextResponse.json({ success: true, txHash: withdrawTx.getTransactionHash() }); + } catch (error) { + console.error('Failed to withdraw:', error); + return NextResponse.json({ + error: 'Failed to withdraw', + details: error instanceof Error ? error.message : String(error) + }, { status: 500 }); + } + } + return NextResponse.json({ error: 'Invalid action' }, { status: 400 }); } diff --git a/app/usdcflow/page.tsx b/app/usdcflow/page.tsx index b163cd9..5f959c6 100644 --- a/app/usdcflow/page.tsx +++ b/app/usdcflow/page.tsx @@ -13,20 +13,24 @@ export default function AaveInteraction() { const [supplyAmount, setSupplyAmount] = useState(''); const [borrowAmount, setBorrowAmount] = useState(''); const [repayAmount, setRepayAmount] = useState(''); + const [withdrawAmount, setWithdrawAmount] = useState(''); const [isLoading, setIsLoading] = useState(true); const [isSupplying, setIsSupplying] = useState(false); const [isBorrowing, setIsBorrowing] = useState(false); const [isRepaying, setIsRepaying] = useState(false); + const [isWithdrawing, setIsWithdrawing] = useState(false); const [error, setError] = useState(''); const [supplyOutput, setSupplyOutput] = useState<{ amount: string, txHash: string } | null>(null); const [borrowOutput, setBorrowOutput] = useState<{ amount: string, txHash: string } | null>(null); const [repayOutput, setRepayOutput] = useState<{ amount: string, txHash: string } | null>(null); + const [withdrawOutput, setWithdrawOutput] = useState<{ amount: string, txHash: string } | null>(null); const clearTransactionData = () => { setSupplyOutput(null); setBorrowOutput(null); setRepayOutput(null); + setWithdrawOutput(null); setError(''); }; @@ -111,6 +115,29 @@ export default function AaveInteraction() { } }; + const withdrawFromAave = async () => { + if (!withdrawAmount) return; + clearTransactionData(); + setIsWithdrawing(true); + try { + const response = await fetch('/api/aave', { + method: 'POST', + headers: {'Content-Type': 'application/json'}, + body: JSON.stringify({action: 'withdraw', amount: withdrawAmount}), + }); + if (!response.ok) throw new Error('Failed to withdraw assets'); + const data = await response.json(); + setWithdrawOutput({ amount: withdrawAmount, txHash: data.txHash }); + getUserAccountData(); + } catch (err) { + console.error('Failed to withdraw from Aave:', err); + setError('Failed to withdraw assets. Please try again.'); + } finally { + setIsWithdrawing(false); + } + }; + + const closeIntro = () => { setShowIntro(false); }; @@ -205,8 +232,8 @@ export default function AaveInteraction() { -
- +
+ Supply Assets @@ -287,7 +314,31 @@ export default function AaveInteraction() { )} - + + + Withdraw Assets + + + setWithdrawAmount(e.target.value)} + className="mb-4" + /> + + + {withdrawOutput && ( + +

Withdrawn Amount: {withdrawOutput.amount} USDC

+

Transaction Hash: {withdrawOutput.txHash}

+
+ )} +
)}