genlayer-js 0.17.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
 
2
2
 
3
+ ## 0.18.0 (2025-09-04)
4
+
5
+
6
+ ### Features
7
+
8
+ * format in genlayer js to convert to an object ([#107](https://github.com/genlayerlabs/genlayer-js/issues/107)) ([3ca4076](https://github.com/genlayerlabs/genlayer-js/commit/3ca40765bc03e8d7174c8e337e6104ccea455e5a))
9
+
10
+ ## 0.17.1 (2025-09-03)
11
+
12
+
13
+ ### Bug Fixes
14
+
15
+ * returning the whole data when an error occurs ([#111](https://github.com/genlayerlabs/genlayer-js/issues/111)) ([bebb025](https://github.com/genlayerlabs/genlayer-js/commit/bebb025a0f07d4ed05783e1a77abe76979fc4ac0))
16
+
3
17
  ## 0.17.0 (2025-09-03)
4
18
 
5
19
 
@@ -332,6 +332,7 @@ type GenLayerClient<TGenLayerChain extends GenLayerChain> = Omit<Client<Transpor
332
332
  [key: string]: CalldataEncodable;
333
333
  };
334
334
  rawReturn?: RawReturn;
335
+ jsonSafeReturn?: boolean;
335
336
  transactionHashVariant?: TransactionHashVariant;
336
337
  }) => Promise<RawReturn extends true ? `0x${string}` : CalldataEncodable>;
337
338
  writeContract: (args: {
@@ -332,6 +332,7 @@ type GenLayerClient<TGenLayerChain extends GenLayerChain> = Omit<Client<Transpor
332
332
  [key: string]: CalldataEncodable;
333
333
  };
334
334
  rawReturn?: RawReturn;
335
+ jsonSafeReturn?: boolean;
335
336
  transactionHashVariant?: TransactionHashVariant;
336
337
  }) => Promise<RawReturn extends true ? `0x${string}` : CalldataEncodable>;
337
338
  writeContract: (args: {
package/dist/index.cjs CHANGED
@@ -419,6 +419,7 @@ var calldata = calldata_exports;
419
419
  var transactions = transactions_exports;
420
420
 
421
421
  // src/utils/jsonifier.ts
422
+
422
423
  function b64ToArray(b64) {
423
424
  return Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
424
425
  }
@@ -457,6 +458,54 @@ function resultToUserFriendlyJson(cd64) {
457
458
  payload
458
459
  };
459
460
  }
461
+ function toJsonSafeDeep(value) {
462
+ return _toJsonSafeDeep(value, /* @__PURE__ */ new WeakSet());
463
+ }
464
+ function _toJsonSafeDeep(value, seen) {
465
+ if (value === null || value === void 0) {
466
+ return null;
467
+ }
468
+ const primitiveType = typeof value;
469
+ if (primitiveType === "string" || primitiveType === "boolean" || primitiveType === "number") {
470
+ return value;
471
+ }
472
+ if (primitiveType === "bigint") {
473
+ const big = value;
474
+ const abs = big < 0n ? -big : big;
475
+ const maxSafe = 9007199254740991n;
476
+ return abs <= maxSafe ? Number(big) : big.toString();
477
+ }
478
+ if (typeof value === "object") {
479
+ if (seen.has(value)) {
480
+ return null;
481
+ }
482
+ seen.add(value);
483
+ if (value instanceof Uint8Array) {
484
+ return _viem.toHex.call(void 0, value);
485
+ }
486
+ if (value instanceof Array) {
487
+ return value.map((v) => _toJsonSafeDeep(v, seen));
488
+ }
489
+ if (value instanceof Map) {
490
+ const obj = {};
491
+ for (const [k, v] of value.entries()) {
492
+ obj[k] = _toJsonSafeDeep(v, seen);
493
+ }
494
+ return obj;
495
+ }
496
+ if (value instanceof _chunkH4ZYXVV2cjs.CalldataAddress) {
497
+ return _viem.toHex.call(void 0, value.bytes);
498
+ }
499
+ if (Object.getPrototypeOf(value) === Object.prototype) {
500
+ const obj = {};
501
+ for (const [k, v] of Object.entries(value)) {
502
+ obj[k] = _toJsonSafeDeep(v, seen);
503
+ }
504
+ return obj;
505
+ }
506
+ }
507
+ return value;
508
+ }
460
509
 
461
510
  // src/contracts/actions.ts
462
511
  var contractActions = (client, publicClient) => {
@@ -499,6 +548,7 @@ var contractActions = (client, publicClient) => {
499
548
  functionName,
500
549
  args: callArgs,
501
550
  kwargs,
551
+ jsonSafeReturn = false,
502
552
  leaderOnly = false,
503
553
  transactionHashVariant = "latest-nonfinal" /* LATEST_NONFINAL */
504
554
  } = args;
@@ -521,7 +571,11 @@ var contractActions = (client, publicClient) => {
521
571
  return prefixedResult;
522
572
  }
523
573
  const resultBinary = _viem.fromHex.call(void 0, prefixedResult, "bytes");
524
- return decode(resultBinary);
574
+ const decoded = decode(resultBinary);
575
+ if (!jsonSafeReturn) {
576
+ return decoded;
577
+ }
578
+ return toJsonSafeDeep(decoded);
525
579
  },
526
580
  simulateWriteContract: async (args) => {
527
581
  const {
@@ -1199,11 +1253,11 @@ var getCustomTransportConfig = (config) => {
1199
1253
  });
1200
1254
  const data = await response.json();
1201
1255
  if (data.error) {
1202
- throw new Error(data.error.message);
1256
+ throw data.error;
1203
1257
  }
1204
1258
  return data.result;
1205
1259
  } catch (err) {
1206
- console.error(`Error fetching ${method} from GenLayer RPC:`, err);
1260
+ console.error(`Error fetching ${method} from GenLayer RPC`);
1207
1261
  throw err;
1208
1262
  }
1209
1263
  }
