koilib 7.1.1 → 8.1.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
@@ -342,6 +342,37 @@ const receipt = await tx.send();
342
342
  await tx.wait();
343
343
  ```
344
344
 
345
+ ### Estimate mana
346
+
347
+ Here is an example to estimate the mana. First create a transaction and sign it:
348
+
349
+ ```ts
350
+ const tx = new Transaction({ signer, provider });
351
+ await tx.pushOperation(koin.transfer, {
352
+ from: "1NRYHBYr9qxYQAeVqfdSvyjJemRQ4qD3Mt",
353
+ to: "13UdKjYuzfBYbB6bGLQkUN9DJRFPCmG1mU",
354
+ value: "1000",
355
+ });
356
+ await tx.sign();
357
+ ```
358
+
359
+ Now send it with broadcast:false. With this option the rpc node will compute the
360
+ transaction but it will not broadcast it, then it will not be included in a block:
361
+
362
+ ```ts
363
+ let receipt = await tx.send({ broadcast: false });
364
+ ```
365
+
366
+ Finally, adjust the mana by taking a look to the rc used. In this example we will
367
+ adjust it by increasing it 10%. Then sign and send with the default option
368
+ (broadcast:true):
369
+
370
+ ```ts
371
+ tx.adjustRcLimit(Math.round(1.1 * Number(receipt.rc_used)));
372
+ await tx.sign();
373
+ receipt = await tx.send();
374
+ ```
375
+
345
376
  ### Create ABIs
346
377
 
347
378
  ABIs are composed of 2 elements: methods and types.
package/dist/koinos.js CHANGED
@@ -23766,7 +23766,7 @@ class Provider {
23766
23766
  }
23767
23767
  /**
23768
23768
  * Function to get the contract metadata of a specific contract.
23769
- * @param contractId contract ID
23769
+ * @param contractId - contract ID
23770
23770
  *
23771
23771
  * @example
23772
23772
  * ```ts
@@ -23844,6 +23844,56 @@ class Provider {
23844
23844
  contract_id: contractId,
23845
23845
  });
23846
23846
  }
23847
+ /**
23848
+ * Function to get the address of a system contract
23849
+ * @param name - contract name
23850
+ *
23851
+ * @example
23852
+ * * ```ts
23853
+ * const provider = new Provider("https://api.koinos.io");
23854
+ * const result = await provider.invokeGetContractAddress("koin");
23855
+ * console.log(result);
23856
+ *
23857
+ * // { value: { address: '15DJN4a8SgrbGhhGksSBASiSYjGnMU8dGL' } }
23858
+ * ```
23859
+ */
23860
+ async invokeGetContractAddress(name) {
23861
+ const serializer = new Serializer_1.Serializer({
23862
+ nested: {
23863
+ get_address_arguments: {
23864
+ fields: {
23865
+ name: {
23866
+ type: "string",
23867
+ id: 1,
23868
+ },
23869
+ },
23870
+ },
23871
+ get_address_result: {
23872
+ fields: {
23873
+ value: {
23874
+ type: "address_record",
23875
+ id: 1,
23876
+ },
23877
+ },
23878
+ },
23879
+ address_record: {
23880
+ fields: {
23881
+ address: {
23882
+ type: "bytes",
23883
+ id: 1,
23884
+ options: {
23885
+ "(koinos.btype)": "ADDRESS",
23886
+ },
23887
+ },
23888
+ },
23889
+ },
23890
+ },
23891
+ }, {
23892
+ argumentsTypeName: "get_address_arguments",
23893
+ returnTypeName: "get_address_result",
23894
+ });
23895
+ return this.invokeSystemCall(serializer, "get_contract_address", { name });
23896
+ }
23847
23897
  }
23848
23898
  exports.Provider = Provider;
23849
23899
  exports["default"] = Provider;
@@ -24190,7 +24240,6 @@ exports.Signer = void 0;
24190
24240
  /* eslint-disable no-param-reassign, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment */
24191
24241
  const sha256_1 = __webpack_require__(3061);
24192
24242
  const secp = __importStar(__webpack_require__(9656));
24193
- const Transaction_1 = __webpack_require__(7592);
24194
24243
  const utils_1 = __webpack_require__(8593);
24195
24244
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
24196
24245
  // @ts-ignore
