-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroute.ts
More file actions
105 lines (93 loc) · 3.06 KB
/
route.ts
File metadata and controls
105 lines (93 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { NextRequest, NextResponse } from 'next/server'
import { simulateTransaction, isTenderlyConfigured, type SimulationResult } from '@/lib/tenderly'
import { encodeFunctionData, encodeAbiParameters, parseAbiParameters, type Hex } from 'viem'
import { validateProposal, isAddress, isHex } from '@/lib/validation'
export const dynamic = 'force-dynamic'
const GUARDIAN_ADDRESS = process.env.NEXT_PUBLIC_GUARDIAN_ADDRESS ?? ''
const DEPLOYER = '0x23fC03ec91D319e4Aa14e90b6d3664540FDf2446'
// Minimal ABI for processVerdict
const GUARDIAN_ABI = [
{
name: 'processVerdict',
type: 'function',
stateMutability: 'nonpayable',
inputs: [{ name: 'reportData', type: 'bytes' }],
outputs: [],
},
] as const
export async function POST(req: NextRequest) {
if (!isTenderlyConfigured()) {
return NextResponse.json(
{ error: 'Tenderly API not configured' },
{ status: 503 },
)
}
try {
const body = await req.json()
const { mode, proposal, custom } = body
let result: SimulationResult
if (mode === 'custom') {
if (custom?.to && !isAddress(custom.to)) {
return NextResponse.json({ error: 'Invalid target address' }, { status: 400 })
}
if (custom?.input && !isHex(custom.input)) {
return NextResponse.json({ error: 'Invalid calldata' }, { status: 400 })
}
result = await simulateTransaction({
from: custom.from || DEPLOYER,
to: custom.to,
input: custom.input || '0x',
value: custom.value || '0',
gas: custom.gas || 8_000_000,
})
} else {
const proposalError = validateProposal(proposal)
if (proposalError) {
return NextResponse.json({ error: proposalError }, { status: 400 })
}
const reportData = encodeAbiParameters(
parseAbiParameters('bytes32, bool, string, address, bytes4, uint256, uint256'),
[
proposal.agentId as Hex,
true, // simulate as if AI approved — let on-chain policy decide
'Simulated verdict',
proposal.targetContract as `0x${string}`,
proposal.functionSignature as Hex,
BigInt(proposal.value || '0'),
BigInt(proposal.mintAmount || '0'),
],
)
const calldata = encodeFunctionData({
abi: GUARDIAN_ABI,
functionName: 'processVerdict',
args: [reportData],
})
result = await simulateTransaction({
from: DEPLOYER,
to: GUARDIAN_ADDRESS,
input: calldata,
value: '0',
})
}
return NextResponse.json({
success: result.success,
gasUsed: result.gasUsed,
revertReason: result.revertReason,
stateChanges: result.stateChanges.slice(0, 20),
balanceChanges: result.balanceChanges,
callTrace: result.callTrace,
logs: result.logs,
})
} catch (err: any) {
return NextResponse.json(
{ error: err.message ?? 'Simulation failed' },
{ status: 500 },
)
}
}
export async function GET() {
return NextResponse.json({
configured: isTenderlyConfigured(),
guardian: GUARDIAN_ADDRESS,
})
}