Add support for apiAt.tx(...) (#4696)

* Add support for apiAt.tx(...)

* CHANGELOG
This commit is contained in:
Jaco
2022-04-09 16:05:36 +03:00
committed by GitHub
parent 6c3bd2f408
commit fb5ce83f74
5 changed files with 31 additions and 30 deletions
+1
View File
@@ -11,6 +11,7 @@ Contributed:
Changes: Changes:
- Add support for `apiAt.tx(...)` to construct at a specific point
- Add basic `api.derive.bagsList.*` support - Add basic `api.derive.bagsList.*` support
- Return sorted names for `api.registry.lookup.names` - Return sorted names for `api.registry.lookup.names`
- Optimize JSONRPC requests/response handling - Optimize JSONRPC requests/response handling
+16 -20
View File
@@ -199,18 +199,23 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
return this._rpcCore.provider.hasSubscriptions || !!this._rpcCore.state.queryStorageAt; return this._rpcCore.provider.hasSubscriptions || !!this._rpcCore.state.queryStorageAt;
} }
protected _emptyDecorated (registry: Registry, blockHash?: Uint8Array): ApiDecoration<ApiType> {
return {
consts: {},
errors: {},
events: {},
query: {},
registry,
rx: {
query: {}
},
tx: createSubmittable(this._type, this._rx, this._decorateMethod, registry, blockHash)
} as ApiDecoration<ApiType>;
}
protected _createDecorated (registry: VersionedRegistry<ApiType>, fromEmpty: boolean, decoratedApi: ApiDecoration<ApiType> | null, blockHash?: Uint8Array): FullDecoration<ApiType> { protected _createDecorated (registry: VersionedRegistry<ApiType>, fromEmpty: boolean, decoratedApi: ApiDecoration<ApiType> | null, blockHash?: Uint8Array): FullDecoration<ApiType> {
if (!decoratedApi) { if (!decoratedApi) {
decoratedApi = { decoratedApi = this._emptyDecorated(registry.registry, blockHash);
consts: {},
errors: {},
events: {},
query: {},
registry: registry.registry,
rx: {
query: {}
}
} as ApiDecoration<ApiType>;
} }
if (fromEmpty || !registry.decoratedMeta) { if (fromEmpty || !registry.decoratedMeta) {
@@ -243,16 +248,7 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
protected _injectMetadata (registry: VersionedRegistry<ApiType>, fromEmpty = false): void { protected _injectMetadata (registry: VersionedRegistry<ApiType>, fromEmpty = false): void {
// clear the decoration, we are redoing it here // clear the decoration, we are redoing it here
if (fromEmpty || !registry.decoratedApi) { if (fromEmpty || !registry.decoratedApi) {
registry.decoratedApi = { registry.decoratedApi = this._emptyDecorated(registry.registry);
consts: {},
errors: {},
events: {},
query: {},
registry: registry.registry,
rx: {
query: {}
}
} as ApiDecoration<ApiType>;
} }
const { decoratedApi, decoratedMeta } = this._createDecorated(registry, fromEmpty, registry.decoratedApi); const { decoratedApi, decoratedMeta } = this._createDecorated(registry, fromEmpty, registry.decoratedApi);
+6 -5
View File
@@ -21,6 +21,7 @@ import { SubmittableResult } from './Result';
interface SubmittableOptions<ApiType extends ApiTypes> { interface SubmittableOptions<ApiType extends ApiTypes> {
api: ApiInterfaceRx; api: ApiInterfaceRx;
apiType: ApiTypes; apiType: ApiTypes;
blockHash?: Uint8Array;
decorateMethod: ApiBase<ApiType>['_decorateMethod']; decorateMethod: ApiBase<ApiType>['_decorateMethod'];
} }
@@ -80,7 +81,7 @@ function optionsOrNonce (partialOptions: Partial<SignerOptions> = {}): Partial<S
: partialOptions; : partialOptions;
} }
export function createClass <ApiType extends ApiTypes> ({ api, apiType, decorateMethod }: SubmittableOptions<ApiType>): Constructor<SubmittableExtrinsic<ApiType>> { export function createClass <ApiType extends ApiTypes> ({ api, apiType, blockHash, decorateMethod }: SubmittableOptions<ApiType>): Constructor<SubmittableExtrinsic<ApiType>> {
// an instance of the base extrinsic for us to extend // an instance of the base extrinsic for us to extend
const ExtrinsicBase = api.registry.createClass('Extrinsic'); const ExtrinsicBase = api.registry.createClass('Extrinsic');
@@ -97,10 +98,10 @@ export function createClass <ApiType extends ApiTypes> ({ api, apiType, decorate
// dry run an extrinsic // dry run an extrinsic
public dryRun (account: AddressOrPair, optionsOrHash?: Partial<SignerOptions> | Uint8Array | string): SubmittableDryRunResult<ApiType> { public dryRun (account: AddressOrPair, optionsOrHash?: Partial<SignerOptions> | Uint8Array | string): SubmittableDryRunResult<ApiType> {
if (isString(optionsOrHash) || isU8a(optionsOrHash)) { if (blockHash || isString(optionsOrHash) || isU8a(optionsOrHash)) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return // eslint-disable-next-line @typescript-eslint/no-unsafe-return
return decorateMethod( return decorateMethod(
() => api.rpc.system.dryRun(this.toHex(), optionsOrHash) () => api.rpc.system.dryRun(this.toHex(), blockHash || optionsOrHash as string)
); );
} }
@@ -115,10 +116,10 @@ export function createClass <ApiType extends ApiTypes> ({ api, apiType, decorate
// calculate the payment info for this transaction (if signed and submitted) // calculate the payment info for this transaction (if signed and submitted)
public paymentInfo (account: AddressOrPair, optionsOrHash?: Partial<SignerOptions> | Uint8Array | string): SubmittablePaymentResult<ApiType> { public paymentInfo (account: AddressOrPair, optionsOrHash?: Partial<SignerOptions> | Uint8Array | string): SubmittablePaymentResult<ApiType> {
if (isString(optionsOrHash) || isU8a(optionsOrHash)) { if (blockHash || isString(optionsOrHash) || isU8a(optionsOrHash)) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return // eslint-disable-next-line @typescript-eslint/no-unsafe-return
return decorateMethod( return decorateMethod(
(): Observable<RuntimeDispatchInfo> => api.rpc.payment.queryInfo(this.toHex(), optionsOrHash) (): Observable<RuntimeDispatchInfo> => api.rpc.payment.queryInfo(this.toHex(), blockHash || optionsOrHash as string)
); );
} }
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
import type { Call, Extrinsic } from '@polkadot/types/interfaces'; import type { Call, Extrinsic } from '@polkadot/types/interfaces';
import type { Registry } from '@polkadot/types-codec/types';
import type { ApiInterfaceRx, ApiTypes } from '../types'; import type { ApiInterfaceRx, ApiTypes } from '../types';
import type { SubmittableExtrinsic } from './types'; import type { SubmittableExtrinsic } from './types';
@@ -10,9 +11,9 @@ import { createClass } from './createClass';
type Creator<ApiType extends ApiTypes> = (extrinsic: Call | Uint8Array | string) => SubmittableExtrinsic<ApiType>; type Creator<ApiType extends ApiTypes> = (extrinsic: Call | Uint8Array | string) => SubmittableExtrinsic<ApiType>;
export function createSubmittable<ApiType extends ApiTypes> (apiType: ApiTypes, api: ApiInterfaceRx, decorateMethod: ApiBase<ApiType>['_decorateMethod']): Creator<ApiType> { export function createSubmittable<ApiType extends ApiTypes> (apiType: ApiTypes, api: ApiInterfaceRx, decorateMethod: ApiBase<ApiType>['_decorateMethod'], registry?: Registry, blockHash?: Uint8Array): Creator<ApiType> {
const Submittable = createClass<ApiType>({ api, apiType, decorateMethod }); const Submittable = createClass<ApiType>({ api, apiType, blockHash, decorateMethod });
return (extrinsic: Call | Extrinsic | Uint8Array | string): SubmittableExtrinsic<ApiType> => return (extrinsic: Call | Extrinsic | Uint8Array | string): SubmittableExtrinsic<ApiType> =>
new Submittable(api.registry, extrinsic); new Submittable(registry || api.registry, extrinsic);
} }
+4 -2
View File
@@ -9,11 +9,12 @@ import type { DecoratedEvents } from '@polkadot/api-base/types/events';
import type { QueryableStorage } from '@polkadot/api-base/types/storage'; import type { QueryableStorage } from '@polkadot/api-base/types/storage';
import type { ProviderInterface, ProviderInterfaceEmitted } from '@polkadot/rpc-provider/types'; import type { ProviderInterface, ProviderInterfaceEmitted } from '@polkadot/rpc-provider/types';
import type { ExtDef } from '@polkadot/types/extrinsic/signedExtensions/types'; import type { ExtDef } from '@polkadot/types/extrinsic/signedExtensions/types';
import type { Hash } from '@polkadot/types/interfaces'; import type { Call, Extrinsic, Hash } from '@polkadot/types/interfaces';
import type { CallFunction, DefinitionRpc, DefinitionRpcSub, RegisteredTypes, Registry, RegistryError, SignatureOptions, Signer } from '@polkadot/types/types'; import type { CallFunction, DefinitionRpc, DefinitionRpcSub, RegisteredTypes, Registry, RegistryError, SignatureOptions, Signer } from '@polkadot/types/types';
import type { BN } from '@polkadot/util'; import type { BN } from '@polkadot/util';
import type { HexString } from '@polkadot/util/types'; import type { HexString } from '@polkadot/util/types';
import type { ApiBase } from '../base'; import type { ApiBase } from '../base';
import type { SubmittableExtrinsic } from '../types/submittable';
import type { AllDerives } from '../util/decorate'; import type { AllDerives } from '../util/decorate';
export * from '@polkadot/api-base/types'; export * from '@polkadot/api-base/types';
@@ -96,7 +97,8 @@ export interface ApiDecoration<ApiType extends ApiTypes> {
registry: Registry; registry: Registry;
rx: { rx: {
query: QueryableStorage<'rxjs'>; query: QueryableStorage<'rxjs'>;
} };
tx: (extrinsic: Call | Extrinsic | Uint8Array | string) => SubmittableExtrinsic<ApiType>;
findCall (callIndex: Uint8Array | string): CallFunction; findCall (callIndex: Uint8Array | string): CallFunction;
findError (errorIndex: Uint8Array | string): RegistryError; findError (errorIndex: Uint8Array | string): RegistryError;