polkadot-api 1.9.13 → 1.10.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/dist/index.js CHANGED
@@ -490,14 +490,13 @@ const createStorageEntry = (pallet, name, chainHead, getWatchEntries, {
490
490
  const { at: _at } = isLastArgOptional ? lastArg : {};
491
491
  const at = _at ?? null;
492
492
  if (isSystemNumber)
493
- return chainHead.bestBlocks$.pipe(
493
+ return chainHead.pinnedBlocks$.pipe(
494
494
  rxjs.map((blocks) => {
495
- if (at === "finalized" || !at) return blocks.at(-1);
496
- if (at === "best") return blocks.at(0);
497
- return blocks.find((block) => block.hash === at);
498
- }),
499
- rxjs.map((block) => {
500
- if (!block) throw new observableClient.NotBestBlockError();
495
+ const hash = at === "finalized" || !at ? blocks.finalized : at === "best" ? blocks.best : at;
496
+ const block = blocks.blocks.get(hash);
497
+ if (!block) {
498
+ throw new observableClient.BlockNotPinnedError(hash, "System.Number");
499
+ }
501
500
  return block.number;
502
501
  }),
503
502
  rxjs.distinctUntilChanged(),
@@ -740,7 +739,7 @@ const lenToDecoder = {
740
739
  4: substrateBindings.u32.dec,
741
740
  8: substrateBindings.u64.dec
742
741
  };
743
- const getNonce$ = (call$, from, at) => call$(at, NONCE_RUNTIME_CALL, from).pipe(
742
+ const getNonceAtBlock$ = (call$, from, at) => call$(at, NONCE_RUNTIME_CALL, from).pipe(
744
743
  rxjs.map((result) => {
745
744
  const bytes = utils.fromHex(result);
746
745
  const decoder = lenToDecoder[bytes.length];
@@ -750,7 +749,7 @@ const getNonce$ = (call$, from, at) => call$(at, NONCE_RUNTIME_CALL, from).pipe(
750
749
  })
751
750
  );
752
751
  const createTx = (chainHead, signer, callData, atBlock, customSignedExtensions, hinted = {}) => rxjs.combineLatest([
753
- hinted.nonce ? rxjs.of(hinted.nonce) : getNonce$(chainHead.call$, utils.toHex(signer.publicKey), atBlock.hash),
752
+ hinted.nonce ? rxjs.of(hinted.nonce) : getNonce$(chainHead, utils.toHex(signer.publicKey)),
754
753
  chainHead.getRuntimeContext$(atBlock.hash),
755
754
  chainHead.genesis$
756
755
  ]).pipe(
@@ -783,6 +782,54 @@ const createTx = (chainHead, signer, callData, atBlock, customSignedExtensions,
783
782
  );
784
783
  })
785
784
  );
785
+ const getNonce$ = (chainHead, from) => {
786
+ const followHead$ = (head) => chainHead.newBlocks$.pipe(
787
+ rxjs.scan((acc, block) => block.parent === acc ? block.hash : acc, head),
788
+ rxjs.startWith(head),
789
+ rxjs.distinctUntilChanged()
790
+ );
791
+ const followNonce$ = (head) => followHead$(head).pipe(
792
+ rxjs.take(2),
793
+ rxjs.switchMap((hash) => getNonceAtBlock$(chainHead.call$, from, hash))
794
+ );
795
+ const getHeadsNonce$ = (heads) => rxjs.combineLatest(
796
+ heads.map(
797
+ (head) => followNonce$(head).pipe(
798
+ rxjs.map((value) => ({
799
+ success: true,
800
+ value
801
+ })),
802
+ rxjs.catchError(
803
+ (err) => rxjs.of({
804
+ success: false,
805
+ value: err
806
+ })
807
+ )
808
+ )
809
+ )
810
+ ).pipe(rxjs.take(1));
811
+ return chainHead.pinnedBlocks$.pipe(
812
+ rxjs.filter((v) => !v.recovering && v.blocks.size > 0),
813
+ rxjs.take(1),
814
+ rxjs.map(({ blocks, best }) => {
815
+ const bestBlock = blocks.get(best);
816
+ return [...blocks.values()].filter(
817
+ (v) => !v.unpinnable && v.children.size === 0 && v.number >= bestBlock.number
818
+ ).map((v) => v.hash);
819
+ }),
820
+ rxjs.switchMap(getHeadsNonce$),
821
+ rxjs.map((result) => {
822
+ const winner = result.reduce(
823
+ (acc, v) => v.success ? v.value >= (acc ?? 0) ? v.value : acc : acc,
824
+ null
825
+ );
826
+ if (winner == null) {
827
+ throw result[0].value;
828
+ }
829
+ return winner;
830
+ })
831
+ );
832
+ };
786
833
 
787
834
  var __defProp = Object.defineProperty;
788
835
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
@@ -895,7 +942,7 @@ const submit$ = (chainHead, broadcastTx$, tx, at, emitSign = false) => {
895
942
  rxjs.take(1),
896
943
  rxjs.map((blocks) => {
897
944
  const block = blocks.blocks.get(at);
898
- return block && !block.unpinned ? block.hash : blocks.finalized;
945
+ return block ? block.hash : blocks.finalized;
899
946
  })
900
947
  );
901
948
  const validate$ = at$.pipe(
@@ -1510,6 +1557,7 @@ function createClient(provider) {
1510
1557
  const { broadcastTx$ } = client;
1511
1558
  return {
1512
1559
  getChainSpecData,
1560
+ blocks$: chainHead.newBlocks$,
1513
1561
  finalizedBlock$: chainHead.finalized$,
1514
1562
  getFinalizedBlock: () => rxjs.firstValueFrom(chainHead.finalized$),
1515
1563
  bestBlocks$: chainHead.bestBlocks$,
@@ -1614,6 +1662,53 @@ const getOfflineApi = async ({ genesis: genesisHex, getMetadata }) => {
1614
1662
  return { constants, tx };
1615
1663
  };
1616
1664
 
1665
+ const withError = (builder, errName) => (pallet, name) => {
1666
+ try {
1667
+ return builder(pallet, name);
1668
+ } catch {
1669
+ throw new Error(`Runtime entry ${errName}(${pallet}.${name}) not found`);
1670
+ }
1671
+ };
1672
+ const getTypedCodecs = async (descriptors) => {
1673
+ const metadata = substrateBindings.decAnyMetadata(await descriptors.getMetadata()).metadata.value;
1674
+ const target = {};
1675
+ const createProxy = (propCall) => new Proxy(target, {
1676
+ get(_, prop) {
1677
+ return propCall(prop);
1678
+ }
1679
+ });
1680
+ const createProxyPath = (pathCall) => {
1681
+ const cache = {};
1682
+ return createProxy((a) => {
1683
+ if (!cache[a]) cache[a] = {};
1684
+ return createProxy((b) => {
1685
+ if (!cache[a][b]) cache[a][b] = pathCall(a, b);
1686
+ return cache[a][b];
1687
+ });
1688
+ });
1689
+ };
1690
+ const lookup = metadataBuilders.getLookupFn(metadata);
1691
+ const {
1692
+ buildRuntimeCall,
1693
+ buildStorage,
1694
+ buildConstant,
1695
+ buildCall,
1696
+ buildEvent
1697
+ } = metadataBuilders.getDynamicBuilder(lookup);
1698
+ return {
1699
+ query: createProxyPath((...a) => {
1700
+ const { value, args } = withError(buildStorage, "Storage")(...a);
1701
+ return { value, args };
1702
+ }),
1703
+ tx: createProxyPath((...a) => withError(buildCall, "Call")(...a).codec),
1704
+ event: createProxyPath(
1705
+ (...args) => withError(buildEvent, "Event")(...args).codec
1706
+ ),
1707
+ apis: createProxyPath(withError(buildRuntimeCall, "Runtime API")),
1708
+ constants: createProxyPath(withError(buildConstant, "Constant"))
1709
+ };
1710
+ };
1711
+
1617
1712
  Object.defineProperty(exports, "CompatibilityLevel", {
1618
1713
  enumerable: true,
1619
1714
  get: function () { return metadataCompatibility.CompatibilityLevel; }
@@ -1645,4 +1740,5 @@ Object.defineProperty(exports, "getSs58AddressInfo", {
1645
1740
  exports.InvalidTxError = InvalidTxError;
1646
1741
  exports.createClient = createClient;
1647
1742
  exports.getOfflineApi = getOfflineApi;
1743
+ exports.getTypedCodecs = getTypedCodecs;
1648
1744
  //# sourceMappingURL=index.js.map