genlayer-js 0.7.0 → 0.9.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.9.0 (2025-03-12)
4
+
5
+
6
+ ### Features
7
+
8
+ * decoding params ([#77](https://github.com/yeagerai/genlayer-js/issues/77)) ([ada41ce](https://github.com/yeagerai/genlayer-js/commit/ada41ce375eaf6a94886bc50c13c3f0df247d7c2))
9
+
10
+ ## 0.8.0 (2025-03-12)
11
+
12
+
13
+ ### Features
14
+
15
+ * overwrite defaultConsensusMaxRotations ([#75](https://github.com/yeagerai/genlayer-js/issues/75)) ([8d64b42](https://github.com/yeagerai/genlayer-js/commit/8d64b428d8232394cfe1ac5b56edba7c1837d0e5))
16
+
3
17
  ## 0.7.0 (2025-03-05)
4
18
 
5
19
 
@@ -51,6 +51,7 @@ type GenLayerTransaction = {
51
51
  node_config: Record<string, unknown>;
52
52
  pending_transactions: unknown[];
53
53
  vote: string;
54
+ result: string;
54
55
  };
55
56
  validators?: Record<string, unknown>[];
56
57
  votes?: Record<string, string>;
@@ -153,6 +154,7 @@ type GenLayerClient<TSimulatorChain extends SimulatorChain> = Omit<Client<Transp
153
154
  };
154
155
  value: bigint;
155
156
  leaderOnly?: boolean;
157
+ consensusMaxRotations?: number;
156
158
  }) => Promise<any>;
157
159
  deployContract: (args: {
158
160
  account?: Account;
@@ -162,6 +164,7 @@ type GenLayerClient<TSimulatorChain extends SimulatorChain> = Omit<Client<Transp
162
164
  [key: string]: CalldataEncodable;
163
165
  };
164
166
  leaderOnly?: boolean;
167
+ consensusMaxRotations?: number;
165
168
  }) => Promise<`0x${string}`>;
166
169
  getTransaction: (args: {
167
170
  hash: TransactionHash;
@@ -51,6 +51,7 @@ type GenLayerTransaction = {
51
51
  node_config: Record<string, unknown>;
52
52
  pending_transactions: unknown[];
53
53
  vote: string;
54
+ result: string;
54
55
  };
55
56
  validators?: Record<string, unknown>[];
56
57
  votes?: Record<string, string>;
@@ -153,6 +154,7 @@ type GenLayerClient<TSimulatorChain extends SimulatorChain> = Omit<Client<Transp
153
154
  };
154
155
  value: bigint;
155
156
  leaderOnly?: boolean;
157
+ consensusMaxRotations?: number;
156
158
  }) => Promise<any>;
157
159
  deployContract: (args: {
158
160
  account?: Account;
@@ -162,6 +164,7 @@ type GenLayerClient<TSimulatorChain extends SimulatorChain> = Omit<Client<Transp
162
164
  [key: string]: CalldataEncodable;
163
165
  };
164
166
  leaderOnly?: boolean;
167
+ consensusMaxRotations?: number;
165
168
  }) => Promise<`0x${string}`>;
