@dedot/api 0.0.1-alpha.39 → 0.0.1-alpha.40

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,7 +1,7 @@
1
1
  import type { AccountId32Like, BytesLike, Data, Extrinsic, FixedBytes, FixedU128, FixedU64, H256, MultiAddressLike, Perbill, Percent, Perquintill } from '@dedot/codecs';
2
- import type { GenericChainTx, GenericTxCall, IRuntimeTxCall, ISubmittableExtrinsic, ISubmittableExtrinsicLegacy, ISubmittableResult, RpcV2, RpcVersion, TransactionStatusLegacy, TransactionStatusV2 } from '@dedot/types';
2
+ import type { GenericChainTx, GenericTxCall, IRuntimeTxCall, ISubmittableExtrinsic, ISubmittableExtrinsicLegacy, ISubmittableResult, RpcV2, RpcVersion, TransactionEvent } from '@dedot/types';
3
3
  import type { FrameSupportPreimagesBounded, FrameSupportScheduleDispatchTime, FrameSupportTokensFungibleUnionOfNativeOrWithId, FrameSystemEventRecord, KitchensinkRuntimeOriginCaller, KitchensinkRuntimeProxyType, KitchensinkRuntimeRuntimeCallLike, KitchensinkRuntimeRuntimeParameters, KitchensinkRuntimeSessionKeys, PalletAllianceCid, PalletAllianceDisbandWitness, PalletAllianceUnscrupulousItem, PalletBalancesAdjustmentDirection, PalletBrokerConfigRecord, PalletBrokerCoreMask, PalletBrokerFinality, PalletBrokerRegionId, PalletBrokerScheduleItem, PalletContractsWasmDeterminism, PalletConvictionVotingConviction, PalletConvictionVotingVoteAccountVote, PalletCoreFellowshipParamsType, PalletCoreFellowshipWish, PalletDemocracyConviction, PalletDemocracyMetadataOwner, PalletDemocracyVoteAccountVote, PalletElectionProviderMultiPhaseRawSolution, PalletElectionProviderMultiPhaseSolutionOrSnapshotSize, PalletElectionsPhragmenRenouncing, PalletIdentityJudgement, PalletIdentityLegacyIdentityInfo, PalletImOnlineHeartbeat, PalletImOnlineSr25519AppSr25519Signature, PalletMigrationsHistoricCleanupSelector, PalletMigrationsMigrationCursor, PalletMixnetRegistration, PalletMultisigTimepoint, PalletNftsAttributeNamespace, PalletNftsBitFlags, PalletNftsCancelAttributesApprovalWitness, PalletNftsCollectionConfig, PalletNftsDestroyWitness, PalletNftsItemConfig, PalletNftsItemTip, PalletNftsMintSettings, PalletNftsMintWitness, PalletNftsPreSignedAttributes, PalletNftsPreSignedMint, PalletNftsPriceWithDirection, PalletNominationPoolsBondExtra, PalletNominationPoolsClaimPermission, PalletNominationPoolsCommissionChangeRate, PalletNominationPoolsCommissionClaimPermission, PalletNominationPoolsConfigOp, PalletNominationPoolsConfigOp004, PalletNominationPoolsConfigOpPerbill, PalletNominationPoolsConfigOpU32, PalletNominationPoolsPoolState, PalletStakingPalletConfigOp, PalletStakingPalletConfigOpPerbill, PalletStakingPalletConfigOpPercent, PalletStakingPalletConfigOpU32, PalletStakingRewardDestination, PalletStakingValidatorPrefs, PalletStateTrieMigrationMigrationLimits, PalletStateTrieMigrationMigrationTask, PalletStateTrieMigrationProgress, PalletUniquesDestroyWitness, PalletVestingVestingInfo, SpConsensusBabeDigestsNextConfigDescriptor, SpConsensusBeefyEquivocationProof, SpConsensusGrandpaEquivocationProof, SpConsensusSlotsEquivocationProof, SpMixnetAppSignature, SpNposElectionsElectionScore, SpNposElectionsSupport, SpRuntimeMultiSignature, SpSessionMembershipProof, SpTransactionStorageProofTransactionStorageProof, SpWeightsWeightV2Weight } from './types.js';
