Skip to content

Commit fb5ce83

Browse files
authored
Add support for apiAt.tx(...) (#4696)
* Add support for apiAt.tx(...) * CHANGELOG
1 parent 6c3bd2f commit fb5ce83

File tree

5 files changed

+31
-30
lines changed

5 files changed

+31
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Contributed:
1111

1212
Changes:
1313

14+
- Add support for `apiAt.tx(...)` to construct at a specific point
1415
- Add basic `api.derive.bagsList.*` support
1516
- Return sorted names for `api.registry.lookup.names`
1617
- Optimize JSONRPC requests/response handling

packages/api/src/base/Decorate.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -199,18 +199,23 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
199199
return this._rpcCore.provider.hasSubscriptions || !!this._rpcCore.state.queryStorageAt;
200200
}
201201

202+
protected _emptyDecorated (registry: Registry, blockHash?: Uint8Array): ApiDecoration<ApiType> {
203+
return {
204+
consts: {},
205+
errors: {},
206+
events: {},
207+
query: {},
208+
registry,
209+
rx: {
210+
query: {}
211+
},
212+
tx: createSubmittable(this._type, this._rx, this._decorateMethod, registry, blockHash)
213+
} as ApiDecoration<ApiType>;
214+
}
215+
202216
protected _createDecorated (registry: VersionedRegistry<ApiType>, fromEmpty: boolean, decoratedApi: ApiDecoration<ApiType> | null, blockHash?: Uint8Array): FullDecoration<ApiType> {
203217
if (!decoratedApi) {
204-
decoratedApi = {
205-
consts: {},
206-
errors: {},
207-
events: {},
208-
query: {},
209-
registry: registry.registry,
210-
rx: {
211-
query: {}
212-
}
213-
} as ApiDecoration<ApiType>;
218+
decoratedApi = this._emptyDecorated(registry.registry, blockHash);
214219
}
215220

216221
if (fromEmpty || !registry.decoratedMeta) {
@@ -243,16 +248,7 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
243248
protected _injectMetadata (registry: VersionedRegistry<ApiType>, fromEmpty = false): void {
244249
// clear the decoration, we are redoing it here
245250
if (fromEmpty || !registry.decoratedApi) {
246-
registry.decoratedApi = {
247-
consts: {},
248-
errors: {},
249-
events: {},
250-
query: {},
251-
registry: registry.registry,
252-
rx: {
253-
query: {}
254-
}
255-
} as ApiDecoration<ApiType>;
251+
registry.decoratedApi = this._emptyDecorated(registry.registry);
256252
}
257253

258254
const { decoratedApi, decoratedMeta } = this._createDecorated(registry, fromEmpty, registry.decoratedApi);

packages/api/src/submittable/createClass.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { SubmittableResult } from './Result';
2121
interface SubmittableOptions<ApiType extends ApiTypes> {
2222
api: ApiInterfaceRx;
2323
apiType: ApiTypes;
24+
blockHash?: Uint8Array;
2425
decorateMethod: ApiBase<ApiType>['_decorateMethod'];
2526
}
2627

@@ -80,7 +81,7 @@ function optionsOrNonce (partialOptions: Partial<SignerOptions> = {}): Partial<S
8081
: partialOptions;
8182
}
8283

83-
export function createClass <ApiType extends ApiTypes> ({ api, apiType, decorateMethod }: SubmittableOptions<ApiType>): Constructor<SubmittableExtrinsic<ApiType>> {
84+
export function createClass <ApiType extends ApiTypes> ({ api, apiType, blockHash, decorateMethod }: SubmittableOptions<ApiType>): Constructor<SubmittableExtrinsic<ApiType>> {
8485
// an instance of the base extrinsic for us to extend
8586
const ExtrinsicBase = api.registry.createClass('Extrinsic');
8687

@@ -97,10 +98,10 @@ export function createClass <ApiType extends ApiTypes> ({ api, apiType, decorate
9798

9899
// dry run an extrinsic
99100
public dryRun (account: AddressOrPair, optionsOrHash?: Partial<SignerOptions> | Uint8Array | string): SubmittableDryRunResult<ApiType> {
100-
if (isString(optionsOrHash) || isU8a(optionsOrHash)) {
101+
if (blockHash || isString(optionsOrHash) || isU8a(optionsOrHash)) {
101102
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
102103
return decorateMethod(
103-
() => api.rpc.system.dryRun(this.toHex(), optionsOrHash)
104+
() => api.rpc.system.dryRun(this.toHex(), blockHash || optionsOrHash as string)
104105
);
105106
}
106107

@@ -115,10 +116,10 @@ export function createClass <ApiType extends ApiTypes> ({ api, apiType, decorate
115116

116117
// calculate the payment info for this transaction (if signed and submitted)
117118
public paymentInfo (account: AddressOrPair, optionsOrHash?: Partial<SignerOptions> | Uint8Array | string): SubmittablePaymentResult<ApiType> {
118-
if (isString(optionsOrHash) || isU8a(optionsOrHash)) {
119+
if (blockHash || isString(optionsOrHash) || isU8a(optionsOrHash)) {
119120
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
120121
return decorateMethod(
121-
(): Observable<RuntimeDispatchInfo> => api.rpc.payment.queryInfo(this.toHex(), optionsOrHash)
122+
(): Observable<RuntimeDispatchInfo> => api.rpc.payment.queryInfo(this.toHex(), blockHash || optionsOrHash as string)
122123
);
123124
}
124125

packages/api/src/submittable/createSubmittable.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import type { Call, Extrinsic } from '@polkadot/types/interfaces';
5+
import type { Registry } from '@polkadot/types-codec/types';
56
import type { ApiInterfaceRx, ApiTypes } from '../types';
67
import type { SubmittableExtrinsic } from './types';
78

@@ -10,9 +11,9 @@ import { createClass } from './createClass';
1011

1112
type Creator<ApiType extends ApiTypes> = (extrinsic: Call | Uint8Array | string) => SubmittableExtrinsic<ApiType>;
1213

13-
export function createSubmittable<ApiType extends ApiTypes> (apiType: ApiTypes, api: ApiInterfaceRx, decorateMethod: ApiBase<ApiType>['_decorateMethod']): Creator<ApiType> {
14-
const Submittable = createClass<ApiType>({ api, apiType, decorateMethod });
14+
export function createSubmittable<ApiType extends ApiTypes> (apiType: ApiTypes, api: ApiInterfaceRx, decorateMethod: ApiBase<ApiType>['_decorateMethod'], registry?: Registry, blockHash?: Uint8Array): Creator<ApiType> {
15+
const Submittable = createClass<ApiType>({ api, apiType, blockHash, decorateMethod });
1516

1617
return (extrinsic: Call | Extrinsic | Uint8Array | string): SubmittableExtrinsic<ApiType> =>
17-
new Submittable(api.registry, extrinsic);
18+
new Submittable(registry || api.registry, extrinsic);
1819
}

packages/api/src/types/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import type { DecoratedEvents } from '@polkadot/api-base/types/events';
99
import type { QueryableStorage } from '@polkadot/api-base/types/storage';
1010
import type { ProviderInterface, ProviderInterfaceEmitted } from '@polkadot/rpc-provider/types';
1111
import type { ExtDef } from '@polkadot/types/extrinsic/signedExtensions/types';
12-
import type { Hash } from '@polkadot/types/interfaces';
12+
import type { Call, Extrinsic, Hash } from '@polkadot/types/interfaces';
1313
import type { CallFunction, DefinitionRpc, DefinitionRpcSub, RegisteredTypes, Registry, RegistryError, SignatureOptions, Signer } from '@polkadot/types/types';
1414
import type { BN } from '@polkadot/util';
1515
import type { HexString } from '@polkadot/util/types';
1616
import type { ApiBase } from '../base';
17+
import type { SubmittableExtrinsic } from '../types/submittable';
1718
import type { AllDerives } from '../util/decorate';
1819

1920
export * from '@polkadot/api-base/types';
@@ -96,7 +97,8 @@ export interface ApiDecoration<ApiType extends ApiTypes> {
9697
registry: Registry;
9798
rx: {
9899
query: QueryableStorage<'rxjs'>;
99-
}
100+
};
101+
tx: (extrinsic: Call | Extrinsic | Uint8Array | string) => SubmittableExtrinsic<ApiType>;
100102

101103
findCall (callIndex: Uint8Array | string): CallFunction;
102104
findError (errorIndex: Uint8Array | string): RegistryError;

0 commit comments

Comments
 (0)