166
169
  getTransaction: (args: {
167
170
  hash: TransactionHash;
package/dist/index.cjs CHANGED
@@ -440,18 +440,18 @@ var overrideContractActions = (client) => {
440
440
  return decode(resultBinary);
441
441
  };
442
442
  client.writeContract = async (args) => {
443
- const { account, address, functionName, args: callArgs, kwargs, value = 0n, leaderOnly = false } = args;
443
+ const { account, address, functionName, args: callArgs, kwargs, value = 0n, leaderOnly = false, consensusMaxRotations = client.chain.defaultConsensusMaxRotations } = args;
444
444
  const data = [encode(makeCalldataObject(functionName, callArgs, kwargs)), leaderOnly];
445
445
  const serializedData = serialize(data);
446
- return _sendTransaction(address, serializedData, account || client.account, value);
446
+ return _sendTransaction(address, serializedData, account || client.account, consensusMaxRotations, value);
447
447
  };
448
448
  client.deployContract = async (args) => {
449
- const { account, code, args: constructorArgs, kwargs, leaderOnly = false } = args;
449
+ const { account, code, args: constructorArgs, kwargs, leaderOnly = false, consensusMaxRotations = client.chain.defaultConsensusMaxRotations } = args;
450
450
  const data = [code, encode(makeCalldataObject(void 0, constructorArgs, kwargs)), leaderOnly];
451
451
  const serializedData = serialize(data);
452
- return _sendTransaction(_viem.zeroAddress, serializedData, account || client.account);
452
+ return _sendTransaction(_viem.zeroAddress, serializedData, account || client.account, consensusMaxRotations);
453
453
  };
454
- const _sendTransaction = async (recipient, data, senderAccount, value) => {
454
+ const _sendTransaction = async (recipient, data, senderAccount, consensusMaxRotations, value) => {
455
455
  if (!senderAccount) {
456
456
  throw new Error(
457
457
  "No account set. Configure the client with an account or pass an account to this function."
@@ -469,7 +469,7 @@ var overrideContractActions = (client) => {
469
469
  senderAccount.address,
470
470
  recipient,
471
471
  client.chain.defaultNumberOfInitialValidators,
472
- client.chain.defaultConsensusMaxRotations,
472
+ consensusMaxRotations,
473
473
  data
474
474
  ]
475
475
  });
@@ -514,6 +514,55 @@ async function sleep(ms) {
514
514
  return new Promise((resolve) => setTimeout(resolve, ms));
515
515
  }
516
516
 
517
+ // src/abi/index.ts
518
+ var abi_exports = {};
519
+ _chunk75ZPJI57cjs.__export.call(void 0, abi_exports, {
520
+ calldata: () => calldata,
521
+ transactions: () => transactions
522
+ });
523
+ var calldata = calldata_exports;
524
+ var transactions = transactions_exports;
525
+
526
+ // src/utils/jsonifier.ts
527
+ function b64ToArray(b64) {
528
+ return Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
529
+ }
530
+ function calldataToUserFriendlyJson(cd) {
531
+ return {
532
+ raw: Array.from(cd),
533
+ readable: calldata.toString(calldata.decode(cd))
534
+ };
535
+ }
536
+ var RESULT_CODES = /* @__PURE__ */ new Map([
537
+ [0, "return"],
538
+ [1, "rollback"],
539
+ [2, "contract_error"],
540
+ [3, "error"],
541
+ [4, "none"],
542
+ [5, "no_leaders"]
543
+ ]);
544
+ function resultToUserFriendlyJson(cd64) {
545
+ const raw = b64ToArray(cd64);
546
+ const code = RESULT_CODES.get(raw[0]);
547
+ let status;
548
+ let payload = null;
549
+ if (code === void 0) {
550
+ status = "<unknown>";
551
+ } else {
552
+ status = code;
553
+ if ([1, 2].includes(raw[0])) {
554
+ payload = new TextDecoder("utf-8").decode(raw.slice(1));
555
+ } else if (raw[0] == 0) {
556
+ payload = calldataToUserFriendlyJson(raw.slice(1));
557
+ }
558
+ }
559
+ return {
560
+ raw: cd64,
561
+ status,
562
+ payload
563
+ };
564
+ }
565
+
517
566
  // src/transactions/actions.ts
518
567
  var transactionActions = (client) => ({
519
568
  waitForTransactionReceipt: async ({
@@ -527,7 +576,7 @@ var transactionActions = (client) => ({
527
576
  throw new Error("Transaction not found");
528
577
  }
529
578
  if (transaction.status === status || status === "ACCEPTED" /* ACCEPTED */ && transaction.status === "FINALIZED" /* FINALIZED */) {
530
- return transaction;
579
+ return _decodeTransaction(transaction);
531
580
  }
532
581
  if (retries === 0) {
533
582
  throw new Error("Transaction status is not " + status);
@@ -541,6 +590,40 @@ var transactionActions = (client) => ({
541
590
  });
542
591
  }
543
592
  });
593
+ var _decodeTransaction = (tx) => {
594
+ if (!tx.data) return tx;
595
+ try {
596
+ const leaderReceipt = _optionalChain([tx, 'access', _19 => _19.consensus_data, 'optionalAccess', _20 => _20.leader_receipt]);
597
+ if (leaderReceipt) {
598
+ if (leaderReceipt.result) {
599
+ leaderReceipt.result = resultToUserFriendlyJson(leaderReceipt.result);
600
+ }
601
+ if (leaderReceipt.calldata) {
602
+ leaderReceipt.calldata = {
603
+ base64: leaderReceipt.calldata,
604
+ ...calldataToUserFriendlyJson(b64ToArray(leaderReceipt.calldata))
605
+ };
606
+ }
607
+ if (leaderReceipt.eq_outputs) {
608
+ leaderReceipt.eq_outputs = Object.fromEntries(
609
+ Object.entries(leaderReceipt.eq_outputs).map(([key, value]) => {
610
+ const decodedValue = new TextDecoder().decode(b64ToArray(String(value)));
611
+ return [key, resultToUserFriendlyJson(decodedValue)];
612
+ })
613
+ );
614
+ }
615
+ }
616
+ if (tx.data.calldata) {
617
+ tx.data.calldata = {
618
+ base64: tx.data.calldata,
619
+ ...calldataToUserFriendlyJson(b64ToArray(tx.data.calldata))
620
+ };
621
+ }
622
+ } catch (e) {
623
+ console.error("Error decoding transaction:", e);
624
+ }
625
+ return tx;
626
+ };
544
627
 
545
628
  // src/config/snapID.ts
546
629
  var snapID = {
@@ -569,7 +652,7 @@ var connect = async (client, network = "localnet", snapSource = "npm") => {
569
652
  chainName: selectedNetwork.name,
570
653
  rpcUrls: selectedNetwork.rpcUrls.default.http,
571
654
  nativeCurrency: selectedNetwork.nativeCurrency,
572
- blockExplorerUrls: [_optionalChain([selectedNetwork, 'access', _19 => _19.blockExplorers, 'optionalAccess', _20 => _20.default, 'access', _21 => _21.url])]
655
+ blockExplorerUrls: [_optionalChain([selectedNetwork, 'access', _21 => _21.blockExplorers, 'optionalAccess', _22 => _22.default, 'access', _23 => _23.url])]
573
656
  };
574
657
  const currentChainId = await window.ethereum.request({ method: "eth_chainId" });
575
658
  if (currentChainId !== chainIdHex) {
@@ -605,10 +688,10 @@ var metamaskClient = async (snapSource = "npm") => {
605
688
  }
606
689
  const isFlask = async () => {
607
690
  try {
608
- const clientVersion = await _optionalChain([window, 'access', _22 => _22.ethereum, 'optionalAccess', _23 => _23.request, 'call', _24 => _24({
691
+ const clientVersion = await _optionalChain([window, 'access', _24 => _24.ethereum, 'optionalAccess', _25 => _25.request, 'call', _26 => _26({
609
692
  method: "web3_clientVersion"
610
693
  })]);
611
- return _optionalChain([clientVersion, 'optionalAccess', _25 => _25.includes, 'call', _26 => _26("flask")]);
694
+ return _optionalChain([clientVersion, 'optionalAccess', _27 => _27.includes, 'call', _28 => _28("flask")]);
612
695
  } catch (error) {
613
696
  console.error("Error detecting Flask:", error);
614
697
  return false;
@@ -616,7 +699,7 @@ var metamaskClient = async (snapSource = "npm") => {
616
699
  };
617
700
  const installedSnaps = async () => {
618
701
  try {
619
- return await _optionalChain([window, 'access', _27 => _27.ethereum, 'optionalAccess', _28 => _28.request, 'call', _29 => _29({
702
+ return await _optionalChain([window, 'access', _29 => _29.ethereum, 'optionalAccess', _30 => _30.request, 'call', _31 => _31({
620
703
  method: "wallet_getSnaps"
621
704
  })]);
622
705
  } catch (error) {
@@ -651,10 +734,10 @@ function walletActions(client) {
651
734
  function chainActions(client) {
652
735
  return {
653
736
  initializeConsensusSmartContract: async (forceReset = false) => {
654
- if (_optionalChain([client, 'access', _30 => _30.chain, 'optionalAccess', _31 => _31.id]) !== _chunkI6HC44KDcjs.simulator.id) {
737
+ if (_optionalChain([client, 'access', _32 => _32.chain, 'optionalAccess', _33 => _33.id]) !== _chunkI6HC44KDcjs.simulator.id) {
655
738
  throw new Error("Client is not connected to the simulator");
656
739
  }
657
- if (!forceReset && _optionalChain([client, 'access', _32 => _32.chain, 'access', _33 => _33.consensusMainContract, 'optionalAccess', _34 => _34.address]) && _optionalChain([client, 'access', _35 => _35.chain, 'access', _36 => _36.consensusMainContract, 'optionalAccess', _37 => _37.abi])) {
740
+ if (!forceReset && _optionalChain([client, 'access', _34 => _34.chain, 'access', _35 => _35.consensusMainContract, 'optionalAccess', _36 => _36.address]) && _optionalChain([client, 'access', _37 => _37.chain, 'access', _38 => _38.consensusMainContract, 'optionalAccess', _39 => _39.abi])) {
658
741
  return;
659
742
  }
660
743
  const contractsResponse = await fetch(client.chain.rpcUrls.default.http[0], {
@@ -689,7 +772,7 @@ var createClient = (config = { chain: _chunkI6HC44KDcjs.localnet }) => {
689
772
  async request({ method, params }) {
690
773
  if (method.startsWith("eth_") && isAddress) {
691
774
  try {
692
- return await _optionalChain([window, 'access', _38 => _38.ethereum, 'optionalAccess', _39 => _39.request, 'call', _40 => _40({ method, params })]);
775
+ return await _optionalChain([window, 'access', _40 => _40.ethereum, 'optionalAccess', _41 => _41.request, 'call', _42 => _42({ method, params })]);
693
776
  } catch (err) {
694
777
  console.warn(`Error using window.ethereum for method ${method}:`, err);
695
778
  throw err;
@@ -740,15 +823,6 @@ var createAccount = (accountPrivateKey) => {
740
823
  return account;
741
824
  };
742
825
 
743
- // src/abi/index.ts
744
- var abi_exports = {};
745
- _chunk75ZPJI57cjs.__export.call(void 0, abi_exports, {
746
- calldata: () => calldata,
747
- transactions: () => transactions
748
- });
749
- var calldata = calldata_exports;
750
- var transactions = transactions_exports;
751
-
752
826
 
753
827
 
754
828
 
package/dist/index.d.cts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as viem from 'viem';
2
2
  import { Account, Address } from 'viem';
3
3
  import { S as SimulatorChain } from './chains-C5PI_Nr_.cjs';
4
- import { G as GenLayerClient, C as CalldataEncodable, T as TransactionDataElement } from './index-DJTO3-o3.cjs';
4
+ import { G as GenLayerClient, C as CalldataEncodable, T as TransactionDataElement } from './index-BCbofn6t.cjs';
5
5
  import * as abitype from 'abitype';
6
6
  import * as viem__types_experimental_eip7702_types_authorization from 'viem/_types/experimental/eip7702/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 } from 'viem';
3
3
  import { S as SimulatorChain } from './chains-C5PI_Nr_.js';
4
- import { G as GenLayerClient, C as CalldataEncodable, T as TransactionDataElement } from './index-CpcNUbXb.js';
4
+ import { G as GenLayerClient, C as CalldataEncodable, T as TransactionDataElement } from './index-Ctmshvtv.js';
5
5
  import * as abitype from 'abitype';
6
6
  import * as viem__types_experimental_eip7702_types_authorization from 'viem/_types/experimental/eip7702/types/authorization';
7
7
  import * as viem_accounts from 'viem/accounts';
package/dist/index.js CHANGED
@@ -440,18 +440,18 @@ var overrideContractActions = (client) => {
440
440
  return decode(resultBinary);
441
441
  };
442
442
  client.writeContract = async (args) => {
443
- const { account, address, functionName, args: callArgs, kwargs, value = 0n, leaderOnly = false } = args;
443
+ const { account, address, functionName, args: callArgs, kwargs, value = 0n, leaderOnly = false, consensusMaxRotations = client.chain.defaultConsensusMaxRotations } = args;
444
444
  const data = [encode(makeCalldataObject(functionName, callArgs, kwargs)), leaderOnly];
445
445
  const serializedData = serialize(data);
446
- return _sendTransaction(address, serializedData, account || client.account, value);
446
+ return _sendTransaction(address, serializedData, account || client.account, consensusMaxRotations, value);
447
447
  };
448
448
  client.deployContract = async (args) => {
449
- const { account, code, args: constructorArgs, kwargs, leaderOnly = false } = args;
449
+ const { account, code, args: constructorArgs, kwargs, leaderOnly = false, consensusMaxRotations = client.chain.defaultConsensusMaxRotations } = args;
450
450
  const data = [code, encode(makeCalldataObject(void 0, constructorArgs, kwargs)), leaderOnly];
451
451
  const serializedData = serialize(data);
452
- return _sendTransaction(zeroAddress, serializedData, account || client.account);
452
+ return _sendTransaction(zeroAddress, serializedData, account || client.account, consensusMaxRotations);
453
453
  };
454
- const _sendTransaction = async (recipient, data, senderAccount, value) => {
454
+ const _sendTransaction = async (recipient, data, senderAccount, consensusMaxRotations, value) => {
455
455
  if (!senderAccount) {
456
456
  throw new Error(
457
457
  "No account set. Configure the client with an account or pass an account to this function."
@@ -469,7 +469,7 @@ var overrideContractActions = (client) => {
469
469
  senderAccount.address,
470
470
  recipient,
471
471
  client.chain.defaultNumberOfInitialValidators,
472
- client.chain.defaultConsensusMaxRotations,
472
+ consensusMaxRotations,
473
473
  data
474
474
  ]
475
475
  });
@@ -514,6 +514,55 @@ async function sleep(ms) {
514
514
  return new Promise((resolve) => setTimeout(resolve, ms));
515
515
  }
516
516
 
517
+ // src/abi/index.ts
518
+ var abi_exports = {};
519
+ __export(abi_exports, {
520
+ calldata: () => calldata,
521
+ transactions: () => transactions
522
+ });
523
+ var calldata = calldata_exports;
524
+ var transactions = transactions_exports;
525
+
526
+ // src/utils/jsonifier.ts
527
+ function b64ToArray(b64) {
528
+ return Uint8Array.from(atob(b64), (c) => c.charCodeAt(0));
529
+ }
530
+ function calldataToUserFriendlyJson(cd) {
531
+ return {
532
+ raw: Array.from(cd),
533
+ readable: calldata.toString(calldata.decode(cd))
534
+ };
535
+ }
536
+ var RESULT_CODES = /* @__PURE__ */ new Map([
537
+ [0, "return"],
538
+ [1, "rollback"],
539
+ [2, "contract_error"],
540
+ [3, "error"],
541
+ [4, "none"],
542
+ [5, "no_leaders"]
543
+ ]);
544
+ function resultToUserFriendlyJson(cd64) {
545
+ const raw = b64ToArray(cd64);
546
+ const code = RESULT_CODES.get(raw[0]);
547
+ let status;
548
+ let payload = null;
549
+ if (code === void 0) {
550
+ status = "<unknown>";
551
+ } else {
552
+ status = code;
553
+ if ([1, 2].includes(raw[0])) {
554
+ payload = new TextDecoder("utf-8").decode(raw.slice(1));
555
+ } else if (raw[0] == 0) {
556
+ payload = calldataToUserFriendlyJson(raw.slice(1));
557
+ }
558
+ }
559
+ return {
560
+ raw: cd64,
561
+ status,
562
+ payload
563
+ };
564
+ }
565
+
517
566
  // src/transactions/actions.ts
518
567
  var transactionActions = (client) => ({
519
568
  waitForTransactionReceipt: async ({
@@ -527,7 +576,7 @@ var transactionActions = (client) => ({
527
576
  throw new Error("Transaction not found");
528
577
  }
529
578
  if (transaction.status === status || status === "ACCEPTED" /* ACCEPTED */ && transaction.status === "FINALIZED" /* FINALIZED */) {
530
- return transaction;
579
+ return _decodeTransaction(transaction);
531
580
  }
532
581
  if (retries === 0) {
533
582
  throw new Error("Transaction status is not " + status);
@@ -541,6 +590,40 @@ var transactionActions = (client) => ({
541
590
  });
542
591
  }
543
592
  });
593
+ var _decodeTransaction = (tx) => {
594
+ if (!tx.data) return tx;
595
+ try {
596
+ const leaderReceipt = tx.consensus_data?.leader_receipt;
597
+ if (leaderReceipt) {
598
+ if (leaderReceipt.result) {
599
+ leaderReceipt.result = resultToUserFriendlyJson(leaderReceipt.result);
600
+ }
601
+ if (leaderReceipt.calldata) {
602
+ leaderReceipt.calldata = {
603
+ base64: leaderReceipt.calldata,
604
+ ...calldataToUserFriendlyJson(b64ToArray(leaderReceipt.calldata))
605
+ };
606
+ }
607
+ if (leaderReceipt.eq_outputs) {
608
+ leaderReceipt.eq_outputs = Object.fromEntries(
609
+ Object.entries(leaderReceipt.eq_outputs).map(([key, value]) => {
610
+ const decodedValue = new TextDecoder().decode(b64ToArray(String(value)));
611
+ return [key, resultToUserFriendlyJson(decodedValue)];
612
+ })
613
+ );
614
+ }
615
+ }
616
+ if (tx.data.calldata) {
617
+ tx.data.calldata = {
618
+ base64: tx.data.calldata,
619
+ ...calldataToUserFriendlyJson(b64ToArray(tx.data.calldata))
620
+ };
621
+ }
622
+ } catch (e) {
623
+ console.error("Error decoding transaction:", e);
624
+ }
625
+ return tx;
626
+ };
544
627
 
545
628
  // src/config/snapID.ts
546
629
  var snapID = {
@@ -739,15 +822,6 @@ var createAccount = (accountPrivateKey) => {
739
822
  const account = privateKeyToAccount(privateKey);
740
823
  return account;
741
824
  };
742
-
743
- // src/abi/index.ts
744
- var abi_exports = {};
745
- __export(abi_exports, {
746
- calldata: () => calldata,
747
- transactions: () => transactions
748
- });
749
- var calldata = calldata_exports;
750
- var transactions = transactions_exports;
751
825
  export {
752
826
  abi_exports as abi,
753
827
  chains_exports as chains,
@@ -1,3 +1,3 @@
1
- export { A as Address, a as CalldataAddress, C as CalldataEncodable, f as ContractMethod, e as ContractMethodBase, c as ContractParamsArraySchemaElement, d as ContractParamsSchema, g as ContractSchema, G as GenLayerClient, b as GenLayerMethod, j as GenLayerTransaction, M as MethodDescription, N as Network, S as SnapSource, k as TransactionDataElement, h as TransactionHash, i as TransactionStatus } from '../index-DJTO3-o3.cjs';
1
+ export { A as Address, a as CalldataAddress, C as CalldataEncodable, f as ContractMethod, e as ContractMethodBase, c as ContractParamsArraySchemaElement, d as ContractParamsSchema, g as ContractSchema, G as GenLayerClient, b as GenLayerMethod, j as GenLayerTransaction, M as MethodDescription, N as Network, S as SnapSource, k as TransactionDataElement, h as TransactionHash, i as TransactionStatus } from '../index-BCbofn6t.cjs';
2
2
  export { S as SimulatorChain } from '../chains-C5PI_Nr_.cjs';
3
3
  export { Account } from 'viem';
@@ -1,3 +1,3 @@
1
- export { A as Address, a as CalldataAddress, C as CalldataEncodable, f as ContractMethod, e as ContractMethodBase, c as ContractParamsArraySchemaElement, d as ContractParamsSchema, g as ContractSchema, G as GenLayerClient, b as GenLayerMethod, j as GenLayerTransaction, M as MethodDescription, N as Network, S as SnapSource, k as TransactionDataElement, h as TransactionHash, i as TransactionStatus } from '../index-CpcNUbXb.js';
1
+ export { A as Address, a as CalldataAddress, C as CalldataEncodable, f as ContractMethod, e as ContractMethodBase, c as ContractParamsArraySchemaElement, d as ContractParamsSchema, g as ContractSchema, G as GenLayerClient, b as GenLayerMethod, j as GenLayerTransaction, M as MethodDescription, N as Network, S as SnapSource, k as TransactionDataElement, h as TransactionHash, i as TransactionStatus } from '../index-Ctmshvtv.js';
2
2
  export { S as SimulatorChain } from '../chains-C5PI_Nr_.js';
3
3
  export { Account } from 'viem';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "genlayer-js",
3
3
  "type": "module",
4
- "version": "0.7.0",
4
+ "version": "0.9.0",
5
5
  "description": "GenLayer JavaScript SDK",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -115,11 +115,12 @@ export const overrideContractActions = (client: GenLayerClient<SimulatorChain>)
115
115
  kwargs?: Map<string, CalldataEncodable> | {[key: string]: CalldataEncodable};
116
116
  value: bigint;
117
117
  leaderOnly?: boolean;
118
+ consensusMaxRotations?: number;
118
119
  }): Promise<`0x${string}`> => {
119
- const {account, address, functionName, args: callArgs, kwargs, value = 0n, leaderOnly = false} = args;
120
+ const {account, address, functionName, args: callArgs, kwargs, value = 0n, leaderOnly = false, consensusMaxRotations = client.chain.defaultConsensusMaxRotations} = args;
120
121
  const data = [calldata.encode(makeCalldataObject(functionName, callArgs, kwargs)), leaderOnly];
121
122
  const serializedData = serialize(data);
122
- return _sendTransaction(address, serializedData, account || client.account, value);
123
+ return _sendTransaction(address, serializedData, account || client.account, consensusMaxRotations, value);
123
124
  };
124
125
 
125
126
  client.deployContract = async (args: {
@@ -128,17 +129,19 @@ export const overrideContractActions = (client: GenLayerClient<SimulatorChain>)
128
129
  args?: CalldataEncodable[];
129
130
  kwargs?: Map<string, CalldataEncodable> | {[key: string]: CalldataEncodable};
130
131
  leaderOnly?: boolean;
132
+ consensusMaxRotations?: number;
131
133
  }) => {
132
- const {account, code, args: constructorArgs, kwargs, leaderOnly = false} = args;
134
+ const {account, code, args: constructorArgs, kwargs, leaderOnly = false, consensusMaxRotations = client.chain.defaultConsensusMaxRotations} = args;
133
135
  const data = [code, calldata.encode(makeCalldataObject(undefined, constructorArgs, kwargs)), leaderOnly];
134
136
  const serializedData = serialize(data);
135
- return _sendTransaction(zeroAddress, serializedData, account || client.account);
137
+ return _sendTransaction(zeroAddress, serializedData, account || client.account, consensusMaxRotations);
136
138
  };
137
139
 
138
140
  const _sendTransaction = async (
139
141
  recipient: `0x${string}`,
140
142
  data: `0x${string}`,
141
143
  senderAccount?: Account,
144
+ consensusMaxRotations?: number,
142
145
  value?: bigint,
143
146
  ) => {
144
147
  if (!senderAccount) {
@@ -160,7 +163,7 @@ export const overrideContractActions = (client: GenLayerClient<SimulatorChain>)
160
163
  senderAccount.address,
161
164
  recipient,
162
165
  client.chain.defaultNumberOfInitialValidators,
163
- client.chain.defaultConsensusMaxRotations,
166
+ consensusMaxRotations,
164
167
  data,
165
168
  ],
166
169
  });
@@ -3,6 +3,7 @@ import {TransactionHash, TransactionStatus, GenLayerTransaction} from "../types/
3
3
  import {transactionsConfig} from "../config/transactions";
4
4
  import {sleep} from "../utils/async";
5
5
  import {SimulatorChain} from "@/types";
6
+ import {b64ToArray, calldataToUserFriendlyJson, resultToUserFriendlyJson} from "@/utils/jsonifier";
6
7
 
7
8
  export const transactionActions = (client: GenLayerClient<SimulatorChain>) => ({
8
9
  waitForTransactionReceipt: async ({
@@ -26,7 +27,7 @@ export const transactionActions = (client: GenLayerClient<SimulatorChain>) => ({
26
27
  transaction.status === status ||
27
28
  (status === TransactionStatus.ACCEPTED && transaction.status === TransactionStatus.FINALIZED)
28
29
  ) {
29
- return transaction;
30
+ return _decodeTransaction(transaction);
30
31
  }
31
32
 
32
33
  if (retries === 0) {
@@ -42,3 +43,43 @@ export const transactionActions = (client: GenLayerClient<SimulatorChain>) => ({
42
43
  });
43
44
  },
44
45
  });
46
+
47
+ const _decodeTransaction = (tx: GenLayerTransaction): GenLayerTransaction => {
48
+ if (!tx.data) return tx;
49
+
50
+ try {
51
+ const leaderReceipt = tx.consensus_data?.leader_receipt;
52
+ if (leaderReceipt) {
53
+ if (leaderReceipt.result) {
54
+ leaderReceipt.result = resultToUserFriendlyJson(leaderReceipt.result);
55
+ }
56
+
57
+ if (leaderReceipt.calldata) {
58
+ leaderReceipt.calldata = {
59
+ base64: leaderReceipt.calldata,
60
+ ...calldataToUserFriendlyJson(b64ToArray(leaderReceipt.calldata)),
61
+ };
62
+ }
63
+
64
+ if (leaderReceipt.eq_outputs) {
65
+ leaderReceipt.eq_outputs = Object.fromEntries(
66
+ Object.entries(leaderReceipt.eq_outputs).map(([key, value]) => {
67
+ const decodedValue = new TextDecoder().decode(b64ToArray(String(value)));
68
+ return [key, resultToUserFriendlyJson(decodedValue)];
69
+ })
70
+ );
71
+ }
72
+ }
73
+
74
+ if (tx.data.calldata) {
75
+ tx.data.calldata = {
76
+ base64: tx.data.calldata as string,
77
+ ...calldataToUserFriendlyJson(b64ToArray(tx.data.calldata as string)),
78
+ };
79
+ }
80
+ } catch (e) {
81
+ console.error("Error decoding transaction:", e);
82
+ }
83
+
84
+ return tx;
85
+ }
@@ -55,6 +55,7 @@ export type GenLayerClient<TSimulatorChain extends SimulatorChain> = Omit<
55
55
  kwargs?: Map<string, CalldataEncodable> | {[key: string]: CalldataEncodable};
56
56
  value: bigint;
57
57
  leaderOnly?: boolean;
58
+ consensusMaxRotations?: number;
58
59
  }) => Promise<any>;
59
60
  deployContract: (args: {
60
61
  account?: Account;
@@ -62,6 +63,7 @@ export type GenLayerClient<TSimulatorChain extends SimulatorChain> = Omit<
62
63
  args?: CalldataEncodable[];
63
64
  kwargs?: Map<string, CalldataEncodable> | {[key: string]: CalldataEncodable};
64
65
  leaderOnly?: boolean;
66
+ consensusMaxRotations?: number;
65
67
  }) => Promise<`0x${string}`>;
66
68
  getTransaction: (args: {hash: TransactionHash}) => Promise<GenLayerTransaction>;
67
69
  getCurrentNonce: (args: {address: string}) => Promise<number>;
@@ -31,6 +31,7 @@ export type GenLayerTransaction = {
31
31
  node_config: Record<string, unknown>;
32
32
  pending_transactions: unknown[];
33
33
  vote: string;
34
+ result: string;
34
35
  };
35
36
  validators?: Record<string, unknown>[];
36
37
  votes?: Record<string, string>;
@@ -0,0 +1,47 @@
1
+ import {calldata} from "@/abi";
2
+
3
+
4
+ export function b64ToArray(b64: string): Uint8Array {
5
+ return Uint8Array.from(atob(b64 as string), (c) => c.charCodeAt(0));
6
+ }
7
+
8
+ export function calldataToUserFriendlyJson(cd: Uint8Array): any {
9
+ return {
10
+ raw: Array.from(cd),
11
+ readable: calldata.toString(calldata.decode(cd)),
12
+ };
13
+ }
14
+
15
+ const RESULT_CODES = new Map([
16
+ [0, 'return'],
17
+ [1, 'rollback'],
18
+ [2, 'contract_error'],
19
+ [3, 'error'],
20
+ [4, 'none'],
21
+ [5, 'no_leaders'],
22
+ ]);
23
+
24
+ export function resultToUserFriendlyJson(cd64: string): any {
25
+ const raw = b64ToArray(cd64);
26
+
27
+ const code = RESULT_CODES.get(raw[0]);
28
+ let status: string;
29
+ let payload: string | null = null;
30
+
31
+ if (code === undefined) {
32
+ status = '<unknown>';
33
+ } else {
34
+ status = code;
35
+ if ([1, 2].includes(raw[0])) {
36
+ payload = new TextDecoder('utf-8').decode(raw.slice(1));
37
+ } else if (raw[0] == 0) {
38
+ payload = calldataToUserFriendlyJson(raw.slice(1));
39
+ }
40
+ }
41
+
42
+ return {
43
+ raw: cd64,
44
+ status,
45
+ payload,
46
+ };
47
+ }