Skip to content

Commit c4662e6

Browse files
authored
Adjust types based on use (#2742)
* Adjust types based on use * MetaRegistry * Remove FIXME * Split path extraction * casing * Rewrite * Rewrite interfaces * Remove unused vuepress
1 parent 995de20 commit c4662e6

File tree

24 files changed

+1340
-1351
lines changed

24 files changed

+1340
-1351
lines changed

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@
2424
"test:watch": "polkadot-dev-run-test --watch"
2525
},
2626
"devDependencies": {
27-
"@babel/core": "^7.11.6",
28-
"@babel/register": "^7.11.5",
29-
"@babel/runtime": "^7.11.2",
30-
"@polkadot/dev": "^0.58.1",
27+
"@babel/core": "^7.12.3",
28+
"@babel/register": "^7.12.1",
29+
"@babel/runtime": "^7.12.1",
30+
"@polkadot/dev": "^0.58.3",
3131
"@polkadot/ts": "^0.3.49",
3232
"@polkadot/typegen": "workspace:packages/typegen",
3333
"@types/jest": "^26.0.14",
34-
"@vuepress/plugin-search": "^1.6.0",
3534
"copyfiles": "^2.4.0"
3635
},
3736
"version": "2.2.2-10"

packages/api-contract/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
},
2727
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api-contract#readme",
2828
"dependencies": {
29-
"@babel/runtime": "^7.11.2",
29+
"@babel/runtime": "^7.12.1",
3030
"@polkadot/api": "2.2.2-10",
3131
"@polkadot/rpc-core": "2.2.2-10",
3232
"@polkadot/types": "2.2.2-10",
33-
"@polkadot/util": "^3.6.0-beta.9",
33+
"@polkadot/util": "^3.6.0-beta.16",
3434
"bn.js": "^5.1.3",
3535
"rxjs": "^6.6.3"
3636
}

packages/api-contract/src/Abi.ts

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

44
import { AnyJson } from '@polkadot/types/types';
5-
import { ChainProperties, InkConstructorSpec, InkMessageSpec, InkProject } from '@polkadot/types/interfaces';
5+
import { ChainProperties, ContractConstructorSpec, ContractMessageSpec, ContractProject } from '@polkadot/types/interfaces';
66
import { AbiConstructor, AbiMessage, AbiMessageParam } from './types';
77

