Skip to content

Commit

Permalink
route: prevent cache storage on get logs fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
sehyunc committed Oct 30, 2024
1 parent 5d63773 commit 54d48b0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 30 deletions.
53 changes: 38 additions & 15 deletions app/api/get-logs/route.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import { NextRequest } from "next/server"

import { createPublicClient, http, parseAbiItem } from "viem"

import { chain } from "@/lib/viem"
import { encodeEventTopics, numberToHex, parseAbiItem, toHex } from "viem"

export const runtime = "edge"

// Necessary because public RPC does not support getting logs
const viemClient = createPublicClient({
transport: http(process.env.RPC_URL),
})

export async function GET(req: NextRequest) {
try {
const blinderShare = BigInt(
Expand All @@ -19,17 +12,47 @@ export async function GET(req: NextRequest) {
if (!blinderShare) {
throw new Error("Blinder share is required")
}
const logs = await viemClient.getLogs({
address: process.env.NEXT_PUBLIC_DARKPOOL_CONTRACT,
event: parseAbiItem(
"event WalletUpdated(uint256 indexed wallet_blinder_share)",
),

const topics = encodeEventTopics({
abi: [
parseAbiItem(
"event WalletUpdated(uint256 indexed wallet_blinder_share)",
),
],
args: {
wallet_blinder_share: blinderShare,
},
fromBlock: BigInt(process.env.FROM_BLOCK || 0),
})
return new Response(JSON.stringify({ logs: logs.length }))

// Make raw JSON-RPC call
const response = await fetch(process.env.RPC_URL!, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
id: 1,
jsonrpc: "2.0",
method: "eth_getLogs",
params: [
{
address: process.env.NEXT_PUBLIC_DARKPOOL_CONTRACT,
topics,
fromBlock: toHex(process.env.FROM_BLOCK ?? 0),
},
],
}),
cache: "no-store",
})

if (!response.ok) {
throw new Error(`Failed to fetch logs because ${response.statusText}`)
}

const result = await response.json()
if (result.error) {
throw new Error(`RPC error: ${result.error.message}`)
}

return new Response(JSON.stringify({ logs: result.result.length }))
} catch (error) {
console.error(error)
return new Response(JSON.stringify({ error }), { status: 500 })
Expand Down
30 changes: 15 additions & 15 deletions components/dialogs/sign-in-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,21 @@ export function SignInDialog({
const id = getWalletId(config)
config.setState((x) => ({ ...x, id }))
setIsConnecting(true)
try {
// GET wallet from relayer
const wallet = await getWalletFromRelayer(config)
// If success, return
if (wallet) {
config.setState((x) => ({ ...x, status: "in relayer" }))
toast.success("Successfully signed in", {
id: toastId,
icon: undefined,
})
onOpenChange(false)
setIsConnecting(false)
return
}
} catch (error) {}
// try {
// // GET wallet from relayer
// const wallet = await getWalletFromRelayer(config)
// // If success, return
// if (wallet) {
// config.setState((x) => ({ ...x, status: "in relayer" }))
// toast.success("Successfully signed in", {
// id: toastId,
// icon: undefined,
// })
// onOpenChange(false)
// setIsConnecting(false)
// return
// }
// } catch (error) {}

// GET # logs
const blinderShare = config.utils.derive_blinder_share(data)
Expand Down

0 comments on commit 54d48b0

Please sign in to comment.