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