@talismn/sapi 0.0.11 → 0.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.
@@ -1,3 +1,4 @@
1
+ export declare const MAX_SUPPORTED_METADATA_VERSION = 15;
1
2
  type RpcSendFunc = <T>(method: string, params: unknown[], isCacheable?: boolean) => Promise<T>;
2
3
  /**
3
4
  * Fetches the highest supported version of metadata from the chain.
@@ -1,3 +1,3 @@
1
1
  import { SapiConnectorProps } from "../types";
2
2
  export type SapiConnector = Required<SapiConnectorProps>;
3
- export declare const getSapiConnector: ({ chainId, send, submit }: SapiConnectorProps) => SapiConnector;
3
+ export declare const getSapiConnector: ({ chainId, send, submit, submitWithBittensorMevShield, }: SapiConnectorProps) => SapiConnector;
@@ -0,0 +1,6 @@
1
+ import { SignerPayloadJSON } from "@polkadot/types/types";
2
+ import { Chain } from "./types";
3
+ export type ScaleApiSubmitMode = "default" | "bittensor-mev-shield";
4
+ export declare const submit: (chain: Chain, payload: SignerPayloadJSON, signature?: `0x${string}`, txInfo?: unknown, mode?: ScaleApiSubmitMode) => Promise<{
5
+ hash: `0x${string}`;
6
+ }>;
@@ -1,5 +1,6 @@
1
1
  import { ExtDef } from "@polkadot/types/extrinsic/signedExtensions/types";
2
2
  import { SignerPayloadJSON } from "@polkadot/types/types";
3
+ import { ScaleApiSubmitMode } from "./helpers/submit";
3
4
  import { Chain } from "./helpers/types";
4
5
  import { DecodedCall, PayloadSignerConfig, SapiConnectorProps } from "./types";
5
6
  export type ScaleApi = NonNullable<ReturnType<typeof getScaleApi>>;
@@ -38,7 +39,7 @@ export declare const getScaleApi: (connector: SapiConnectorProps, hexMetadata: `
38
39
  getFeeEstimate: (payload: SignerPayloadJSON) => Promise<bigint>;
39
40
  getRuntimeCallValue: <T>(apiName: string, method: string, args: unknown[]) => Promise<T>;
40
41
  getTypeRegistry: (payload: SignerPayloadJSON) => import("@polkadot/types").TypeRegistry;
41
- submit: (payload: SignerPayloadJSON, signature?: `0x${string}`, txInfo?: unknown) => Promise<{
42
+ submit: (payload: SignerPayloadJSON, signature?: `0x${string}`, txInfo?: unknown, mode?: ScaleApiSubmitMode) => Promise<{
42
43
  hash: `0x${string}`;
43
44
  }>;
44
45
  getCallDocs: (pallet: string, method: string) => string | null;
@@ -15,4 +15,7 @@ export type SapiConnectorProps = {
15
15
  submit?: (payload: SignerPayloadJSON, signature?: `0x${string}`, txInfo?: any) => Promise<{
16
16
  hash: `0x${string}`;
17
17
  }>;
18
+ submitWithBittensorMevShield?: (payload: SignerPayloadJSON, txInfo?: any) => Promise<{
19
+ hash: `0x${string}`;
20
+ }>;
18
21
  };
@@ -315,13 +315,18 @@ const getFeeEstimate = async (chain, payload, chainInfo) => {
315
315
  const getSapiConnector = ({
316
316
  chainId,
317
317
  send,
318
- submit
318
+ submit,
319
+ submitWithBittensorMevShield
319
320
  }) => ({
320
321
  chainId,
321
322
  send,
322
323
  submit: (...args) => {
323
324
  if (submit) return submit(...args);
324
325
  throw new Error("submit handler not provided");
326
+ },
327
+ submitWithBittensorMevShield: (...args) => {
328
+ if (submitWithBittensorMevShield) return submitWithBittensorMevShield(...args);
329
+ throw new Error("submitWithBittensorMevShield handler not provided");
325
330
  }
326
331
  });
327
332
 
@@ -340,16 +345,18 @@ const getPayloadWithMetadataHash = (chain, chainInfo, payload) => {
340
345
  specName,
341
346
  specVersion
342
347
  } = chainInfo;
343
-
344
- // since ultimately this needs a V15 object, would be nice if this accepted one directly as input
345
- const merkleizedMetadata = merkleizeMetadata.merkleizeMetadata(chain.hexMetadata, {
348
+ const metadataHashInputs = {
346
349
  tokenSymbol,
347
350
  decimals,
348
351
  base58Prefix,
349
352
  specName,
350
353
  specVersion
351
- });
354
+ };
355
+
356
+ // since ultimately this needs a V15 object, would be nice if this accepted one directly as input
357
+ const merkleizedMetadata = merkleizeMetadata.merkleizeMetadata(chain.hexMetadata, metadataHashInputs);
352
358
  const metadataHash = utils.toHex(merkleizedMetadata.digest());
359
+ log.log("metadataHash", metadataHash, metadataHashInputs);
353
360
  const payloadWithMetadataHash = {
354
361
  ...payload,
355
362
  mode: 1,
@@ -480,6 +487,16 @@ const getSignerPayloadJSON = async (chain, palletName, methodName, args, signerC
480
487
  };
481
488
  };
482
489
 
490
+ const submit = async (chain, payload, signature, txInfo, mode) => {
491
+ switch (mode) {
492
+ case "bittensor-mev-shield":
493
+ if (signature) throw new Error("Signature should not be provided when using bittensor-mev-shield mode");
494
+ return chain.connector.submitWithBittensorMevShield(payload, txInfo);
495
+ default:
496
+ return chain.connector.submit(payload, signature, txInfo);
497
+ }
498
+ };
499
+
483
500
  const getScaleApi = (connector, hexMetadata, token, hasCheckMetadataHash, signedExtensions, registryTypes) => {
484
501
  const {
485
502
  unifiedMetadata: metadata,
@@ -521,7 +538,7 @@ const getScaleApi = (connector, hexMetadata, token, hasCheckMetadataHash, signed
521
538
  getFeeEstimate: payload => getFeeEstimate(chain, payload, chainInfo),
522
539
  getRuntimeCallValue: (apiName, method, args) => getRuntimeCallResult(chain, apiName, method, args),
523
540
  getTypeRegistry: payload => getTypeRegistry(chain, payload),
524
- submit: (payload, signature, txInfo) => chain.connector.submit(payload, signature, txInfo),
541
+ submit: (payload, signature, txInfo, mode) => submit(chain, payload, signature, txInfo, mode),
525
542
  getCallDocs: (pallet, method) => getCallDocs(chain, pallet, method),
526
543
  getDryRunCall: (from, decodedCall) => getDryRunCall(chain, from, decodedCall),
527
544
  isApiAvailable: (name, method) => isApiAvailable(chain, name, method)
@@ -531,7 +548,8 @@ const getScaleApi = (connector, hexMetadata, token, hasCheckMetadataHash, signed
531
548
  const MAGIC_NUMBER = 1635018093;
532
549
 
533
550
  // it's important to set a max because some chains also return high invalid version numbers in the metadata_versions list (ex on Polkadot, related to JAM?)
534
- const MAX_SUPPORTED_METADATA_VERSION = 16;
551
+ const MAX_SUPPORTED_METADATA_VERSION = 15; // v16 sometimes outputs different metadata hashes, ignore v16 until that is fixed in PAPI
552
+
535
553
  /**
536
554
  * Fetches the highest supported version of metadata from the chain.
537
555
  *
@@ -578,5 +596,6 @@ const normalizeMetadata = metadata => {
578
596
  return `0x${metadata.slice(magicNumberIndex)}`;
579
597
  };
580
598
 
599
+ exports.MAX_SUPPORTED_METADATA_VERSION = MAX_SUPPORTED_METADATA_VERSION;
581
600
  exports.fetchBestMetadata = fetchBestMetadata;
582
601
  exports.getScaleApi = getScaleApi;
@@ -315,13 +315,18 @@ const getFeeEstimate = async (chain, payload, chainInfo) => {
315
315
  const getSapiConnector = ({
316
316
  chainId,
317
317
  send,
318
- submit
318
+ submit,
319
+ submitWithBittensorMevShield
319
320
  }) => ({
320
321
  chainId,
321
322
  send,
322
323
  submit: (...args) => {
323
324
  if (submit) return submit(...args);
324
325
  throw new Error("submit handler not provided");
326
+ },
327
+ submitWithBittensorMevShield: (...args) => {
328
+ if (submitWithBittensorMevShield) return submitWithBittensorMevShield(...args);
329
+ throw new Error("submitWithBittensorMevShield handler not provided");
325
330
  }
326
331
  });
327
332
 
@@ -340,16 +345,18 @@ const getPayloadWithMetadataHash = (chain, chainInfo, payload) => {
340
345
  specName,
341
346
  specVersion
342
347
  } = chainInfo;
343
-
344
- // since ultimately this needs a V15 object, would be nice if this accepted one directly as input
345
- const merkleizedMetadata = merkleizeMetadata.merkleizeMetadata(chain.hexMetadata, {
348
+ const metadataHashInputs = {
346
349
  tokenSymbol,
347
350
  decimals,
348
351
  base58Prefix,
349
352
  specName,
350
353
  specVersion
351
- });
354
+ };
355
+
356
+ // since ultimately this needs a V15 object, would be nice if this accepted one directly as input
357
+ const merkleizedMetadata = merkleizeMetadata.merkleizeMetadata(chain.hexMetadata, metadataHashInputs);
352
358
  const metadataHash = utils.toHex(merkleizedMetadata.digest());
359
+ log.log("metadataHash", metadataHash, metadataHashInputs);
353
360
  const payloadWithMetadataHash = {
354
361
  ...payload,
355
362
  mode: 1,
@@ -480,6 +487,16 @@ const getSignerPayloadJSON = async (chain, palletName, methodName, args, signerC
480
487
  };
481
488
  };
482
489
 
490
+ const submit = async (chain, payload, signature, txInfo, mode) => {
491
+ switch (mode) {
492
+ case "bittensor-mev-shield":
493
+ if (signature) throw new Error("Signature should not be provided when using bittensor-mev-shield mode");
494
+ return chain.connector.submitWithBittensorMevShield(payload, txInfo);
495
+ default:
496
+ return chain.connector.submit(payload, signature, txInfo);
497
+ }
498
+ };
499
+
483
500
  const getScaleApi = (connector, hexMetadata, token, hasCheckMetadataHash, signedExtensions, registryTypes) => {
484
501
  const {
485
502
  unifiedMetadata: metadata,
@@ -521,7 +538,7 @@ const getScaleApi = (connector, hexMetadata, token, hasCheckMetadataHash, signed
521
538
  getFeeEstimate: payload => getFeeEstimate(chain, payload, chainInfo),
522
539
  getRuntimeCallValue: (apiName, method, args) => getRuntimeCallResult(chain, apiName, method, args),
523
540
  getTypeRegistry: payload => getTypeRegistry(chain, payload),
524
- submit: (payload, signature, txInfo) => chain.connector.submit(payload, signature, txInfo),
541
+ submit: (payload, signature, txInfo, mode) => submit(chain, payload, signature, txInfo, mode),
525
542
  getCallDocs: (pallet, method) => getCallDocs(chain, pallet, method),
526
543
  getDryRunCall: (from, decodedCall) => getDryRunCall(chain, from, decodedCall),
527
544
  isApiAvailable: (name, method) => isApiAvailable(chain, name, method)
@@ -531,7 +548,8 @@ const getScaleApi = (connector, hexMetadata, token, hasCheckMetadataHash, signed
531
548
  const MAGIC_NUMBER = 1635018093;
532
549
 
533
550
  // it's important to set a max because some chains also return high invalid version numbers in the metadata_versions list (ex on Polkadot, related to JAM?)
534
- const MAX_SUPPORTED_METADATA_VERSION = 16;
551
+ const MAX_SUPPORTED_METADATA_VERSION = 15; // v16 sometimes outputs different metadata hashes, ignore v16 until that is fixed in PAPI
552
+
535
553
  /**
536
554
  * Fetches the highest supported version of metadata from the chain.
537
555
  *
@@ -578,5 +596,6 @@ const normalizeMetadata = metadata => {
578
596
  return `0x${metadata.slice(magicNumberIndex)}`;
579
597
  };
580
598
 
599
+ exports.MAX_SUPPORTED_METADATA_VERSION = MAX_SUPPORTED_METADATA_VERSION;
581
600
  exports.fetchBestMetadata = fetchBestMetadata;
582
601
  exports.getScaleApi = getScaleApi;
@@ -309,13 +309,18 @@ const getFeeEstimate = async (chain, payload, chainInfo) => {
309
309
  const getSapiConnector = ({
310
310
  chainId,
311
311
  send,
312
- submit
312
+ submit,
313
+ submitWithBittensorMevShield
313
314
  }) => ({
314
315
  chainId,
315
316
  send,
316
317
  submit: (...args) => {
317
318
  if (submit) return submit(...args);
318
319
  throw new Error("submit handler not provided");
320
+ },
321
+ submitWithBittensorMevShield: (...args) => {
322
+ if (submitWithBittensorMevShield) return submitWithBittensorMevShield(...args);
323
+ throw new Error("submitWithBittensorMevShield handler not provided");
319
324
  }
320
325
  });
321
326
 
@@ -334,16 +339,18 @@ const getPayloadWithMetadataHash = (chain, chainInfo, payload) => {
334
339
  specName,
335
340
  specVersion
336
341
  } = chainInfo;
337
-
338
- // since ultimately this needs a V15 object, would be nice if this accepted one directly as input
339
- const merkleizedMetadata = merkleizeMetadata(chain.hexMetadata, {
342
+ const metadataHashInputs = {
340
343
  tokenSymbol,
341
344
  decimals,
342
345
  base58Prefix,
343
346
  specName,
344
347
  specVersion
345
- });
348
+ };
349
+
350
+ // since ultimately this needs a V15 object, would be nice if this accepted one directly as input
351
+ const merkleizedMetadata = merkleizeMetadata(chain.hexMetadata, metadataHashInputs);
346
352
  const metadataHash = toHex(merkleizedMetadata.digest());
353
+ log.log("metadataHash", metadataHash, metadataHashInputs);
347
354
  const payloadWithMetadataHash = {
348
355
  ...payload,
349
356
  mode: 1,
@@ -474,6 +481,16 @@ const getSignerPayloadJSON = async (chain, palletName, methodName, args, signerC
474
481
  };
475
482
  };
476
483
 
484
+ const submit = async (chain, payload, signature, txInfo, mode) => {
485
+ switch (mode) {
486
+ case "bittensor-mev-shield":
487
+ if (signature) throw new Error("Signature should not be provided when using bittensor-mev-shield mode");
488
+ return chain.connector.submitWithBittensorMevShield(payload, txInfo);
489
+ default:
490
+ return chain.connector.submit(payload, signature, txInfo);
491
+ }
492
+ };
493
+
477
494
  const getScaleApi = (connector, hexMetadata, token, hasCheckMetadataHash, signedExtensions, registryTypes) => {
478
495
  const {
479
496
  unifiedMetadata: metadata,
@@ -515,7 +532,7 @@ const getScaleApi = (connector, hexMetadata, token, hasCheckMetadataHash, signed
515
532
  getFeeEstimate: payload => getFeeEstimate(chain, payload, chainInfo),
516
533
  getRuntimeCallValue: (apiName, method, args) => getRuntimeCallResult(chain, apiName, method, args),
517
534
  getTypeRegistry: payload => getTypeRegistry(chain, payload),
518
- submit: (payload, signature, txInfo) => chain.connector.submit(payload, signature, txInfo),
535
+ submit: (payload, signature, txInfo, mode) => submit(chain, payload, signature, txInfo, mode),
519
536
  getCallDocs: (pallet, method) => getCallDocs(chain, pallet, method),
520
537
  getDryRunCall: (from, decodedCall) => getDryRunCall(chain, from, decodedCall),
521
538
  isApiAvailable: (name, method) => isApiAvailable(chain, name, method)
@@ -525,7 +542,8 @@ const getScaleApi = (connector, hexMetadata, token, hasCheckMetadataHash, signed
525
542
  const MAGIC_NUMBER = 1635018093;
526
543
 
527
544
  // it's important to set a max because some chains also return high invalid version numbers in the metadata_versions list (ex on Polkadot, related to JAM?)
528
- const MAX_SUPPORTED_METADATA_VERSION = 16;
545
+ const MAX_SUPPORTED_METADATA_VERSION = 15; // v16 sometimes outputs different metadata hashes, ignore v16 until that is fixed in PAPI
546
+
529
547
  /**
530
548
  * Fetches the highest supported version of metadata from the chain.
531
549
  *
@@ -572,4 +590,4 @@ const normalizeMetadata = metadata => {
572
590
  return `0x${metadata.slice(magicNumberIndex)}`;
573
591
  };
574
592
 
575
- export { fetchBestMetadata, getScaleApi };
593
+ export { MAX_SUPPORTED_METADATA_VERSION, fetchBestMetadata, getScaleApi };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@talismn/sapi",
3
- "version": "0.0.11",
3
+ "version": "0.1.0",
4
4
  "author": "Talisman",
5
5
  "homepage": "https://talisman.xyz",
6
6
  "license": "GPL-3.0-or-later",
@@ -31,7 +31,7 @@
31
31
  "anylogger": "^1.0.11",
32
32
  "polkadot-api": "1.13.1",
33
33
  "scale-ts": "^1.6.1",
34
- "@talismn/scale": "0.2.1"
34
+ "@talismn/scale": "0.3.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/jest": "^29.5.14",
@@ -39,8 +39,8 @@
39
39
  "jest": "^29.7",
40
40
  "ts-jest": "^29.2.5",
41
41
  "typescript": "^5.6.3",
42
- "@talismn/eslint-config": "0.0.3",
43
- "@talismn/tsconfig": "0.0.2"
42
+ "@talismn/tsconfig": "0.0.3",
43
+ "@talismn/eslint-config": "0.0.3"
44
44
  },
45
45
  "eslintConfig": {
46
46
  "root": true,