Skip to content

Commit

Permalink
trade: prevent create wallet when rpc getLogs req fails
Browse files Browse the repository at this point in the history
  • Loading branch information
sehyunc committed Jul 15, 2024
1 parent ee8d64b commit 20fc137
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 14 deletions.
36 changes: 36 additions & 0 deletions trade.renegade.fi/app/api/get-logs/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { chain } from "@/lib/viem"
import { NextRequest } from "next/server"
import { createPublicClient, http, parseAbiItem } from "viem"

export const runtime = "edge"

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

export async function GET(req: NextRequest) {
try {
const blinderShare = BigInt(
req.nextUrl.searchParams.get("blinderShare") || "0"
)
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)"
),
args: {
wallet_blinder_share: blinderShare,
},
fromBlock: BigInt(process.env.FROM_BLOCK || 0),
})
return new Response(JSON.stringify({ logs: logs.length }))
} catch (error) {
console.error(error)
return new Response(JSON.stringify({ error }), { status: 500 })
}
}
7 changes: 5 additions & 2 deletions trade.renegade.fi/app/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ import {
createConfig as createSDKConfig,
useStatus,
} from "@renegade-fi/react"
import { focusManager } from "@tanstack/react-query"
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
import {
QueryClient,
QueryClientProvider,
focusManager,
} from "@tanstack/react-query"
import { ReactQueryDevtools } from "@tanstack/react-query-devtools"
import { ConnectKitProvider, getDefaultConfig } from "connectkit"
import dayjs from "dayjs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
Text,
} from "@chakra-ui/react"
import { useConfig } from "@renegade-fi/react"
import { connect } from "@renegade-fi/react/actions"
import { connect, getSkRoot } from "@renegade-fi/react/actions"
import { CircleHelp, Unplug } from "lucide-react"
import { toast } from "sonner"
import { useLocalStorage } from "usehooks-ts"
Expand Down Expand Up @@ -66,7 +66,17 @@ export function DefaultStep() {
throw new Error("Invalid signature")
}
config.setState((x) => ({ ...x, seed: data }))
const res = await connect(config)
const skRoot = getSkRoot(config)
const blinderShare = config.utils.derive_blinder_share(skRoot)

// Check if WalletUpdated events exist onchain
const logs = await fetch(`/api/get-logs?blinderShare=${blinderShare}`)
.then((res) => res.json())
.then((data) => data.logs)

const res = await connect(config, {
isCreateWallet: logs === 0,
})
if (res?.job) {
const { isLookup, job } = res
toast.promise(job, {
Expand Down
2 changes: 1 addition & 1 deletion trade.renegade.fi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@datadog/browser-rum": "^5.15.0",
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
"@renegade-fi/react": "^0.0.47",
"@renegade-fi/react": "^0.0.49",
"@t3-oss/env-nextjs": "^0.6.0",
"@tanstack/react-query": "^5.24.1",
"@vercel/analytics": "^1.2.2",
Expand Down
18 changes: 9 additions & 9 deletions trade.renegade.fi/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 20fc137

Please sign in to comment.