package/dist/index.d.cts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as viem from 'viem';
2
2
  import { Account, Address, Hex } from 'viem';
3
3
  import { G as GenLayerChain } from './chains-BYSCF33g.cjs';
4
- import { G as GenLayerClient, D as DecodedDeployData, a as DecodedCallData, b as GenLayerRawTransaction, c as GenLayerTransaction, C as CalldataEncodable, T as TransactionDataElement } from './index-DrEvzYFA.cjs';
4
+ import { G as GenLayerClient, D as DecodedDeployData, a as DecodedCallData, b as GenLayerRawTransaction, c as GenLayerTransaction, C as CalldataEncodable, T as TransactionDataElement } from './index-CxNl4CK5.cjs';
5
5
  import * as abitype from 'abitype';
6
6
  import * as viem__types_types_authorization from 'viem/_types/types/authorization';
7
7
  import * as viem_accounts from 'viem/accounts';
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as viem from 'viem';
2
2
  import { Account, Address, Hex } from 'viem';
3
3
  import { G as GenLayerChain } from './chains-BYSCF33g.js';
4
- import { G as GenLayerClient, D as DecodedDeployData, a as DecodedCallData, b as GenLayerRawTransaction, c as GenLayerTransaction, C as CalldataEncodable, T as TransactionDataElement } from './index-BIyk5Fv7.js';
4
+ import { G as GenLayerClient, D as DecodedDeployData, a as DecodedCallData, b as GenLayerRawTransaction, c as GenLayerTransaction, C as CalldataEncodable, T as TransactionDataElement } from './index-BnFbqUhu.js';
5
5
  import * as abitype from 'abitype';
6
6
  import * as viem__types_types_authorization from 'viem/_types/types/authorization';
7
7
  import * as viem_accounts from 'viem/accounts';
package/dist/index.js CHANGED
@@ -407,7 +407,7 @@ function serialize(data) {
407
407
  }
408
408
 
409
409
  // src/contracts/actions.ts
410
- import { fromHex, toHex as toHex2, zeroAddress, encodeFunctionData, parseEventLogs } from "viem";
410
+ import { fromHex, toHex as toHex3, zeroAddress, encodeFunctionData, parseEventLogs } from "viem";
411
411
 
412
412
  // src/abi/index.ts
413
413
  var abi_exports = {};
@@ -419,6 +419,7 @@ var calldata = calldata_exports;
419
419
  var transactions = transactions_exports;
420
420
 
421
421
  // src/utils/jsonifier.ts
422
+ import { toHex as toHex2 } from "viem";
422
423
  function b64ToArray(b64) {
423
424
  return Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
424
425
  }
@@ -457,6 +458,54 @@ function resultToUserFriendlyJson(cd64) {
457
458
  payload
458
459
  };
459
460
  }
