koilib 5.3.0 → 5.4.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/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Koilib
2
2
 
3
3
  Koinos Library for Javascript and Typescript. It can be used in node and browsers.
4
+ The complete documentation can be found at https://joticajulian.github.io/koilib/
4
5
 
5
6
  ## Table of Contents
6
7
 
@@ -60,20 +61,35 @@ You can also load it directly to the browser by downloading the bunble file loca
60
61
  With Typescript import the library
61
62
 
62
63
  ```typescript
63
- import { Signer, Contract, Provider, Serializer, utils } from "koilib";
64
+ import {
65
+ Signer,
66
+ Contract,
67
+ Provider,
68
+ Transaction,
69
+ Serializer,
70
+ utils,
71
+ } from "koilib";
64
72
  ```
65
73
 
66
74
  With Javascript import the library with require
67
75
 
68
76
  ```javascript
69
- const { Signer, Contract, Provider, Serializer, utils } = require("koilib");
77
+ const {
78
+ Signer,
79
+ Contract,
80
+ Provider,
81
+ Transaction,
82
+ Serializer,
83
+ utils,
84
+ } = require("koilib");
70
85
  ```
71
86
 
72
- There are 4 principal classes:
87
+ There are 5 principal classes:
73
88
 
74
89
  - **Signer**: Class containing the private key. It is used to sign.
75
90
  - **Provider**: Class to connect with the RPC node.
76
91
  - **Contract**: Class defining the contract to interact with.
92
+ - **Transaction**: Class to create transactions.
77
93
  - **Serializer**: Class with the protocol buffers definitions to
78
94
  serialize/deserialize data types.
79
95
 
@@ -267,6 +283,71 @@ const { transaction } = await koin.transfer(
267
283
  );
