genlayer-js 0.2.0 → 0.3.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,12 @@
1
1
 
2
2
 
3
+ ## 0.3.0 (2024-10-25)
4
+
5
+
6
+ ### Features
7
+
8
+ * add deploy contract action ([#17](https://github.com/yeagerai/genlayer-js/issues/17)) ([23d5bc2](https://github.com/yeagerai/genlayer-js/commit/23d5bc28fb58c73d64b1fd629185a0565d84cb91))
9
+
3
10
  ## 0.2.0 (2024-10-17)
4
11
 
5
12
 
@@ -1,10 +1,26 @@
1
1
  import { Transport, Account, Client } from 'viem';
2
2
  import { S as SimulatorChain } from './chains-CtZJFz4Q.cjs';
3
3
 
4
- type Address = `0x${string}` & {
4
+ type Address$1 = `0x${string}` & {
5
5
  length: 42;
6
6
  };
7
7
 
8
+ declare class Address {
9
+ bytes: Uint8Array;
10
+ constructor(addr: Uint8Array);
11
+ }
12
+ type CalldataEncodable = null | boolean | Address | number | bigint | string | Uint8Array | Address | Array<CalldataEncodable> | Map<string, CalldataEncodable> | {
13
+ [key: string]: CalldataEncodable;
14
+ };
15
+ type MethodDescription = {
16
+ method: string;
17
+ args: Array<CalldataEncodable>;
18
+ };
19
+ type TransactionData = {
20
+ method: string;
21
+ args: CalldataEncodable[];
22
+ };
23
+
8
24
  type TransactionHash = `0x${string}` & {
9
25
  length: 66;
10
26
  };
@@ -66,17 +82,22 @@ type GenLayerClient<TTransport extends Transport, TSimulatorChain extends Simula
66
82
  };
67
83
  readContract: (args: {
68
84
  account: Account;
69
- address: Address;
85
+ address: Address$1;
70
86
  functionName: string;
71
87
  args: any[];
72
88
  }) => Promise<any>;
73
89
  writeContract: (args: {
74
90
  account: Account;
75
- address: Address;
91
+ address: Address$1;
76
92
  functionName: string;
77
93
  args: any[];
78
94
  value: bigint;
79
95
  }) => Promise<any>;
96
+ deployContract: (args: {
97
+ account: Account;
98
+ code: string;
99
+ args: CalldataEncodable[];
100
+ }) => Promise<any>;
80
101
  getTransaction: (args: {
81
102
  hash: TransactionHash;
82
103
  }) => Promise<GenLayerTransaction>;
@@ -85,4 +106,4 @@ type GenLayerClient<TTransport extends Transport, TSimulatorChain extends Simula
85
106
  }) => Promise<number>;
86
107
  };
87
108
 
88
- export { type Address as A, type GenLayerClient as G, type TransactionHash as T, type GenLayerMethod as a, TransactionStatus as b, type GenLayerTransaction as c, type TransactionDataElement as d };
109
+ export { type Address$1 as A, type CalldataEncodable as C, type GenLayerClient as G, type MethodDescription as M, type TransactionData as T, type GenLayerMethod as a, type TransactionHash as b, TransactionStatus as c, type GenLayerTransaction as d, type TransactionDataElement as e };
@@ -1,10 +1,26 @@
1
1
  import { Transport, Account, Client } from 'viem';
2
2
  import { S as SimulatorChain } from './chains-CtZJFz4Q.js';
3
3
 
4
- type Address = `0x${string}` & {
4
+ type Address$1 = `0x${string}` & {
5
5
  length: 42;
6
6
  };
7
7
 
8
+ declare class Address {
9
+ bytes: Uint8Array;
10
+ constructor(addr: Uint8Array);
11
+ }
12
+ type CalldataEncodable = null | boolean | Address | number | bigint | string | Uint8Array | Address | Array<CalldataEncodable> | Map<string, CalldataEncodable> | {
13
+ [key: string]: CalldataEncodable;
14
+ };
15
+ type MethodDescription = {
16
+ method: string;
17
+ args: Array<CalldataEncodable>;
18
+ };
19
+ type TransactionData = {
20
+ method: string;
21
+ args: CalldataEncodable[];
22
+ };
23
+
8
24
  type TransactionHash = `0x${string}` & {
9
25
  length: 66;
10
26
  };
@@ -66,17 +82,22 @@ type GenLayerClient<TTransport extends Transport, TSimulatorChain extends Simula
66
82
  };
