Skip to content

Commit 62f8bad

Browse files
authored
refactor: Improve argument parsing in readContract function (#749)
* refactor: Improve argument parsing in readContract function The readContract function in the src/server/routes/contract/read/read.ts file has been refactored to improve the parsing of arguments. Previously, the function split the arguments string by comma and mapped each argument. Now, the function first tries to parse the arguments as a JSON array. If successful, it uses the parsed arguments. If parsing fails, it falls back to the previous method of splitting the string. This change improves the flexibility and reliability of argument parsing in the readContract function. * refactor: Update test description for reading a contract method with struct and number params
1 parent e441ba4 commit 62f8bad

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

src/server/routes/contract/read/read.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,16 @@ export async function readContract(fastify: FastifyInstance) {
4343
contractAddress,
4444
});
4545

46-
const parsedArgs = args?.split(",").map((arg) => {
46+
let parsedArgs: unknown[] | undefined = [];
47+
48+
try {
49+
const jsonStringArgs = `[${args}]`;
50+
parsedArgs = JSON.parse(jsonStringArgs);
51+
} catch {
52+
// fallback to string split
53+
}
54+
55+
parsedArgs ??= args?.split(",").map((arg) => {
4756
if (arg === "true") {
4857
return true;
4958
}

src/server/utils/convertor.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BigNumber } from "ethers";
22

3-
export const bigNumberReplacer = (value: any): string => {
3+
export const bigNumberReplacer = (value: any): any => {
44
// if we find a BigNumber then make it into a string (since that is safe)
55
if (
66
BigNumber.isBigNumber(value) ||
@@ -11,5 +11,10 @@ export const bigNumberReplacer = (value: any): string => {
1111
) {
1212
return BigNumber.from(value).toString();
1313
}
14+
15+
if (Array.isArray(value)) {
16+
return value.map(bigNumberReplacer);
17+
}
18+
1419
return value;
1520
};

test/e2e/tests/read.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { beforeAll, describe, test } from "bun:test";
2+
import { sepolia } from "thirdweb/chains";
3+
import { expect } from "vitest";
4+
import type { setupEngine } from "../utils/engine";
5+
import { setup } from "./setup";
6+
7+
const structContractAddress = "0x83ca896ef0a66d39f0e6fcc1a93c0a09366b85b1";
8+
const chainIdString = sepolia.id.toString();
9+
10+
describe("Read Tests", () => {
11+
let engine: ReturnType<typeof setupEngine>;
12+
13+
beforeAll(async () => {
14+
const { engine: _engine, backendWallet: _backendWallet } = await setup();
15+
engine = _engine;
16+
});
17+
18+
test("Read a contract method with struct and number params", async () => {
19+
const structValues = {
20+
name: "test",
21+
value: 123,
22+
};
23+
24+
const structString = JSON.stringify(structValues);
25+
26+
const { result } = await engine.contract.read(
27+
"readStructAndInts",
28+
chainIdString,
29+
structContractAddress,
30+
`${structString},1,2`,
31+
);
32+
33+
expect(result[0]).toEqual("test");
34+
expect(result[1]).toEqual("123");
35+
expect(result[2]).toEqual("1");
36+
expect(result[3]).toEqual("2");
37+
});
38+
});

0 commit comments

Comments
 (0)