4
- export type ChainSubmittableExtrinsic<Rv extends RpcVersion, T extends IRuntimeTxCall = KitchensinkRuntimeRuntimeCallLike> = Extrinsic<MultiAddressLike, T, SpRuntimeMultiSignature, any[]> & (Rv extends RpcV2 ? ISubmittableExtrinsic<ISubmittableResult<FrameSystemEventRecord, TransactionStatusV2>> : ISubmittableExtrinsicLegacy<ISubmittableResult<FrameSystemEventRecord, TransactionStatusLegacy>>);
4
+ export type ChainSubmittableExtrinsic<Rv extends RpcVersion, T extends IRuntimeTxCall = KitchensinkRuntimeRuntimeCallLike> = Extrinsic<MultiAddressLike, T, SpRuntimeMultiSignature, any[]> & (Rv extends RpcV2 ? ISubmittableExtrinsic<ISubmittableResult<FrameSystemEventRecord, TransactionEvent>> : ISubmittableExtrinsicLegacy<ISubmittableResult<FrameSystemEventRecord, TransactionEvent>>);
5
5
  export type TxCall<Rv extends RpcVersion> = (...args: any[]) => ChainSubmittableExtrinsic<Rv>;
6
6
  export interface ChainTx<Rv extends RpcVersion> extends GenericChainTx<Rv, TxCall<Rv>> {
7
7
  /**
@@ -4,6 +4,7 @@ exports.SubmittableExtrinsic = void 0;
4
4
  const utils_1 = require("@dedot/utils");
5
5
  const BaseSubmittableExtrinsic_js_1 = require("./BaseSubmittableExtrinsic.js");
6
6
  const SubmittableResult_js_1 = require("./SubmittableResult.js");
7
+ const utils_js_1 = require("./utils.js");
7
8
  /**
8
9
  * @name SubmittableExtrinsic
9
10
  * @description A wrapper around an Extrinsic that exposes methods to sign, send, and other utility around Extrinsic.
@@ -22,9 +23,9 @@ class SubmittableExtrinsic extends BaseSubmittableExtrinsic_js_1.BaseSubmittable
22
23
  const txHex = this.toHex();
23
24
  const txHash = this.hash;
24
25
  if (isSubscription) {
25
- return this.api.rpc.author_submitAndWatchExtrinsic(txHex, async (status) => {
26
- if (status.type === 'InBlock' || status.type === 'Finalized') {
27
- const blockHash = status.value;
26
+ return this.api.rpc.author_submitAndWatchExtrinsic(txHex, async (txStatus) => {
27
+ if (txStatus.type === 'InBlock' || txStatus.type === 'Finalized') {
28
+ const blockHash = txStatus.value;
28
29
  const [signedBlock, blockEvents] = await Promise.all([
29
30
  this.api.rpc.chain_getBlock(blockHash),
30
31
  this.getSystemEventsAt(blockHash),
@@ -32,9 +33,11 @@ class SubmittableExtrinsic extends BaseSubmittableExtrinsic_js_1.BaseSubmittable
32
33
  const txIndex = signedBlock.block.extrinsics.indexOf(txHex);
33
34
  (0, utils_1.assert)(txIndex >= 0, 'Extrinsic not found!');
34
35
  const events = blockEvents.filter(({ phase }) => phase.type === 'ApplyExtrinsic' && phase.value === txIndex);
36
+ const status = (0, utils_js_1.toTransactionEvent)(txStatus, txIndex);
35
37
  return callback(new SubmittableResult_js_1.SubmittableResult({ status, txHash, events, txIndex }));
36
38
  }
37
39
  else {
40
+ const status = (0, utils_js_1.toTransactionEvent)(txStatus);
38
41
  return callback(new SubmittableResult_js_1.SubmittableResult({ status, txHash }));
39
42
  }
40
43
  });
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.signRaw = exports.isKeyringPair = void 0;
3
+ exports.toTransactionEvent = exports.signRaw = exports.isKeyringPair = void 0;
4
4
  const utils_1 = require("@dedot/utils");
5
5
  function isKeyringPair(account) {
6
6
  return (0, utils_1.isFunction)(account.sign);
@@ -18,3 +18,68 @@ function signRaw(signerPair, raw) {
18
18
  return signerPair.sign(toSignRaw, { withType: true });
19
19
  }
20
20
  exports.signRaw = signRaw;
21
+ /**
22
+ * Convert transaction status to transaction event
23
+ *
24
+ * Ref: https://github.com/paritytech/polkadot-sdk/blob/98a364fe6e7abf10819f5fddd3de0588f7c38700/substrate/client/rpc-spec-v2/src/transaction/transaction.rs#L132-L159
25
+ * @param txStatus
26
+ * @param txIndex
27
+ */
28
+ function toTransactionEvent(txStatus, txIndex) {
29
+ switch (txStatus.type) {
30
+ case 'Ready':
31
+ case 'Future':
32
+ return { type: 'Validated' };
33
+ case 'Broadcast':
34
+ return { type: 'Broadcasting' };
35
+ case 'Retracted':
36
+ return { type: 'NoLongerInBestChain' };
37
+ case 'InBlock':
38
+ (0, utils_1.assert)(txIndex, 'TxIndex is required');
39
+ return {
40
+ type: 'BestChainBlockIncluded',
41
+ value: {
42
+ blockHash: txStatus.value,
43
+ txIndex,
44
+ },
45
+ };
46
+ case 'Finalized':
47
+ (0, utils_1.assert)(txIndex, 'TxIndex is required');
48
+ return {
49
+ type: 'Finalized',
50
+ value: {
51
+ blockHash: txStatus.value,
52
+ txIndex,
53
+ },
54
+ };
55
+ case 'FinalityTimeout':
56
+ return {
57
+ type: 'Drop',
58
+ value: {
59
+ error: 'Maximum number of finality watchers has been reached',
60
+ },
61
+ };
62
+ case 'Dropped':
63
+ return {
64
+ type: 'Drop',
65
+ value: {
66
+ error: 'Extrinsic dropped from the pool due to exceeding limits',
67
+ },
68
+ };
69
+ case 'Usurped':
70
+ return {
71
+ type: 'Invalid',
72
+ value: {
73
+ error: 'Extrinsic was rendered invalid by another extrinsic',
74
+ },
75
+ };
76
+ case 'Invalid':
77
+ return {
78
+ type: 'Invalid',
79
+ value: {
80
+ error: 'Extrinsic marked as invalid',
81
+ },
82
+ };
83
+ }
84
+ }
85
+ exports.toTransactionEvent = toTransactionEvent;
@@ -1,6 +1,7 @@
1
1
  import { assert, isHex } from '@dedot/utils';
2
2
  import { BaseSubmittableExtrinsic } from './BaseSubmittableExtrinsic.js';
3
3
  import { SubmittableResult } from './SubmittableResult.js';
4
+ import { toTransactionEvent } from './utils.js';
4
5
  /**
5
6
  * @name SubmittableExtrinsic
6
7
  * @description A wrapper around an Extrinsic that exposes methods to sign, send, and other utility around Extrinsic.
@@ -19,9 +20,9 @@ export class SubmittableExtrinsic extends BaseSubmittableExtrinsic {
19
20
  const txHex = this.toHex();
20
21
  const txHash = this.hash;
21
22
  if (isSubscription) {
22
- return this.api.rpc.author_submitAndWatchExtrinsic(txHex, async (status) => {
23
- if (status.type === 'InBlock' || status.type === 'Finalized') {
24
- const blockHash = status.value;
23
+ return this.api.rpc.author_submitAndWatchExtrinsic(txHex, async (txStatus) => {
24
+ if (txStatus.type === 'InBlock' || txStatus.type === 'Finalized') {
25
+ const blockHash = txStatus.value;
25
26
  const [signedBlock, blockEvents] = await Promise.all([
26
27
  this.api.rpc.chain_getBlock(blockHash),
27
28
  this.getSystemEventsAt(blockHash),
@@ -29,9 +30,11 @@ export class SubmittableExtrinsic extends BaseSubmittableExtrinsic {
29
30
  const txIndex = signedBlock.block.extrinsics.indexOf(txHex);
30
31
  assert(txIndex >= 0, 'Extrinsic not found!');
31
32
  const events = blockEvents.filter(({ phase }) => phase.type === 'ApplyExtrinsic' && phase.value === txIndex);
33
+ const status = toTransactionEvent(txStatus, txIndex);
32
34
  return callback(new SubmittableResult({ status, txHash, events, txIndex }));
33
35
  }
34
36
  else {
37
+ const status = toTransactionEvent(txStatus);
35
38
  return callback(new SubmittableResult({ status, txHash }));
36
39
  }
37
40
  });
@@ -1,5 +1,6 @@
1
1
  import { IKeyringPair } from '@polkadot/types/types';
2
- import type { AddressOrPair } from '@dedot/types';
2
+ import { TransactionStatus } from '@dedot/codecs';
3
+ import type { AddressOrPair, TransactionEvent } from '@dedot/types';
3
4
  import { HexString } from '@dedot/utils';
4
5
  export declare function isKeyringPair(account: AddressOrPair): account is IKeyringPair;
5
6
  /**
@@ -8,3 +9,11 @@ export declare function isKeyringPair(account: AddressOrPair): account is IKeyri
8
9
  * @param raw
9
10
  */
10
11
  export declare function signRaw(signerPair: IKeyringPair, raw: HexString): Uint8Array;
12
+ /**
13
+ * Convert transaction status to transaction event
14
+ *
15
+ * Ref: https://github.com/paritytech/polkadot-sdk/blob/98a364fe6e7abf10819f5fddd3de0588f7c38700/substrate/client/rpc-spec-v2/src/transaction/transaction.rs#L132-L159
16
+ * @param txStatus
17
+ * @param txIndex
18
+ */
19
+ export declare function toTransactionEvent(txStatus: TransactionStatus, txIndex?: number): TransactionEvent;
@@ -1,4 +1,4 @@
1
- import { blake2AsU8a, hexToU8a, isFunction } from '@dedot/utils';
1
+ import { assert, blake2AsU8a, hexToU8a, isFunction } from '@dedot/utils';
2
2
  export function isKeyringPair(account) {
3
3
  return isFunction(account.sign);
4
4
  }
@@ -13,3 +13,67 @@ export function signRaw(signerPair, raw) {
13
13
  const toSignRaw = u8a.length > 256 ? blake2AsU8a(u8a, 256) : u8a;
14
14
  return signerPair.sign(toSignRaw, { withType: true });
15
15
  }
16
+ /**
17
+ * Convert transaction status to transaction event
18
+ *
19
+ * Ref: https://github.com/paritytech/polkadot-sdk/blob/98a364fe6e7abf10819f5fddd3de0588f7c38700/substrate/client/rpc-spec-v2/src/transaction/transaction.rs#L132-L159
20
+ * @param txStatus
21
+ * @param txIndex
22
+ */
23
+ export function toTransactionEvent(txStatus, txIndex) {
24
+ switch (txStatus.type) {
25
+ case 'Ready':
26
+ case 'Future':
27
+ return { type: 'Validated' };
28
+ case 'Broadcast':
29
+ return { type: 'Broadcasting' };
30
+ case 'Retracted':
31
+ return { type: 'NoLongerInBestChain' };
32
+ case 'InBlock':
33
+ assert(txIndex, 'TxIndex is required');
34
+ return {
35
+ type: 'BestChainBlockIncluded',
36
+ value: {
37
+ blockHash: txStatus.value,
38
+ txIndex,
39
+ },
40
+ };
41
+ case 'Finalized':
42
+ assert(txIndex, 'TxIndex is required');
43
+ return {
44
+ type: 'Finalized',
45
+ value: {
46
+ blockHash: txStatus.value,
47
+ txIndex,
48
+ },
49
+ };
50
+ case 'FinalityTimeout':
51
+ return {
52
+ type: 'Drop',
53
+ value: {
54
+ error: 'Maximum number of finality watchers has been reached',
55
+ },
56
+ };
57
+ case 'Dropped':
58
+ return {
59
+ type: 'Drop',
60
+ value: {
61
+ error: 'Extrinsic dropped from the pool due to exceeding limits',
62
+ },
63
+ };
64
+ case 'Usurped':
65
+ return {
66
+ type: 'Invalid',
67
+ value: {
68
+ error: 'Extrinsic was rendered invalid by another extrinsic',
69
+ },
70
+ };
71
+ case 'Invalid':
72
+ return {
73
+ type: 'Invalid',
74
+ value: {
75
+ error: 'Extrinsic marked as invalid',
76
+ },
77
+ };
78
+ }
79
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dedot/api",
3
- "version": "0.0.1-alpha.39",
3
+ "version": "0.0.1-alpha.40",
4
4
  "description": "A delightful JavaScript/TypeScript client for Polkadot & Substrate",
5
5
  "author": "Thang X. Vu <thang@coongcrafts.io>",
6
6
  "homepage": "https://github.com/dedotdev/dedot/tree/main/packages/api",
@@ -13,13 +13,13 @@
13
13
  "type": "module",
14
14
  "sideEffects": false,
15
15
  "dependencies": {
16
- "@dedot/codecs": "0.0.1-alpha.39",
17
- "@dedot/providers": "0.0.1-alpha.39",
18
- "@dedot/runtime-specs": "0.0.1-alpha.39",
19
- "@dedot/shape": "0.0.1-alpha.39",
20
- "@dedot/storage": "0.0.1-alpha.39",
21
- "@dedot/types": "0.0.1-alpha.39",
22
- "@dedot/utils": "0.0.1-alpha.39"
16
+ "@dedot/codecs": "0.0.1-alpha.40",
17
+ "@dedot/providers": "0.0.1-alpha.40",
18
+ "@dedot/runtime-specs": "0.0.1-alpha.40",
19
+ "@dedot/shape": "0.0.1-alpha.40",
20
+ "@dedot/storage": "0.0.1-alpha.40",
21
+ "@dedot/types": "0.0.1-alpha.40",
22
+ "@dedot/utils": "0.0.1-alpha.40"
23
23
  },
24
24
  "scripts": {
25
25
  "build": "tsc --project tsconfig.build.json && tsc --project tsconfig.build.cjs.json",
@@ -48,7 +48,7 @@
48
48
  "devDependencies": {
49
49
  "@polkadot/types": "^12.1.1"
50
50
  },
51
- "gitHead": "1dc9046a8bbd7dfc38f4f4c4a9fbd4033eca181d",
51
+ "gitHead": "c6c3d9d748851f01ce79c13541754809be6628fc",
52
52
  "module": "./index.js",
53
53
  "types": "./index.d.ts"
54
54
  }