67
83
  readContract: (args: {
68
84
  account: Account;
69
- address: Address;
85
+ address: Address$1;
70
86
  functionName: string;
71
87
  args: any[];
72
88
  }) => Promise<any>;
73
89
  writeContract: (args: {
74
90
  account: Account;
75
- address: Address;
91
+ address: Address$1;
76
92
  functionName: string;
77
93
  args: any[];
78
94
  value: bigint;
79
95
  }) => Promise<any>;
96
+ deployContract: (args: {
97
+ account: Account;
98
+ code: string;
99
+ args: CalldataEncodable[];
100
+ }) => Promise<any>;
80
101
  getTransaction: (args: {
81
102
  hash: TransactionHash;
82
103
  }) => Promise<GenLayerTransaction>;
@@ -85,4 +106,4 @@ type GenLayerClient<TTransport extends Transport, TSimulatorChain extends Simula
85
106
  }) => Promise<number>;
86
107
  };
87
108
 
88
- export { type Address as A, type GenLayerClient as G, type TransactionHash as T, type GenLayerMethod as a, TransactionStatus as b, type GenLayerTransaction as c, type TransactionDataElement as d };
109
+ export { type Address$1 as A, type CalldataEncodable as C, type GenLayerClient as G, type MethodDescription as M, type TransactionData as T, type GenLayerMethod as a, type TransactionHash as b, TransactionStatus as c, type GenLayerTransaction as d, type TransactionDataElement as e };
package/dist/index.cjs CHANGED
@@ -174,8 +174,13 @@ function encodeImpl(to, data) {
174
174
  function encode(data) {
175
175
  const arr = [];
176
176
  encodeImpl(arr, data);
177
- const encodedData = new Uint8Array(arr);
178
- return _viem.toRlp.call(void 0, [encodedData].map((param) => _viem.toHex.call(void 0, param)));
177
+ return new Uint8Array(arr);
178
+ }
179
+ function serialize(data) {
180
+ return _viem.toRlp.call(void 0, data.map((param) => _viem.toHex.call(void 0, param)));
181
+ }
182
+ function encodeAndSerialize(data) {
183
+ return serialize([encode(data)]);
179
184
  }
180
185
 
181
186
  // src/contracts/actions.ts
@@ -200,7 +205,7 @@ var contractActions = (client) => {
200
205
  var overrideContractActions = (client) => {
201
206
  client.readContract = async (args) => {
202
207
  const { account, address, functionName, args: params } = args;
203
- const encodedData = encode({ method: functionName, args: params });
208
+ const encodedData = encodeAndSerialize({ method: functionName, args: params });
204
209
  const requestParams = {
205
210
  to: address,
206
211
  from: _optionalChain([account, 'optionalAccess', _5 => _5.address]) || _optionalChain([client, 'access', _6 => _6.account, 'optionalAccess', _7 => _7.address]),
@@ -214,7 +219,7 @@ var overrideContractActions = (client) => {
214
219
  };
215
220
  client.writeContract = async (args) => {
216
221
  const { account, address, functionName, args: params, value = 0n } = args;
217
- const encodedData = encode({ method: functionName, args: params });
222
+ const encodedData = encodeAndSerialize({ method: functionName, args: params });
218
223
  const senderAccount = account || client.account;
219
224
  if (!senderAccount) {
220
225
  throw new Error(
@@ -238,6 +243,31 @@ var overrideContractActions = (client) => {
238
243
  });
239
244
  return result;
240
245
  };
246
+ client.deployContract = async (args) => {
247
+ const { account, code, args: constructorArgs } = args;
248
+ const data = [code, encode({ args: constructorArgs })];
249
+ const serializedData = serialize(data);
250
+ const senderAccount = account || client.account;
251
+ if (!senderAccount) {
252
+ throw new Error(
253
+ "No account set. Configure the client with an account or pass an account to this function."
254
+ );
255
+ }
256
+ if (!_optionalChain([senderAccount, 'optionalAccess', _9 => _9.signTransaction])) {
257
+ throw new Error("Account does not support signTransaction");
258
+ }
259
+ const nonce = await client.getCurrentNonce({ address: senderAccount.address });
260
+ const signedTransaction = await senderAccount.signTransaction({
261
+ data: serializedData,
262
+ type: "legacy",
263
+ nonce
264
+ });
265
+ const result = await client.request({
266
+ method: "eth_sendRawTransaction",
267
+ params: [signedTransaction]
268
+ });
269
+ return result;
270
+ };
241
271
  return client;
242
272
  };
243
273
 
package/dist/index.d.cts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as viem from 'viem';
2
2
  import { Transport, Account } from 'viem';
3
3
  import { S as SimulatorChain } from './chains-CtZJFz4Q.cjs';
4
- import { G as GenLayerClient } from './clients-B1y56AIF.cjs';
4
+ import { G as GenLayerClient } from './clients-BamlAEcy.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 { Transport, Account } from 'viem';
3
3
  import { S as SimulatorChain } from './chains-CtZJFz4Q.js';
4
- import { G as GenLayerClient } from './clients-C-WgCMQF.js';
4
+ import { G as GenLayerClient } from './clients-CJ820ePR.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
@@ -174,8 +174,13 @@ function encodeImpl(to, data) {
174
174
  function encode(data) {
175
175
  const arr = [];
176
176
  encodeImpl(arr, data);
177
- const encodedData = new Uint8Array(arr);
178
- return toRlp([encodedData].map((param) => toHex(param)));
177
+ return new Uint8Array(arr);
178
+ }
179
+ function serialize(data) {
180
+ return toRlp(data.map((param) => toHex(param)));
181
+ }
182
+ function encodeAndSerialize(data) {
183
+ return serialize([encode(data)]);
179
184
  }
180
185
 
181
186
  // src/contracts/actions.ts
@@ -200,7 +205,7 @@ var contractActions = (client) => {
200
205
  var overrideContractActions = (client) => {
201
206
  client.readContract = async (args) => {
202
207
  const { account, address, functionName, args: params } = args;
203
- const encodedData = encode({ method: functionName, args: params });
208
+ const encodedData = encodeAndSerialize({ method: functionName, args: params });
204
209
  const requestParams = {
205
210
  to: address,
206
211
  from: account?.address || client.account?.address,
@@ -214,7 +219,7 @@ var overrideContractActions = (client) => {
214
219
  };
215
220
  client.writeContract = async (args) => {
216
221
  const { account, address, functionName, args: params, value = 0n } = args;
217
- const encodedData = encode({ method: functionName, args: params });
222
+ const encodedData = encodeAndSerialize({ method: functionName, args: params });
218
223
  const senderAccount = account || client.account;
219
224
  if (!senderAccount) {
220
225
  throw new Error(
@@ -238,6 +243,31 @@ var overrideContractActions = (client) => {
238
243
  });
239
244
  return result;
240
245
  };
246
+ client.deployContract = async (args) => {
247
+ const { account, code, args: constructorArgs } = args;
248
+ const data = [code, encode({ args: constructorArgs })];
249
+ const serializedData = serialize(data);
250
+ const senderAccount = account || client.account;
251
+ if (!senderAccount) {
252
+ throw new Error(
253
+ "No account set. Configure the client with an account or pass an account to this function."
254
+ );
255
+ }
256
+ if (!senderAccount?.signTransaction) {
257
+ throw new Error("Account does not support signTransaction");
258
+ }
259
+ const nonce = await client.getCurrentNonce({ address: senderAccount.address });
260
+ const signedTransaction = await senderAccount.signTransaction({
261
+ data: serializedData,
262
+ type: "legacy",
263
+ nonce
264
+ });
265
+ const result = await client.request({
266
+ method: "eth_sendRawTransaction",
267
+ params: [signedTransaction]
268
+ });
269
+ return result;
270
+ };
241
271
  return client;
242
272
  };
243
273
 
@@ -1,23 +1,7 @@
1
- export { A as Address, G as GenLayerClient, a as GenLayerMethod, c as GenLayerTransaction, d as TransactionDataElement, T as TransactionHash, b as TransactionStatus } from '../clients-B1y56AIF.cjs';
1
+ export { A as Address, C as CalldataEncodable, G as GenLayerClient, a as GenLayerMethod, d as GenLayerTransaction, M as MethodDescription, T as TransactionData, e as TransactionDataElement, b as TransactionHash, c as TransactionStatus } from '../clients-BamlAEcy.cjs';
2
2
  export { S as SimulatorChain } from '../chains-CtZJFz4Q.cjs';
3
3
  export { Account } from 'viem';
4
4
 
5
- declare class Address {
6
- bytes: Uint8Array;
7
- constructor(addr: Uint8Array);
8
- }
9
- type CalldataEncodable = null | boolean | Address | number | bigint | string | Uint8Array | Address | Array<CalldataEncodable> | Map<string, CalldataEncodable> | {
10
- [key: string]: CalldataEncodable;
11
- };
12
- type MethodDescription = {
13
- method: string;
14
- args: Array<CalldataEncodable>;
15
- };
16
- type TransactionData = {
17
- method: string;
18
- args: CalldataEncodable[];
19
- };
20
-
21
5
  type ContractSchema = {
22
6
  abi: Array<{
23
7
  inputs?: Array<{
@@ -34,4 +18,4 @@ type ContractSchema = {
34
18
  class: string;
35
19
  };
36
20
 
37
- export type { CalldataEncodable, ContractSchema, MethodDescription, TransactionData };
21
+ export type { ContractSchema };
@@ -1,23 +1,7 @@
1
- export { A as Address, G as GenLayerClient, a as GenLayerMethod, c as GenLayerTransaction, d as TransactionDataElement, T as TransactionHash, b as TransactionStatus } from '../clients-C-WgCMQF.js';
1
+ export { A as Address, C as CalldataEncodable, G as GenLayerClient, a as GenLayerMethod, d as GenLayerTransaction, M as MethodDescription, T as TransactionData, e as TransactionDataElement, b as TransactionHash, c as TransactionStatus } from '../clients-CJ820ePR.js';
2
2
  export { S as SimulatorChain } from '../chains-CtZJFz4Q.js';
3
3
  export { Account } from 'viem';
4
4
 
5
- declare class Address {
6
- bytes: Uint8Array;
7
- constructor(addr: Uint8Array);
8
- }
9
- type CalldataEncodable = null | boolean | Address | number | bigint | string | Uint8Array | Address | Array<CalldataEncodable> | Map<string, CalldataEncodable> | {
10
- [key: string]: CalldataEncodable;
11
- };
12
- type MethodDescription = {
13
- method: string;
14
- args: Array<CalldataEncodable>;
15
- };
16
- type TransactionData = {
17
- method: string;
18
- args: CalldataEncodable[];
19
- };
20
-
21
5
  type ContractSchema = {
22
6
  abi: Array<{
23
7
  inputs?: Array<{
@@ -34,4 +18,4 @@ type ContractSchema = {
34
18
  class: string;
35
19
  };
36
20
 
37
- export type { CalldataEncodable, ContractSchema, MethodDescription, TransactionData };
21
+ export type { ContractSchema };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "genlayer-js",
3
3
  "type": "module",
4
- "version": "0.2.0",
4
+ "version": "0.3.0",
5
5
  "description": "GenLayer JavaScript SDK",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -1,5 +1,5 @@
1
1
  import {toHex, toRlp} from "viem";
2
- import type {CalldataEncodable} from "../../types/calldata";
2
+ import type {CalldataEncodable, TransactionDataElement} from "../../types/calldata";
3
3
  import {Address} from "../../types/calldata";
4
4
 
5
5
  const BITS_IN_TYPE = 3;
@@ -152,10 +152,17 @@ function encodeImpl(to: number[], data: CalldataEncodable) {
152
152
  }
153
153
  }
154
154
 
155
- export function encode(data: CalldataEncodable): `0x${string}` {
155
+ export function encode(data: CalldataEncodable): Uint8Array {
156
156
  // FIXME: find a better growable type
157
157
  const arr: number[] = [];
158
158
  encodeImpl(arr, data);
159
- const encodedData = new Uint8Array(arr);
160
- return toRlp([encodedData].map(param => toHex(param)));
159
+ return new Uint8Array(arr);
160
+ }
161
+
162
+ export function serialize(data: TransactionDataElement[]): `0x${string}` {
163
+ return toRlp(data.map(param => toHex(param)));
164
+ }
165
+
166
+ export function encodeAndSerialize(data: CalldataEncodable): `0x${string}` {
167
+ return serialize([encode(data)]);
161
168
  }
@@ -1,6 +1,6 @@
1
1
  import {Transport, Account, Address} from "viem";
2
2
 
3
- import {encode} from "@/abi/calldata/encoder";
3
+ import {encode, serialize, encodeAndSerialize} from "@/abi/calldata/encoder";
4
4
  import {ContractSchema, SimulatorChain, GenLayerClient, CalldataEncodable} from "@/types";
5
5
 
6
6
  export const contractActions = (client: GenLayerClient<Transport, SimulatorChain, Account>) => {
@@ -30,7 +30,7 @@ export const overrideContractActions = (client: GenLayerClient<Transport, Simula
30
30
  args: CalldataEncodable[];
31
31
  }): Promise<any> => {
32
32
  const {account, address, functionName, args: params} = args;
33
- const encodedData = encode({method: functionName, args: params});
33
+ const encodedData = encodeAndSerialize({method: functionName, args: params});
34
34
 
35
35
  const requestParams = {
36
36
  to: address,
@@ -52,7 +52,7 @@ export const overrideContractActions = (client: GenLayerClient<Transport, Simula
52
52
  value: bigint;
53
53
  }): Promise<any> => {
54
54
  const {account, address, functionName, args: params, value = 0n} = args;
55
- const encodedData = encode({method: functionName, args: params});
55
+ const encodedData = encodeAndSerialize({method: functionName, args: params});
56
56
 
57
57
  const senderAccount = account || client.account;
58
58
  if (!senderAccount) {
@@ -81,5 +81,36 @@ export const overrideContractActions = (client: GenLayerClient<Transport, Simula
81
81
  return result;
82
82
  };
83
83
 
84
+ client.deployContract = async (args: {account: Account; code: string; args: CalldataEncodable[]}) => {
85
+ const {account, code, args: constructorArgs} = args;
86
+ const data = [code, encode({args: constructorArgs})];
87
+ const serializedData = serialize(data);
88
+
89
+ const senderAccount = account || client.account;
90
+ if (!senderAccount) {
91
+ throw new Error(
92
+ "No account set. Configure the client with an account or pass an account to this function.",
93
+ );
94
+ }
95
+
96
+ if (!senderAccount?.signTransaction) {
97
+ throw new Error("Account does not support signTransaction");
98
+ }
99
+
100
+ const nonce = await client.getCurrentNonce({address: senderAccount.address});
101
+
102
+ const signedTransaction = await senderAccount.signTransaction({
103
+ data: serializedData,
104
+ type: "legacy",
105
+ nonce,
106
+ });
107
+
108
+ const result = await client.request({
109
+ method: "eth_sendRawTransaction",
110
+ params: [signedTransaction],
111
+ });
112
+ return result;
113
+ };
114
+
84
115
  return client;
85
116
  };
@@ -34,3 +34,5 @@ export type TransactionData = {
34
34
 
35
35
  args: CalldataEncodable[];
36
36
  };
37
+
38
+ export type TransactionDataElement = string | number | bigint | boolean | Uint8Array;
@@ -2,6 +2,7 @@ import {Account, Transport, Client} from "viem";
2
2
  import {GenLayerTransaction, TransactionHash} from "./transactions";
3
3
  import {SimulatorChain} from "./chains";
4
4
  import {Address} from "./accounts";
5
+ import {CalldataEncodable} from "./calldata";
5
6
 
6
7
  export type GenLayerMethod =
7
8
  | {method: "sim_fundAccount"; params: [address: string, amount: number]}
@@ -35,6 +36,7 @@ export type GenLayerClient<
35
36
  args: any[];
36
37
  value: bigint;
37
38
  }) => Promise<any>;
39
+ deployContract: (args: {account: Account; code: string; args: CalldataEncodable[]}) => Promise<any>;
38
40
  getTransaction: (args: {hash: TransactionHash}) => Promise<GenLayerTransaction>;
39
41
  getCurrentNonce: (args: {address: string}) => Promise<number>;
40
42
  };