koilib 2.4.1 → 2.6.1

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.
Files changed (45) hide show
  1. package/README.md +4 -4
  2. package/dist/koinos.js +2977 -4588
  3. package/dist/koinos.min.js +1 -1
  4. package/dist/koinos.min.js.LICENSE.txt +2 -2
  5. package/lib/Contract.js +8 -8
  6. package/lib/Contract.js.map +1 -1
  7. package/lib/Provider.d.ts +6 -4
  8. package/lib/Provider.js +59 -47
  9. package/lib/Provider.js.map +1 -1
  10. package/lib/Serializer.js +9 -9
  11. package/lib/Serializer.js.map +1 -1
  12. package/lib/Signer.d.ts +6 -6
  13. package/lib/Signer.js +19 -19
  14. package/lib/Signer.js.map +1 -1
  15. package/lib/browser/Contract.d.ts +202 -0
  16. package/lib/browser/Contract.js +298 -0
  17. package/lib/browser/Contract.js.map +1 -0
  18. package/lib/browser/Provider.d.ts +153 -0
  19. package/lib/browser/Provider.js +244 -0
  20. package/lib/browser/Provider.js.map +1 -0
  21. package/lib/browser/Serializer.d.ts +81 -0
  22. package/lib/browser/Serializer.js +169 -0
  23. package/lib/browser/Serializer.js.map +1 -0
  24. package/lib/browser/Signer.d.ts +296 -0
  25. package/lib/browser/Signer.js +421 -0
  26. package/lib/browser/Signer.js.map +1 -0
  27. package/lib/browser/index.d.ts +7 -0
  28. package/lib/browser/index.js +34 -0
  29. package/lib/browser/index.js.map +1 -0
  30. package/lib/browser/index2.d.ts +2 -0
  31. package/lib/browser/index2.js +33 -0
  32. package/lib/browser/index2.js.map +1 -0
  33. package/lib/browser/interface.d.ts +278 -0
  34. package/lib/browser/interface.js +3 -0
  35. package/lib/browser/interface.js.map +1 -0
  36. package/lib/browser/jsonDescriptors/krc20-proto.json +183 -0
  37. package/lib/browser/jsonDescriptors/protocol-proto.json +246 -0
  38. package/lib/browser/utils.d.ts +326 -0
  39. package/lib/browser/utils.js +252 -0
  40. package/lib/browser/utils.js.map +1 -0
  41. package/lib/interface.d.ts +21 -5
  42. package/lib/utils.d.ts +12 -8
  43. package/lib/utils.js +7 -7
  44. package/lib/utils.js.map +1 -1
  45. package/package.json +34 -31
