Skip to content

Commit

Permalink
fix: normalize RPC request parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
roushou committed Jun 20, 2024
1 parent 825e1ee commit 1a178d4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-mirrors-collect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@coinbase-platform/onchain": patch
---

fix: normalize RPC parameters
10 changes: 2 additions & 8 deletions packages/onchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
"publishConfig": {
"access": "public"
},
"keywords": [
"coinbase",
"coinbase platform"
],
"keywords": ["coinbase", "coinbase platform"],
"scripts": {
"build": "pnpm clean && tsup",
"clean": "rimraf ./dist",
Expand All @@ -46,10 +43,7 @@
},
"./package.json": "./package.json"
},
"files": [
"src",
"dist"
],
"files": ["src", "dist"],
"engine": {
"node": "^18.0.0 || >=20.0.0"
}
Expand Down
38 changes: 37 additions & 1 deletion packages/onchain/src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function createRpcClient({
jsonrpc: "2.0",
id: 1,
method: config.method,
params: config.parameters,
params: normalize(config.parameters),
}),
});
},
Expand Down Expand Up @@ -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,
});
}
});
}
4 changes: 3 additions & 1 deletion packages/onchain/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
// Some stricter flags (disabled by default)
"noUnusedLocals": true,
"noUnusedParameters": true,
"noPropertyAccessFromIndexSignature": false
"noPropertyAccessFromIndexSignature": false,

"types": ["vitest/importMeta"]
}
}
4 changes: 4 additions & 0 deletions packages/onchain/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ export default defineConfig({
root: __dirname,
environment: "node",
setupFiles: ["./vitest.setup.ts"],
includeSource: ["src/**/*.ts"],
},
define: {
"import.meta.vitest": "undefined",
},
});

0 comments on commit 1a178d4

Please sign in to comment.