koilib 7.0.0 → 8.0.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
@@ -23682,7 +23682,32 @@ class Provider {
23682
23682
  * and the transaction with the arrow function "wait" (see [[wait]])
23683
23683
  */
23684
23684
  async sendTransaction(transaction, broadcast = true) {
23685
- const response = await this.call("chain.submit_transaction", { transaction, broadcast });
23685
+ let response;
23686
+ try {
23687
+ response = await this.call("chain.submit_transaction", { transaction, broadcast });
23688
+ }
23689
+ catch (error) {
23690
+ if (!error.message.includes("rpc failed, context deadline exceeded")) {
23691
+ throw error;
23692
+ }
23693
+ response = {
23694
+ receipt: {
23695
+ id: transaction.id,
23696
+ payer: transaction.header.payer,
23697
+ max_payer_rc: "",
23698
+ rc_limit: transaction.header.rc_limit.toString(),
23699
+ rc_used: "",
23700
+ disk_storage_used: "",
23701
+ network_bandwidth_used: "",
23702
+ compute_bandwidth_used: "",
23703
+ reverted: false,
23704
+ events: [],
23705
+ state_delta_entries: [],
23706
+ logs: [],
23707
+ rpc_error: JSON.parse(error.message),
23708
+ },
23709
+ };
23710
+ }
23686
23711
  if (broadcast) {
23687
23712
  transaction.wait = async (type = "byBlock", timeout = 15000) => {
23688
23713
  return this.wait(transaction.id, type, timeout);
@@ -23741,7 +23766,7 @@ class Provider {
23741
23766
  }
23742
23767
  /**
23743
23768
  * Function to get the contract metadata of a specific contract.
23744
- * @param contractId contract ID
23769
+ * @param contractId - contract ID
23745
23770
  *
23746
23771
  * @example
23747
23772
  * ```ts
@@ -24165,7 +24190,6 @@ exports.Signer = void 0;
24165
24190
  /* eslint-disable no-param-reassign, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment */
24166
24191
  const sha256_1 = __webpack_require__(3061);
24167
24192
  const secp = __importStar(__webpack_require__(9656));
24168
- const Transaction_1 = __webpack_require__(7592);
24169
24193
  const utils_1 = __webpack_require__(8593);
24170
24194
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
24171
24195
  // @ts-ignore
@@ -24250,7 +24274,6 @@ class Signer {
24250
24274
  * @param compressed - compressed format is true by default
24251
24275
  * @param provider - provider to connect with the blockchain
24252
24276
  * @param sendOptions - Send options
24253
- * @param rcOptions - options for mana estimation
24254
24277
  * @example
24255
24278
  * ```ts
24256
24279
  * const privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
@@ -24258,42 +24281,8 @@ class Signer {
24258
24281
  * console.log(signer.getAddress());
24259
24282
  * // 1MbL6mG8ASAvSYdoMnGUfG3ZXkmQ2dpL5b
24260
24283
  * ```
24261
- *
24262
- * By default the mana is estimated as 110% the mana used. This
24263
- * estimation is computed using the "broadcast:false" option.
24264
- * For instance, if the transaction consumes 1 mana, the rc_limit
24265
- * will be set to 1.1 mana.
24266
- *
24267
- * You can also adjust the rc limit.
24268
- * @example
24269
- * ```ts
24270
- * const privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
24271
- * cons signer = new Signer({
24272
- * privateKey,
24273
- * provider,
24274
- * rcOptions: {
24275
- * estimateRc: true,
24276
- * // use 2 times rc_used as rc_limit
24277
- * adjustRcLimit: (r) => 2 * Number(r.rc_used),
24278
- * },
24279
- * });
24280
- * ```
24281
- *
24282
- * The rpc node must be highly trusted because the transaction
24283
- * is signed during the estimation of the mana. You can also
24284
- * disable the mana estimation:
24285
- * @example
24286
- * ```ts
24287
- * const privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
24288
- * cons signer = new Signer({
24289
- * privateKey,
24290
- * provider,
24291
- * rcOptions: { estimateRc: false },
24292
- * });
24293
- * ```
24294
24284
  */
24295
24285
  constructor(c) {
24296
- var _a;
24297
24286
  this.compressed = typeof c.compressed === "undefined" ? true : c.compressed;
24298
24287
  this.privateKey = c.privateKey;
24299
24288
  this.provider = c.provider;
@@ -24309,10 +24298,6 @@ class Signer {
24309
24298
  broadcast: true,
24310
24299
  ...c.sendOptions,
24311
24300
  };
24312
- this.rcOptions = (_a = c.rcOptions) !== null && _a !== void 0 ? _a : {
24313
- estimateRc: true,
24314
- adjustRcLimit: (receipt) => Promise.resolve(Math.min(Number(receipt.max_payer_rc), Math.floor(1.1 * Number(receipt.rc_used)))),
24315
- };
24316
24301
  }
24317
24302
  /**
24318
24303
  * Function to import a private key from the WIF
@@ -24432,15 +24417,6 @@ class Signer {
24432
24417
  async signTransaction(tx, _abis) {
24433
24418
  if (!tx.id)
24434
24419
  throw new Error("Missing transaction id");
24435
- // estimation of rcLimit
24436
- if (this.rcOptions.estimateRc &&
24437
- (!tx.signatures || tx.signatures.length === 0)) {
24438
- const receipt = await this.estimateReceipt(tx);
24439
- tx.header.rc_limit = this.rcOptions.adjustRcLimit
24440
- ? await this.rcOptions.adjustRcLimit(receipt)
24441
- : receipt.rc_used;
24442
- tx.id = Transaction_1.Transaction.computeTransactionId(tx.header);
24443
- }
24444
24420
  // multihash 0x1220. 12: code sha2-256. 20: length (32 bytes)
24445
24421
  // tx id is a stringified multihash, need to extract the hash digest only
24446
24422
  const hash = (0, utils_1.toUint8Array)(tx.id.slice(6));
@@ -24489,43 +24465,6 @@ class Signer {
24489
24465
  }
24490
24466
  return this.provider.sendTransaction(transaction, opts.broadcast);
24491
24467
  }
24492
- /**
24493
- * Estimate the receipt associated to the transaction if
24494
- * it sent to the blockchain. It is useful to estimate the
24495
- * consumption of mana.
24496
- * The transaction is signed during this process and sent
24497
- * to the rpc node with the "broadcast:false" option to
24498
- * just compute the transaction without broadcasting it to
24499
- * the network.
24500
- * After that, the initial signatures are restored (if any)
24501
- * and the ones used for the estimation will be removed.
24502
- */
24503
- async estimateReceipt(tx) {
24504
- if (!tx.id)
24505
- throw new Error("Missing transaction id");
24506
- if (!tx.signatures)
24507
- tx.signatures = [];
24508
- const signaturesCopy = [...tx.signatures];
24509
- // sign if there are no signatures
24510
- if (tx.signatures.length === 0) {
24511
- const hash = (0, utils_1.toUint8Array)(tx.id.slice(6));
24512
- const signature = await this.signHash(hash);
24513
- tx.signatures.push((0, utils_1.encodeBase64url)(signature));
24514
- }
24515
- try {
24516
- const { receipt } = await this.sendTransaction(tx, {
24517
- broadcast: false,
24518
- });
24519
- // restore signatures
24520
- tx.signatures = signaturesCopy;
24521
- return receipt;
24522
- }
24523
- catch (error) {
24524
- // restore signatures
24525
- tx.signatures = signaturesCopy;
24526
- throw error;
24527
- }
24528
- }
24529
24468
  /**
24530
24469
  * Function to recover the public key from hash and signature
24531
24470
  * @param hash - hash sha256
@@ -24602,7 +24541,7 @@ class Signer {
24602
24541
  * });
24603
24542
  * ```
24604
24543
  */
24605
- async recoverPublicKeys(txOrBlock, opts) {
24544
+ static async recoverPublicKeys(txOrBlock, opts) {
24606
24545
  let compressed = true;
24607
24546
  if (opts && opts.compressed !== undefined) {
24608
24547
  compressed = opts.compressed;
@@ -24687,7 +24626,7 @@ class Signer {
24687
24626
  * });
24688
24627
  * ```
24689
24628
  */
24690
- async recoverAddresses(txOrBlock, opts) {
24629
+ static async recoverAddresses(txOrBlock, opts) {
24691
24630
  const publicKeys = await this.recoverPublicKeys(txOrBlock, opts);
24692
24631
  return publicKeys.map((publicKey) => (0, utils_1.bitcoinAddress)((0, utils_1.toUint8Array)(publicKey)));
24693
24632
  }
@@ -24851,6 +24790,7 @@ class Transaction {
24851
24790
  ...(((_f = c === null || c === void 0 ? void 0 : c.options) === null || _f === void 0 ? void 0 : _f.payee) && { payee: c.options.payee }),
24852
24791
  },
24853
24792
  operations: [],
24793
+ ...c === null || c === void 0 ? void 0 : c.transaction,
24854
24794
  };
24855
24795
  }
24856
24796
  /**
@@ -25037,6 +24977,21 @@ class Transaction {
25037
24977
  this.transaction = await Transaction.prepareTransaction(this.transaction, this.provider, (_a = this.signer) === null || _a === void 0 ? void 0 : _a.getAddress());
25038
24978
  return this.transaction;
25039
24979
  }
24980
+ /**
24981
+ * Update the rc limit with a new value and update the
24982
+ * transaction ID accordingly. The signatures will be removed
24983
+ * if the transaction ID changed
24984
+ */
24985
+ adjustRcLimit(newRcLimit) {
24986
+ if (!this.transaction.header)
24987
+ throw new Error("transaction header not defined");
24988
+ this.transaction.header.rc_limit = newRcLimit;
24989
+ const newTxId = Transaction.computeTransactionId(this.transaction.header);
24990
+ if (this.transaction.id !== newTxId) {
24991
+ this.transaction.signatures = [];
24992
+ }
24993
+ this.transaction.id = newTxId;
24994
+ }
25040
24995
  /**
25041
24996
  * Function to sign the transaction
25042
24997
  */