Skip to content

Commit

Permalink
refactor: Improve argument parsing in readContract function (#749)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
d4mr authored Nov 1, 2024
1 parent e441ba4 commit 62f8bad
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/server/routes/contract/read/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@ export async function readContract(fastify: FastifyInstance) {
contractAddress,
});

const parsedArgs = args?.split(",").map((arg) => {
let parsedArgs: unknown[] | undefined = [];

try {
const jsonStringArgs = `[${args}]`;
parsedArgs = JSON.parse(jsonStringArgs);
} catch {
// fallback to string split
}

parsedArgs ??= args?.split(",").map((arg) => {
if (arg === "true") {
return true;
}
Expand Down
7 changes: 6 additions & 1 deletion src/server/utils/convertor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BigNumber } from "ethers";

export const bigNumberReplacer = (value: any): string => {
export const bigNumberReplacer = (value: any): any => {
// if we find a BigNumber then make it into a string (since that is safe)
if (
BigNumber.isBigNumber(value) ||
Expand All @@ -11,5 +11,10 @@ export const bigNumberReplacer = (value: any): string => {
) {
return BigNumber.from(value).toString();
}

if (Array.isArray(value)) {
return value.map(bigNumberReplacer);
}

return value;
};
38 changes: 38 additions & 0 deletions test/e2e/tests/read.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { beforeAll, describe, test } from "bun:test";
import { sepolia } from "thirdweb/chains";
import { expect } from "vitest";
import type { setupEngine } from "../utils/engine";
import { setup } from "./setup";

const structContractAddress = "0x83ca896ef0a66d39f0e6fcc1a93c0a09366b85b1";
const chainIdString = sepolia.id.toString();

describe("Read Tests", () => {
let engine: ReturnType<typeof setupEngine>;

beforeAll(async () => {
const { engine: _engine, backendWallet: _backendWallet } = await setup();
engine = _engine;
});

test("Read a contract method with struct and number params", async () => {
const structValues = {
name: "test",
value: 123,
};

const structString = JSON.stringify(structValues);

const { result } = await engine.contract.read(
"readStructAndInts",
chainIdString,
structContractAddress,
`${structString},1,2`,
);

expect(result[0]).toEqual("test");
expect(result[1]).toEqual("123");
expect(result[2]).toEqual("1");
expect(result[3]).toEqual("2");
});
});

0 comments on commit 62f8bad

Please sign in to comment.