@@ -0,0 +1,202 @@
1
+ import { Signer, SignerInterface } from "./Signer";
2
+ import { Provider } from "./Provider";
3
+ import { Serializer } from "./Serializer";
4
+ import { CallContractOperationNested, UploadContractOperationNested, TransactionJson, Abi, TransactionOptions, DecodedOperationJson, SendTransactionResponse } from "./interface";
5
+ /**
6
+ * The contract class contains the contract ID and contract entries
7
+ * definition needed to encode/decode operations during the
8
+ * interaction with the user and the communication with the RPC node.
9
+ *
10
+ * @example
11
+ *
12
+ * ```ts
13
+ * const { Contract, Provider, Signer, utils } = require("koilib");
14
+ * const rpcNodes = ["http://api.koinos.io:8080"];
15
+ * const privateKey = "f186a5de49797bfd52dc42505c33d75a46822ed5b60046e09d7c336242e20200";
16
+ * const provider = new Provider(rpcNodes);
17
+ * const signer = new Signer({ privateKey, provider });
18
+ * const koinContract = new Contract({
19
+ * id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
20
+ * abi: utils.Krc20Abi,
21
+ * provider,
22
+ * signer,
23
+ * });
24
+ * const koin = koinContract.functions;
25
+ *
26
+ * // optional: preformat input/output
27
+ * koinContract.abi.methods.balanceOf.preformatInput = (owner) =>
28
+ * ({ owner });
29
+ * koinContract.abi.methods.balanceOf.preformatOutput = (res) =>
30
+ * utils.formatUnits(res.value, 8);
31
+ * koinContract.abi.methods.transfer.preformatInput = (input) => ({
32
+ * from: signer.getAddress(),
33
+ * to: input.to,
34
+ * value: utils.parseUnits(input.value, 8),
35
+ * });
36
+ *
37
+ * async funtion main() {
38
+ * // Get balance
39
+ * const { result } = await koin.balanceOf("12fN2CQnuJM8cMnWZ1hPtM4knjLME8E4PD");
40
+ * console.log(result)
41
+ *
42
+ * // Transfer
43
+ * const { transaction, transactionResponse } = await koin.transfer({
44
+ * to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
45
+ * value: "10.0001",
46
+ * });
47
+ * console.log(`Transaction id ${transaction.id} submitted`);
48
+ *
49
+ * // wait to be mined
50
+ * const blockId = await transactionResponse.wait();
51
+ * console.log(`Transaction mined. Block id: ${blockId}`);
52
+ * }
53
+ *
54
+ * main();
55
+ * ```
56
+ */
57
+ export declare class Contract {
58
+ /**
59
+ * Contract ID
60
+ */
61
+ id?: Uint8Array;
62
+ /**
63
+ * Set of functions to interact with the smart
64
+ * contract. These functions are automatically generated
65
+ * in the constructor of the class
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * const owner = "1Gvqdo9if6v6tFomEuTuMWP1D7H7U9yksb";
70
+ * await koinContract.functions.balanceOf({ owner });
71
+ * ```
72
+ */
73
+ functions: {
74
+ [x: string]: <T = Record<string, unknown>>(args?: unknown, opts?: TransactionOptions) => Promise<{
75
+ operation: CallContractOperationNested;
76
+ transaction?: TransactionJson;
77
+ transactionResponse?: SendTransactionResponse;
78
+ result?: T;
79
+ }>;
80
+ };
81
+ /**
82
+ * Application Binary Interface
83
+ */
84
+ abi?: Abi;
85
+ /**
86
+ * Signer interacting with the smart contract
87
+ */
88
+ signer?: SignerInterface;
89
+ /**
90
+ * Provider to connect with the blockchain
91
+ */
92
+ provider?: Provider;
93
+ /**
94
+ * Serializer to serialize/deserialize data types
95
+ */
96
+ serializer?: Serializer;
97
+ /**
98
+ * Bytecode. Needed to deploy the smart contract.
99
+ */
100
+ bytecode?: Uint8Array;
101
+ /**
102
+ * Options to apply when creating transactions.
103
+ * By default it set rc_limit to 1e8, sendTransaction true,
104
+ * sendAbis true, and nonce undefined (to get it from the blockchain)
105
+ */
106
+ options: TransactionOptions;
107
+ constructor(c: {
108
+ id?: string;
109
+ abi?: Abi;
110
+ bytecode?: Uint8Array;
111
+ options?: TransactionOptions;
112
+ signer?: Signer;
113
+ provider?: Provider;
114
+ /**
115
+ * Set this option if you can not use _eval_ functions
116
+ * in the current environment. In such cases, the
117
+ * serializer must come from an environment where it
118
+ * is able to use those functions.
119
+ */
120
+ serializer?: Serializer;
121
+ });
122
+ /**
123
+ * Compute contract Id
124
+ */
125
+ static computeContractId(address: string): Uint8Array;
126
+ /**
127
+ * Get contract Id
128
+ */
129
+ getId(): string;
130
+ /**
131
+ * Function to deploy a new smart contract.
132
+ * The Bytecode must be defined in the constructor of the class
133
+ * @example
134
+ * ```ts
135
+ * const privateKey = "f186a5de49797bfd52dc42505c33d75a46822ed5b60046e09d7c336242e20200";
136
+ * const provider = new Provider(["http://api.koinos.io:8080"]);
137
+ * const signer = new Signer({ privateKey, provider });
138
+ * const bytecode = new Uint8Array([1, 2, 3, 4]);
139
+ * const contract = new Contract({ signer, provider, bytecode });
140
+ * const { transactionResponse } = await contract.deploy();
141
+ * // wait to be mined
142
+ * const blockId = await transactionResponse.wait();
143
+ * console.log(`Contract uploaded in block id ${blockId}`);
144
+ * ```
145
+ */
146
+ deploy(options?: TransactionOptions): Promise<{
147
+ operation: UploadContractOperationNested;
148
+ transaction?: TransactionJson;
149
+ transactionResponse?: SendTransactionResponse;
150
+ }>;
151
+ /**
152
+ * Encondes a contract operation using Koinos serialization
153
+ * and taking the contract entries as reference to build it
154
+ * @param op - Operation to encode
155
+ * @returns Operation encoded
156
+ * @example
157
+ * ```ts
158
+ * const opEncoded = contract.encodeOperation({
159
+ * name: "transfer",
160
+ * args: {
161
+ * from: "12fN2CQnuJM8cMnWZ1hPtM4knjLME8E4PD",
162
+ * to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
163
+ * value: "1000",
164
+ * }
165
+ * });
166
+ *
167
+ * console.log(opEncoded);
168
+ * // {
169
+ * // call_contract: {
170
+ * // contract_id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
171
+ * // entry_point: 0x62efa292,
172
+ * // args: "MBWFsaWNlA2JvYgAAAAAAAAPo",
173
+ * // }
174
+ * // }
175
+ * ```
176
+ */
177
+ encodeOperation(op: DecodedOperationJson): Promise<CallContractOperationNested>;
178
+ /**
179
+ * Decodes a contract operation to be human readable
180
+ * @example
181
+ * ```ts
182
+ * const opDecoded = contract.decodeOperation({
183
+ * call_contract: {
184
+ * contract_id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
185
+ * entry_point: 0x62efa292,
186
+ * args: "MBWFsaWNlA2JvYgAAAAAAAAPo",
187
+ * }
188
+ * });
189
+ * console.log(opDecoded);
190
+ * // {
191
+ * // name: "transfer",
192
+ * // args: {
193
+ * // from: "12fN2CQnuJM8cMnWZ1hPtM4knjLME8E4PD",
194
+ * // to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
195
+ * // value: "1000",
196
+ * // },
197
+ * // }
198
+ * ```
199
+ */
200
+ decodeOperation(op: CallContractOperationNested): Promise<DecodedOperationJson>;
201
+ }
202
+ export default Contract;
@@ -0,0 +1,298 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Contract = void 0;
4
+ const Serializer_1 = require("./Serializer");
5
+ const utils_1 = require("./utils");
6
+ /**
7
+ * The contract class contains the contract ID and contract entries
8
+ * definition needed to encode/decode operations during the
9
+ * interaction with the user and the communication with the RPC node.
10
+ *
11
+ * @example
12
+ *
13
+ * ```ts
14
+ * const { Contract, Provider, Signer, utils } = require("koilib");
15
+ * const rpcNodes = ["http://api.koinos.io:8080"];
16
+ * const privateKey = "f186a5de49797bfd52dc42505c33d75a46822ed5b60046e09d7c336242e20200";
17
+ * const provider = new Provider(rpcNodes);
18
+ * const signer = new Signer({ privateKey, provider });
19
+ * const koinContract = new Contract({
20
+ * id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
21
+ * abi: utils.Krc20Abi,
22
+ * provider,
23
+ * signer,
24
+ * });
25
+ * const koin = koinContract.functions;
26
+ *
27
+ * // optional: preformat input/output
28
+ * koinContract.abi.methods.balanceOf.preformatInput = (owner) =>
29
+ * ({ owner });
30
+ * koinContract.abi.methods.balanceOf.preformatOutput = (res) =>
31
+ * utils.formatUnits(res.value, 8);
32
+ * koinContract.abi.methods.transfer.preformatInput = (input) => ({
33
+ * from: signer.getAddress(),
34
+ * to: input.to,
35
+ * value: utils.parseUnits(input.value, 8),
36
+ * });
37
+ *
38
+ * async funtion main() {
39
+ * // Get balance
40
+ * const { result } = await koin.balanceOf("12fN2CQnuJM8cMnWZ1hPtM4knjLME8E4PD");
41
+ * console.log(result)
42
+ *
43
+ * // Transfer
44
+ * const { transaction, transactionResponse } = await koin.transfer({
45
+ * to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
46
+ * value: "10.0001",
47
+ * });
48
+ * console.log(`Transaction id ${transaction.id} submitted`);
49
+ *
50
+ * // wait to be mined
51
+ * const blockId = await transactionResponse.wait();
52
+ * console.log(`Transaction mined. Block id: ${blockId}`);
53
+ * }
54
+ *
55
+ * main();
56
+ * ```
57
+ */
58
+ class Contract {
59
+ constructor(c) {
60
+ var _a;
61
+ if (c.id)
62
+ this.id = (0, utils_1.decodeBase58)(c.id);
63
+ this.signer = c.signer;
64
+ this.provider = c.provider || ((_a = c.signer) === null || _a === void 0 ? void 0 : _a.provider);
65
+ this.abi = c.abi;
66
+ this.bytecode = c.bytecode;
67
+ if (c.serializer) {
68
+ this.serializer = c.serializer;
69
+ }
70
+ else if (c.abi && c.abi.types) {
71
+ this.serializer = new Serializer_1.Serializer(c.abi.types);
72
+ }
73
+ this.options = {
74
+ rc_limit: 1e8,
75
+ sendTransaction: true,
76
+ sendAbis: true,
77
+ ...c.options,
78
+ };
79
+ this.functions = {};
80
+ if (this.signer &&
81
+ this.provider &&
82
+ this.abi &&
83
+ this.abi.methods &&
84
+ this.serializer) {
85
+ Object.keys(this.abi.methods).forEach((name) => {
86
+ this.functions[name] = async (argu = {}, options) => {
87
+ if (!this.provider)
88
+ throw new Error("provider not found");
89
+ if (!this.abi || !this.abi.methods)
90
+ throw new Error("Methods are not defined");
91
+ if (!this.abi.methods[name])
92
+ throw new Error(`Method ${name} not defined in the ABI`);
93
+ const opts = {
94
+ ...this.options,
95
+ ...options,
96
+ };
97
+ const { readOnly, output, defaultOutput, preformatInput, preformatOutput, } = this.abi.methods[name];
98
+ let args;
99
+ if (typeof preformatInput === "function") {
100
+ args = preformatInput(argu);
101
+ }
102
+ else {
103
+ args = argu;
104
+ }
105
+ const operation = await this.encodeOperation({ name, args });
106
+ if (readOnly) {
107
+ if (!output)
108
+ throw new Error(`No output defined for ${name}`);
109
+ // read contract
110
+ const { result: resultEncoded } = await this.provider.readContract({
111
+ contract_id: (0, utils_1.encodeBase58)(operation.call_contract.contract_id),
112
+ entry_point: operation.call_contract.entry_point,
113
+ args: (0, utils_1.encodeBase64)(operation.call_contract.args),
114
+ });
115
+ let result = defaultOutput;
116
+ if (resultEncoded) {
117
+ result = await this.serializer.deserialize(resultEncoded, output);
118
+ }
119
+ if (typeof preformatOutput === "function") {
120
+ result = preformatOutput(result);
121
+ }
122
+ return { operation, result };
123
+ }
124
+ // return operation if send is false
125
+ if (!(opts === null || opts === void 0 ? void 0 : opts.sendTransaction))
126
+ return { operation };
127
+ // write contract (sign and send)
128
+ if (!this.signer)
129
+ throw new Error("signer not found");
130
+ const transaction = await this.signer.encodeTransaction({
131
+ ...opts,
132
+ operations: [operation],
133
+ });
134
+ const abis = {};
135
+ if (opts === null || opts === void 0 ? void 0 : opts.sendAbis) {
136
+ const contractId = (0, utils_1.encodeBase58)(this.id);
137
+ abis[contractId] = this.abi;
138
+ }
139
+ const transactionResponse = await this.signer.sendTransaction(transaction, abis);
140
+ return { operation, transaction, transactionResponse };
141
+ };
142
+ });
143
+ }
144
+ }
145
+ /**
146
+ * Compute contract Id
147
+ */
148
+ static computeContractId(address) {
149
+ return (0, utils_1.decodeBase58)(address);
150
+ }
151
+ /**
152
+ * Get contract Id
153
+ */
154
+ getId() {
155
+ if (!this.id)
156
+ throw new Error("id is not defined");
157
+ return (0, utils_1.encodeBase58)(this.id);
158
+ }
159
+ /**
160
+ * Function to deploy a new smart contract.
161
+ * The Bytecode must be defined in the constructor of the class
162
+ * @example
163
+ * ```ts
164
+ * const privateKey = "f186a5de49797bfd52dc42505c33d75a46822ed5b60046e09d7c336242e20200";
165
+ * const provider = new Provider(["http://api.koinos.io:8080"]);
166
+ * const signer = new Signer({ privateKey, provider });
167
+ * const bytecode = new Uint8Array([1, 2, 3, 4]);
168
+ * const contract = new Contract({ signer, provider, bytecode });
169
+ * const { transactionResponse } = await contract.deploy();
170
+ * // wait to be mined
171
+ * const blockId = await transactionResponse.wait();
172
+ * console.log(`Contract uploaded in block id ${blockId}`);
173
+ * ```
174
+ */
175
+ async deploy(options) {
176
+ if (!this.signer)
177
+ throw new Error("signer not found");
178
+ if (!this.bytecode)
179
+ throw new Error("bytecode not found");
180
+ const opts = {
181
+ ...this.options,
182
+ ...options,
183
+ };
184
+ const operation = {
185
+ upload_contract: {
186
+ contract_id: Contract.computeContractId(this.signer.getAddress()),
187
+ bytecode: this.bytecode,
188
+ },
189
+ };
190
+ // return operation if send is false
191
+ if (!(opts === null || opts === void 0 ? void 0 : opts.sendTransaction))
192
+ return { operation };
193
+ const transaction = await this.signer.encodeTransaction({
194
+ ...opts,
195
+ operations: [operation],
196
+ });
197
+ const transactionResponse = await this.signer.sendTransaction(transaction);
198
+ return { operation, transaction, transactionResponse };
199
+ }
200
+ /**
201
+ * Encondes a contract operation using Koinos serialization
202
+ * and taking the contract entries as reference to build it
203
+ * @param op - Operation to encode
204
+ * @returns Operation encoded
205
+ * @example
206
+ * ```ts
207
+ * const opEncoded = contract.encodeOperation({
208
+ * name: "transfer",
209
+ * args: {
210
+ * from: "12fN2CQnuJM8cMnWZ1hPtM4knjLME8E4PD",
211
+ * to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
212
+ * value: "1000",
213
+ * }
214
+ * });
215
+ *
216
+ * console.log(opEncoded);
217
+ * // {
218
+ * // call_contract: {
219
+ * // contract_id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
220
+ * // entry_point: 0x62efa292,
221
+ * // args: "MBWFsaWNlA2JvYgAAAAAAAAPo",
222
+ * // }
223
+ * // }
224
+ * ```
225
+ */
226
+ async encodeOperation(op) {
227
+ if (!this.abi || !this.abi.methods || !this.abi.methods[op.name])
228
+ throw new Error(`Operation ${op.name} unknown`);
229
+ if (!this.serializer)
230
+ throw new Error("Serializer is not defined");
231
+ if (!this.id)
232
+ throw new Error("Contract id is not defined");
233
+ const method = this.abi.methods[op.name];
234
+ let bufferInputs = new Uint8Array(0);
235
+ if (method.input) {
236
+ if (!op.args)
237
+ throw new Error(`No arguments defined for type '${method.input}'`);
238
+ bufferInputs = await this.serializer.serialize(op.args, method.input);
239
+ }
240
+ return {
241
+ call_contract: {
242
+ contract_id: this.id,
243
+ entry_point: method.entryPoint,
244
+ args: bufferInputs,
245
+ },
246
+ };
247
+ }
248
+ /**
249
+ * Decodes a contract operation to be human readable
250
+ * @example
251
+ * ```ts
252
+ * const opDecoded = contract.decodeOperation({
253
+ * call_contract: {
254
+ * contract_id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
255
+ * entry_point: 0x62efa292,
256
+ * args: "MBWFsaWNlA2JvYgAAAAAAAAPo",
257
+ * }
258
+ * });
259
+ * console.log(opDecoded);
260
+ * // {
261
+ * // name: "transfer",
262
+ * // args: {
263
+ * // from: "12fN2CQnuJM8cMnWZ1hPtM4knjLME8E4PD",
264
+ * // to: "172AB1FgCsYrRAW5cwQ8KjadgxofvgPFd6",
265
+ * // value: "1000",
266
+ * // },
267
+ * // }
268
+ * ```
269
+ */
270
+ async decodeOperation(op) {
271
+ if (!this.id)
272
+ throw new Error("Contract id is not defined");
273
+ if (!this.abi || !this.abi.methods)
274
+ throw new Error("Methods are not defined");
275
+ if (!this.serializer)
276
+ throw new Error("Serializer is not defined");
277
+ if (!op.call_contract)
278
+ throw new Error("Operation is not CallContractOperation");
279
+ if ((0, utils_1.encodeBase58)(op.call_contract.contract_id) !== (0, utils_1.encodeBase58)(this.id))
280
+ throw new Error(`Invalid contract id. Expected: ${(0, utils_1.encodeBase58)(this.id)}. Received: ${(0, utils_1.encodeBase58)(op.call_contract.contract_id)}`);
281
+ for (let i = 0; i < Object.keys(this.abi.methods).length; i += 1) {
282
+ const opName = Object.keys(this.abi.methods)[i];
283
+ const method = this.abi.methods[opName];
284
+ if (op.call_contract.entry_point === method.entryPoint) {
285
+ if (!method.input)
286
+ return { name: opName };
287
+ return {
288
+ name: opName,
289
+ args: await this.serializer.deserialize(op.call_contract.args, method.input),
290
+ };
291
+ }
292
+ }
293
+ throw new Error(`Unknown method id ${op.call_contract.entry_point}`);
294
+ }
295
+ }
296
+ exports.Contract = Contract;
297
+ exports.default = Contract;
298
+ //# sourceMappingURL=Contract.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Contract.js","sourceRoot":"","sources":["../../src/Contract.ts"],"names":[],"mappings":";;;AAGA,6CAA0C;AAU1C,mCAAmE;AAEnE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AACH,MAAa,QAAQ;IA6DnB,YAAY,CAcX;;QACC,IAAI,CAAC,CAAC,EAAE;YAAE,IAAI,CAAC,EAAE,GAAG,IAAA,oBAAY,EAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QACvC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC;QACvB,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC,QAAQ,KAAI,MAAA,CAAC,CAAC,MAAM,0CAAE,QAAQ,CAAA,CAAC;QACjD,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,GAAG,CAAC;QACjB,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC,QAAQ,CAAC;QAC3B,IAAI,CAAC,CAAC,UAAU,EAAE;YAChB,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC,UAAU,CAAC;SAChC;aAAM,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE;YAC/B,IAAI,CAAC,UAA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AI,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE;YAChE,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC;YAChD,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YACxC,IAAI,EAAE,CAAC,aAAa,CAAC,WAAW,KAAK,MAAM,CAAC,UAAU,EAAE;gBACtD,IAAI,CAAC,MAAM,CAAC,KAAK;oBAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;gBAC3C,OAAO;oBACL,IAAI,EAAE,MAAM;oBACZ,IAAI,EAAE,MAAM,IAAI,CAAC,UAAU,CAAC,WAAW,CACrC,EAAE,CAAC,aAAa,CAAC,IAAI,EACrB,MAAM,CAAC,KAAK,CACb;iBACF,CAAC;aACH;SACF;QACD,MAAM,IAAI,KAAK,CAAC,qBAAqB,EAAE,CAAC,aAAa,CAAC,WAAW,EAAE,CAAC,CAAC;IACvE,CAAC;CACF;AA3VD,4BA2VC;AAED,kBAAe,QAAQ,CAAC"}
@@ -0,0 +1,153 @@
1
+ import { BlockJson, TransactionJson, CallContractOperationJson, SendTransactionResponse } from "./interface";
2
+ /**
3
+ * Class to connect with the RPC node
4
+ */
5
+ export declare class Provider {
6
+ /**
7
+ * Array of URLs of RPC nodes
8
+ */
9
+ rpcNodes: string[];
10
+ /**
11
+ * Function triggered when a node is down. Returns a
12
+ * boolean determining if the call should be aborted.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const provider = new Provider([
17
+ * "http://45.56.104.152:8080",
18
+ * "http://159.203.119.0:8080"
19
+ * ]);
20
+ *
21
+ * provider.onError = (error, node, newNode) => {
22
+ * console.log(`Error from node ${node}: ${error.message}`);
23
+ * console.log(`changing node to ${newNode}`);
24
+ * const abort = false;
25
+ * return abort;
26
+ * }
27
+ * ```
28
+ */
29
+ onError: (error: Error,
30
+ /** node that threw the error */
31
+ currentNode: string,
32
+ /** node used for the next iteration */
33
+ newNode: string) => boolean;
34
+ /**
35
+ * Index of current node in rpcNodes
36
+ */
37
+ currentNodeId: number;
38
+ /**
39
+ *
40
+ * @param rpcNodes - URL of the rpc node, or array of urls
41
+ * to switch between them when someone is down
42
+ * @example
43
+ * ```ts
44
+ * const provider = new Provider([
45
+ * "http://45.56.104.152:8080",
46
+ * "http://159.203.119.0:8080"
47
+ * ]);
48
+ * ```
49
+ */
50
+ constructor(rpcNodes: string | string[]);
51
+ /**
52
+ * Function to make jsonrpc requests to the RPC node
53
+ * @param method - jsonrpc method
54
+ * @param params - jsonrpc params
55
+ * @returns Result of jsonrpc response
56
+ */
57
+ call<T = unknown>(method: string, params: unknown): Promise<T>;
58
+ /**
59
+ * Function to call "chain.get_account_nonce" to return the number of
60
+ * transactions for a particular account. This call is used
61
+ * when creating new transactions.
62
+ * @param account - account address
63
+ * @returns Nonce
64
+ */
65
+ getNonce(account: string): Promise<number>;
66
+ getAccountRc(account: string): Promise<string>;
67
+ /**
68
+ * Get transactions by id and their corresponding block ids
69
+ */
70
+ getTransactionsById(transactionIds: string[]): Promise<{
71
+ transactions: {
72
+ transaction: TransactionJson[];
73
+ containing_blocks: string[];
74
+ }[];
75
+ }>;
76
+ getBlocksById(blockIds: string[]): Promise<{
77
+ block_items: {
78
+ block_id: string;
79
+ block_height: string;
80
+ block: BlockJson;
81
+ }[];
82
+ }>;
83
+ /**
84
+ * Function to get info from the head block in the blockchain
85
+ */
86
+ getHeadInfo(): Promise<{
87
+ head_topology: {
88
+ id: string;
89
+ height: string;
90
+ previous: string;
91
+ };
92
+ last_irreversible_block: string;
93
+ }>;
94
+ /**
95
+ * Function to get consecutive blocks in descending order
96
+ * @param height - Starting block height
97
+ * @param numBlocks - Number of blocks to fetch
98
+ * @param idRef - Block ID reference to speed up searching blocks.
99
+ * This ID must be from a greater block height. By default it
100
+ * gets the ID from the block head.
101
+ */
102
+ getBlocks(height: number, numBlocks?: number, idRef?: string): Promise<{
103
+ block_id: string;
104
+ block_height: string;
105
+ block: BlockJson;
106
+ block_receipt: {
107
+ [x: string]: unknown;
108
+ };
109
+ }[]>;
110
+ /**
111
+ * Function to get a block by its height
112
+ */
113
+ getBlock(height: number): Promise<{
114
+ block_id: string;
115
+ block_height: string;
116
+ block: BlockJson;
117
+ block_receipt: {
118
+ [x: string]: unknown;
119
+ };
120
+ }>;
121
+ /**
122
+ * Function to call "chain.submit_transaction" to send a signed
123
+ * transaction to the blockchain. It returns an object with the async
124
+ * function "wait", which can be called to wait for the
125
+ * transaction to be mined (see [[SendTransactionResponse]]).
126
+ * @param transaction - Signed transaction
127
+ * @example
128
+ * ```ts
129
+ * const { transactionResponse } = await provider.sendTransaction({
130
+ * id: "1220...",
131
+ * active: "...",
132
+ * signatureData: "...",
133
+ * });
134
+ * console.log("Transaction submitted to the mempool");
135
+ * // wait to be mined
136
+ * const blockNumber = await transactionResponse.wait();
137
+ * // const blockNumber = await transactionResponse.wait("byBlock", 30000);
138
+ * // const blockId = await transactionResponse.wait("byTransactionId", 30000);
139
+ * console.log("Transaction mined")
140
+ * ```
141
+ */
142
+ sendTransaction(transaction: TransactionJson): Promise<SendTransactionResponse>;
143
+ /**
144
+ * Function to call "chain.read_contract" to read a contract.
145
+ * This function is used by [[Contract]] class when read methods
146
+ * are invoked.
147
+ */
148
+ readContract(operation: CallContractOperationJson): Promise<{
149
+ result: string;
150
+ logs: string;
151
+ }>;
152
+ }
153
+ export default Provider;