@dusk-network/app-core 0.2.0 → 0.3.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.
package/src/history.d.ts CHANGED
@@ -108,9 +108,15 @@ export type HistoryEntry = {
108
108
  /**
109
109
  * What left or reached the account, in lux, fees aside. A gas refund the
110
110
  * transaction paid to another account than its sender is what left the
111
- * sender, beside its fee, and what reached that account.
111
+ * sender, beside its fee, and what reached that account (`refund`).
112
112
  */
113
113
  value: bigint;
114
+ /**
115
+ * Of `value`, the unspent gas the transaction paid to another account
116
+ * than its sender, in lux; left out when there's none. An entry an
117
+ * earlier version kept has none, whatever its value holds.
118
+ */
119
+ refund?: bigint;
114
120
  /** The memo as text, or as 0x hex when it isn't text; "" for none. */
115
121
  memo: string;
116
122
  /** Whether the account paid the fee. */
@@ -160,9 +166,10 @@ export type EntryDetails = {
160
166
  * stores as JSON, with its details once read, and any fields the app keeps
161
167
  * beside them (as its hook's facts).
162
168
  */
163
- export type KeptEntry = Omit<HistoryEntry, "value"> &
169
+ export type KeptEntry = Omit<HistoryEntry, "value" | "refund"> &
164
170
  Partial<EntryDetails> & {
165
171
  value: string;
172
+ refund?: string;
166
173
  [field: string]: unknown;
167
174
  };
168
175
 
@@ -366,6 +373,8 @@ export type RangeOptions = {
366
373
  direction?: "in" | "out" | "both";
367
374
  /** The most pages to read, 1 by default. */
368
375
  pages?: number;
376
+ /** The first page to read, 1 by default: a read of the same range carries on where an earlier one stopped. */
377
+ page?: number;
369
378
  };
370
379
 
371
380
  /**
package/src/history.js CHANGED
@@ -437,6 +437,8 @@ export function parseHistoryEntry(account, raw) {
437
437
 
438
438
  let inValue = 0n;
439
439
  let outValue = 0n;
440
+ // Of those, unspent gas paid to another account than its sender.
441
+ let refunded = 0n;
440
442
  /** @type {bigint | null} */
441
443
  let selfValue = null;
442
444
  let payer = false;
@@ -493,9 +495,11 @@ export function parseHistoryEntry(account, raw) {
493
495
 
494
496
  if (d.refund_info[0] === account) {
495
497
  inValue += refund;
498
+ refunded += refund;
496
499
  receivedFrom ??= d.sender;
497
500
  } else if (d.sender === account) {
498
501
  outValue += refund;
502
+ refunded += refund;
499
503
  }
500
504
  }
501
505
  } else if (topic === "convert") {
@@ -570,7 +574,7 @@ export function parseHistoryEntry(account, raw) {
570
574
  else counterparty = direction === "out" ? sentTo : receivedFrom;
571
575
  }
572
576
 
573
- return { blockHeight, counterparty: counterparty || null, direction, hash, kind, memo, payer, value };
577
+ return { blockHeight, counterparty: counterparty || null, direction, hash, kind, memo, payer, value, ...(refunded ? { refund: refunded } : {}) };
574
578
  }
575
579
 
576
580
  /**
@@ -589,9 +593,10 @@ export function historyEntry(row, { network = null, pools } = {}) {
589
593
  const sozu = Boolean(
590
594
  net && row.contractId && row.fnName && isSozuCall(net, { contractId: row.contractId, fnName: row.fnName }, { pools: pools ?? [] })
591
595
  );
596
+ const { refund, ...fields } = row;
592
597
 
593
598
  return {
594
- ...row,
599
+ ...fields,
595
600
  blockHash: row.blockHash,
596
601
  blockHeight: row.blockHeight,
597
602
  bridge: row.bridge,
@@ -615,6 +620,7 @@ export function historyEntry(row, { network = null, pools } = {}) {
615
620
  success: row.success ?? null,
616
621
  timestamp: row.timestamp ?? null,
617
622
  value: u64(row.value),
623
+ ...(refund === undefined ? {} : { refund: u64(refund) }),
618
624
  };
619
625
  }
620
626
 
@@ -629,7 +635,7 @@ export function historyEntry(row, { network = null, pools } = {}) {
629
635
  function keptOf(entry) {
630
636
  if (typeof entry?.value !== "bigint") return /** @type {KeptEntry} */ (entry);
631
637
 
632
- const { route, sozu, ...rest } = /** @type {ReadEntry} */ (entry);
638
+ const { refund, route, sozu, ...rest } = /** @type {ReadEntry} */ (entry);
633
639
  const { gasPrice, gasSpent } = rest;
634
640
 
635
641
  return {
@@ -638,6 +644,7 @@ function keptOf(entry) {
638
644
  success: rest.success ?? undefined,
639
645
  timestamp: rest.detailsAt ? rest.timestamp : undefined,
640
646
  value: rest.value.toString(),
647
+ ...(refund === undefined ? {} : { refund: refund.toString() }),
641
648
  };
642
649
  }
643
650
 
@@ -809,7 +816,7 @@ function nextWindow(window, count) {
809
816
  * @param {RangeOptions} options
810
817
  * @returns {Promise<{ entries: HistoryEntry[], complete: boolean, requests: number }>}
811
818
  */
812
- export async function historyRange(archive, { account, from = 0, to, direction = "both", pages = 1 }) {
819
+ export async function historyRange(archive, { account, from = 0, to, direction = "both", pages = 1, page: first = 1 }) {
813
820
  /** @type {Record<string, ReadonlyArray<"in" | "out">>} */
814
821
  const ASKED = { both: ["out", "in"], in: ["in"], out: ["out"] };
815
822
  const aliases = Object.hasOwn(ASKED, direction) ? ASKED[direction] : null;
@@ -820,8 +827,9 @@ export async function historyRange(archive, { account, from = 0, to, direction =
820
827
  const entries = [];
821
828
  let complete = false;
822
829
  let requests = 0;
830
+ const start = Math.max(1, Math.floor(Number(first)) || 1);
823
831
 
824
- for (let page = 1; page <= Math.max(1, Math.floor(pages)) && !complete; page += 1) {
832
+ for (let page = start; page < start + Math.max(1, Math.floor(pages)) && !complete; page += 1) {
825
833
  requests += 1;
826
834
 
827
835
  const data = await archive.query(rangeQuery(account, { aliases, from, page, to }));
@@ -977,7 +985,7 @@ export async function fetchTxNonce(archive, hash) {
977
985
  * @param {HistoryEntry} entry
978
986
  * @returns {KeptEntry}
979
987
  */
980
- const toKept = (entry) => ({ ...entry, value: entry.value.toString() });
988
+ const toKept = ({ refund, ...entry }) => ({ ...entry, value: entry.value.toString(), ...(refund === undefined ? {} : { refund: refund.toString() }) });
981
989
 
982
990
  /**
983
991
  * @param {HistoryOptions} options