-
Notifications
You must be signed in to change notification settings - Fork 172
rpc proxy #523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shafu0x
wants to merge
9
commits into
main
Choose a base branch
from
shafu/rpc-proxy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
rpc proxy #523
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b8eb38d
setup
shafu0x 3af9064
route works
shafu0x 7bb1618
refactor
shafu0x 1c67754
refactor
shafu0x 5182c01
error handling
shafu0x c7a2958
use neverthrow
shafu0x b8bc810
refactor
shafu0x 08f5dad
neverthrow
shafu0x bf46a2c
scan rpc balance
shafu0x File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| BASE_RPC_URL= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # Build outputs | ||
| dist/ | ||
| *.js | ||
| *.mjs | ||
|
|
||
| # Node modules | ||
| node_modules/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # @x402scan/rpc-proxy | ||
|
|
||
| Standalone HTTP service exposing a single endpoint: | ||
|
|
||
| - `GET /balance/:address` | ||
| - EVM addresses return **Base USDC** (ERC-20) balance | ||
|
|
||
| ## Configuration | ||
|
|
||
| Set environment variables: | ||
|
|
||
| - `PORT` (default: `6970`) | ||
| - `BASE_RPC_URL` (required for EVM/Base lookups) | ||
|
|
||
| ## Run locally | ||
|
|
||
| ```bash | ||
| pnpm -w -F @x402scan/rpc-proxy dev | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import { config } from '@x402scan/eslint-config/base'; | ||
|
|
||
| /** @type {import("eslint").Linter.Config[]} */ | ||
| export default config; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| { | ||
| "name": "@x402scan/rpc-proxy", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "tsx watch src/server.ts", | ||
| "build": "tsc", | ||
| "start": "node dist/server.js", | ||
| "types:check": "tsc --noEmit", | ||
| "lint": "eslint . --max-warnings 0", | ||
| "lint:fix": "eslint . --fix", | ||
| "knip": "pnpm -w knip --workspace ./apps/rpc-proxy", | ||
| "format": "pnpm -w format:dir ./apps/rpc-proxy", | ||
| "format:check": "pnpm -w format:check:dir ./apps/rpc-proxy" | ||
| }, | ||
| "dependencies": { | ||
| "@hono/node-server": "^1.13.8", | ||
| "dotenv": "catalog:env", | ||
| "hono": "^4.7.1", | ||
| "neverthrow": "^8.2.0", | ||
| "viem": "catalog:" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "catalog:", | ||
| "@x402scan/eslint-config": "workspace:*", | ||
| "@x402scan/typescript-config": "workspace:*", | ||
| "eslint": "catalog:", | ||
| "tsx": "catalog:", | ||
| "typescript": "catalog:" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import type { Context, Hono } from 'hono'; | ||
| import type { Address } from 'viem'; | ||
|
|
||
| import { | ||
| createPublicClient, | ||
| erc20Abi, | ||
| formatUnits, | ||
| getAddress, | ||
| http, | ||
| isAddress, | ||
| } from 'viem'; | ||
| import { base } from 'viem/chains'; | ||
|
|
||
| import { errAsync, ResultAsync } from 'neverthrow'; | ||
|
|
||
| const BASE_USDC_ADDRESS = '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913'; | ||
| const USDC_DECIMALS = 6; | ||
| const USDC_SYMBOL = 'USDC'; | ||
| const BASE_CHAIN = base; | ||
|
|
||
| const ERROR_NO_RPC = 'no_rpc'; | ||
| const ERROR_RPC_FAILED = 'rpc_failed'; | ||
|
|
||
| type GetBalanceError = | ||
| | { type: typeof ERROR_NO_RPC; message: string } | ||
| | { type: typeof ERROR_RPC_FAILED; message: string }; | ||
|
|
||
| function getBalance(address: Address): ResultAsync<bigint, GetBalanceError> { | ||
| const rpcUrl = process.env.BASE_RPC_URL; | ||
| if (!rpcUrl) { | ||
| return errAsync({ type: ERROR_NO_RPC, message: 'No RPC URL provided' }); | ||
| } | ||
| const client = createPublicClient({ chain: base, transport: http(rpcUrl) }); | ||
| return ResultAsync.fromPromise( | ||
| client.readContract({ | ||
| address: BASE_USDC_ADDRESS, | ||
| abi: erc20Abi, | ||
| functionName: 'balanceOf', | ||
| args: [address], | ||
| }), | ||
| (): GetBalanceError => ({ | ||
| type: ERROR_RPC_FAILED, | ||
| message: 'RPC balanceOf call failed', | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| async function balanceHandler(c: Context) { | ||
| const addressParam = c.req.param('address'); | ||
| if (!isAddress(addressParam)) { | ||
| return c.json({ error: 'Invalid EVM address' }, 400); | ||
| } | ||
| const address = getAddress(addressParam); | ||
|
|
||
| return await getBalance(address).match( | ||
| balance => | ||
| c.json({ | ||
| chain: BASE_CHAIN.id, | ||
| address, | ||
| balance: formatUnits(balance, USDC_DECIMALS), | ||
| rawBalance: balance.toString(), | ||
| token: { | ||
| symbol: USDC_SYMBOL, | ||
| decimals: USDC_DECIMALS, | ||
| address: BASE_USDC_ADDRESS, | ||
| }, | ||
| }), | ||
| error => { | ||
| switch (error.type) { | ||
| case ERROR_NO_RPC: | ||
| return c.json({ error: error.message }, 503); | ||
| case ERROR_RPC_FAILED: | ||
| return c.json({ error: error.message }, 502); | ||
| default: | ||
| return c.json({ error: 'Unhandled error' }, 500); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| export function registerBalanceRouter(app: Hono) { | ||
| app.get('/balance/:address', balanceHandler); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import 'dotenv/config'; | ||
| import { Hono } from 'hono'; | ||
| import { serve } from '@hono/node-server'; | ||
| import { cors } from 'hono/cors'; | ||
|
|
||
| import { registerBalanceRouter } from './routes/balance.js'; | ||
|
|
||
| const app = new Hono(); | ||
|
|
||
| // Enable CORS for all origins (simple service endpoint) | ||
| app.use( | ||
| '*', | ||
| cors({ | ||
| origin: '*', | ||
| allowMethods: ['GET', 'OPTIONS'], | ||
| allowHeaders: ['*'], | ||
| exposeHeaders: ['*'], | ||
| credentials: true, | ||
vercel[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
| ); | ||
|
|
||
| app.get('/', c => { | ||
| return c.json({ | ||
| service: 'rpc-proxy', | ||
| version: '1.0.0', | ||
| endpoints: { | ||
| balance: '/balance/:address', | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| registerBalanceRouter(app); | ||
|
|
||
| const port = Number(process.env.PORT) || 6970; | ||
|
|
||
| serve({ | ||
| fetch: app.fetch, | ||
| port, | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "extends": "@x402scan/typescript-config/base.json", | ||
| "compilerOptions": { | ||
| "outDir": "./dist", | ||
| "rootDir": "./src" | ||
| }, | ||
| "include": ["src/**/*"], | ||
| "exclude": ["node_modules", "dist"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "$schema": "https://turbo.build/schema.json", | ||
| "extends": ["//"], | ||
| "tasks": { | ||
| "dev": { | ||
| "env": ["PORT", "BASE_RPC_URL"] | ||
| }, | ||
| "build": { | ||
| "env": ["PORT", "BASE_RPC_URL"] | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { NextResponse } from 'next/server'; | ||
| import { | ||
| ERROR_NO_RPC, | ||
| ERROR_RPC_FAILED, | ||
| getBalance, | ||
| USDC_DECIMALS, | ||
| } from '@/services/rpc/balance'; | ||
| import { formatUnits, getAddress, isAddress } from 'viem'; | ||
|
|
||
| export async function GET( | ||
| _: Request, | ||
| { params }: { params: Promise<{ address: string }> } | ||
| ) { | ||
| const { address } = await params; | ||
|
|
||
| if (!isAddress(address)) { | ||
| return NextResponse.json({ error: 'Invalid EVM address' }, { status: 400 }); | ||
| } | ||
|
|
||
| const parsedAddress = getAddress(address); | ||
|
|
||
| const balance = await getBalance(parsedAddress).match( | ||
| balance => | ||
| NextResponse.json( | ||
| { | ||
| balance: formatUnits(balance, USDC_DECIMALS), | ||
| rawBalance: balance.toString(), | ||
| }, | ||
| { status: 200 } | ||
| ), | ||
| error => { | ||
| switch (error.type) { | ||
| case ERROR_NO_RPC: | ||
| return NextResponse.json({ error: error.message }, { status: 503 }); | ||
| case ERROR_RPC_FAILED: | ||
| return NextResponse.json({ error: error.message }, { status: 502 }); | ||
| default: | ||
| return NextResponse.json( | ||
| { error: 'Unhandled error' }, | ||
| { status: 500 } | ||
| ); | ||
|
Comment on lines
+38
to
+41
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| } | ||
| ); | ||
|
|
||
| return balance; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { env } from '@/env'; | ||
| import { errAsync, ResultAsync } from 'neverthrow'; | ||
| import { base } from 'wagmi/chains'; | ||
| import { | ||
| createPublicClient, | ||
| http, | ||
| erc20Abi, | ||
| type Address, | ||
| } from 'viem'; | ||
|
|
||
| export const USDC_DECIMALS = 6; | ||
| const BASE_USDC_ADDRESS = '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913'; | ||
|
|
||
| export const ERROR_NO_RPC = 'no_rpc'; | ||
| export const ERROR_RPC_FAILED = 'rpc_failed'; | ||
|
|
||
| type GetBalanceError = | ||
| | { type: typeof ERROR_NO_RPC; message: string } | ||
| | { type: typeof ERROR_RPC_FAILED; message: string }; | ||
|
|
||
| export function getBalance( | ||
| address: Address | ||
| ): ResultAsync<bigint, GetBalanceError> { | ||
| const rpcUrl = env.NEXT_PUBLIC_BASE_RPC_URL; | ||
| if (!rpcUrl) { | ||
| return errAsync({ type: ERROR_NO_RPC, message: 'No RPC URL provided' }); | ||
| } | ||
| const client = createPublicClient({ chain: base, transport: http(rpcUrl) }); | ||
| return ResultAsync.fromPromise( | ||
| client.readContract({ | ||
| address: BASE_USDC_ADDRESS, | ||
| abi: erc20Abi, | ||
| functionName: 'balanceOf', | ||
| args: [address], | ||
| }), | ||
| (): GetBalanceError => ({ | ||
| type: ERROR_RPC_FAILED, | ||
| message: 'RPC balanceOf call failed', | ||
| }) | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.