Skip to content

Commit

Permalink
add usdcflow back
Browse files Browse the repository at this point in the history
  • Loading branch information
jazz-cb committed Sep 19, 2024
1 parent fa9b148 commit ef96eda
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 40 deletions.
28 changes: 26 additions & 2 deletions app/api/aave/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async function getUserAccountData(address: string) {
console.log("address ", address)

return {
totalCollateralBase: data[0], // This is the user's total deposited amount
totalCollateralBase: data[0],
totalDebtBase: data[1],
availableBorrowsBase: data[2],
currentLiquidationThreshold: data[3],
Expand Down Expand Up @@ -320,7 +320,7 @@ async function importWallet(): Promise<Wallet>{

const apiKeyString = CDP_API_KEY_PRIVATE_KEY as string;

new Coinbase({
Coinbase.configure({
apiKeyName: CDP_API_KEY_NAME as string,
privateKey: apiKeyString.replaceAll("\\n", "\n") as string,
});
Expand All @@ -329,6 +329,30 @@ async function importWallet(): Promise<Wallet>{
// Parse the wallet data
const seedData = JSON.parse(WALLET_DATA || "{}");

if (Object.keys(seedData).length === 0) {
// Create a new wallet if WALLET_DATA is empty
const newWallet = await Wallet.create();

// Fund the wallet with USDC
let faucetTransaction = await newWallet.faucet(Coinbase.assets.Usdc);
console.log(`Faucet transaction for USDC: ${faucetTransaction}`);

// Fund the wallet with ETH.
faucetTransaction = await newWallet.faucet();
console.log(`Faucet transaction for ETH: ${faucetTransaction}`);

let exportData = await newWallet.export()

// Create and assign the new WALLET_DATA
const newWalletData =JSON.stringify({ [exportData['walletId'] as string]: { 'seed': exportData['seed'] as string } });

console.log(`Created new wallet: ${exportData['walletId']}`)

process.env.WALLET_DATA = newWalletData;

return newWallet;
}

// Get the wallet id
const walletId = Object.keys(seedData)[0];

Expand Down
23 changes: 12 additions & 11 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ export default function Home() {
</ul>
</div>

<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-10 max-w-6xl mx-auto">
<div className="grid grid-cols-2 md:grid-cols-2 lg:grid-cols-2 gap-10 max-w-6xl mx-auto">
{[
// {
// name: 'USDC Lending App',
// route: '/usdcflow',
// description: 'Explore Aave integration with CDP SDK for decentralized lending and borrowing.',
// buildLink: 'https://docs.cdp.coinbase.com/mpc-wallet/docs/aave-integration'
// },
{
name: 'USDC Lending App',
route: '/usdcflow',
description: 'Explore a simple app that uses CDP SDK for decentralized lending and borrowing.',
buildLink: 'https://docs.cdp.coinbase.com/mpc-wallet/docs/aave-integration'
},
{
name: 'Onchain AI',
route: 'https://aiwalletdemo.com/',
Expand All @@ -74,17 +74,18 @@ export default function Home() {
<h2 className="text-xl font-medium mb-4 text-blue-600">{app.name}</h2>
<p className="mb-8 text-gray-600">{app.description}</p>
</div>
<div className="flex flex-col sm:flex-row space-y-4 sm:space-y-0 sm:space-x-4 mt-auto">
<div className="flex flex-col sm:flex-row justify-center items-center space-y-4 sm:space-y-0 sm:space-x-4 mt-auto">
<a
href={app.route}
className="inline-block bg-gradient-to-r from-lavender-400 to-blue-500 text-white px-6 py-3 rounded-full font-medium hover:from-lavender-500 hover:to-blue-600 transition-all duration-300 transform hover:-translate-y-1 hover:shadow-xl text-center"
{...(app.name !== 'Lending App' ? { target: "_blank", rel: "noopener noreferrer" } : {})}
className="inline-block bg-gradient-to-r from-lavender-400 to-blue-500 text-white px-6 py-3 rounded-full font-medium hover:from-lavender-500 hover:to-blue-600 transition-all duration-300 transform hover:-translate-y-1 hover:shadow-xl text-center w-full sm:w-auto"
target="_blank"
rel="noopener noreferrer"
>
Demo
</a>
<a
href={app.buildLink}
className="inline-block bg-gradient-to-r from-lavender-400 to-blue-500 text-white px-6 py-3 rounded-full font-medium hover:from-lavender-500 hover:to-blue-600 transition-all duration-300 transform hover:-translate-y-1 hover:shadow-xl text-center"
className="inline-block bg-gradient-to-r from-lavender-400 to-blue-500 text-white px-6 py-3 rounded-full font-medium hover:from-lavender-500 hover:to-blue-600 transition-all duration-300 transform hover:-translate-y-1 hover:shadow-xl text-center w-full sm:w-auto"
target="_blank"
rel="noopener noreferrer"
>
Expand Down
49 changes: 24 additions & 25 deletions app/usdcflow/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default function AaveInteraction() {
if (!response.ok) throw new Error('Failed to supply assets');
const data = await response.json();
setSupplyOutput({ amount: supplyAmount, txHash: data.txHash });
getUserAccountData();
getUserAccountData();
} catch (err) {
console.error('Failed to supply to Aave:', err);
setError('Failed to supply assets. Please try again.');
Expand All @@ -84,7 +84,7 @@ export default function AaveInteraction() {
if (!response.ok) throw new Error('Failed to borrow assets');
const data = await response.json();
setBorrowOutput({ amount: borrowAmount, txHash: data.txHash });
getUserAccountData();
getUserAccountData();
} catch (err) {
console.error('Failed to borrow to Aave:', err);
setError('Failed to borrow assets. Please try again.');
Expand All @@ -106,7 +106,7 @@ export default function AaveInteraction() {
if (!response.ok) throw new Error('Failed to repay loan');
const data = await response.json();
setRepayOutput({ amount: repayAmount, txHash: data.txHash });
getUserAccountData();
getUserAccountData();
} catch (err) {
console.error('Failed to repay loan:', err);
setError('Failed to repay loan. Please try again.');
Expand All @@ -128,7 +128,7 @@ export default function AaveInteraction() {
if (!response.ok) throw new Error('Failed to withdraw assets');
const data = await response.json();
setWithdrawOutput({ amount: withdrawAmount, txHash: data.txHash });
getUserAccountData();
getUserAccountData();
} catch (err) {
console.error('Failed to withdraw from Aave:', err);
setError('Failed to withdraw assets. Please try again.');
Expand Down Expand Up @@ -157,15 +157,14 @@ export default function AaveInteraction() {
<CardContent>
<p className="mb-4">USDCFlow is a lending App on Base-Sepolia that showcases:</p>
<ul className="list-disc list-inside text-gray-700 mb-4">
<li>MPC Wallets and CDP SDK for seamless, user-first onchain DeFi experiences</li>
<li>Customizable, accessible DeFi with global reach, low fees, and instant transactions</li>
<li>AI code assistants for quick frontend creation of onchain DeFi apps</li>
<li>Easily extend the app with layout changes and DeFi smart contract integration</li>
<li>MPC Wallets and CDP SDK for seamless, user-first onchain DeFi experiences</li>
<li>Customizable, accessible DeFi with global reach, low fees, and instant transactions</li>
<li>Easily extend the app with layout changes and DeFi smart contract integration</li>
</ul>
<p className="text-sm text-gray-600">Note: This app uses testnet USDC</p>
</CardContent>
<CardFooter className="flex justify-end">
<Button
<Button
onClick={closeIntro}
className="bg-gradient-to-r from-lavender-400 to-blue-500 hover:from-lavender-500 hover:to-blue-600 text-white transition-all duration-300"
>
Expand Down Expand Up @@ -218,22 +217,22 @@ export default function AaveInteraction() {
</Alert>
)}
<Button onClick={getUserAccountData} className="mb-4 bg-gradient-to-r from-lavender-400 to-blue-500 hover:from-lavender-500 hover:to-blue-600 text-white transition-all duration-300">
Refresh Account Data
</Button>
Refresh Account Data
</Button>
{accountData && (
<div className="bg-lavender-50 p-4 rounded-lg">
<p className="mb-1">Wallet Address: {accountData.walletAddress}</p>
<p className="mb-1">Wallet Balance: {parseFloat(accountData.usdcBalance).toFixed(2)} USDC</p>
<p className="mb-1">Total Supplied: {accountData.totalDeposited} USDC</p>
<p className="mb-1">Total Borrowed: {accountData.totalDebtBase} USDC</p>
<p className="mb-1">Available to borrow: {accountData.availableBorrowsBase} USDC</p>
</div>
<div className="bg-lavender-50 p-4 rounded-lg">
<p className="mb-1">Wallet Address: {accountData.walletAddress}</p>
<p className="mb-1">Wallet Balance: {parseFloat(accountData.usdcBalance).toFixed(3)} USDC</p>
<p className="mb-1">Total Supplied: {accountData.totalDeposited} USDC</p>
<p className="mb-1">Total Borrowed: {accountData.totalDebtBase} USDC</p>
<p className="mb-1">Available to borrow: {accountData.availableBorrowsBase} USDC</p>
</div>
)}
</CardContent>
</Card>

<div className="grid grid-cols-1 lg: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 @@ -285,7 +284,7 @@ export default function AaveInteraction() {
</CardFooter>
)}
</Card>

<Card className="bg-white shadow-lg">
<CardHeader>
<CardTitle className="text-xl text-blue-600">Repay Assets</CardTitle>
Expand All @@ -298,9 +297,9 @@ export default function AaveInteraction() {
onChange={(e) => setRepayAmount(e.target.value)}
className="mb-4"
/>
<Button
onClick={repayToAave}
disabled={isRepaying}
<Button
onClick={repayToAave}
disabled={isRepaying}
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"
>
{isRepaying && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
Expand All @@ -327,7 +326,7 @@ export default function AaveInteraction() {
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">
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 Assets
</Button>
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"@aave/core-v3": "^1.19.3",
"@aave/deploy-v3": "^1.56.2",
"@coinbase/coinbase-sdk": "^0.5.0",
"@coinbase/coinbase-sdk": "^0.5.1",
"@ethereumjs/common": "^4.4.0",
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-slot": "^1.1.0",
Expand Down

0 comments on commit ef96eda

Please sign in to comment.