Skip to content
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

fix: graceful balance query error #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,38 @@ export interface Env {
RELAYER_AUTH: string;
}

type JsonRPC = {
type JsonRpc = {
jsonrpc: string;
id: string | number | null;
method: string;
params: any;
};

type BaseJsonRPCResponse = {
type RpcError = {
code: number;
message: string;
};

type BaseJsonRpcResponse = {
jsonrpc: string;
id: string | number | null;
result?: string;
error?: RpcError;
};

type SuccessJsonRpcResponse = {
result: string;
error?: never;
};

type ErrorJsonRpcResponse = {
result?: never;
error: RpcError;
};

type JsonRpcResponse = BaseJsonRpcResponse &
(SuccessJsonRpcResponse | ErrorJsonRpcResponse);

const SUPPORTED_METHODS = [
"faucet_status",
"faucet_getAddress",
Expand Down Expand Up @@ -55,7 +74,7 @@ const query = ({
"Content-Type": "application/json",
},
body: JSON.stringify({ jsonrpc: "2.0", method, params, id: 1 }),
}).then((res) => res.json<BaseJsonRPCResponse>());
}).then((res) => res.json<JsonRpcResponse>());

type DomainArray = { id: string }[];

Expand Down Expand Up @@ -132,7 +151,7 @@ export default {
return makeResponse(`Unsupported method: ${request.method}`, 405);
}

const body = await request.json<JsonRPC>().catch(() => null);
const body = await request.json<JsonRpc>().catch(() => null);

if (!body || !body.jsonrpc || !body.method || !body.params) {
return makeRpcResponse(
Expand Down Expand Up @@ -184,6 +203,19 @@ export default {
method: "eth_getBalance",
params: [item.address, "latest"],
});
if ("error" in balanceResponse && balanceResponse.error) {
const { code, message } = balanceResponse.error;
return makeRpcResponse(
{
error: {
code: -32000,
message: `Error fetching balance (${code}): ${message}`,
},
},
body.id,
400
);
}
const balance = BigInt(balanceResponse.result);

if (claimAmount > balance) {
Expand Down