461
+ function toJsonSafeDeep(value) {
462
+ return _toJsonSafeDeep(value, /* @__PURE__ */ new WeakSet());
463
+ }
464
+ function _toJsonSafeDeep(value, seen) {
465
+ if (value === null || value === void 0) {
466
+ return null;
467
+ }
468
+ const primitiveType = typeof value;
469
+ if (primitiveType === "string" || primitiveType === "boolean" || primitiveType === "number") {
470
+ return value;
471
+ }
472
+ if (primitiveType === "bigint") {
473
+ const big = value;
474
+ const abs = big < 0n ? -big : big;
475
+ const maxSafe = 9007199254740991n;
476
+ return abs <= maxSafe ? Number(big) : big.toString();
477
+ }
478
+ if (typeof value === "object") {
479
+ if (seen.has(value)) {
480
+ return null;
481
+ }
482
+ seen.add(value);
483
+ if (value instanceof Uint8Array) {
484
+ return toHex2(value);
485
+ }
486
+ if (value instanceof Array) {
487
+ return value.map((v) => _toJsonSafeDeep(v, seen));
488
+ }
489
+ if (value instanceof Map) {
490
+ const obj = {};
491
+ for (const [k, v] of value.entries()) {
492
+ obj[k] = _toJsonSafeDeep(v, seen);
493
+ }
494
+ return obj;
495
+ }
496
+ if (value instanceof CalldataAddress) {
497
+ return toHex2(value.bytes);
498
+ }
499
+ if (Object.getPrototypeOf(value) === Object.prototype) {
500
+ const obj = {};
501
+ for (const [k, v] of Object.entries(value)) {
502
+ obj[k] = _toJsonSafeDeep(v, seen);
503
+ }
504
+ return obj;
505
+ }
506
+ }
507
+ return value;
508
+ }
460
509
 
461
510
  // src/contracts/actions.ts
462
511
  var contractActions = (client, publicClient) => {
@@ -488,7 +537,7 @@ var contractActions = (client, publicClient) => {
488
537
  }
489
538
  const schema = await client.request({
490
539
  method: "gen_getContractSchemaForCode",
491
- params: [toHex2(contractCode)]
540
+ params: [toHex3(contractCode)]
492
541
  });
493
542
  return schema;
494
543
  },
@@ -499,6 +548,7 @@ var contractActions = (client, publicClient) => {
499
548
  functionName,
500
549
  args: callArgs,
501
550
  kwargs,
551
+ jsonSafeReturn = false,
502
552
  leaderOnly = false,
503
553
  transactionHashVariant = "latest-nonfinal" /* LATEST_NONFINAL */
504
554
  } = args;
@@ -521,7 +571,11 @@ var contractActions = (client, publicClient) => {
521
571
  return prefixedResult;
522
572
  }
523
573
  const resultBinary = fromHex(prefixedResult, "bytes");
524
- return decode(resultBinary);
574
+ const decoded = decode(resultBinary);
575
+ if (!jsonSafeReturn) {
576
+ return decoded;
577
+ }
578
+ return toJsonSafeDeep(decoded);
525
579
  },
526
580
  simulateWriteContract: async (args) => {
527
581
  const {
@@ -1199,11 +1253,11 @@ var getCustomTransportConfig = (config) => {
1199
1253
  });
1200
1254
  const data = await response.json();
1201
1255
  if (data.error) {
1202
- throw new Error(data.error.message);
1256
+ throw data.error;
1203
1257
  }
1204
1258
  return data.result;
1205
1259
  } catch (err) {
1206
- console.error(`Error fetching ${method} from GenLayer RPC:`, err);
1260
+ console.error(`Error fetching ${method} from GenLayer RPC`);
1207
1261
  throw err;
1208
1262
  }
1209
1263
  }
@@ -1,3 +1,3 @@
1
1
  export { Account, Address } from 'viem';
2
- export { d as CalldataAddress, C as CalldataEncodable, i as ContractMethod, h as ContractMethodBase, f as ContractParamsArraySchemaElement, g as ContractParamsSchema, j as ContractSchema, a as DecodedCallData, D as DecodedDeployData, G as GenLayerClient, e as GenLayerMethod, b as GenLayerRawTransaction, c as GenLayerTransaction, H as Hash, M as MethodDescription, N as Network, S as SnapSource, k as TransactionHash, s as TransactionHashVariant, m as TransactionResult, p as TransactionResultNameToNumber, l as TransactionStatus, r as TransactionType, V as VoteType, o as transactionResultNumberToName, n as transactionsStatusNameToNumber, t as transactionsStatusNumberToName, q as voteTypeNameToNumber, v as voteTypeNumberToName } from '../index-DrEvzYFA.cjs';
2
+ export { d as CalldataAddress, C as CalldataEncodable, i as ContractMethod, h as ContractMethodBase, f as ContractParamsArraySchemaElement, g as ContractParamsSchema, j as ContractSchema, a as DecodedCallData, D as DecodedDeployData, G as GenLayerClient, e as GenLayerMethod, b as GenLayerRawTransaction, c as GenLayerTransaction, H as Hash, M as MethodDescription, N as Network, S as SnapSource, k as TransactionHash, s as TransactionHashVariant, m as TransactionResult, p as TransactionResultNameToNumber, l as TransactionStatus, r as TransactionType, V as VoteType, o as transactionResultNumberToName, n as transactionsStatusNameToNumber, t as transactionsStatusNumberToName, q as voteTypeNameToNumber, v as voteTypeNumberToName } from '../index-CxNl4CK5.cjs';
3
3
  export { G as GenLayerChain } from '../chains-BYSCF33g.cjs';
