Skip to content

Commit

Permalink
add withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
jazz-cb committed Sep 17, 2024
1 parent 1f2c803 commit 7153444
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
38 changes: 38 additions & 0 deletions app/api/aave/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}

Expand Down
57 changes: 54 additions & 3 deletions app/usdcflow/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@ export default function AaveInteraction() {
const [supplyAmount, setSupplyAmount] = useState<string>('');
const [borrowAmount, setBorrowAmount] = useState<string>('');
const [repayAmount, setRepayAmount] = useState<string>('');
const [withdrawAmount, setWithdrawAmount] = useState<string>('');
const [isLoading, setIsLoading] = useState<boolean>(true);
const [isSupplying, setIsSupplying] = useState<boolean>(false);
const [isBorrowing, setIsBorrowing] = useState<boolean>(false);
const [isRepaying, setIsRepaying] = useState<boolean>(false);
const [isWithdrawing, setIsWithdrawing] = useState<boolean>(false);
const [error, setError] = useState<string>('');

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('');
};

Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -205,8 +232,8 @@ export default function AaveInteraction() {
</CardContent>
</Card>

<div className="grid grid-cols-1 md:grid-cols-2 gap-8">
<Card className="bg-white shadow-lg">
<div className="grid grid-cols-1 lg:grid-cols-2 gap-8">
<Card className="bg-white shadow-lg">
<CardHeader>
<CardTitle className="text-xl text-blue-600">Supply Assets</CardTitle>
</CardHeader>
Expand Down Expand Up @@ -287,7 +314,31 @@ export default function AaveInteraction() {
</CardFooter>
)}
</Card>

<Card className="bg-white shadow-lg">
<CardHeader>
<CardTitle className="text-xl text-blue-600">Withdraw Assets</CardTitle>
</CardHeader>
<CardContent>
<Input
type="number"
placeholder="Amount to withdraw (USDC)"
value={withdrawAmount}
onChange={(e) => setWithdrawAmount(e.target.value)}
className="mb-4"
/>
<Button onClick={withdrawFromAave} disabled={isWithdrawing}
className="w-full bg-gradient-to-r from-lavender-400 to-blue-500 hover:from-lavender-500 hover:to-blue-600 text-white transition-all duration-300">
{isWithdrawing && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
Withdraw from Aave
</Button>
</CardContent>
{withdrawOutput && (
<CardFooter className="flex flex-col items-start">
<p className="mb-2">Withdrawn Amount: {withdrawOutput.amount} USDC</p>
<p className="text-sm break-all">Transaction Hash: {withdrawOutput.txHash}</p>
</CardFooter>
)}
</Card>
</div>
</>
)}
Expand Down

0 comments on commit 7153444

Please sign in to comment.