268
284
  ```
269
285
 
286
+ ### Multiple operations in a single transaction
287
+
288
+ There are 3 ways to define a transaction with several operations. Use one of them as it is more convenient for you.
289
+
290
+ Using the Contract class. You can use `previousOperations` or `nextOperations` to attach more operations in the transaction:
291
+
292
+ ```ts
293
+ const fn = contract.functions;
294
+ contract.options.onlyOperation = true;
295
+ const { operation: op1 } = await fn.set_owner({
296
+ account: "16MT1VQFgsVxEfJrSGinrA5buiqBsN5ViJ",
297
+ });
298
+ const { operation: op3 } = await fn.set_address({
299
+ account: "1Gvqdo9if6v6tFomEuTuMWP1D7H7U9yksb",
300
+ });
301
+ const { transaction, receipt } = await fn.set_name(
302
+ {
303
+ name: "test",
304
+ },
305
+ {
306
+ previousOperations: [op1],
307
+ nextOperations: [op3],
308
+ }
309
+ );
310
+ await transaction.wait();
311
+ ```
312
+
313
+ Using the Signer class with the Contract class. First all operations are created and then passed to the signer to create and send the transaction:
314
+
315
+ ```ts
316
+ const fn = contract.functions;
317
+ contract.options.onlyOperation = true;
318
+ const { operation: op1 } = await fn.set_owner({
319
+ account: "16MT1VQFgsVxEfJrSGinrA5buiqBsN5ViJ",
320
+ });
321
+ const { operation: op2 } = await fn.set_name({
322
+ name: "test",
323
+ });
324
+ const { operation: op3 } = await fn.set_address({
325
+ account: "1Gvqdo9if6v6tFomEuTuMWP1D7H7U9yksb",
326
+ });
327
+ const tx = await signer.prepareTransaction({
328
+ operations: [op1, op2, op3],
329
+ });
330
+ const { transaction, receipt } = await signer.sendTransaction(tx);
331
+ await transaction.wait();
332
+ ```
333
+
334
+ Using the Transaction class:
335
+
336
+ ```ts
337
+ const tx = new Transaction({ signer });
338
+ await tx.pushOperation(fn.set_owner, {
339
+ account: "16MT1VQFgsVxEfJrSGinrA5buiqBsN5ViJ",
340
+ });
341
+ await tx.pushOperation(fn.set_name, {
342
+ name: "test",
343
+ });
344
+ await tx.pushOperation(fn.set_address, {
345
+ account: "1Gvqdo9if6v6tFomEuTuMWP1D7H7U9yksb",
346
+ });
347
+ const receipt = await tx.send();
348
+ await tx.wait();
349
+ ```
350
+
270
351
  ### Create ABIs
271
352
 
272
353
  ABIs are composed of 2 elements: methods and types.
@@ -348,7 +429,7 @@ The complete documentation can be found at https://joticajulian.github.io/koilib
348
429
 
349
430
  ## Acknowledgments
350
431
 
351
- Many thanks to the sponsors of this library: @levineam, @Amikob, @motoeng, @isaacdozier, @imjwalker, and the private sponsors.
432
+ Many thanks to the sponsors of this library: @levineam, @Amikob, @motoeng, @isaacdozier, @imjwalker, @brklyn8900, and the private sponsors.
352
433
 
353
434
  If you would like to contribute to the development of this library consider becoming a sponsor in https://github.com/sponsors/joticajulian.
354
435
 
package/dist/koinos.js CHANGED
@@ -9987,9 +9987,14 @@ const utils_1 = __webpack_require__(8593);
9987
9987
  class Contract {
9988
9988
  constructor(c) {
9989
9989
  var _a;
9990
+ this.signer = c.signer;
9990
9991
  if (c.id)
9991
9992
  this.id = (0, utils_1.decodeBase58)(c.id);
9992
- this.signer = c.signer;
9993
+ else {
9994
+ if (!this.signer)
9995
+ throw new Error("at least signer or contract id must be defined");
9996
+ this.id = (0, utils_1.decodeBase58)(this.signer.getAddress());
9997
+ }
9993
9998
  this.provider = c.provider || ((_a = c.signer) === null || _a === void 0 ? void 0 : _a.provider);
9994
9999
  this.abi = c.abi;
9995
10000
  this.bytecode = c.bytecode;
@@ -10063,14 +10068,12 @@ class Contract {
10063
10068
  ...(opts.nextOperations ? opts.nextOperations : []),
10064
10069
  ],
10065
10070
  });
10066
- const optsSend = {
10067
- broadcast: opts.broadcast,
10068
- beforeSend: opts.beforeSend,
10069
- };
10070
10071
  if (opts.sendAbis) {
10071
- optsSend.abis = {};
10072
- const contractId = (0, utils_1.encodeBase58)(this.id);
10073
- optsSend.abis[contractId] = this.abi;
10072
+ if (!opts.abis)
10073
+ opts.abis = {};
10074
+ const contractId = this.getId();
10075
+ if (!opts.abis[contractId])
10076
+ opts.abis[contractId] = this.abi;
10074
10077
  }
10075
10078
  // return result if the transaction will not be broadcasted
10076
10079
  if (!opts.sendTransaction) {
@@ -10078,10 +10081,10 @@ class Contract {
10078
10081
  throw new Error("This transaction was not broadcasted");
10079
10082
  };
10080
10083
  if (opts.signTransaction)
10081
- tx = await this.signer.signTransaction(tx, optsSend.abis);
10084
+ tx = await this.signer.signTransaction(tx, opts.sendAbis ? opts.abis : undefined);
10082
10085
  return { operation, transaction: { ...tx, wait: noWait } };
10083
10086
  }
10084
- const { transaction, receipt } = await this.signer.sendTransaction(tx, optsSend);
10087
+ const { transaction, receipt } = await this.signer.sendTransaction(tx, opts);
10085
10088
  return { operation, transaction, receipt };
10086
10089
  };
10087
10090
  });
@@ -10091,8 +10094,6 @@ class Contract {
10091
10094
  * Get contract Id
10092
10095
  */
10093
10096
  getId() {
10094
- if (!this.id)
10095
- throw new Error("id is not defined");
10096
10097
  return (0, utils_1.encodeBase58)(this.id);
10097
10098
  }
10098
10099
  /**
@@ -10148,9 +10149,7 @@ class Contract {
10148
10149
  ...this.options,
10149
10150
  ...options,
10150
10151
  };
10151
- const contractId = this.id
10152
- ? (0, utils_1.encodeBase58)(this.id)
10153
- : this.signer.getAddress();
10152
+ const contractId = this.getId();
10154
10153
  const operation = {
10155
10154
  upload_contract: {
10156
10155
  contract_id: contractId,
@@ -10184,20 +10183,16 @@ class Contract {
10184
10183
  ...(opts.nextOperations ? opts.nextOperations : []),
10185
10184
  ],
10186
10185
  });
10187
- const optsSend = {
10188
- broadcast: opts.broadcast,
10189
- beforeSend: opts.beforeSend,
10190
- };
10191
10186
  // return result if the transaction will not be broadcasted
10192
10187
  if (!opts.sendTransaction) {
10193
10188
  const noWait = () => {
10194
10189
  throw new Error("This transaction was not broadcasted");
10195
10190
  };
10196
10191
  if (opts.signTransaction)
10197
- tx = await this.signer.signTransaction(tx);
10192
+ tx = await this.signer.signTransaction(tx, opts.sendAbis ? opts.abis : undefined);
10198
10193
  return { operation, transaction: { ...tx, wait: noWait } };
10199
10194
  }
10200
- const { transaction, receipt } = await this.signer.sendTransaction(tx, optsSend);
10195
+ const { transaction, receipt } = await this.signer.sendTransaction(tx, opts);
10201
10196
  return { operation, transaction, receipt };
10202
10197
  }
10203
10198
  /**
@@ -10231,8 +10226,6 @@ class Contract {
10231
10226
  throw new Error(`Operation ${op.name} unknown`);
10232
10227
  if (!this.serializer)
10233
10228
  throw new Error("Serializer is not defined");
10234
- if (!this.id)
10235
- throw new Error("Contract id is not defined");
10236
10229
  const method = this.abi.methods[op.name];
10237
10230
  let bufferArguments = new Uint8Array(0);
10238
10231
  if (method.argument) {
@@ -10242,7 +10235,7 @@ class Contract {
10242
10235
  }
10243
10236
  return {
10244
10237
  call_contract: {
10245
- contract_id: (0, utils_1.encodeBase58)(this.id),
10238
+ contract_id: this.getId(),
10246
10239
  entry_point: method.entry_point,
10247
10240
  args: (0, utils_1.encodeBase64url)(bufferArguments),
10248
10241
  },
@@ -10271,16 +10264,14 @@ class Contract {
10271
10264
  * ```
