Skip to content

Commit 5d17a25

Browse files
committed
add transactions api endpoint
1 parent f88e48e commit 5d17a25

3 files changed

Lines changed: 208 additions & 3 deletions

File tree

packages/explorer/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"next": "14.2.5",
7070
"node-sql-parser": "^5.3.3",
7171
"nuqs": "^1.19.2",
72+
"pg": "^8.13.1",
7273
"query-string": "^9.1.0",
7374
"react": "^18",
7475
"react-dom": "^18",
@@ -85,6 +86,7 @@
8586
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
8687
"@types/better-sqlite3": "^7.6.4",
8788
"@types/debug": "^4.1.7",
89+
"@types/pg": "^8.11.10",
8890
"@types/react": "18.2.22",
8991
"@types/react-dom": "18.2.7",
9092
"@types/yargs": "^17.0.10",
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Client } from "pg";
2+
import { Hex } from "viem";
3+
4+
const postgresConnectionUrl = process.env.DATABASE_URL;
5+
6+
export async function GET(req: Request) {
7+
const client = new Client({ connectionString: postgresConnectionUrl });
8+
const { searchParams } = new URL(req.url);
9+
const worldAddress = searchParams.get("worldAddress") as Hex;
10+
const offset = Number(searchParams.get("offset")) || 0;
11+
const limit = Number(searchParams.get("limit")) || 10;
12+
const columns = searchParams.get("columns") || "*";
13+
14+
if (!worldAddress) {
15+
return Response.json({ error: "Missing worldAddress" }, { status: 400 });
16+
}
17+
18+
try {
19+
await client.connect();
20+
21+
const transactions = await client.query(
22+
`SELECT ${columns} FROM transactions WHERE tx_to = decode($1, 'hex') ORDER BY block_num DESC OFFSET $2 LIMIT $3`,
23+
[worldAddress, offset, limit],
24+
);
25+
26+
return Response.json({
27+
transactions: transactions.rows,
28+
});
29+
} catch (error) {
30+
console.error("Database error:", error);
31+
return Response.json({ error: "Failed to fetch transactions" }, { status: 500 });
32+
} finally {
33+
await client.end();
34+
}
35+
}

pnpm-lock.yaml

Lines changed: 171 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)