@@ -1,3 +1,3 @@
1
1
  export { Account, Address } from 'viem';
2
- export { d as CalldataAddress, C as CalldataEncodable, i as ContractMethod, h as ContractMethodBase, f as ContractParamsArraySchemaElement, g as ContractParamsSchema, j as ContractSchema, a as DecodedCallData, D as DecodedDeployData, G as GenLayerClient, e as GenLayerMethod, b as GenLayerRawTransaction, c as GenLayerTransaction, H as Hash, M as MethodDescription, N as Network, S as SnapSource, k as TransactionHash, s as TransactionHashVariant, m as TransactionResult, p as TransactionResultNameToNumber, l as TransactionStatus, r as TransactionType, V as VoteType, o as transactionResultNumberToName, n as transactionsStatusNameToNumber, t as transactionsStatusNumberToName, q as voteTypeNameToNumber, v as voteTypeNumberToName } from '../index-BIyk5Fv7.js';
2
+ export { d as CalldataAddress, C as CalldataEncodable, i as ContractMethod, h as ContractMethodBase, f as ContractParamsArraySchemaElement, g as ContractParamsSchema, j as ContractSchema, a as DecodedCallData, D as DecodedDeployData, G as GenLayerClient, e as GenLayerMethod, b as GenLayerRawTransaction, c as GenLayerTransaction, H as Hash, M as MethodDescription, N as Network, S as SnapSource, k as TransactionHash, s as TransactionHashVariant, m as TransactionResult, p as TransactionResultNameToNumber, l as TransactionStatus, r as TransactionType, V as VoteType, o as transactionResultNumberToName, n as transactionsStatusNameToNumber, t as transactionsStatusNumberToName, q as voteTypeNameToNumber, v as voteTypeNumberToName } from '../index-BnFbqUhu.js';
3
3
  export { G as GenLayerChain } from '../chains-BYSCF33g.js';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "genlayer-js",
3
3
  "type": "module",
4
- "version": "0.17.0",
4
+ "version": "0.18.0",
5
5
  "description": "GenLayer JavaScript SDK",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -70,12 +70,12 @@ const getCustomTransportConfig = (config: ClientConfig) => {
70
70
  const data = await response.json();
71
71
 
72
72
  if (data.error) {
73
- throw new Error(data.error.message);
73
+ throw data.error
74
74
  }
75
75
 
76
76
  return data.result;
77
77
  } catch (err) {
78
- console.error(`Error fetching ${method} from GenLayer RPC:`, err);
78
+ console.error(`Error fetching ${method} from GenLayer RPC`);
79
79
  throw err;
80
80
  }
81
81
  }
@@ -11,7 +11,7 @@ import {
11
11
  TransactionHashVariant,
12
12
  } from "@/types";
13
13
  import {fromHex, toHex, zeroAddress, encodeFunctionData, PublicClient, parseEventLogs} from "viem";
14
- import {b64ToArray} from "@/utils/jsonifier";
14
+ import {toJsonSafeDeep, b64ToArray} from "@/utils/jsonifier";
15
15
 