88
import { assert, isNumber, isObject, isString, stringCamelCase } from '@polkadot/util';
@@ -28,7 +28,7 @@ export default class Abi {
2828

2929
public readonly messages: AbiMessage[];
3030

31-
public readonly project: InkProject;
31+
public readonly project: ContractProject;
3232

3333
public readonly registry: MetaRegistry;
3434

@@ -41,17 +41,17 @@ export default class Abi {
4141

4242
this.json = json;
4343
this.registry = new MetaRegistry(chainProperties);
44-
this.project = this.registry.createType('InkProject', json);
44+
this.project = this.registry.createType('ContractProject', json);
4545

4646
this.registry.setMetaTypes(this.project.types);
4747

48-
this.project.types.forEach((_, index) => this.registry.getMetaTypeDef(this.registry.createType('MtLookupTypeId', index + 1)));
49-
this.constructors = this.project.spec.constructors.map((spec: InkConstructorSpec, index) =>
48+
this.project.types.forEach((_, index) => this.registry.getMetaTypeDef(this.registry.createType('SiLookupTypeId', index + 1)));
49+
this.constructors = this.project.spec.constructors.map((spec: ContractConstructorSpec, index) =>
5050
this.#createBase(spec, index, {
5151
isConstructor: true
5252
})
5353
);
54-
this.messages = this.project.spec.messages.map((spec: InkMessageSpec, index): AbiMessage => {
54+
this.messages = this.project.spec.messages.map((spec: ContractMessageSpec, index): AbiMessage => {
5555
const typeSpec = spec.returnType.unwrapOr(null);
5656

5757
return this.#createBase(spec, index, {
@@ -72,7 +72,7 @@ export default class Abi {
7272
return findMessage(this.messages, messageOrId);
7373
}
7474

75-
#createBase = (spec: InkMessageSpec | InkConstructorSpec, index: number, add: Partial<AbiMessage> = {}): AbiMessage => {
75+
#createBase = (spec: ContractMessageSpec | ContractConstructorSpec, index: number, add: Partial<AbiMessage> = {}): AbiMessage => {
7676
const identifier = spec.name.toString();
7777
const args = spec.args.map((arg, index): AbiMessageParam => {
7878
try {

packages/api-contract/src/MetaRegistry.ts

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
import { ChainProperties, MtField, MtLookupTypeId, MtType, MtTypeDefArray, MtTypeDefVariant, MtTypeDefSequence, MtTypeDefTuple, MtVariant } from '@polkadot/types/interfaces';
4+
import { ChainProperties, SiField, SiLookupTypeId, SiType, SiTypeDefArray, SiTypeDefVariant, SiTypeDefSequence, SiTypeDefTuple, SiVariant } from '@polkadot/types/interfaces';
55
import { InterfaceTypes, TypeDef, TypeDefInfo } from '@polkadot/types/types';
66

77
import { assert, isUndefined } from '@polkadot/util';
88
import { TypeRegistry, withTypeString } from '@polkadot/types';
99

1010
// convert the offset into project-specific, index-1
11-
export function getRegistryOffset (id: MtLookupTypeId): number {
11+
export function getRegistryOffset (id: SiLookupTypeId): number {
1212
return id.toNumber() - 1;
1313
}
1414

1515
const PRIMITIVE_ALIAS: Record<string, keyof InterfaceTypes> = {
16+
Char: 'u32', // Rust char is 4-bytes
1617
Str: 'Text'
1718
};
1819

1920
export default class MetaRegistry extends TypeRegistry {
2021
public readonly metaTypeDefs: TypeDef[] = [];
2122

22-
#metaTypes: MtType[] = [];
23+
#siTypes: SiType[] = [];
2324

2425
constructor (chainProperties?: ChainProperties) {
2526
super();
@@ -29,11 +30,11 @@ export default class MetaRegistry extends TypeRegistry {
2930
}
3031
}
3132

32-
public setMetaTypes (metaTypes: MtType[]): void {
33-
this.#metaTypes = metaTypes;
33+
public setMetaTypes (metaTypes: SiType[]): void {
34+
this.#siTypes = metaTypes;
3435
}
3536

36-
public getMetaTypeDef (id: MtLookupTypeId): TypeDef {
37+
public getMetaTypeDef (id: SiLookupTypeId): TypeDef {
3738
const offset = getRegistryOffset(id);
3839
let typeDef = this.metaTypeDefs[offset];
3940

@@ -54,19 +55,22 @@ export default class MetaRegistry extends TypeRegistry {
5455
return typeDef;
5556
}
5657

57-
#getMetaType = (id: MtLookupTypeId): MtType => {
58-
const type = this.#metaTypes[getRegistryOffset(id)];
58+
#getMetaType = (id: SiLookupTypeId): SiType => {
59+
const type = this.#siTypes[getRegistryOffset(id)];
5960

6061
assert(!isUndefined(type), `getMetaType:: Unable to find ${id.toNumber()} in type values`);
6162

62-
return this.createType('MtType', type);
63+
return this.createType('SiType', type);
6364
}
6465

65-
#extract = (type: MtType, id: MtLookupTypeId): TypeDef => {
66+
#extract = (type: SiType, id: SiLookupTypeId): TypeDef => {
6667
const path = [...type.path];
6768
let typeDef: Omit<TypeDef, 'type'>;
6869

69-
if (type.path.join('::').startsWith('ink_env::types::') || type.def.isPrimitive) {
70+
// TODO solang?
71+
if (type.path.join('::').startsWith('ink_env::types::')) {
72+
typeDef = this.#extractPrimitivePath(type);
73+
} else if (type.def.isPrimitive) {
7074
typeDef = this.#extractPrimitive(type);
7175
} else if (type.def.isComposite) {
7276
typeDef = this.#extractFields(type.def.asComposite.fields);
@@ -101,8 +105,8 @@ export default class MetaRegistry extends TypeRegistry {
101105
});
102106
}
103107

104-
#extractArray = ({ len: length, type }: MtTypeDefArray): Omit<TypeDef, 'type'> => {
105-
assert(!length || length.toNumber() <= 256, 'ContractRegistry: Only support for [Type; <length>], where length > 256');
108+
#extractArray = ({ len: length, type }: SiTypeDefArray): Omit<TypeDef, 'type'> => {
109+
assert(!length || length.toNumber() <= 256, 'MetaRegistry: Only support for [Type; <length>], where length > 256');
106110

107111
return {
108112
info: TypeDefInfo.VecFixed,
@@ -111,7 +115,7 @@ export default class MetaRegistry extends TypeRegistry {
111115
};
112116
}
113117

114-
#extractFields = (fields: MtField[]): Omit<TypeDef, 'type'> => {
118+
#extractFields = (fields: SiField[]): Omit<TypeDef, 'type'> => {
115119
const [isStruct, isTuple] = fields.reduce(([isAllNamed, isAllUnnamed], { name }) => ([
116120
isAllNamed && name.isSome,
117121
isAllUnnamed && name.isNone
@@ -141,26 +145,23 @@ export default class MetaRegistry extends TypeRegistry {
141145
: { info, sub };
142146
}
143147

144-
#extractPrimitive = (type: MtType): TypeDef => {
145-
if (type.def.isPrimitive) {
146-
const typeStr = type.def.asPrimitive.type.toString();
148+
#extractPrimitive = (type: SiType): TypeDef => {
149+
const typeStr = type.def.asPrimitive.type.toString();
147150

148-
return {
149-
info: TypeDefInfo.Plain,
150-
// FIXME This should not be as a blanket toLowerCase
151-
type: PRIMITIVE_ALIAS[typeStr] || typeStr.toLowerCase()
152-
};
153-
} else if (type.path.length > 1) {
154-
return {
155-
info: TypeDefInfo.Plain,
156-
type: type.path[type.path.length - 1].toString()
157-
};
158-
}
151+
return {
152+
info: TypeDefInfo.Plain,
153+
type: PRIMITIVE_ALIAS[typeStr] || typeStr.toLowerCase()
154+
};
155+
}
159156

160-
throw new Error('Invalid primitive type');
157+
#extractPrimitivePath = (type: SiType): TypeDef => {
158+
return {
159+
info: TypeDefInfo.Plain,
160+
type: type.path[type.path.length - 1].toString()
161+
};
161162
}
162163

163-
#extractSequence = ({ type }: MtTypeDefSequence, id: MtLookupTypeId): Omit<TypeDef, 'type'> => {
164+
#extractSequence = ({ type }: SiTypeDefSequence, id: SiLookupTypeId): Omit<TypeDef, 'type'> => {
164165
assert(!!type, `ContractRegistry: Invalid sequence type found at id ${id.toString()}`);
165166

166167
return {
@@ -169,7 +170,7 @@ export default class MetaRegistry extends TypeRegistry {
169170
};
170171
}
171172

172-
#extractTuple = (ids: MtTypeDefTuple): Omit<TypeDef, 'type'> => {
173+
#extractTuple = (ids: SiTypeDefTuple): Omit<TypeDef, 'type'> => {
173174
return ids.length === 1
174175
? this.getMetaTypeDef(ids[0])
175176
: {
@@ -178,7 +179,7 @@ export default class MetaRegistry extends TypeRegistry {
178179
};
179180
}
180181

181-
#extractVariant = ({ variants }: MtTypeDefVariant, id: MtLookupTypeId): Omit<TypeDef, 'type'> => {
182+
#extractVariant = ({ variants }: SiTypeDefVariant, id: SiLookupTypeId): Omit<TypeDef, 'type'> => {
182183
const { params, path } = this.#getMetaType(id);
183184
const specialVariant = path[0].toString();
184185

@@ -203,7 +204,7 @@ export default class MetaRegistry extends TypeRegistry {
203204
};
204205
}
205206

206-
#extractVariantSub = (variants: MtVariant[]): TypeDef[] => {
207+
#extractVariantSub = (variants: SiVariant[]): TypeDef[] => {
207208
const isAllUnitVariants = variants.every(({ fields }) => fields.length === 0);
208209

209210
if (isAllUnitVariants) {

packages/api-contract/src/types.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 { ApiTypes } from '@polkadot/api/types';
5-
import { ContractExecResult, InkSelector } from '@polkadot/types/interfaces';
5+
import { ContractExecResult, ContractSelector } from '@polkadot/types/interfaces';
66
import { Codec, TypeDef } from '@polkadot/types/types';
77

88
import ApiBase from '@polkadot/api/base';
@@ -30,7 +30,7 @@ export interface AbiMessage {
3030
isMutating?: boolean;
3131
isPayable?: boolean;
3232
returnType?: TypeDef | null;
33-
selector: InkSelector;
33+
selector: ContractSelector;
3434
}
3535

3636
export type AbiConstructor = AbiMessage;

packages/api-contract/src/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function encodeMessage (registry: Registry, message: AbiMessage | AbiCons
3535
r[name] = type.displayName || encodeTypeDef(type);
3636

3737
return r;
38-
}, { __selector: 'InkSelector' })
38+
}, { __selector: 'ContractSelector' })
3939
));
4040

4141
return compactAddLength(new Clazz(registry, message.args.reduce((r: Record<string, CodecArg>, { name }, index): Record<string, CodecArg> => {

packages/api-derive/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@
2727
},
2828
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api-derive#readme",
2929
"dependencies": {
30-
"@babel/runtime": "^7.11.2",
30+
"@babel/runtime": "^7.12.1",
3131
"@polkadot/api": "2.2.2-10",
3232
"@polkadot/rpc-core": "2.2.2-10",
3333
"@polkadot/rpc-provider": "2.2.2-10",
3434
"@polkadot/types": "2.2.2-10",
35-
"@polkadot/util": "^3.6.0-beta.9",
36-
"@polkadot/util-crypto": "^3.6.0-beta.9",
35+
"@polkadot/util": "^3.6.0-beta.16",
36+
"@polkadot/util-crypto": "^3.6.0-beta.16",
3737
"bn.js": "^5.1.3",
3838
"memoizee": "^0.4.14",
3939
"rxjs": "^6.6.3"
4040
},
4141
"devDependencies": {
42-
"@polkadot/keyring": "^3.6.0-beta.9"
42+
"@polkadot/keyring": "^3.6.0-beta.16"
4343
}
4444
}

packages/api/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@
2626
},
2727
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/api#readme",
2828
"dependencies": {
29-
"@babel/runtime": "^7.11.2",
29+
"@babel/runtime": "^7.12.1",
3030
"@polkadot/api-derive": "2.2.2-10",
31-
"@polkadot/keyring": "^3.6.0-beta.9",
31+
"@polkadot/keyring": "^3.6.0-beta.16",
3232
"@polkadot/metadata": "2.2.2-10",
3333
"@polkadot/rpc-core": "2.2.2-10",
3434
"@polkadot/rpc-provider": "2.2.2-10",
3535
"@polkadot/types": "2.2.2-10",
3636
"@polkadot/types-known": "2.2.2-10",
37-
"@polkadot/util": "^3.6.0-beta.9",
38-
"@polkadot/util-crypto": "^3.6.0-beta.9",
37+
"@polkadot/util": "^3.6.0-beta.16",
38+
"@polkadot/util-crypto": "^3.6.0-beta.16",
3939
"bn.js": "^5.1.3",
4040
"eventemitter3": "^4.0.7",
4141
"rxjs": "^6.6.3"
4242
},
4343
"devDependencies": {
44-
"@polkadot/keyring": "^3.6.0-beta.9"
44+
"@polkadot/keyring": "^3.6.0-beta.16"
4545
}
4646
}

packages/metadata/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
},
2727
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/type-metadata#readme",
2828
"dependencies": {
29-
"@babel/runtime": "^7.11.2",
29+
"@babel/runtime": "^7.12.1",
3030
"@polkadot/types": "2.2.2-10",
3131
"@polkadot/types-known": "2.2.2-10",
32-
"@polkadot/util": "^3.6.0-beta.9",
33-
"@polkadot/util-crypto": "^3.6.0-beta.9",
32+
"@polkadot/util": "^3.6.0-beta.16",
33+
"@polkadot/util-crypto": "^3.6.0-beta.16",
3434
"bn.js": "^5.1.3"
3535
},
3636
"devDependencies": {
37-
"@polkadot/keyring": "^3.6.0-beta.9"
37+
"@polkadot/keyring": "^3.6.0-beta.16"
3838
}
3939
}

packages/rpc-core/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626
},
2727
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/rpc-core#readme",
2828
"dependencies": {
29-
"@babel/runtime": "^7.11.2",
29+
"@babel/runtime": "^7.12.1",
3030
"@polkadot/metadata": "2.2.2-10",
3131
"@polkadot/rpc-provider": "2.2.2-10",
3232
"@polkadot/types": "2.2.2-10",
33-
"@polkadot/util": "^3.6.0-beta.9",
33+
"@polkadot/util": "^3.6.0-beta.16",
3434
"memoizee": "^0.4.14",
3535
"rxjs": "^6.6.3"
3636
},
3737
"devDependencies": {
38-
"@polkadot/keyring": "^3.6.0-beta.9"
38+
"@polkadot/keyring": "^3.6.0-beta.16"
3939
}
4040
}

0 commit comments

Comments
 (0)