genlayer-js 0.0.1 → 0.0.3

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 ADDED
@@ -0,0 +1,3 @@
1
+
2
+
3
+ ## 0.0.3 (2024-09-30)
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # [Your Project Name]
1
+ # GenLayerJS
2
2
 
3
3
  [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/license/mit/)
4
4
  [![Discord](https://dcbadge.vercel.app/api/server/8Jm4v89VAu?compact=true&style=flat)](https://discord.gg/VpfmXEMN66)
@@ -0,0 +1,7 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+ var _chunkBAGFDCQEcjs = require('../chunk-BAGFDCQE.cjs');
4
+ require('../chunk-75ZPJI57.cjs');
5
+
6
+
7
+ exports.simulator = _chunkBAGFDCQEcjs.simulator;
@@ -0,0 +1,3 @@
1
+ export { s as simulator } from '../index-DLN-2YXE.cjs';
2
+ import '../chains-CtZJFz4Q.cjs';
3
+ import 'viem';
@@ -0,0 +1,3 @@
1
+ export { s as simulator } from '../index-vHLY9ggt.js';
2
+ import '../chains-CtZJFz4Q.js';
3
+ import 'viem';
@@ -0,0 +1,7 @@
1
+ import {
2
+ simulator
3
+ } from "../chunk-D7VZGQ4K.js";
4
+ import "../chunk-MLKGABMK.js";
5
+ export {
6
+ simulator
7
+ };
@@ -0,0 +1,6 @@
1
+ import { Chain } from 'viem';
2
+
3
+ interface SimulatorChain extends Chain {
4
+ }
5
+
6
+ export type { SimulatorChain as S };
@@ -0,0 +1,6 @@
1
+ import { Chain } from 'viem';
2
+
3
+ interface SimulatorChain extends Chain {
4
+ }
5
+
6
+ export type { SimulatorChain as S };
@@ -0,0 +1,9 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+
8
+
9
+ exports.__export = __export;
@@ -0,0 +1,16 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/types/transactions.ts
2
+ var TransactionStatus = /* @__PURE__ */ ((TransactionStatus2) => {
3
+ TransactionStatus2["PENDING"] = "PENDING";
4
+ TransactionStatus2["CANCELED"] = "CANCELED";
5
+ TransactionStatus2["PROPOSING"] = "PROPOSING";
6
+ TransactionStatus2["COMMITTING"] = "COMMITTING";
7
+ TransactionStatus2["REVEALING"] = "REVEALING";
8
+ TransactionStatus2["ACCEPTED"] = "ACCEPTED";
9
+ TransactionStatus2["FINALIZED"] = "FINALIZED";
10
+ TransactionStatus2["UNDETERMINED"] = "UNDETERMINED";
11
+ return TransactionStatus2;
12
+ })(TransactionStatus || {});
13
+
14
+
15
+
16
+ exports.TransactionStatus = TransactionStatus;
@@ -0,0 +1,39 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+ var _chunk75ZPJI57cjs = require('./chunk-75ZPJI57.cjs');
4
+
5
+ // src/chains/index.ts
6
+ var chains_exports = {};
7
+ _chunk75ZPJI57cjs.__export.call(void 0, chains_exports, {
8
+ simulator: () => simulator
9
+ });
10
+
11
+ // src/chains/simulator.ts
12
+ var _viem = require('viem');
13
+ var SIMULATOR_JSON_RPC_URL = "http://127.0.0.1:4000/api";
14
+ var simulator = _viem.defineChain.call(void 0, {
15
+ id: 61999,
16
+ name: "GenLayer Simulator",
17
+ rpcUrls: {
18
+ default: {
19
+ http: [SIMULATOR_JSON_RPC_URL]
20
+ }
21
+ },
22
+ nativeCurrency: {
23
+ name: "GEN Token",
24
+ symbol: "GEN",
25
+ decimals: 18
26
+ },
27
+ blockExplorers: {
28
+ default: {
29
+ name: "GenLayer Explorer",
30
+ url: "http://127.0.0.1:4000/api"
31
+ }
32
+ },
33
+ testnet: true
34
+ });
35
+
36
+
37
+
38
+
39
+ exports.simulator = simulator; exports.chains_exports = chains_exports;
@@ -0,0 +1,39 @@
1
+ import {
2
+ __export
3
+ } from "./chunk-MLKGABMK.js";
4
+
5
+ // src/chains/index.ts
6
+ var chains_exports = {};
7
+ __export(chains_exports, {
8
+ simulator: () => simulator
9
+ });
10
+
11
+ // src/chains/simulator.ts
12
+ import { defineChain } from "viem";
13
+ var SIMULATOR_JSON_RPC_URL = "http://127.0.0.1:4000/api";
14
+ var simulator = defineChain({
15
+ id: 61999,
16
+ name: "GenLayer Simulator",
17
+ rpcUrls: {
18
+ default: {
19
+ http: [SIMULATOR_JSON_RPC_URL]
20
+ }
21
+ },
22
+ nativeCurrency: {
23
+ name: "GEN Token",
24
+ symbol: "GEN",
25
+ decimals: 18
26
+ },
27
+ blockExplorers: {
28
+ default: {
29
+ name: "GenLayer Explorer",
30
+ url: "http://127.0.0.1:4000/api"
31
+ }
32
+ },
33
+ testnet: true
34
+ });
35
+
36
+ export {
37
+ simulator,
38
+ chains_exports
39
+ };
@@ -0,0 +1,9 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ export {
8
+ __export
9
+ };
@@ -0,0 +1,16 @@
1
+ // src/types/transactions.ts
2
+ var TransactionStatus = /* @__PURE__ */ ((TransactionStatus2) => {
3
+ TransactionStatus2["PENDING"] = "PENDING";
4
+ TransactionStatus2["CANCELED"] = "CANCELED";
5
+ TransactionStatus2["PROPOSING"] = "PROPOSING";
6
+ TransactionStatus2["COMMITTING"] = "COMMITTING";
7
+ TransactionStatus2["REVEALING"] = "REVEALING";
8
+ TransactionStatus2["ACCEPTED"] = "ACCEPTED";
9
+ TransactionStatus2["FINALIZED"] = "FINALIZED";
10
+ TransactionStatus2["UNDETERMINED"] = "UNDETERMINED";
11
+ return TransactionStatus2;
12
+ })(TransactionStatus || {});
13
+
14
+ export {
15
+ TransactionStatus
16
+ };
@@ -0,0 +1,81 @@
1
+ import { Transport, Account, Client } from 'viem';
2
+ import { S as SimulatorChain } from './chains-CtZJFz4Q.js';
3
+
4
+ type Address = `0x${string}` & {
5
+ length: 42;
6
+ };
7
+
8
+ type TransactionHash = `0x${string}` & {
9
+ length: 66;
10
+ };
11
+ declare enum TransactionStatus {
12
+ PENDING = "PENDING",
13
+ CANCELED = "CANCELED",
14
+ PROPOSING = "PROPOSING",
15
+ COMMITTING = "COMMITTING",
16
+ REVEALING = "REVEALING",
17
+ ACCEPTED = "ACCEPTED",
18
+ FINALIZED = "FINALIZED",
19
+ UNDETERMINED = "UNDETERMINED"
20
+ }
21
+ type GenLayerTransaction = {
22
+ hash: TransactionHash;
23
+ status: TransactionStatus;
24
+ from_address?: string;
25
+ to_address?: string;
26
+ data?: Record<string, unknown>;
27
+ consensus_data?: Record<string, unknown>;
28
+ nonce?: number;
29
+ value?: number;
30
+ type?: number;
31
+ gaslimit?: bigint;
32
+ created_at?: Date;
33
+ r?: number;
34
+ s?: number;
35
+ v?: number;
36
+ };
37
+
38
+ type GenLayerMethod = {
39
+ method: "sim_fundAccount";
40
+ params: [address: string, amount: number];
41
+ } | {
42
+ method: "eth_getTransactionByHash";
43
+ params: [hash: TransactionHash];
44
+ } | {
45
+ method: "eth_call";
46
+ params: [requestParams: any, blockNumberOrHash: string];
47
+ } | {
48
+ method: "eth_sedRawTransaction";
49
+ params: [signedTransaction: string];
50
+ } | {
51
+ method: "gen_getContractSchema";
52
+ params: [address: string];
53
+ } | {
54
+ method: "gen_getContractSchemaForCode";
55
+ params: [contractCode: string];
56
+ };
57
+ type GenLayerClient<TTransport extends Transport, TSimulatorChain extends SimulatorChain, TAccount extends Account> = Client<TTransport, TSimulatorChain, TAccount> & {
58
+ request: Client<TTransport, TSimulatorChain, TAccount>["request"] & {
59
+ <TMethod extends GenLayerMethod>(args: Extract<GenLayerMethod, {
60
+ method: TMethod["method"];
61
+ }>): Promise<unknown>;
62
+ };
63
+ readContract: (args: {
64
+ account: Account;
65
+ address: Address;
66
+ functionName: string;
67
+ args: any[];
68
+ }) => Promise<any>;
69
+ writeContract: (args: {
70
+ account: Account;
71
+ address: Address;
72
+ functionName: string;
73
+ args: any[];
74
+ value: bigint;
75
+ }) => Promise<any>;
76
+ getTransaction: (args: {
77
+ hash: TransactionHash;
78
+ }) => Promise<GenLayerTransaction>;
79
+ };
80
+
81
+ export { type Address as A, type GenLayerClient as G, type TransactionHash as T, type GenLayerMethod as a, TransactionStatus as b, type GenLayerTransaction as c };
@@ -0,0 +1,81 @@
1
+ import { Transport, Account, Client } from 'viem';
2
+ import { S as SimulatorChain } from './chains-CtZJFz4Q.cjs';
3
+
4
+ type Address = `0x${string}` & {
5
+ length: 42;
6
+ };
7
+
8
+ type TransactionHash = `0x${string}` & {
9
+ length: 66;
10
+ };
11
+ declare enum TransactionStatus {
12
+ PENDING = "PENDING",
13
+ CANCELED = "CANCELED",
14
+ PROPOSING = "PROPOSING",
15
+ COMMITTING = "COMMITTING",
16
+ REVEALING = "REVEALING",
17
+ ACCEPTED = "ACCEPTED",
18
+ FINALIZED = "FINALIZED",
19
+ UNDETERMINED = "UNDETERMINED"
20
+ }
21
+ type GenLayerTransaction = {
22
+ hash: TransactionHash;
23
+ status: TransactionStatus;
24
+ from_address?: string;
25
+ to_address?: string;
26
+ data?: Record<string, unknown>;
27
+ consensus_data?: Record<string, unknown>;
28
+ nonce?: number;
29
+ value?: number;
30
+ type?: number;
31
+ gaslimit?: bigint;
32
+ created_at?: Date;
33
+ r?: number;
34
+ s?: number;
35
+ v?: number;
36
+ };
37
+
38
+ type GenLayerMethod = {
39
+ method: "sim_fundAccount";
40
+ params: [address: string, amount: number];
41
+ } | {
42
+ method: "eth_getTransactionByHash";
43
+ params: [hash: TransactionHash];
44
+ } | {
45
+ method: "eth_call";
46
+ params: [requestParams: any, blockNumberOrHash: string];
47
+ } | {
48
+ method: "eth_sedRawTransaction";
49
+ params: [signedTransaction: string];
50
+ } | {
51
+ method: "gen_getContractSchema";
52
+ params: [address: string];
53
+ } | {
54
+ method: "gen_getContractSchemaForCode";
55
+ params: [contractCode: string];
56
+ };
57
+ type GenLayerClient<TTransport extends Transport, TSimulatorChain extends SimulatorChain, TAccount extends Account> = Client<TTransport, TSimulatorChain, TAccount> & {
58
+ request: Client<TTransport, TSimulatorChain, TAccount>["request"] & {
59
+ <TMethod extends GenLayerMethod>(args: Extract<GenLayerMethod, {
60
+ method: TMethod["method"];
61
+ }>): Promise<unknown>;
62
+ };
63
+ readContract: (args: {
64
+ account: Account;
65
+ address: Address;
66
+ functionName: string;
67
+ args: any[];
68
+ }) => Promise<any>;
69
+ writeContract: (args: {
70
+ account: Account;
71
+ address: Address;
72
+ functionName: string;
73
+ args: any[];
74
+ value: bigint;
75
+ }) => Promise<any>;
76
+ getTransaction: (args: {
77
+ hash: TransactionHash;
78
+ }) => Promise<GenLayerTransaction>;
79
+ };
80
+
81
+ export { type Address as A, type GenLayerClient as G, type TransactionHash as T, type GenLayerMethod as a, TransactionStatus as b, type GenLayerTransaction as c };
@@ -0,0 +1,10 @@
1
+ import { S as SimulatorChain } from './chains-CtZJFz4Q.cjs';
2
+
3
+ declare const simulator: SimulatorChain;
4
+
5
+ declare const index_simulator: typeof simulator;
6
+ declare namespace index {
7
+ export { index_simulator as simulator };
8
+ }
9
+
10
+ export { index as i, simulator as s };
@@ -0,0 +1,10 @@
1
+ import { S as SimulatorChain } from './chains-CtZJFz4Q.js';
2
+
3
+ declare const simulator: SimulatorChain;
4
+
5
+ declare const index_simulator: typeof simulator;
6
+ declare namespace index {
7
+ export { index_simulator as simulator };
8
+ }
9
+
10
+ export { index as i, simulator as s };
package/dist/index.cjs ADDED
@@ -0,0 +1,161 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
+
3
+
4
+ var _chunkBAGFDCQEcjs = require('./chunk-BAGFDCQE.cjs');
5
+ require('./chunk-AR7OM4HI.cjs');
6
+ require('./chunk-75ZPJI57.cjs');
7
+
8
+ // src/client/client.ts
9
+ var _viem = require('viem');
10
+
11
+ // src/accounts/actions.ts
12
+ function accountActions(client) {
13
+ return {
14
+ fundAccount: async ({ address, amount }) => {
15
+ if (_optionalChain([client, 'access', _ => _.chain, 'optionalAccess', _2 => _2.id]) !== _chunkBAGFDCQEcjs.simulator.id) {
16
+ throw new Error("Client is not connected to the simulator");
17
+ }
18
+ return client.request({
19
+ method: "sim_fundAccount",
20
+ params: [address, amount]
21
+ });
22
+ }
23
+ };
24
+ }
25
+
26
+ // src/contracts/actions.ts
27
+
28
+ var contractActions = (client) => {
29
+ return {
30
+ getContractSchema: async (address) => {
31
+ const schema = await client.request({
32
+ method: "gen_getContractSchema",
33
+ params: [address]
34
+ });
35
+ return schema;
36
+ },
37
+ getContractSchemaForCode: async (contractCode) => {
38
+ const schema = await client.request({
39
+ method: "gen_getContractSchemaForCode",
40
+ params: [contractCode]
41
+ });
42
+ return schema;
43
+ }
44
+ };
45
+ };
46
+ var overrideContractActions = (client) => {
47
+ client.readContract = async (args) => {
48
+ const { account, address, functionName, args: params } = args;
49
+ const methodParamsAsString = JSON.stringify(params);
50
+ const data = [functionName, methodParamsAsString];
51
+ const encodedData = _viem.toRlp.call(void 0, data.map((param) => _viem.toHex.call(void 0, param)));
52
+ const requestParams = {
53
+ to: address,
54
+ from: _optionalChain([account, 'optionalAccess', _3 => _3.address]) || _optionalChain([client, 'access', _4 => _4.account, 'optionalAccess', _5 => _5.address]),
55
+ data: encodedData
56
+ };
57
+ const result = await client.request({
58
+ method: "eth_call",
59
+ params: [requestParams, "latest"]
60
+ });
61
+ return result;
62
+ };
63
+ client.writeContract = async (args) => {
64
+ const { account, address, functionName, args: params, value = 0n } = args;
65
+ const methodParamsAsString = JSON.stringify(params);
66
+ const data = [functionName, methodParamsAsString];
67
+ const encodedData = _viem.toRlp.call(void 0, data.map((param) => _viem.toHex.call(void 0, param)));
68
+ const senderAccount = account || client.account;
69
+ if (!senderAccount) {
70
+ throw new Error(
71
+ "No account set. Configure the client with an account or pass an account to this function."
72
+ );
73
+ }
74
+ if (!_optionalChain([senderAccount, 'optionalAccess', _6 => _6.signTransaction])) {
75
+ throw new Error("Account does not support signTransaction");
76
+ }
77
+ const signedTransaction = await senderAccount.signTransaction({
78
+ data: encodedData,
79
+ to: address,
80
+ value,
81
+ type: "legacy"
82
+ });
83
+ const result = await client.request({
84
+ method: "eth_sendRawTransaction",
85
+ params: [signedTransaction]
86
+ });
87
+ return result;
88
+ };
89
+ return client;
90
+ };
91
+
92
+ // src/config/transactions.ts
93
+ var transactionsConfig = {
94
+ waitInterval: 3e3,
95
+ retries: 10
96
+ };
97
+
98
+ // src/utils/async.ts
99
+ async function sleep(ms) {
100
+ return new Promise((resolve) => setTimeout(resolve, ms));
101
+ }
102
+
103
+ // src/transactions/actions.ts
104
+ var transactionActions = (client) => ({
105
+ waitForTransactionReceipt: async ({
106
+ hash,
107
+ status = "FINALIZED" /* FINALIZED */,
108
+ interval = transactionsConfig.waitInterval,
109
+ retries = transactionsConfig.retries
110
+ }) => {
111
+ const transaction = await client.getTransaction({ hash });
112
+ if (!transaction) {
113
+ throw new Error("Transaction not found");
114
+ }
115
+ if (transaction.status === status || status === "ACCEPTED" /* ACCEPTED */ && transaction.status === "FINALIZED" /* FINALIZED */) {
116
+ return transaction;
117
+ }
118
+ if (retries === 0) {
119
+ throw new Error("Transaction status is not " + status);
120
+ }
121
+ await sleep(interval);
122
+ return transactionActions(client).waitForTransactionReceipt({
123
+ hash,
124
+ status,
125
+ interval,
126
+ retries: retries - 1
127
+ });
128
+ }
129
+ });
130
+
131
+ // src/client/client.ts
132
+ var createClient = (config = { chain: _chunkBAGFDCQEcjs.simulator }) => {
133
+ const chainConfig = config.chain || _chunkBAGFDCQEcjs.simulator;
134
+ const rpcUrl = config.endpoint || chainConfig.rpcUrls.default.http[0];
135
+ const baseClient = _viem.createClient.call(void 0, {
136
+ chain: chainConfig,
137
+ transport: _viem.http.call(void 0, rpcUrl),
138
+ ...config.account ? { account: config.account } : {}
139
+ }).extend(_viem.publicActions).extend((client) => accountActions(client)).extend(
140
+ (client) => transactionActions(client)
141
+ ).extend(
142
+ (client) => contractActions(client)
143
+ );
144
+ const genLayerClient = overrideContractActions(
145
+ baseClient
146
+ );
147
+ return genLayerClient;
148
+ };
149
+
150
+ // src/accounts/account.ts
151
+ var _accounts = require('viem/accounts');
152
+ var createAccount = () => {
153
+ const privateKey = _accounts.generatePrivateKey.call(void 0, );
154
+ const account = _accounts.privateKeyToAccount.call(void 0, privateKey);
155
+ return account;
156
+ };
157
+
158
+
159
+
160
+
161
+ exports.chains = _chunkBAGFDCQEcjs.chains_exports; exports.createAccount = createAccount; exports.createClient = createClient;
@@ -0,0 +1,55 @@
1
+ import * as viem from 'viem';
2
+ import { Transport, Account } from 'viem';
3
+ import { S as SimulatorChain } from './chains-CtZJFz4Q.cjs';
4
+ import { G as GenLayerClient } from './clients-CL75M8Ma.cjs';
5
+ import * as abitype from 'abitype';
6
+ import * as viem__types_experimental_eip7702_types_authorization from 'viem/_types/experimental/eip7702/types/authorization';
7
+ import * as viem_accounts from 'viem/accounts';
8
+ export { i as chains } from './index-DLN-2YXE.cjs';
9
+
10
+ interface ClientConfig {
11
+ chain?: {
12
+ id: number;
13
+ name: string;
14
+ rpcUrls: {
15
+ default: {
16
+ http: readonly string[];
17
+ };
18
+ };
19
+ nativeCurrency: {
20
+ name: string;
21
+ symbol: string;
22
+ decimals: number;
23
+ };
24
+ blockExplorers?: {
25
+ default: {
26
+ name: string;
27
+ url: string;
28
+ };
29
+ };
30
+ };
31
+ endpoint?: string;
32
+ account?: Account;
33
+ }
34
+ declare const createClient: (config?: ClientConfig) => GenLayerClient<Transport, SimulatorChain, Account>;
35
+
36
+ declare const createAccount: () => {
37
+ address: viem_accounts.Address;
38
+ nonceManager?: viem_accounts.NonceManager | undefined;
39
+ sign: (parameters: {
40
+ hash: viem.Hash;
41
+ }) => Promise<viem.Hex>;
42
+ experimental_signAuthorization: (parameters: viem__types_experimental_eip7702_types_authorization.Authorization) => Promise<viem_accounts.SignAuthorizationReturnType>;
43
+ signMessage: ({ message }: {
44
+ message: viem.SignableMessage;
45
+ }) => Promise<viem.Hex>;
46
+ signTransaction: <serializer extends viem.SerializeTransactionFn<viem.TransactionSerializable> = viem.SerializeTransactionFn<viem.TransactionSerializable>, transaction extends Parameters<serializer>[0] = Parameters<serializer>[0]>(transaction: transaction, options?: {
47
+ serializer?: serializer | undefined;
48
+ } | undefined) => Promise<viem.IsNarrowable<viem.TransactionSerialized<viem.GetTransactionType<transaction>>, viem.Hex> extends true ? viem.TransactionSerialized<viem.GetTransactionType<transaction>> : viem.Hex>;
49
+ signTypedData: <const typedData extends abitype.TypedData | Record<string, unknown>, primaryType extends keyof typedData | "EIP712Domain" = keyof typedData>(parameters: viem.TypedDataDefinition<typedData, primaryType>) => Promise<viem.Hex>;
50
+ publicKey: viem.Hex;
51
+ source: "privateKey";
52
+ type: "local";
53
+ };
54
+
55
+ export { createAccount, createClient };
@@ -0,0 +1,55 @@
1
+ import * as viem from 'viem';
2
+ import { Transport, Account } from 'viem';
3
+ import { S as SimulatorChain } from './chains-CtZJFz4Q.js';
4
+ import { G as GenLayerClient } from './clients-Bt_8fb_v.js';
5
+ import * as abitype from 'abitype';
6
+ import * as viem__types_experimental_eip7702_types_authorization from 'viem/_types/experimental/eip7702/types/authorization';
7
+ import * as viem_accounts from 'viem/accounts';
8
+ export { i as chains } from './index-vHLY9ggt.js';
9
+
10
+ interface ClientConfig {
11
+ chain?: {
12
+ id: number;
13
+ name: string;
14
+ rpcUrls: {
15
+ default: {
16
+ http: readonly string[];
17
+ };
18
+ };
19
+ nativeCurrency: {
20
+ name: string;
21
+ symbol: string;
22
+ decimals: number;
23
+ };
24
+ blockExplorers?: {
25
+ default: {
26
+ name: string;
27
+ url: string;
28
+ };
29
+ };
30
+ };
31
+ endpoint?: string;
32
+ account?: Account;
33
+ }
34
+ declare const createClient: (config?: ClientConfig) => GenLayerClient<Transport, SimulatorChain, Account>;
35
+
36
+ declare const createAccount: () => {
37
+ address: viem_accounts.Address;
38
+ nonceManager?: viem_accounts.NonceManager | undefined;
39
+ sign: (parameters: {
40
+ hash: viem.Hash;
41
+ }) => Promise<viem.Hex>;
42
+ experimental_signAuthorization: (parameters: viem__types_experimental_eip7702_types_authorization.Authorization) => Promise<viem_accounts.SignAuthorizationReturnType>;
43
+ signMessage: ({ message }: {
44
+ message: viem.SignableMessage;
45
+ }) => Promise<viem.Hex>;
46
+ signTransaction: <serializer extends viem.SerializeTransactionFn<viem.TransactionSerializable> = viem.SerializeTransactionFn<viem.TransactionSerializable>, transaction extends Parameters<serializer>[0] = Parameters<serializer>[0]>(transaction: transaction, options?: {
47
+ serializer?: serializer | undefined;
48
+ } | undefined) => Promise<viem.IsNarrowable<viem.TransactionSerialized<viem.GetTransactionType<transaction>>, viem.Hex> extends true ? viem.TransactionSerialized<viem.GetTransactionType<transaction>> : viem.Hex>;
49
+ signTypedData: <const typedData extends abitype.TypedData | Record<string, unknown>, primaryType extends keyof typedData | "EIP712Domain" = keyof typedData>(parameters: viem.TypedDataDefinition<typedData, primaryType>) => Promise<viem.Hex>;
50
+ publicKey: viem.Hex;
51
+ source: "privateKey";
52
+ type: "local";
53
+ };
54
+
55
+ export { createAccount, createClient };
package/dist/index.js ADDED
@@ -0,0 +1,161 @@
1
+ import {
2
+ chains_exports,
3
+ simulator
4
+ } from "./chunk-D7VZGQ4K.js";
5
+ import "./chunk-VQTD5PMI.js";
6
+ import "./chunk-MLKGABMK.js";
7
+
8
+ // src/client/client.ts
9
+ import { createClient as createViemClient, http, publicActions } from "viem";
10
+
11
+ // src/accounts/actions.ts
12
+ function accountActions(client) {
13
+ return {
14
+ fundAccount: async ({ address, amount }) => {
15
+ if (client.chain?.id !== simulator.id) {
16
+ throw new Error("Client is not connected to the simulator");
17
+ }
18
+ return client.request({
19
+ method: "sim_fundAccount",
20
+ params: [address, amount]
21
+ });
22
+ }
23
+ };
24
+ }
25
+
26
+ // src/contracts/actions.ts
27
+ import { toRlp, toHex } from "viem";
28
+ var contractActions = (client) => {
29
+ return {
30
+ getContractSchema: async (address) => {
31
+ const schema = await client.request({
32
+ method: "gen_getContractSchema",
33
+ params: [address]
34
+ });
35
+ return schema;
36
+ },
37
+ getContractSchemaForCode: async (contractCode) => {
38
+ const schema = await client.request({
39
+ method: "gen_getContractSchemaForCode",
40
+ params: [contractCode]
41
+ });
42
+ return schema;
43
+ }
44
+ };
45
+ };
46
+ var overrideContractActions = (client) => {
47
+ client.readContract = async (args) => {
48
+ const { account, address, functionName, args: params } = args;
49
+ const methodParamsAsString = JSON.stringify(params);
50
+ const data = [functionName, methodParamsAsString];
51
+ const encodedData = toRlp(data.map((param) => toHex(param)));
52
+ const requestParams = {
53
+ to: address,
54
+ from: account?.address || client.account?.address,
55
+ data: encodedData
56
+ };
57
+ const result = await client.request({
58
+ method: "eth_call",
59
+ params: [requestParams, "latest"]
60
+ });
61
+ return result;
62
+ };
63
+ client.writeContract = async (args) => {
64
+ const { account, address, functionName, args: params, value = 0n } = args;
65
+ const methodParamsAsString = JSON.stringify(params);
66
+ const data = [functionName, methodParamsAsString];
67
+ const encodedData = toRlp(data.map((param) => toHex(param)));
68
+ const senderAccount = account || client.account;
69
+ if (!senderAccount) {
70
+ throw new Error(
71
+ "No account set. Configure the client with an account or pass an account to this function."
72
+ );
73
+ }
74
+ if (!senderAccount?.signTransaction) {
75
+ throw new Error("Account does not support signTransaction");
76
+ }
77
+ const signedTransaction = await senderAccount.signTransaction({
78
+ data: encodedData,
79
+ to: address,
80
+ value,
81
+ type: "legacy"
82
+ });
83
+ const result = await client.request({
84
+ method: "eth_sendRawTransaction",
85
+ params: [signedTransaction]
86
+ });
87
+ return result;
88
+ };
89
+ return client;
90
+ };
91
+
92
+ // src/config/transactions.ts
93
+ var transactionsConfig = {
94
+ waitInterval: 3e3,
95
+ retries: 10
96
+ };
97
+
98
+ // src/utils/async.ts
99
+ async function sleep(ms) {
100
+ return new Promise((resolve) => setTimeout(resolve, ms));
101
+ }
102
+
103
+ // src/transactions/actions.ts
104
+ var transactionActions = (client) => ({
105
+ waitForTransactionReceipt: async ({
106
+ hash,
107
+ status = "FINALIZED" /* FINALIZED */,
108
+ interval = transactionsConfig.waitInterval,
109
+ retries = transactionsConfig.retries
110
+ }) => {
111
+ const transaction = await client.getTransaction({ hash });
112
+ if (!transaction) {
113
+ throw new Error("Transaction not found");
114
+ }
115
+ if (transaction.status === status || status === "ACCEPTED" /* ACCEPTED */ && transaction.status === "FINALIZED" /* FINALIZED */) {
116
+ return transaction;
117
+ }
118
+ if (retries === 0) {
119
+ throw new Error("Transaction status is not " + status);
120
+ }
121
+ await sleep(interval);
122
+ return transactionActions(client).waitForTransactionReceipt({
123
+ hash,
124
+ status,
125
+ interval,
126
+ retries: retries - 1
127
+ });
128
+ }
129
+ });
130
+
131
+ // src/client/client.ts
132
+ var createClient = (config = { chain: simulator }) => {
133
+ const chainConfig = config.chain || simulator;
134
+ const rpcUrl = config.endpoint || chainConfig.rpcUrls.default.http[0];
135
+ const baseClient = createViemClient({
136
+ chain: chainConfig,
137
+ transport: http(rpcUrl),
138
+ ...config.account ? { account: config.account } : {}
139
+ }).extend(publicActions).extend((client) => accountActions(client)).extend(
140
+ (client) => transactionActions(client)
141
+ ).extend(
142
+ (client) => contractActions(client)
143
+ );
144
+ const genLayerClient = overrideContractActions(
145
+ baseClient
146
+ );
147
+ return genLayerClient;
148
+ };
149
+
150
+ // src/accounts/account.ts
151
+ import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
152
+ var createAccount = () => {
153
+ const privateKey = generatePrivateKey();
154
+ const account = privateKeyToAccount(privateKey);
155
+ return account;
156
+ };
157
+ export {
158
+ chains_exports as chains,
159
+ createAccount,
160
+ createClient
161
+ };
@@ -0,0 +1,11 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+ var _chunkAR7OM4HIcjs = require('../chunk-AR7OM4HI.cjs');
4
+ require('../chunk-75ZPJI57.cjs');
5
+
6
+ // src/types/accounts.ts
7
+ var _viem = require('viem');
8
+
9
+
10
+
11
+ exports.Account = _viem.Account; exports.TransactionStatus = _chunkAR7OM4HIcjs.TransactionStatus;
@@ -0,0 +1,21 @@
1
+ export { A as Address, G as GenLayerClient, a as GenLayerMethod, c as GenLayerTransaction, T as TransactionHash, b as TransactionStatus } from '../clients-CL75M8Ma.cjs';
2
+ export { S as SimulatorChain } from '../chains-CtZJFz4Q.cjs';
3
+ export { Account } from 'viem';
4
+
5
+ type ContractSchema = {
6
+ abi: Array<{
7
+ inputs?: Array<{
8
+ name: string;
9
+ type: string;
10
+ }>;
11
+ name?: string;
12
+ outputs?: Array<{
13
+ name: string;
14
+ type: string;
15
+ }>;
16
+ type: "constructor" | "function";
17
+ }>;
18
+ class: string;
19
+ };
20
+
21
+ export type { ContractSchema };
@@ -0,0 +1,21 @@
1
+ export { A as Address, G as GenLayerClient, a as GenLayerMethod, c as GenLayerTransaction, T as TransactionHash, b as TransactionStatus } from '../clients-Bt_8fb_v.js';
2
+ export { S as SimulatorChain } from '../chains-CtZJFz4Q.js';
3
+ export { Account } from 'viem';
4
+
5
+ type ContractSchema = {
6
+ abi: Array<{
7
+ inputs?: Array<{
8
+ name: string;
9
+ type: string;
10
+ }>;
11
+ name?: string;
12
+ outputs?: Array<{
13
+ name: string;
14
+ type: string;
15
+ }>;
16
+ type: "constructor" | "function";
17
+ }>;
18
+ class: string;
19
+ };
20
+
21
+ export type { ContractSchema };
@@ -0,0 +1,11 @@
1
+ import {
2
+ TransactionStatus
3
+ } from "../chunk-VQTD5PMI.js";
4
+ import "../chunk-MLKGABMK.js";
5
+
6
+ // src/types/accounts.ts
7
+ import { Account } from "viem";
8
+ export {
9
+ Account,
10
+ TransactionStatus
11
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "genlayer-js",
3
3
  "type": "module",
4
- "version": "0.0.1",
4
+ "version": "0.0.3",
5
5
  "description": "GenLayer JavaScript SDK",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -1,5 +1,5 @@
1
- import {GenLayerClient, TransactionHash, SimulatorChain} from "@/types";
2
- import {simulator} from "@/chains";
1
+ import {GenLayerClient, TransactionHash, SimulatorChain} from "../types";
2
+ import {simulator} from "../chains";
3
3
  import {Account, Transport} from "viem";
4
4
 
5
5
  export function accountActions<
@@ -4,7 +4,6 @@ import {accountActions} from "../accounts/actions";
4
4
  import {contractActions, overrideContractActions} from "../contracts/actions";
5
5
  import {transactionActions} from "../transactions/actions";
6
6
  import {GenLayerClient, SimulatorChain} from "@/types";
7
- import {createAccount} from "@/accounts/account";
8
7
 
9
8
  // Define the configuration interface for the client
10
9
  interface ClientConfig {
@@ -1 +1,2 @@
1
+ export {Account} from "viem";
1
2
  export type Address = `0x${string}` & {length: 42};
@@ -0,0 +1,11 @@
1
+ // tests/client.test.ts
2
+ import {createClient} from "../src/client/client";
3
+ import {simulator} from "../src/chains/simulator";
4
+
5
+ describe("Client Creation", () => {
6
+ it("should create a client for the simulator", () => {
7
+ const client = createClient({chain: simulator});
8
+ expect(client).toBeDefined();
9
+ expect(client.chain).toBe(simulator);
10
+ });
11
+ });