Skip to content

Commit 5c655ca

Browse files
authored
Use I* interfaces for extrinsic payload getters (#4814)
* Use `I*` interfaces for extrinsic payload getters * Adjust with IText * Text extends IText * Adjust
1 parent 1808cb0 commit 5c655ca

File tree

11 files changed

+77
-68
lines changed

11 files changed

+77
-68
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Changes:
99
- Deupe `wss://` handling in `polkadot-types-from-{chain, defs}`
1010
- Allow for optional `definitions.ts` in typegen (only use chain)
1111
- Optimize `Compact<*>` decoding in Uint8Array streams
12+
- Use `I*` interfaces for extrinsic payload getters
1213
- Update to latest Substrate, Kusama & Polkadot static metadata
1314

1415

packages/types-codec/src/native/Text.ts

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

44
import type { HexString } from '@polkadot/util/types';
5-
import type { AnyU8a, Codec, Inspect, IU8a, Registry } from '../types';
5+
import type { AnyU8a, Inspect, IText, IU8a, Registry } from '../types';
66

77
import { assert, compactAddLength, compactFromU8a, compactToU8a, hexToU8a, isHex, isString, isU8a, stringToU8a, u8aToHex, u8aToString } from '@polkadot/util';
88

@@ -47,7 +47,7 @@ function decodeText (value?: null | Text | string | AnyU8a | { toString: () => s
4747
*/
4848
// TODO
4949
// - Strings should probably be trimmed (docs do come through with extra padding)
50-
export class Text extends String implements Codec {
50+
export class Text extends String implements IText {
5151
public readonly registry: Registry;
5252

5353
public createdAtHash?: IU8a;

packages/types-codec/src/types/interfaces.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ export interface IStruct<K = string, V extends Codec = Codec> extends Map<K, V>,
6161
toArray (): Codec[];
6262
}
6363

64+
export interface IText extends String, Codec {
65+
// nothing additional
66+
}
67+
6468
export type ITuple<T extends AnyTuple = Codec[]> = T & Codec;
6569

6670
export interface IU8a extends Uint8Array, Codec {

packages/types/src/extrinsic/Extrinsic.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// Copyright 2017-2022 @polkadot/types authors & contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import type { Compact } from '@polkadot/types-codec';
54
import type { AnyJson, AnyTuple, AnyU8a, ArgsDef, IMethod, Inspect } from '@polkadot/types-codec/types';
65
import type { HexString } from '@polkadot/util/types';
76
import type { EcdsaSignature, Ed25519Signature, ExtrinsicUnknown, ExtrinsicV4, Sr25519Signature } from '../interfaces/extrinsics';
87
import type { FunctionMetadataLatest } from '../interfaces/metadata';
9-
import type { Address, Balance, Call, CodecHash, Index } from '../interfaces/runtime';
10-
import type { CallBase, ExtrinsicPayloadValue, IExtrinsic, IKeyringPair, Registry, SignatureOptions } from '../types';
8+
import type { Address, Call, CodecHash } from '../interfaces/runtime';
9+
import type { CallBase, ExtrinsicPayloadValue, ICompact, IExtrinsic, IKeyringPair, INumber, Registry, SignatureOptions } from '../types';
1110
import type { GenericExtrinsicEra } from './ExtrinsicEra';
1211
import type { ExtrinsicValueV4 } from './v4/Extrinsic';
1312

@@ -164,7 +163,7 @@ abstract class ExtrinsicBase<A extends AnyTuple> extends Base<ExtrinsicVx | Extr
164163
/**
165164
* @description The nonce for this extrinsic
166165
*/
167-
public get nonce (): Compact<Index> {
166+
public get nonce (): ICompact<INumber> {
168167
return this.inner.signature.nonce;
169168
}
170169

@@ -189,7 +188,7 @@ abstract class ExtrinsicBase<A extends AnyTuple> extends Base<ExtrinsicVx | Extr
189188
/**
190189
* @description Forwards compat
191190
*/
192-
public get tip (): Compact<Balance> {
191+
public get tip (): ICompact<INumber> {
193192
return this.inner.signature.tip;
194193
}
195194

packages/types/src/extrinsic/ExtrinsicEra.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import type { AnyU8a, Registry } from '@polkadot/types-codec/types';
5-
import type { BN } from '@polkadot/util';
6-
import type { IExtrinsicEra } from '../types';
5+
import type { IExtrinsicEra, INumber } from '../types';
76

87
import { Enum, Raw, Tuple, U64 } from '@polkadot/types-codec';
98
import { assert, bnToBn, formatNumber, hexToU8a, isHex, isObject, isU8a, u8aToBn, u8aToU8a } from '@polkadot/util';
109

1110
import { IMMORTAL_ERA } from './constants';
1211

13-
type MortalEraValue = [U64, U64];
12+
type MortalEraValue = [INumber, INumber];
1413

1514
interface MortalMethod {
1615
current: number;
@@ -147,15 +146,15 @@ export class MortalEra extends Tuple {
147146
/**
148147
* @description The period of this Mortal wraps as a [[U64]]
149148
*/
150-
public get period (): U64 {
151-
return this[0] as U64;
149+
public get period (): INumber {
150+
return this[0] as INumber;
152151
}
153152

154153
/**
155154
* @description The phase of this Mortal wraps as a [[U64]]
156155
*/
157-
public get phase (): U64 {
158-
return this[1] as U64;
156+
public get phase (): INumber {
157+
return this[1] as INumber;
159158
}
160159

161160
/**
@@ -201,7 +200,7 @@ export class MortalEra extends Tuple {
201200
/**
202201
* @description Get the block number of the start of the era whose properties this object describes that `current` belongs to.
203202
*/
204-
public birth (current: BN | number): number {
203+
public birth (current: INumber | number): number {
205204
// FIXME No toNumber() here
206205
return Math.floor(
207206
(
@@ -213,7 +212,7 @@ export class MortalEra extends Tuple {
213212
/**
214213
* @description Get the block number of the first block at which the era has ended.
215214
*/
216-
public death (current: BN | number): number {
215+
public death (current: INumber | number): number {
217216
// FIXME No toNumber() here
218217
return this.birth(current) + this.period.toNumber();
219218
}

packages/types/src/extrinsic/ExtrinsicPayload.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import type { AnyJson, BareOpts, Registry } from '@polkadot/types-codec/types';
55
import type { HexString } from '@polkadot/util/types';
66
import type { ExtrinsicPayloadV4 } from '../interfaces/extrinsics';
7-
import type { Balance, Hash, Index } from '../interfaces/runtime';
8-
import type { ExtrinsicPayloadValue, IKeyringPair } from '../types';
7+
import type { Hash } from '../interfaces/runtime';
8+
import type { ExtrinsicPayloadValue, ICompact, IKeyringPair, INumber } from '../types';
99

10-
import { Base, Compact, Raw, u32 } from '@polkadot/types-codec';
10+
import { Base, Raw } from '@polkadot/types-codec';
1111
import { u8aToHex } from '@polkadot/util';
1212

1313
import { DEFAULT_VERSION } from './constants';
@@ -80,30 +80,30 @@ export class GenericExtrinsicPayload extends Base<ExtrinsicPayloadVx> {
8080
/**
8181
* @description The [[Index]]
8282
*/
83-
public get nonce (): Compact<Index> {
83+
public get nonce (): ICompact<INumber> {
8484
return this.inner.nonce;
8585
}
8686

8787
/**
8888
* @description The specVersion as a [[u32]] for this payload
8989
*/
90-
public get specVersion (): u32 {
90+
public get specVersion (): INumber {
9191
// NOTE only v3+
9292
return this.inner.specVersion || this.registry.createTypeUnsafe('u32', []);
9393
}
9494

9595
/**
9696
* @description The [[Balance]]
9797
*/
98-
public get tip (): Compact<Balance> {
98+
public get tip (): ICompact<INumber> {
9999
// NOTE from v2+
100100
return this.inner.tip || this.registry.createTypeUnsafe('Compact<Balance>', []);
101101
}
102102

103103
/**
104104
* @description The transaction version as a [[u32]] for this payload
105105
*/
106-
public get transactionVersion (): u32 {
106+
public get transactionVersion (): INumber {
107107
// NOTE only v4+
108108
return this.inner.transactionVersion || this.registry.createTypeUnsafe('u32', []);
109109
}

packages/types/src/extrinsic/SignerPayload.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33

44
import type { Registry } from '@polkadot/types-codec/types';
55
import type { HexString } from '@polkadot/util/types';
6-
import type { Address, Balance, BlockNumber, Call, ExtrinsicEra, Hash, Index, RuntimeVersion } from '../interfaces';
7-
import type { Codec, ISignerPayload, SignerPayloadJSON, SignerPayloadRaw } from '../types';
6+
import type { Address, Call, ExtrinsicEra, Hash } from '../interfaces';
7+
import type { Codec, ICompact, INumber, IRuntimeVersion, ISignerPayload, SignerPayloadJSON, SignerPayloadRaw } from '../types';
88

9-
import { Compact, Option, Struct, Text, u8, Vec } from '@polkadot/types-codec';
9+
import { Option, Struct, Text, Vec } from '@polkadot/types-codec';
1010
import { objectProperty, objectSpread, u8aToHex } from '@polkadot/util';
1111

1212
export interface SignerPayloadType extends Codec {
1313
address: Address;
1414
blockHash: Hash;
15-
blockNumber: BlockNumber;
15+
blockNumber: INumber;
1616
era: ExtrinsicEra;
1717
genesisHash: Hash;
1818
method: Call;
19-
nonce: Compact<Index>;
20-
runtimeVersion: RuntimeVersion;
19+
nonce: ICompact<INumber>;
20+
runtimeVersion: IRuntimeVersion;
2121
signedExtensions: Vec<Text>;
22-
tip: Compact<Balance>;
23-
version: u8;
22+
tip: ICompact<INumber>;
23+
version: INumber;
2424
}
2525

2626
const knownTypes: Record<string, string> = {
@@ -71,7 +71,7 @@ export class GenericSignerPayload extends Struct implements ISignerPayload, Sign
7171
return this.getT('blockHash');
7272
}
7373

74-
get blockNumber (): BlockNumber {
74+
get blockNumber (): INumber {
7575
return this.getT('blockNumber');
7676
}
7777

@@ -87,23 +87,23 @@ export class GenericSignerPayload extends Struct implements ISignerPayload, Sign
8787
return this.getT('method');
8888
}
8989

90-
get nonce (): Compact<Index> {
90+
get nonce (): ICompact<INumber> {
9191
return this.getT('nonce');
9292
}
9393

94-
get runtimeVersion (): RuntimeVersion {
94+
get runtimeVersion (): IRuntimeVersion {
9595
return this.getT('runtimeVersion');
9696
}
9797

9898
get signedExtensions (): Vec<Text> {
9999
return this.getT('signedExtensions');
100100
}
101101

102-
get tip (): Compact<Balance> {
102+
get tip (): ICompact<INumber> {
103103
return this.getT('tip');
104104
}
105105

106-
get version (): u8 {
106+
get version (): INumber {
107107
return this.getT('version');
108108
}
109109

packages/types/src/extrinsic/v4/ExtrinsicPayload.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import type { SignOptions } from '@polkadot/keyring/types';
55
import type { Registry } from '@polkadot/types-codec/types';
66
import type { HexString } from '@polkadot/util/types';
77
import type { ExtrinsicEra } from '../../interfaces/extrinsics';
8-
import type { AssetId, Balance, Hash, Index } from '../../interfaces/runtime';
9-
import type { ExtrinsicPayloadValue, IKeyringPair } from '../../types';
8+
import type { Hash } from '../../interfaces/runtime';
9+
import type { ExtrinsicPayloadValue, ICompact, IKeyringPair, INumber, IOption } from '../../types';
1010

11-
import { Bytes, Compact, Enum, Option, Struct, u32 } from '@polkadot/types-codec';
11+
import { Bytes, Enum, Struct } from '@polkadot/types-codec';
1212
import { objectSpread } from '@polkadot/util';
1313

1414
import { sign } from '../util';
@@ -68,36 +68,36 @@ export class GenericExtrinsicPayloadV4 extends Struct {
6868
/**
6969
* @description The [[Index]]
7070
*/
71-
public get nonce (): Compact<Index> {
71+
public get nonce (): ICompact<INumber> {
7272
return this.getT('nonce');
7373
}
7474

7575
/**
7676
* @description The specVersion for this signature
7777
*/
78-
public get specVersion (): u32 {
78+
public get specVersion (): INumber {
7979
return this.getT('specVersion');
8080
}
8181

8282
/**
8383
* @description The tip [[Balance]]
8484
*/
85-
public get tip (): Compact<Balance> {
85+
public get tip (): ICompact<INumber> {
8686
return this.getT('tip');
8787
}
8888

8989
/**
9090
* @description The transactionVersion for this signature
9191
*/
92-
public get transactionVersion (): u32 {
92+
public get transactionVersion (): INumber {
9393
return this.getT('transactionVersion');
9494
}
9595

9696
/**
9797
* @description
9898
* The (optional) asset id for this signature for chains that support transaction fees in assets
9999
*/
100-
public get assetId (): Option<AssetId> {
100+
public get assetId (): IOption<INumber> {
101101
return this.getT('assetId');
102102
}
103103

packages/types/src/extrinsic/v4/ExtrinsicSignature.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
import type { HexString } from '@polkadot/util/types';
55
import type { EcdsaSignature, Ed25519Signature, ExtrinsicEra, ExtrinsicSignature, Sr25519Signature } from '../../interfaces/extrinsics';
6-
import type { Address, Balance, Call, Index } from '../../interfaces/runtime';
7-
import type { ExtrinsicPayloadValue, IExtrinsicSignature, IKeyringPair, Registry, SignatureOptions } from '../../types';
6+
import type { Address, Call } from '../../interfaces/runtime';
7+
import type { ExtrinsicPayloadValue, ICompact, IExtrinsicSignature, IKeyringPair, INumber, Registry, SignatureOptions } from '../../types';
88
import type { ExtrinsicSignatureOptions } from '../types';
99

10-
import { Compact, Struct } from '@polkadot/types-codec';
10+
import { Struct } from '@polkadot/types-codec';
1111
import { assert, isU8a, isUndefined, objectProperties, objectSpread, stringify, u8aToHex } from '@polkadot/util';
1212

1313
import { EMPTY_U8A, IMMORTAL_ERA } from '../constants';
@@ -85,7 +85,7 @@ export class GenericExtrinsicSignatureV4 extends Struct implements IExtrinsicSig
8585
/**
8686
* @description The [[Index]] for the signature
8787
*/
88-
public get nonce (): Compact<Index> {
88+
public get nonce (): ICompact<INumber> {
8989
return this.getT('nonce');
9090
}
9191

@@ -118,7 +118,7 @@ export class GenericExtrinsicSignatureV4 extends Struct implements IExtrinsicSig
118118
/**
119119
* @description The [[Balance]] tip
120120
*/
121-
public get tip (): Compact<Balance> {
121+
public get tip (): ICompact<INumber> {
122122
return this.getT('tip');
123123
}
124124

packages/types/src/types/extrinsic.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import type { AnyJson, AnyNumber, AnyTuple, AnyU8a, Codec } from '@polkadot/type
55
import type { HexString } from '@polkadot/util/types';
66
import type { ExtrinsicStatus } from '../interfaces/author';
77
import type { EcdsaSignature, Ed25519Signature, Sr25519Signature } from '../interfaces/extrinsics';
8-
import type { Address, Balance, Call, H256, Hash, Index } from '../interfaces/runtime';
8+
import type { Address, Call, H256, Hash } from '../interfaces/runtime';
99
import type { DispatchError, DispatchInfo, EventRecord } from '../interfaces/system';
10-
import type { ICompact, IKeyringPair, IMethod, IRuntimeVersion } from './interfaces';
10+
import type { ICompact, IKeyringPair, IMethod, INumber, IRuntimeVersionBase } from './interfaces';
1111
import type { Registry } from './registry';
1212

1313
export interface ISubmittableResult {
@@ -159,7 +159,7 @@ export interface SignatureOptions {
159159
era?: IExtrinsicEra;
160160
genesisHash: Uint8Array | string;
161161
nonce: AnyNumber;
162-
runtimeVersion: IRuntimeVersion;
162+
runtimeVersion: IRuntimeVersionBase;
163163
signedExtensions?: string[];
164164
signer?: Signer;
165165
tip?: AnyNumber;
@@ -169,10 +169,10 @@ export interface SignatureOptions {
169169
interface ExtrinsicSignatureBase {
170170
readonly isSigned: boolean;
171171
readonly era: IExtrinsicEra;
172-
readonly nonce: ICompact<Index>;
172+
readonly nonce: ICompact<INumber>;
173173
readonly signature: EcdsaSignature | Ed25519Signature | Sr25519Signature;
174174
readonly signer: Address;
175-
readonly tip: ICompact<Balance>;
175+
readonly tip: ICompact<INumber>;
176176
}
177177

178178
export interface ExtrinsicPayloadValue {

0 commit comments

Comments
 (0)