Skip to content

Commit 7c5ee47

Browse files
committed
fix: refactor info endpoint
1 parent 6aeaa27 commit 7c5ee47

File tree

8 files changed

+109
-108
lines changed

8 files changed

+109
-108
lines changed

examples/api/src/app/api/erc-20/balances/route.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,19 @@ import {
55
getEthUsdPrice,
66
numberWithCommas,
77
parseTokenParam,
8+
parseInfoRequestParams,
89
} from "../lib/utils";
910

1011
export async function GET(request: NextRequest) {
12+
const { blockchain, tokenAddress } = parseInfoRequestParams(request);
1113
const userAddress = request.nextUrl.searchParams
1214
.get("walletAddress")
1315
?.toLowerCase();
14-
const token = request.nextUrl.searchParams.get("token")?.toLowerCase();
15-
let tokenAddress = request.nextUrl.searchParams
16-
.get("tokenAddress")
17-
?.toLowerCase();
18-
let blockchain = request.nextUrl.searchParams
19-
.get("blockchain")
20-
?.toLowerCase();
2116
const buyOptionsUsd = request.nextUrl.searchParams
2217
.get("buyOptionsUsd")
2318
.split(",")
2419
.map((x) => parseFloat(x));
2520

26-
if (token) {
27-
const parsedToken = parseTokenParam(token);
28-
tokenAddress = parsedToken.tokenAddress;
29-
blockchain = parsedToken.blockchain;
30-
}
31-
3221
if (!tokenAddress || !blockchain) {
3322
return new Response("Missing tokenAddress or blockchain", {
3423
status: 400,

examples/api/src/app/api/erc-20/buy/route.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,22 @@
11
import { NextRequest, NextResponse } from "next/server";
22
import { parseEther } from "viem2";
3-
import { chainByName, getEthUsdPrice, parseTokenParam } from "../lib/utils";
3+
import {
4+
chainByName,
5+
getEthUsdPrice,
6+
parseInfoRequestParams,
7+
} from "../lib/utils";
48

59
export async function POST(request: NextRequest) {
10+
const { blockchain, tokenAddress } = parseInfoRequestParams(request);
11+
612
// TODO: Expose separate execution/receiver addresses
713
const userAddress = request.nextUrl.searchParams
814
.get("walletAddress")
915
?.toLowerCase();
10-
const token = request.nextUrl.searchParams.get("token")?.toLowerCase();
11-
let tokenAddress = request.nextUrl.searchParams
12-
.get("tokenAddress")
13-
?.toLowerCase();
14-
let blockchain = request.nextUrl.searchParams
15-
.get("blockchain")
16-
?.toLowerCase();
1716
const buyAmountUsd = parseFloat(
1817
request.nextUrl.searchParams.get("buyAmountUsd")
1918
);
2019

21-
if (token) {
22-
const parsedToken = parseTokenParam(token);
23-
tokenAddress = parsedToken.tokenAddress;
24-
blockchain = parsedToken.blockchain;
25-
}
26-
2720
if (!tokenAddress || !blockchain) {
2821
return new Response("Missing tokenAddress or blockchain", {
2922
status: 400,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { NextRequest } from "next/server";
2+
import {
3+
getFollowingHolderInfo,
4+
parseInfoRequestParams,
5+
} from "../../lib/utils";
6+
7+
export async function GET(request: NextRequest) {
8+
const { fid, tokenAddress, blockchain } = parseInfoRequestParams(request);
9+
const result = await getFollowingHolderInfo({
10+
fid,
11+
blockchain,
12+
tokenAddress,
13+
});
14+
15+
if (!tokenAddress || !blockchain) {
16+
return new Response("Missing tokenAddress or blockchain", {
17+
status: 400,
18+
});
19+
}
20+
21+
return Response.json(result, {
22+
headers: new Headers({
23+
// Cache for 1 day
24+
"Cache-Control": "public, max-age=86400, immutable",
25+
}),
26+
});
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { NextRequest } from "next/server";
2+
import { getPriceData, parseInfoRequestParams } from "../../lib/utils";
3+
4+
export async function GET(request: NextRequest) {
5+
const { tokenAddress, blockchain } = parseInfoRequestParams(request);
6+
const result = await getPriceData({
7+
blockchain,
8+
tokenAddress,
9+
});
10+
11+
if (!tokenAddress || !blockchain) {
12+
return new Response("Missing tokenAddress or blockchain", {
13+
status: 400,
14+
});
15+
}
16+
17+
return Response.json(result, {
18+
headers: new Headers({
19+
// Cache for 1 hour
20+
"Cache-Control": "public, max-age=3600, immutable",
21+
}),
22+
});
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { NextRequest } from "next/server";
2+
import { getTokenInfo, parseInfoRequestParams } from "../../lib/utils";
3+
4+
export async function GET(request: NextRequest) {
5+
const { tokenAddress, blockchain } = parseInfoRequestParams(request);
6+
const result = await getTokenInfo({
7+
blockchain,
8+
tokenAddress,
9+
});
10+
11+
if (!tokenAddress || !blockchain) {
12+
return new Response("Missing tokenAddress or blockchain", {
13+
status: 400,
14+
});
15+
}
16+
17+
return Response.json(result, {
18+
headers: new Headers({
19+
// Cache for 1 month
20+
"Cache-Control": "public, max-age=2592000, immutable",
21+
}),
22+
});
23+
}

examples/api/src/app/api/erc-20/lib/utils.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FarcasterUser } from "@mod-protocol/core";
2+
import { NextRequest } from "next/server";
23
import { publicActionReverseMirage } from "reverse-mirage";
34
import { createPublicClient, http } from "viem2";
45
import * as chains from "viem2/chains";
@@ -312,3 +313,26 @@ export async function getEthUsdPrice(): Promise<number> {
312313

313314
return ethPriceUsd;
314315
}
316+
317+
export function parseInfoRequestParams(request: NextRequest) {
318+
const fid = request.nextUrl.searchParams.get("fid");
319+
const token = request.nextUrl.searchParams.get("token")?.toLowerCase();
320+
let tokenAddress = request.nextUrl.searchParams
321+
.get("tokenAddress")
322+
?.toLowerCase();
323+
let blockchain = request.nextUrl.searchParams
324+
.get("blockchain")
325+
?.toLowerCase();
326+
327+
if (token) {
328+
const parsedToken = parseTokenParam(token);
329+
tokenAddress = parsedToken.tokenAddress;
330+
blockchain = parsedToken.blockchain;
331+
}
332+
333+
return {
334+
fid,
335+
tokenAddress,
336+
blockchain,
337+
};
338+
}

examples/api/src/app/api/erc-20/route.ts

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

mods/erc-20/src/view.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,23 +321,23 @@ const view: ModElement[] = [
321321
type: "horizontal-layout",
322322
onload: {
323323
type: "GET",
324-
url: "{{api}}/erc-20?fid={{user.farcaster.fid}}&token={{embed.url}}&function=token",
324+
url: "{{api}}/erc-20/info/token?fid={{user.farcaster.fid}}&token={{embed.url}}",
325325
ref: "tokenReq",
326326
},
327327
},
328328
{
329329
type: "horizontal-layout",
330330
onload: {
331331
type: "GET",
332-
url: "{{api}}/erc-20?fid={{user.farcaster.fid}}&token={{embed.url}}&function=holders",
332+
url: "{{api}}/erc-20/info/holders?fid={{user.farcaster.fid}}&token={{embed.url}}",
333333
ref: "holdersReq",
334334
},
335335
},
336336
{
337337
type: "horizontal-layout",
338338
onload: {
339339
type: "GET",
340-
url: "{{api}}/erc-20?fid={{user.farcaster.fid}}&token={{embed.url}}&function=price",
340+
url: "{{api}}/erc-20/info/price?fid={{user.farcaster.fid}}&token={{embed.url}}",
341341
ref: "priceReq",
342342
},
343343
},

0 commit comments

Comments
 (0)