|
1 | | -import { NextResponse } from "next/server"; |
2 | | -import { BALANCE_UPDATE_DELAY, logTradingAgentData } from "@/lib/utils"; |
3 | | -import { storeTrade, storePortfolioSnapshot } from "@/lib/api-helpers"; |
4 | | -import { buildTransactionPayload, initializeNearAccount } from "@/lib/near"; |
5 | | -import { buildAgentContext } from "@/lib/agent-context"; |
6 | | -import { callAgent } from "@bitte-ai/agent-sdk"; |
7 | | -import { ToolResult } from "@/lib/types"; |
8 | | -import { withCronSecret } from "@/lib/api-auth"; |
9 | | -import { getEnvVar } from "@/lib/env"; |
| 1 | +import { NextResponse } from 'next/server' |
| 2 | +import { BALANCE_UPDATE_DELAY, logTradingAgentData } from '@/lib/utils' |
| 3 | +import { storeTrade, storePortfolioSnapshot } from '@/lib/api-helpers' |
| 4 | +import { buildTransactionPayload, initializeNearAccount } from '@/lib/near' |
| 5 | +import { AGENT_TRIGGER_MESSAGE, buildAgentContext } from '@/lib/agent-context' |
| 6 | +import { callAgent } from '@bitte-ai/agent-sdk' |
| 7 | +import { ToolResult } from '@/lib/types' |
| 8 | +import { withCronSecret } from '@/lib/api-auth' |
| 9 | +import { getEnvVar } from '@/lib/env' |
10 | 10 |
|
11 | 11 | async function tradeHandler(): Promise<NextResponse> { |
12 | 12 | try { |
13 | | - const accountId = getEnvVar("NEXT_PUBLIC_ACCOUNT_ID"); |
14 | | - const agentId = "trading-agent-kappa.vercel.app"; |
| 13 | + const accountId = getEnvVar('NEXT_PUBLIC_ACCOUNT_ID') |
| 14 | + const agentId = 'trading-agent-kappa.vercel.app' |
15 | 15 |
|
16 | | - const account = await initializeNearAccount(accountId); |
| 16 | + const account = await initializeNearAccount(accountId) |
17 | 17 |
|
18 | | - const context = await buildAgentContext(accountId, account); |
| 18 | + const context = await buildAgentContext(accountId, account) |
19 | 19 |
|
20 | | - const { content, toolResults } = await callAgent( |
| 20 | + const { content, toolResults } = await callAgent({ |
21 | 21 | accountId, |
22 | | - context.systemPrompt, |
| 22 | + message: AGENT_TRIGGER_MESSAGE, |
23 | 23 | agentId, |
24 | | - ); |
| 24 | + systemPrompt: context.systemPrompt, |
| 25 | + }) |
25 | 26 |
|
26 | 27 | const quoteResult = (toolResults as ToolResult[]).find( |
27 | | - (callResult) => callResult.result?.data?.data?.quote, |
28 | | - ); |
29 | | - const quote = quoteResult?.result?.data?.data?.quote; |
| 28 | + (callResult) => callResult.result?.data?.data?.quote |
| 29 | + ) |
| 30 | + const quote = quoteResult?.result?.data?.data?.quote |
30 | 31 |
|
31 | 32 | logTradingAgentData({ |
32 | 33 | context, |
33 | 34 | content, |
34 | 35 | pnlUsd: context.totalPnl, |
35 | 36 | quoteResult, |
36 | | - }); |
| 37 | + }) |
37 | 38 |
|
38 | 39 | if (quote) { |
39 | | - const tx = await account.signAndSendTransaction( |
40 | | - buildTransactionPayload(quote), |
41 | | - ); |
42 | | - console.log("Trade executed:", tx.transaction.hash); |
43 | | - await new Promise((resolve) => setTimeout(resolve, BALANCE_UPDATE_DELAY)); |
44 | | - await storeTrade(accountId, quote); |
| 40 | + const tx = await account.signAndSendTransaction(buildTransactionPayload(quote)) |
| 41 | + console.log('Trade executed:', tx.transaction.hash) |
| 42 | + await new Promise((resolve) => setTimeout(resolve, BALANCE_UPDATE_DELAY)) |
| 43 | + await storeTrade(accountId, quote) |
45 | 44 |
|
46 | | - const updatedContext = await buildAgentContext(accountId, account); |
| 45 | + const updatedContext = await buildAgentContext(accountId, account) |
47 | 46 |
|
48 | 47 | await storePortfolioSnapshot( |
49 | 48 | accountId, |
50 | 49 | updatedContext.positionsWithPnl, |
51 | 50 | updatedContext.totalUsd, |
52 | 51 | context.totalUsd, |
53 | | - content, |
54 | | - ); |
| 52 | + content |
| 53 | + ) |
55 | 54 | } else { |
56 | 55 | await storePortfolioSnapshot( |
57 | 56 | accountId, |
58 | 57 | context.positionsWithPnl, |
59 | 58 | context.totalUsd, |
60 | 59 | context.totalUsd, |
61 | | - content, |
62 | | - ); |
| 60 | + content |
| 61 | + ) |
63 | 62 | } |
64 | | - return NextResponse.json({ content }); |
| 63 | + return NextResponse.json({ content }) |
65 | 64 | } catch (error) { |
66 | | - console.error("Error in trading endpoint:", error); |
67 | | - return NextResponse.json( |
68 | | - { error: "Failed to process trading request" }, |
69 | | - { status: 500 }, |
70 | | - ); |
| 65 | + console.error('Error in trading endpoint:', error) |
| 66 | + return NextResponse.json({ error: 'Failed to process trading request' }, { status: 500 }) |
71 | 67 | } |
72 | 68 | } |
73 | 69 |
|
74 | | -export const GET = withCronSecret(tradeHandler); |
| 70 | +export const GET = withCronSecret(tradeHandler) |
0 commit comments