16
16
  export const contractActions = (client: GenLayerClient<GenLayerChain>, publicClient: PublicClient) => {
17
17
  return {
@@ -53,6 +53,7 @@ export const contractActions = (client: GenLayerClient<GenLayerChain>, publicCli
53
53
  args?: CalldataEncodable[];
54
54
  kwargs?: Map<string, CalldataEncodable> | {[key: string]: CalldataEncodable};
55
55
  rawReturn?: RawReturn;
56
+ jsonSafeReturn?: boolean;
56
57
  leaderOnly?: boolean;
57
58
  transactionHashVariant?: TransactionHashVariant;
58
59
  }): Promise<RawReturn extends true ? `0x${string}` : CalldataEncodable> => {
@@ -62,6 +63,7 @@ export const contractActions = (client: GenLayerClient<GenLayerChain>, publicCli
62
63
  functionName,
63
64
  args: callArgs,
64
65
  kwargs,
66
+ jsonSafeReturn = false,
65
67
  leaderOnly = false,
66
68
  transactionHashVariant = TransactionHashVariant.LATEST_NONFINAL,
67
69
  } = args;
@@ -88,7 +90,12 @@ export const contractActions = (client: GenLayerClient<GenLayerChain>, publicCli
88
90
  return prefixedResult;
89
91
  }
90
92
  const resultBinary = fromHex(prefixedResult, "bytes");
91
- return calldata.decode(resultBinary) as any;
93
+ const decoded = calldata.decode(resultBinary) as any;
94
+ if (!jsonSafeReturn) {
95
+ return decoded;
96
+ }
97
+ // If jsonSafeReturn is requested, convert to JSON-safe recursively
98
+ return toJsonSafeDeep(decoded) as any;
92
99
  },
93
100
  simulateWriteContract: async <RawReturn extends boolean | undefined>(args: {
94
101
  account?: Account;
@@ -47,6 +47,7 @@ export type GenLayerClient<TGenLayerChain extends GenLayerChain> = Omit<
47
47
  args?: CalldataEncodable[];
48
48
  kwargs?: Map<string, CalldataEncodable> | {[key: string]: CalldataEncodable};
49
49
  rawReturn?: RawReturn;
50
+ jsonSafeReturn?: boolean;
50
51
  transactionHashVariant?: TransactionHashVariant;
51
52
  }) => Promise<RawReturn extends true ? `0x${string}` : CalldataEncodable>;
52
53
  writeContract: (args: {
@@ -1,4 +1,7 @@
1
1
  import {calldata} from "@/abi";
2
+ import type {CalldataEncodable} from "@/types/calldata";
3
+ import {CalldataAddress} from "@/types/calldata";
4
+ import {toHex} from "viem";
2
5
 
3
6
 
4
7
  export function b64ToArray(b64: string): Uint8Array {
@@ -44,4 +47,73 @@ export function resultToUserFriendlyJson(cd64: string): any {
44
47
  status,
45
48
  payload,
46
49
  };
50
+ }
51
+
52
+ // Deeply converts CalldataEncodable values into JSON-serializable structures.
53
+ // Rules:
54
+ // - bigint: to number if safe, otherwise to decimal string
55
+ // - Uint8Array: to 0x-prefixed hex string
56
+ // - CalldataAddress: to 0x-prefixed hex string
57
+ // - Map: to Array<[key, value]> preserving order
58
+ // - Arrays and plain objects: converted recursively
59
+ export function toJsonSafeDeep<T extends CalldataEncodable>(value: T): any {
60
+ return _toJsonSafeDeep(value, new WeakSet());
61
+ }
62
+
63
+ function _toJsonSafeDeep(value: CalldataEncodable, seen: WeakSet<object>): any {
64
+ if (value === null || value === undefined) {
65
+ return null;
66
+ }
67
+
68
+ const primitiveType = typeof value;
69
+ if (primitiveType === "string" || primitiveType === "boolean" || primitiveType === "number") {
70
+ return value;
71
+ }
72
+
73
+ if (primitiveType === "bigint") {
74
+ const big = value as bigint;
75
+ const abs = big < 0n ? -big : big;
76
+ const maxSafe = 9007199254740991n; // Number.MAX_SAFE_INTEGER
77
+ return abs <= maxSafe ? Number(big) : big.toString();
78
+ }
79
+
80
+ // Objects and structured values
81
+ if (typeof value === "object") {
82
+ if (seen.has(value as object)) {
83
+ // Prevent potential cycles; represent as null
84
+ return null;
85
+ }
86
+ seen.add(value as object);
87
+
88
+ if (value instanceof Uint8Array) {
89
+ return toHex(value);
90
+ }
91
+
92
+ if (value instanceof Array) {
93
+ return value.map((v) => _toJsonSafeDeep(v as CalldataEncodable, seen));
94
+ }
95
+
96
+ if (value instanceof Map) {
97
+ const obj: Record<string, any> = {};
98
+ for (const [k, v] of value.entries()) {
99
+ obj[k] = _toJsonSafeDeep(v as CalldataEncodable, seen);
100
+ }
101
+ return obj;
102
+ }
103
+
104
+ if (value instanceof CalldataAddress) {
105
+ return toHex(value.bytes);
106
+ }
107
+
108
+ if (Object.getPrototypeOf(value) === Object.prototype) {
109
+ const obj: Record<string, any> = {};
110
+ for (const [k, v] of Object.entries(value)) {
111
+ obj[k] = _toJsonSafeDeep(v as CalldataEncodable, seen);
112
+ }
113
+ return obj;
114
+ }
115
+ }
116
+
117
+ // Fallback: return as-is (shouldn't normally reach here)
118
+ return value as any;
47
119
  }