Skip to content
Merged
Show file tree
Hide file tree
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
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");
});
});
Loading