Skip to content

Commit 1d4546b

Browse files
committed
Added near deposit route
1 parent 5ab9081 commit 1d4546b

File tree

4 files changed

+104
-106
lines changed

4 files changed

+104
-106
lines changed

ONE-CLICK-SETUP.md

Lines changed: 0 additions & 105 deletions
This file was deleted.

app/api/deposit/route.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { NextRequest, NextResponse } from 'next/server'
2+
import { BALANCE_UPDATE_DELAY } from '@/lib/utils'
3+
import { initializeNearAccount, depositUSDC, getUSDCBalance } from '@/lib/near'
4+
5+
export async function GET(request: NextRequest) {
6+
try {
7+
if (process.env.CRON_SECRET) {
8+
const authHeader = request.headers.get('Authorization')
9+
if (authHeader !== `Bearer ${process.env.CRON_SECRET}`) {
10+
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
11+
}
12+
}
13+
14+
const { searchParams } = new URL(request.url)
15+
const amount = searchParams.get('amount') ? Number(searchParams.get('amount')) : undefined
16+
const accountId = process.env.NEXT_PUBLIC_ACCOUNT_ID
17+
if (!accountId) {
18+
return NextResponse.json({ error: 'accountId is not configured' }, { status: 500 })
19+
}
20+
21+
const depositAmount = amount || 10
22+
23+
const account = await initializeNearAccount(accountId)
24+
25+
const usdcBalance = await getUSDCBalance(account)
26+
const usdcBalanceDecimal = Number(usdcBalance) / 1_000_000
27+
28+
if (usdcBalanceDecimal < depositAmount) {
29+
return NextResponse.json({
30+
error: `Insufficient USDC balance (required: $${depositAmount}, available: $${usdcBalanceDecimal.toFixed(2)})`
31+
}, { status: 400 })
32+
}
33+
34+
const amountToDeposit = BigInt(depositAmount * 1_000_000)
35+
36+
const tx = await depositUSDC(account, amountToDeposit)
37+
38+
await new Promise(resolve => setTimeout(resolve, BALANCE_UPDATE_DELAY))
39+
40+
return NextResponse.json({
41+
message: `Successfully deposited $${depositAmount} USDC`,
42+
transactionHash: tx.transaction.hash,
43+
amount: depositAmount
44+
})
45+
46+
} catch (error) {
47+
console.error('Error in deposit endpoint:', error)
48+
return NextResponse.json({ error: 'Failed to process deposit request' }, { status: 500 })
49+
}
50+
}

app/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export default function Page() {
188188
<div className="lg:col-span-2">
189189
<StatsChart data={data.statsChart} totalValue={data.totalValue} />
190190
</div>
191-
<AssetDistributionChart assets={data.assetDistribution} />
191+
<AssetDistributionChart assets={data.assetDistribution as { symbol: string; value: number; percentage: number; change: number; }[]} />
192192
</div>
193193

194194
{/* Bottom Section: Realized P&L and Agent Terminal */}

lib/near.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,56 @@ export function buildTransactionPayload(quote: Quote) {
5757
],
5858
};
5959
}
60+
61+
const USDC_CONTRACT = "17208628f84f5d6ad33f0da3bbbeb27ffcb398eac501a31bd6ad2011e36133a1"
62+
const USDC_TOKEN_ID = "nep141:17208628f84f5d6ad33f0da3bbbeb27ffcb398eac501a31bd6ad2011e36133a1"
63+
64+
export async function getUSDCBalance(account: Account): Promise<bigint> {
65+
try {
66+
const result = await account.viewFunction({
67+
contractId: USDC_CONTRACT,
68+
methodName: "ft_balance_of",
69+
args: { account_id: account.accountId },
70+
});
71+
return BigInt(result as string);
72+
} catch (error) {
73+
console.warn("Failed to fetch USDC balance:", error);
74+
return BigInt(0);
75+
}
76+
}
77+
78+
export async function depositUSDC(account: Account, amount: bigint) {
79+
const result = await account.signAndSendTransaction({
80+
receiverId: USDC_CONTRACT,
81+
actions: [
82+
actionCreators.functionCall(
83+
"ft_transfer_call",
84+
{
85+
receiver_id: INTENTS_CONTRACT_ID,
86+
amount: amount.toString(),
87+
msg: account.accountId,
88+
},
89+
BigInt(TGas * 50),
90+
BigInt(1),
91+
),
92+
],
93+
});
94+
95+
const hasSuccess = result.receipts_outcome.some(receipt =>
96+
receipt.outcome.logs.some(log =>
97+
log.includes('mt_mint') && log.includes(account.accountId)
98+
)
99+
);
100+
101+
const hasRefund = result.receipts_outcome.some(receipt =>
102+
receipt.outcome.logs.some(log =>
103+
log.includes('ft_transfer') && log.includes('"memo":"refund"')
104+
)
105+
);
106+
107+
if (hasRefund || !hasSuccess) {
108+
throw new Error('Deposit failed - transaction was refunded');
109+
}
110+
111+
return result;
112+
}

0 commit comments

Comments
 (0)