From 1a178d455acfc1f8ccfb0d45d7aee38bde2900b0 Mon Sep 17 00:00:00 2001 From: roushou Date: Thu, 20 Jun 2024 20:56:21 +0700 Subject: [PATCH] fix: normalize RPC request parameters --- .changeset/orange-mirrors-collect.md | 5 ++++ packages/onchain/package.json | 10 ++------ packages/onchain/src/rpc.ts | 38 +++++++++++++++++++++++++++- packages/onchain/tsconfig.json | 4 ++- packages/onchain/vitest.config.ts | 4 +++ 5 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 .changeset/orange-mirrors-collect.md diff --git a/.changeset/orange-mirrors-collect.md b/.changeset/orange-mirrors-collect.md new file mode 100644 index 0000000..1fabc5b --- /dev/null +++ b/.changeset/orange-mirrors-collect.md @@ -0,0 +1,5 @@ +--- +"@coinbase-platform/onchain": patch +--- + +fix: normalize RPC parameters diff --git a/packages/onchain/package.json b/packages/onchain/package.json index 42d8e90..b146bd6 100644 --- a/packages/onchain/package.json +++ b/packages/onchain/package.json @@ -20,10 +20,7 @@ "publishConfig": { "access": "public" }, - "keywords": [ - "coinbase", - "coinbase platform" - ], + "keywords": ["coinbase", "coinbase platform"], "scripts": { "build": "pnpm clean && tsup", "clean": "rimraf ./dist", @@ -46,10 +43,7 @@ }, "./package.json": "./package.json" }, - "files": [ - "src", - "dist" - ], + "files": ["src", "dist"], "engine": { "node": "^18.0.0 || >=20.0.0" } diff --git a/packages/onchain/src/rpc.ts b/packages/onchain/src/rpc.ts index 30ea079..567c55f 100644 --- a/packages/onchain/src/rpc.ts +++ b/packages/onchain/src/rpc.ts @@ -55,7 +55,7 @@ export function createRpcClient({ jsonrpc: "2.0", id: 1, method: config.method, - params: config.parameters, + params: normalize(config.parameters), }), }); }, @@ -178,3 +178,39 @@ export type RpcBalanceHistory = { blockHash: string; value: number; }; + +/** + * Transforms addresses into lowercase. + * + * Reference: `https://docs.cdp.coinbase.com/onchain-data/docs/onchain-data-api/#parameters` + */ +function normalize( + parameters: RpcRequestConfig["parameters"], +): RpcRequestConfig["parameters"] { + for (const element of parameters) { + element.address = element.address.toLowerCase(); + } + return parameters; +} + +if (import.meta.vitest) { + const { test, expect } = import.meta.vitest; + + test("should transform addresses into lowercase", () => { + const parameters: RpcRequestConfig["parameters"] = [ + { + address: "0x9C5940dFbd4633A67C60CfDC8B81E1d1916b4a08", + pageSize: 1, + pageToken: "page-token", + }, + ]; + const normalized = normalize(parameters); + for (let i = 0; i < parameters.length; i++) { + expect(normalized[i]).toStrictEqual({ + address: parameters[i].address.toLowerCase(), + pageSize: parameters[i].pageSize, + pageToken: parameters[i].pageToken, + }); + } + }); +} diff --git a/packages/onchain/tsconfig.json b/packages/onchain/tsconfig.json index 53858a2..280ad24 100644 --- a/packages/onchain/tsconfig.json +++ b/packages/onchain/tsconfig.json @@ -21,6 +21,8 @@ // Some stricter flags (disabled by default) "noUnusedLocals": true, "noUnusedParameters": true, - "noPropertyAccessFromIndexSignature": false + "noPropertyAccessFromIndexSignature": false, + + "types": ["vitest/importMeta"] } } diff --git a/packages/onchain/vitest.config.ts b/packages/onchain/vitest.config.ts index ef1aaf1..ddc9051 100644 --- a/packages/onchain/vitest.config.ts +++ b/packages/onchain/vitest.config.ts @@ -5,5 +5,9 @@ export default defineConfig({ root: __dirname, environment: "node", setupFiles: ["./vitest.setup.ts"], + includeSource: ["src/**/*.ts"], + }, + define: { + "import.meta.vitest": "undefined", }, });