10272
10265
  */
10273
10266
  async decodeOperation(op) {
10274
- if (!this.id)
10275
- throw new Error("Contract id is not defined");
10276
10267
  if (!this.abi || !this.abi.methods)
10277
10268
  throw new Error("Methods are not defined");
10278
10269
  if (!this.serializer)
10279
10270
  throw new Error("Serializer is not defined");
10280
10271
  if (!op.call_contract)
10281
10272
  throw new Error("Operation is not CallContractOperation");
10282
- if (op.call_contract.contract_id !== (0, utils_1.encodeBase58)(this.id))
10283
- throw new Error(`Invalid contract id. Expected: ${(0, utils_1.encodeBase58)(this.id)}. Received: ${op.call_contract.contract_id}`);
10273
+ if (op.call_contract.contract_id !== this.getId())
10274
+ throw new Error(`Invalid contract id. Expected: ${this.getId()}. Received: ${op.call_contract.contract_id}`);
10284
10275
  for (let i = 0; i < Object.keys(this.abi.methods).length; i += 1) {
10285
10276
  const opName = Object.keys(this.abi.methods)[i];
10286
10277
  const method = this.abi.methods[opName];
@@ -11222,7 +11213,7 @@ class Signer {
11222
11213
  async sendTransaction(transaction, options) {
11223
11214
  var _a;
11224
11215
  if (!transaction.signatures || !((_a = transaction.signatures) === null || _a === void 0 ? void 0 : _a.length))
11225
- transaction = await this.signTransaction(transaction, options === null || options === void 0 ? void 0 : options.abis);
11216
+ transaction = await this.signTransaction(transaction, (options === null || options === void 0 ? void 0 : options.sendAbis) ? options.abis : undefined);
11226
11217
  if (!this.provider)
11227
11218
  throw new Error("provider is undefined");
11228
11219
  const opts = {
@@ -11544,6 +11535,197 @@ exports.Signer = Signer;
11544
11535
  exports["default"] = Signer;
11545
11536
 
11546
11537
 
11538
+ /***/ }),
11539
+
11540
+ /***/ 7592:
11541
+ /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
11542
+
11543
+ "use strict";
11544
+
11545
+ Object.defineProperty(exports, "__esModule", ({ value: true }));
11546
+ exports.Transaction = void 0;
11547
+ const Signer_1 = __webpack_require__(6991);
11548
+ class Transaction {
11549
+ constructor(c) {
11550
+ var _a, _b, _c, _d, _e;
11551
+ this.signer = c === null || c === void 0 ? void 0 : c.signer;
11552
+ this.provider = c === null || c === void 0 ? void 0 : c.provider;
11553
+ this.options = {
11554
+ broadcast: true,
11555
+ sendAbis: true,
11556
+ ...c === null || c === void 0 ? void 0 : c.options,
11557
+ };
11558
+ this.transaction = {
11559
+ header: {
11560
+ ...(((_a = c === null || c === void 0 ? void 0 : c.options) === null || _a === void 0 ? void 0 : _a.chainId) && { chain_id: c.options.chainId }),
11561
+ ...(((_b = c === null || c === void 0 ? void 0 : c.options) === null || _b === void 0 ? void 0 : _b.rcLimit) && { rc_limit: c.options.rcLimit }),
11562
+ ...(((_c = c === null || c === void 0 ? void 0 : c.options) === null || _c === void 0 ? void 0 : _c.nonce) && { nonce: c.options.nonce }),
11563
+ ...(((_d = c === null || c === void 0 ? void 0 : c.options) === null || _d === void 0 ? void 0 : _d.payer) && { payer: c.options.payer }),
11564
+ ...(((_e = c === null || c === void 0 ? void 0 : c.options) === null || _e === void 0 ? void 0 : _e.payee) && { payee: c.options.payee }),
11565
+ },
11566
+ operations: [],
11567
+ };
11568
+ }
11569
+ /**
11570
+ * Function to push an operation to the transaction. It can be created
11571
+ * in several ways. Example:
11572
+ *
11573
+ * @example
11574
+ * ```ts
11575
+ * const koin = new Contract({
11576
+ * id: "19JntSm8pSNETT9aHTwAUHC5RMoaSmgZPJ",
11577
+ * abi: utils.tokenAbi,
11578
+ * }).functions;
11579
+ * const signer = Signer.fromSeed("my seed");
11580
+ * const provider = new Provider(["https://api.koinos.io"]);
11581
+ * signer.provider = provider;
11582
+ * const tx = new Transaction({ signer });
11583
+ *
11584
+ * // method 1
11585
+ * await tx.pushOperation(koin.transfer, {
11586
+ * from: "1NRYHBYr9qxYQAeVqfdSvyjJemRQ4qD3Mt",
11587
+ * to: "13UdKjYuzfBYbB6bGLQkUN9DJRFPCmG1mU",
11588
+ * value: "1000",
11589
+ * });
11590
+ *
11591
+ * // method 2
11592
+ * await tx.pushOperation(
11593
+ * koin.transfer({
11594
+ * from: "1NRYHBYr9qxYQAeVqfdSvyjJemRQ4qD3Mt",
11595
+ * to: "13UdKjYuzfBYbB6bGLQkUN9DJRFPCmG1mU",
11596
+ * value: "1000",
11597
+ * },{
11598
+ * onlyOperation: true,
11599
+ * })
11600
+ * );
11601
+ *
11602
+ * // method 3
11603
+ * await tx.pushOperation(
11604
+ * await koin.transfer({
11605
+ * from: "1NRYHBYr9qxYQAeVqfdSvyjJemRQ4qD3Mt",
11606
+ * to: "13UdKjYuzfBYbB6bGLQkUN9DJRFPCmG1mU",
11607
+ * value: "1000",
11608
+ * },{
11609
+ * onlyOperation: true,
11610
+ * })
11611
+ * );
11612
+ *
11613
+ * // method 4
11614
+ * const { operation } = await koin.transfer({
11615
+ * from: "1NRYHBYr9qxYQAeVqfdSvyjJemRQ4qD3Mt",
11616
+ * to: "13UdKjYuzfBYbB6bGLQkUN9DJRFPCmG1mU",
11617
+ * value: "1000",
11618
+ * },{
11619
+ * onlyOperation: true,
11620
+ * });
11621
+ * await tx.pushOperation(operation)
11622
+ * ```
11623
+ *
11624
+ */
11625
+ async pushOperation(input, args) {
11626
+ let operation;
11627
+ if (typeof input === "function") {
11628
+ const result = await input(args, { onlyOperation: true });
11629
+ operation = result.operation;
11630
+ }
11631
+ else {
11632
+ let inp;
11633
+ if (input instanceof Promise) {
11634
+ inp = await input;
11635
+ }
11636
+ else {
11637
+ inp = input;
11638
+ }
11639
+ if (inp.operation) {
11640
+ operation = inp.operation;
11641
+ }
11642
+ else {
11643
+ operation = input;
11644
+ }
11645
+ }
11646
+ if (!this.transaction.operations)
11647
+ this.transaction.operations = [];
11648
+ this.transaction.operations.push(operation);
11649
+ }
11650
+ /**
11651
+ * Functon to prepare the transaction (set headers, merkle
11652
+ * root, etc)
11653
+ */
11654
+ async prepare(options) {
11655
+ if (options) {
11656
+ const header = {
11657
+ ...((options === null || options === void 0 ? void 0 : options.chainId) && { chain_id: options.chainId }),
11658
+ ...((options === null || options === void 0 ? void 0 : options.rcLimit) && { rc_limit: options.rcLimit }),
11659
+ ...((options === null || options === void 0 ? void 0 : options.nonce) && { nonce: options.nonce }),
11660
+ ...((options === null || options === void 0 ? void 0 : options.payer) && { payer: options.payer }),
11661
+ ...((options === null || options === void 0 ? void 0 : options.payee) && { payee: options.payee }),
11662
+ };
11663
+ this.transaction.header = {
11664
+ ...this.transaction.header,
11665
+ header,
11666
+ };
11667
+ }
11668
+ if (this.signer) {
11669
+ this.transaction = await this.signer.prepareTransaction(this.transaction);
11670
+ }
11671
+ else {
11672
+ if (!this.transaction.header || !this.transaction.header.payer) {
11673
+ throw new Error("no payer defined");
11674
+ }
11675
+ const signer = Signer_1.Signer.fromSeed("0");
11676
+ signer.provider = this.provider;
11677
+ this.transaction = await signer.prepareTransaction(this.transaction);
11678
+ }
11679
+ return this.transaction;
11680
+ }
11681
+ /**
11682
+ * Function to sign the transaction
11683
+ */
11684
+ async sign(abis) {
11685
+ if (!this.signer)
11686
+ throw new Error("no signer defined");
11687
+ if (!this.transaction.id)
11688
+ await this.prepare();
11689
+ return this.signer.signTransaction(this.transaction, this.options.sendAbis ? abis : undefined);
11690
+ }
11691
+ /**
11692
+ * Function to broadcast the transaction
11693
+ */
11694
+ async send(options) {
11695
+ const opts = {
11696
+ ...this.options,
11697
+ options,
11698
+ };
11699
+ if (!this.transaction.id)
11700
+ await this.prepare();
11701
+ if (this.signer && this.signer.provider) {
11702
+ const { transaction: tx, receipt } = await this.signer.sendTransaction(this.transaction, opts);
11703
+ this.transaction = tx;
11704
+ this.waitFunction = tx.wait;
11705
+ return receipt;
11706
+ }
11707
+ if (!this.provider)
11708
+ throw new Error("provider not defined");
11709
+ if (!this.transaction.signatures || !this.transaction.signatures.length) {
11710
+ throw new Error("transaction without signatures and no signer defined");
11711
+ }
11712
+ if (opts.beforeSend) {
11713
+ await opts.beforeSend(this.transaction, opts);
11714
+ }
11715
+ const { transaction: tx, receipt } = await this.provider.sendTransaction(this.transaction, opts.broadcast);
11716
+ this.transaction = tx;
11717
+ this.waitFunction = tx.wait;
11718
+ return receipt;
11719
+ }
11720
+ async wait(type, timeout) {
11721
+ if (!this.waitFunction)
11722
+ throw new Error("no wait function defined");
11723
+ return this.waitFunction(type, timeout);
11724
+ }
11725
+ }
11726
+ exports.Transaction = Transaction;
11727
+
11728
+
11547
11729
  /***/ }),
11548
11730
 
11549
11731
  /***/ 5738:
@@ -11580,11 +11762,13 @@ const utils = __importStar(__webpack_require__(8593));
11580
11762
  const Contract_1 = __webpack_require__(9822);
11581
11763
  const Signer_1 = __webpack_require__(6991);
11582
11764
  const Provider_1 = __webpack_require__(5635);
11765
+ const Transaction_1 = __webpack_require__(7592);
11583
11766
  const Serializer_1 = __webpack_require__(7187);
11584
11767
  window.utils = utils;
11585
11768
  window.Contract = Contract_1.Contract;
11586
11769
  window.Signer = Signer_1.Signer;
11587
11770
  window.Provider = Provider_1.Provider;
11771
+ window.Transaction = Transaction_1.Transaction;
11588
11772
  window.Serializer = Serializer_1.Serializer;
11589
11773
 
11590
11774