Skip to content

Commit 5eeaa7c

Browse files
authored
fix: handle object response from gen_call on testnet (#147)
Testnet node returns gen_call results as an object with data, status, eqOutputs, stdout, stderr, and logs fields. Studio returns a bare hex string. extractGenCallResult() now handles both formats.
1 parent 0cef518 commit 5eeaa7c

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

src/contracts/actions.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@ import {
1313
import {fromHex, toHex, zeroAddress, encodeFunctionData, PublicClient, parseEventLogs} from "viem";
1414
import {toJsonSafeDeep, b64ToArray} from "@/utils/jsonifier";
1515

16+
/**
17+
* Extract hex data from a gen_call result.
18+
* Studio returns a bare hex string; testnet node returns an object:
19+
* { data: "hex...", status: { code, message }, eqOutputs, stdout, stderr, logs }
20+
*/
21+
function extractGenCallResult(result: unknown): `0x${string}` {
22+
if (typeof result === "string") {
23+
return `0x${result}` as `0x${string}`;
24+
}
25+
if (result && typeof result === "object" && "data" in result) {
26+
const obj = result as {data: string; status?: {code: number; message: string}};
27+
if (obj.status && obj.status.code !== 0) {
28+
throw new Error(`gen_call failed: ${obj.status.message}`);
29+
}
30+
return `0x${obj.data}` as `0x${string}`;
31+
}
32+
throw new Error(`Unexpected gen_call response: ${JSON.stringify(result)}`);
33+
}
34+
1635
export const contractActions = (client: GenLayerClient<GenLayerChain>, publicClient: PublicClient) => {
1736
return {
1837
getContractCode: async (address: Address): Promise<string> => {
@@ -84,7 +103,7 @@ export const contractActions = (client: GenLayerClient<GenLayerChain>, publicCli
84103
method: "gen_call",
85104
params: [requestParams],
86105
});
87-
const prefixedResult = `0x${result}` as `0x${string}`;
106+
const prefixedResult = extractGenCallResult(result);
88107

89108
if (args.rawReturn) {
90109
return prefixedResult;
@@ -133,7 +152,7 @@ export const contractActions = (client: GenLayerClient<GenLayerChain>, publicCli
133152
method: "gen_call",
134153
params: [requestParams],
135154
});
136-
const prefixedResult = `0x${result}` as `0x${string}`;
155+
const prefixedResult = extractGenCallResult(result);
137156

138157
if (args.rawReturn) {
139158
return prefixedResult;

0 commit comments

Comments
 (0)