@@ -24275,7 +24324,6 @@ class Signer {
24275
24324
  * @param compressed - compressed format is true by default
24276
24325
  * @param provider - provider to connect with the blockchain
24277
24326
  * @param sendOptions - Send options
24278
- * @param rcOptions - options for mana estimation
24279
24327
  * @example
24280
24328
  * ```ts
24281
24329
  * const privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
@@ -24283,42 +24331,8 @@ class Signer {
24283
24331
  * console.log(signer.getAddress());
24284
24332
  * // 1MbL6mG8ASAvSYdoMnGUfG3ZXkmQ2dpL5b
24285
24333
  * ```
24286
- *
24287
- * By default the mana is estimated as 110% the mana used. This
24288
- * estimation is computed using the "broadcast:false" option.
24289
- * For instance, if the transaction consumes 1 mana, the rc_limit
24290
- * will be set to 1.1 mana.
24291
- *
24292
- * You can also adjust the rc limit.
24293
- * @example
24294
- * ```ts
24295
- * const privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
24296
- * cons signer = new Signer({
24297
- * privateKey,
24298
- * provider,
24299
- * rcOptions: {
24300
- * estimateRc: true,
24301
- * // use 2 times rc_used as rc_limit
24302
- * adjustRcLimit: (r) => 2 * Number(r.rc_used),
24303
- * },
24304
- * });
24305
- * ```
24306
- *
24307
- * The rpc node must be highly trusted because the transaction
24308
- * is signed during the estimation of the mana. You can also
24309
- * disable the mana estimation:
24310
- * @example
24311
- * ```ts
24312
- * const privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
24313
- * cons signer = new Signer({
24314
- * privateKey,
24315
- * provider,
24316
- * rcOptions: { estimateRc: false },
24317
- * });
24318
- * ```
24319
24334
  */
24320
24335
  constructor(c) {
24321
- var _a;
24322
24336
  this.compressed = typeof c.compressed === "undefined" ? true : c.compressed;
24323
24337
  this.privateKey = c.privateKey;
24324
24338
  this.provider = c.provider;
@@ -24334,10 +24348,6 @@ class Signer {
24334
24348
  broadcast: true,
24335
24349
  ...c.sendOptions,
24336
24350
  };
24337
- this.rcOptions = (_a = c.rcOptions) !== null && _a !== void 0 ? _a : {
24338
- estimateRc: true,
24339
- adjustRcLimit: (receipt) => Promise.resolve(Math.min(Number(receipt.max_payer_rc), Math.floor(1.1 * Number(receipt.rc_used)))),
24340
- };
24341
24351
  }
24342
24352
  /**
24343
24353
  * Function to import a private key from the WIF
@@ -24457,15 +24467,6 @@ class Signer {
24457
24467
  async signTransaction(tx, _abis) {
24458
24468
  if (!tx.id)
24459
24469
  throw new Error("Missing transaction id");
24460
- // estimation of rcLimit
24461
- if (this.rcOptions.estimateRc &&
24462
- (!tx.signatures || tx.signatures.length === 0)) {
24463
- const receipt = await this.estimateReceipt(tx);
24464
- tx.header.rc_limit = this.rcOptions.adjustRcLimit
24465
- ? await this.rcOptions.adjustRcLimit(receipt)
24466
- : receipt.rc_used;
24467
- tx.id = Transaction_1.Transaction.computeTransactionId(tx.header);
24468
- }
24469
24470
  // multihash 0x1220. 12: code sha2-256. 20: length (32 bytes)
24470
24471
  // tx id is a stringified multihash, need to extract the hash digest only
24471
24472
  const hash = (0, utils_1.toUint8Array)(tx.id.slice(6));
@@ -24514,43 +24515,6 @@ class Signer {
24514
24515
  }
24515
24516
  return this.provider.sendTransaction(transaction, opts.broadcast);
24516
24517
  }
24517
- /**
24518
- * Estimate the receipt associated to the transaction if
24519
- * it sent to the blockchain. It is useful to estimate the
24520
- * consumption of mana.
24521
- * The transaction is signed during this process and sent
24522
- * to the rpc node with the "broadcast:false" option to
24523
- * just compute the transaction without broadcasting it to
24524
- * the network.
24525
- * After that, the initial signatures are restored (if any)
24526
- * and the ones used for the estimation will be removed.
24527
- */
24528
- async estimateReceipt(tx) {
24529
- if (!tx.id)
24530
- throw new Error("Missing transaction id");
24531
- if (!tx.signatures)
24532
- tx.signatures = [];
24533
- const signaturesCopy = [...tx.signatures];
24534
- // sign if there are no signatures
24535
- if (tx.signatures.length === 0) {
24536
- const hash = (0, utils_1.toUint8Array)(tx.id.slice(6));
24537
- const signature = await this.signHash(hash);
24538
- tx.signatures.push((0, utils_1.encodeBase64url)(signature));
24539
- }
24540
- try {
24541
- const { receipt } = await this.sendTransaction(tx, {
24542
- broadcast: false,
24543
- });
24544
- // restore signatures
24545
- tx.signatures = signaturesCopy;
24546
- return receipt;
24547
- }
24548
- catch (error) {
24549
- // restore signatures
24550
- tx.signatures = signaturesCopy;
24551
- throw error;
24552
- }
24553
- }
24554
24518
  /**
24555
24519
  * Function to recover the public key from hash and signature
24556
24520
  * @param hash - hash sha256
@@ -24627,7 +24591,7 @@ class Signer {
24627
24591
  * });
24628
24592
  * ```
24629
24593
  */
24630
- async recoverPublicKeys(txOrBlock, opts) {
24594
+ static async recoverPublicKeys(txOrBlock, opts) {
24631
24595
  let compressed = true;
24632
24596
  if (opts && opts.compressed !== undefined) {
24633
24597
  compressed = opts.compressed;
@@ -24712,7 +24676,7 @@ class Signer {
24712
24676
  * });
24713
24677
  * ```
24714
24678
  */
24715
- async recoverAddresses(txOrBlock, opts) {
24679
+ static async recoverAddresses(txOrBlock, opts) {
24716
24680
  const publicKeys = await this.recoverPublicKeys(txOrBlock, opts);
24717
24681
  return publicKeys.map((publicKey) => (0, utils_1.bitcoinAddress)((0, utils_1.toUint8Array)(publicKey)));
24718
24682
  }
@@ -24876,6 +24840,7 @@ class Transaction {
24876
24840
  ...(((_f = c === null || c === void 0 ? void 0 : c.options) === null || _f === void 0 ? void 0 : _f.payee) && { payee: c.options.payee }),
24877
24841
  },
24878
24842
  operations: [],
24843
+ ...c === null || c === void 0 ? void 0 : c.transaction,
24879
24844
  };
24880
24845
  }
24881
24846
  /**
@@ -24893,14 +24858,17 @@ class Transaction {
24893
24858
  * signer.provider = provider;
24894
24859
  * const tx = new Transaction({ signer });
24895
24860
  *
24896
- * // method 1
24861
+ * // Method 1 (using 2 arguments)
24862
+ * // note that with 2 arguments it is not necessary to
24863
+ * // set "onlyOperation: true". For the rest of the
24864
+ * // methods it's necessary to do that.
24897
24865
  * await tx.pushOperation(koin.transfer, {
24898
24866
  * from: "1NRYHBYr9qxYQAeVqfdSvyjJemRQ4qD3Mt",
24899
24867
  * to: "13UdKjYuzfBYbB6bGLQkUN9DJRFPCmG1mU",
24900
24868
  * value: "1000",
24901
24869
  * });
24902
24870
  *
24903
- * // method 2
24871
+ * // Method 2
24904
24872
  * await tx.pushOperation(
24905
24873
  * koin.transfer({
24906
24874
  * from: "1NRYHBYr9qxYQAeVqfdSvyjJemRQ4qD3Mt",
@@ -24911,7 +24879,7 @@ class Transaction {
24911
24879
  * })
24912
24880
  * );
24913
24881
  *
24914
- * // method 3
24882
+ * // Method 3
24915
24883
  * await tx.pushOperation(
24916
24884
  * await koin.transfer({
24917
24885
  * from: "1NRYHBYr9qxYQAeVqfdSvyjJemRQ4qD3Mt",
@@ -24922,7 +24890,7 @@ class Transaction {
24922
24890
  * })
24923
24891
  * );
24924
24892
  *
24925
- * // method 4
24893
+ * // Method 4
24926
24894
  * const { operation } = await koin.transfer({
24927
24895
  * from: "1NRYHBYr9qxYQAeVqfdSvyjJemRQ4qD3Mt",
24928
24896
  * to: "13UdKjYuzfBYbB6bGLQkUN9DJRFPCmG1mU",
@@ -25062,6 +25030,21 @@ class Transaction {
25062
25030
  this.transaction = await Transaction.prepareTransaction(this.transaction, this.provider, (_a = this.signer) === null || _a === void 0 ? void 0 : _a.getAddress());
25063
25031
  return this.transaction;
25064
25032
  }
25033
+ /**
25034
+ * Update the rc limit with a new value and update the
25035
+ * transaction ID accordingly. The signatures will be removed
25036
+ * if the transaction ID changed
25037
+ */
25038
+ adjustRcLimit(newRcLimit) {
25039
+ if (!this.transaction.header)
25040
+ throw new Error("transaction header not defined");
25041
+ this.transaction.header.rc_limit = newRcLimit;
25042
+ const newTxId = Transaction.computeTransactionId(this.transaction.header);
25043
+ if (this.transaction.id !== newTxId) {
25044
+ this.transaction.signatures = [];
25045
+ }
25046
+ this.transaction.id = newTxId;
25047
+ }
25065
25048
  /**
25066
25049
  * Function to sign the transaction
25067
25050
  */