Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- **Breaking change** `api.derive.contract` is now `api.derive.contracts` to align with the substrate 2.x rename. (Feture detection is used so it supports both 1.x and 2.x chains)
- **Breaking change** The api now uses the module name instead of the prefix to generate the storage methods. The methods of the grandpa module changed from `api.query.grandpaFinality` to `api.query.grandpa`.
- Update with latest substrate 2.x types
- Support latest substrate 2.x v6 metadata with module constants

# 0.81.1

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"clean": "polkadot-dev-clean-build",
"postinstall": "polkadot-dev-yarn-only",
"test": "jest --coverage",
"test:skip-e2e": "jest --coverage --testPathIgnorePatterns e2e",
"test:watch": "jest --coverage --watch"
},
"devDependencies": {
Expand Down
Binary file modified packages/api-contract/test/contracts/flipper-pruned.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/api-contract/test/contracts/flipper.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
"return_type": "bool"
}
]
}
}
14 changes: 7 additions & 7 deletions packages/api/src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { AnyFunction, Codec, CodecArg, RegistryTypes } from '@polkadot/types/typ
import {
ApiInterface$Rx, ApiInterface$Events, ApiOptions, ApiTypes, DecorateMethodOptions,
DecoratedRpc, DecoratedRpc$Section,
QueryableModuleStorage, QueryableStorage, QueryableStorageFunction, QueryableStorageMulti, QueryableStorageMultiArg, QueryableStorageMultiArgs,
QueryableModuleStorage, QueryableStorage, QueryableStorageEntry, QueryableStorageMulti, QueryableStorageMultiArg, QueryableStorageMultiArgs,
SubmittableExtrinsicFunction, SubmittableExtrinsics, SubmittableModuleExtrinsics, Signer
} from './types';

Expand All @@ -23,7 +23,7 @@ import storageFromMeta from '@polkadot/storage/fromMetadata';
import { Event, getTypeRegistry, Hash, Metadata, Method, RuntimeVersion, Null, VectorAny } from '@polkadot/types';
import Linkage, { LinkageResult } from '@polkadot/types/codec/Linkage';
import { MethodFunction, ModulesWithMethods } from '@polkadot/types/primitive/Method';
import { StorageFunction } from '@polkadot/types/primitive/StorageKey';
import { StorageEntry } from '@polkadot/types/primitive/StorageKey';
import { assert, compactStripLength, isFunction, isObject, isUndefined, logger, u8aToHex } from '@polkadot/util';
import { cryptoWaitReady } from '@polkadot/util-crypto';

Expand Down Expand Up @@ -523,8 +523,8 @@ export default abstract class ApiBase<ApiType> {
private decorateMulti<ApiType> (decorateMethod: ApiBase<ApiType>['decorateMethod']): QueryableStorageMulti<ApiType> {
return decorateMethod(
(calls: QueryableStorageMultiArgs<ApiType>) => {
const mapped = calls.map((arg: QueryableStorageMultiArg<ApiType>): [QueryableStorageFunction<ApiType>, ...Array<CodecArg>] =>
// the input is a QueryableStorageFunction, convert to StorageFunction
const mapped = calls.map((arg: QueryableStorageMultiArg<ApiType>): [QueryableStorageEntry<ApiType>, ...Array<CodecArg>] =>
// the input is a QueryableStorageEntry, convert to StorageEntry
Array.isArray(arg)
? [arg[0].creator, ...arg.slice(1)]
: [arg.creator] as any
Expand Down Expand Up @@ -575,7 +575,7 @@ export default abstract class ApiBase<ApiType> {
}, {} as QueryableStorage<ApiType>);
}

private decorateStorageEntry<ApiType> (creator: StorageFunction, decorateMethod: ApiBase<ApiType>['decorateMethod']): QueryableStorageFunction<ApiType> {
private decorateStorageEntry<ApiType> (creator: StorageEntry, decorateMethod: ApiBase<ApiType>['decorateMethod']): QueryableStorageEntry<ApiType> {
const decorated = creator.headKey
? this.decorateStorageEntryLinked(creator, decorateMethod)
: decorateMethod(
Expand Down Expand Up @@ -633,10 +633,10 @@ export default abstract class ApiBase<ApiType> {
: [creator, arg1])
);

return this.decorateFunctionMeta(creator, decorated) as unknown as QueryableStorageFunction<ApiType>;
return this.decorateFunctionMeta(creator, decorated) as unknown as QueryableStorageEntry<ApiType>;
}

private decorateStorageEntryLinked<ApiType> (method: StorageFunction, decorateMethod: ApiBase<ApiType>['decorateMethod']): ReturnType<ApiBase<ApiType>['decorateMethod']> {
private decorateStorageEntryLinked<ApiType> (method: StorageEntry, decorateMethod: ApiBase<ApiType>['decorateMethod']): ReturnType<ApiBase<ApiType>['decorateMethod']> {
const result: Map<Codec, [Codec, Linkage<Codec>]> = new Map();
let subject: BehaviorSubject<LinkageResult>;
let head: Codec | null = null;
Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/promise/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { ProviderInterface } from '@polkadot/rpc-provider/types';
import { AnyFunction, Callback, Codec } from '@polkadot/types/types';
import { ApiOptions, DecorateMethodOptions, ObsInnerType, StorageFunctionPromiseOverloads, UnsubscribePromise } from '../types';
import { ApiOptions, DecorateMethodOptions, ObsInnerType, StorageEntryPromiseOverloads, UnsubscribePromise } from '../types';

import { EMPTY } from 'rxjs';
import { catchError, first, tap } from 'rxjs/operators';
Expand Down Expand Up @@ -198,7 +198,7 @@ export default class ApiPromise extends ApiBase<'promise'> {
};
}

protected decorateMethod<Method extends AnyFunction> (method: Method, options?: DecorateMethodOptions): StorageFunctionPromiseOverloads {
protected decorateMethod<Method extends AnyFunction> (method: Method, options?: DecorateMethodOptions): StorageEntryPromiseOverloads {
const needsCallback = options && options.methodName && options.methodName.includes('subscribe');

return function (...args: any[]) {
Expand Down Expand Up @@ -247,6 +247,6 @@ export default class ApiPromise extends ApiBase<'promise'> {
)
.subscribe(callback);
}) as UnsubscribePromise;
} as StorageFunctionPromiseOverloads;
} as StorageEntryPromiseOverloads;
}
}
4 changes: 2 additions & 2 deletions packages/api/src/promise/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

import { QueryableStorageFunction as QueryableStorageFunctionBase, SubmittableExtrinsicFunction as SubmittableExtrinsicFunctionBase } from '../types';
import { QueryableStorageEntry as QueryableStorageEntryBase, SubmittableExtrinsicFunction as SubmittableExtrinsicFunctionBase } from '../types';

import { SubmittableExtrinsic as SubmittableExtrinsicBase } from '../SubmittableExtrinsic';

export type QueryableStorageFunction = QueryableStorageFunctionBase<'promise'>;
export type QueryableStorageEntry = QueryableStorageEntryBase<'promise'>;
export type SubmittableExtrinsic = SubmittableExtrinsicBase<'promise'>;
export type SubmittableExtrinsicFunction = SubmittableExtrinsicFunctionBase<'promise'>;
24 changes: 12 additions & 12 deletions packages/api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ProviderInterface, ProviderInterface$Emitted } from '@polkadot/rpc-prov
import { Hash, RuntimeVersion, u64 as U64 } from '@polkadot/types';
import { AnyFunction, Callback, Codec, CodecArg, IExtrinsic, RegistryTypes, SignatureOptions } from '@polkadot/types/types';
import { MethodFunction } from '@polkadot/types/primitive/Method';
import { StorageFunction } from '@polkadot/types/primitive/StorageKey';
import { StorageEntry } from '@polkadot/types/primitive/StorageKey';

import ApiBase from './Base';
import { ISubmittableResult, SubmittableExtrinsic } from './SubmittableExtrinsic';
Expand Down Expand Up @@ -87,46 +87,46 @@ export interface DecoratedRpc<ApiType> {
system: DecoratedRpc$Section<ApiType>;
}

export interface StorageFunctionObservable {
export interface StorageEntryObservable {
(arg1?: CodecArg, arg2?: CodecArg): Observable<Codec>;
<T extends Codec>(arg1?: CodecArg, arg2?: CodecArg): Observable<T>;
at: (hash: Hash | Uint8Array | string, arg1?: CodecArg, arg2?: CodecArg) => Observable<Codec>;
creator: StorageFunction;
creator: StorageEntry;
hash: (arg1?: CodecArg, arg2?: CodecArg) => Observable<Hash>;
key: (arg1?: CodecArg, arg2?: CodecArg) => string;
multi: (args: Array<CodecArg[] | CodecArg>) => Observable<Codec>;
size: (arg1?: CodecArg, arg2?: CodecArg) => Observable<U64>;
}

export interface StorageFunctionPromiseOverloads {
export interface StorageEntryPromiseOverloads {
(arg1?: CodecArg, arg2?: CodecArg): Promise<Codec>;
<T extends Codec>(arg1?: CodecArg, arg2?: CodecArg): Promise<T>;
<T extends Codec>(callback: Callback<T>): UnsubscribePromise;
<T extends Codec>(arg: CodecArg, callback: Callback<T>): UnsubscribePromise;
<T extends Codec>(arg1: CodecArg, arg2: CodecArg, callback: Callback<T>): UnsubscribePromise;
}

export interface StorageFunctionPromise extends StorageFunctionPromiseOverloads {
export interface StorageEntryPromise extends StorageEntryPromiseOverloads {
at: (hash: Hash | Uint8Array | string, arg1?: CodecArg, arg2?: CodecArg) => Promise<Codec>;
creator: StorageFunction;
creator: StorageEntry;
hash: (arg1?: CodecArg, arg2?: CodecArg) => Promise<Hash>;
key: (arg1?: CodecArg, arg2?: CodecArg) => string;
multi: <T extends Codec>(args: Array<CodecArg[] | CodecArg>, callback?: Callback<Array<T>>) => Promise<Array<T>>;
size: (arg1?: CodecArg, arg2?: CodecArg) => Promise<U64>;
}

export type QueryableStorageFunction<ApiType> =
export type QueryableStorageEntry<ApiType> =
ApiType extends 'rxjs'
? StorageFunctionObservable
: StorageFunctionPromise;
? StorageEntryObservable
: StorageEntryPromise;

export interface QueryableModuleStorage<ApiType> {
[index: string]: QueryableStorageFunction<ApiType>;
[index: string]: QueryableStorageEntry<ApiType>;
}

export type QueryableStorageMultiArg<ApiType> =
QueryableStorageFunction<ApiType> |
[QueryableStorageFunction<ApiType>, ...Array<CodecArg>];
QueryableStorageEntry<ApiType> |
[QueryableStorageEntry<ApiType>, ...Array<CodecArg>];

export type QueryableStorageMultiArgs<ApiType> = Array<QueryableStorageMultiArg<ApiType>>;

Expand Down
3 changes: 0 additions & 3 deletions packages/api/test/e2e/promise-contract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,8 @@ describe.skip('Promise e2e contracts', () => {
api.tx.contracts
.putCode(MAX_GAS, `0x${flipperCode}`)
.signAndSend(keyring.eve, (result: SubmittableResult) => {
console.error('putCode', JSON.stringify(result));

if (result.status.isFinalized) {
const record = result.findRecord('contract', 'CodeStored');

if (record) {
codeHash = record.event.data[0] as Hash;

Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/e2e/promise-tx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe.skip('Promise e2e transactions', () => {
// don't wait for status, just get hash. Here we generate a large-ish payload
// to ensure that we can sign with the hashed version as well (and have it accepted)
const hash: Hash = await api.tx.democracy
.propose(api.tx.consensus.setCode(randomAsHex(4096)), 10000)
.propose(api.tx.system.setCode(randomAsHex(4096)), 10000)
.signAndSend(keyring.bob);

expect(hash.toHex()).toHaveLength(66);
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/e2e/rx-tx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe.skip('Rx e2e transactions', () => {
first(),
switchMap((nonce: Index) =>
api.tx.democracy
.propose(api.tx.consensus.setCode('0xdeadbeef'), 10000)
.propose(api.tx.system.setCode('0xdeadbeef'), 10000)
.sign(keyring.alice, { nonce })
.send()
)
Expand Down
10 changes: 9 additions & 1 deletion packages/rpc-core/src/formatting.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Metadata from '@polkadot/types/Metadata';
import rpcMetadataV3 from '@polkadot/types/Metadata/v3/static';
import rpcMetadataV4 from '@polkadot/types/Metadata/v4/static';
import rpcMetadataV5 from '@polkadot/types/Metadata/v5/static';
import rpcMetadataV6 from '@polkadot/types/Metadata/v6/static';

import Api from '.';

Expand Down Expand Up @@ -111,8 +112,9 @@ function formattingTests (version: string, storage: Storage, encodedValues: [str
});

it('handles the case where Option<Bytes> are retrieved', (done) => {
let call = Number(version.slice(1)) <= 5 ? storage.contract.pristineCode : storage.contracts.pristineCode;
api.state
.subscribeStorage([[storage.contract.pristineCode, '0x00']])
.subscribeStorage([[call, '0x00']])
.subscribe((value: any) => {
// console.error(value);

Expand Down Expand Up @@ -140,3 +142,9 @@ formattingTests('v5', fromMetadata(new Metadata(rpcMetadataV5)), [
'0x1dbb0224910f42a14e7f1406b24c6fe8157296691b02a78756e01946038fffab',
'0xc7879f4faa637a90d782070a3cb6be99a9fb0316e19a0454ce93c4f0a34712f1'
]);

formattingTests('v6', fromMetadata(new Metadata(rpcMetadataV6)), [
'0xec8f96437274a883afcac82d01a9defeb68209cd4f2c084632813692aa5e65ad',
'0x1dbb0224910f42a14e7f1406b24c6fe8157296691b02a78756e01946038fffab',
'0xc7879f4faa637a90d782070a3cb6be99a9fb0316e19a0454ce93c4f0a34712f1'
]);
4 changes: 2 additions & 2 deletions packages/rpc-provider/src/mock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import interfaces from '@polkadot/jsonrpc';
import testKeyring from '@polkadot/keyring/testing';
import storage from '@polkadot/storage/static';
import { Codec } from '@polkadot/types/types';
import rpcMetadataV5 from '@polkadot/types/Metadata/v5/static';
import rpcMetadataV6 from '@polkadot/types/Metadata/v6/static';
import { Header, RuntimeVersion } from '@polkadot/types';
import { bnToU8a, logger, u8aToHex } from '@polkadot/util';
import { randomAsU8a } from '@polkadot/util-crypto';
Expand Down Expand Up @@ -50,7 +50,7 @@ export default class Mock implements ProviderInterface {
);
},
'system_chain': (): string => 'mockChain',
'state_getMetadata': (): string => rpcMetadataV5,
'state_getMetadata': (): string => rpcMetadataV6,
'system_name': (): string => 'mockClient',
'system_version': (): string => '9.8.7'
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

import { FunctionMetadata } from '@polkadot/types/Metadata/v5/Calls';
import { FunctionMetadata } from '@polkadot/types/Metadata/v6/Calls';
import { MethodFunction } from '@polkadot/types/primitive/Method';
import { Method } from '@polkadot/types';
import { assert, stringCamelCase } from '@polkadot/util';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// of the Apache-2.0 license. See the LICENSE file for details.

import { Extrinsic, Metadata } from '@polkadot/types';
import json from '@polkadot/types/Metadata/v5/static';
import json from '@polkadot/types/Metadata/v6/static';

import fromV5 from '.';
import fromV6 from '.';

// Use the pre-generated metadata
const metadata = new Metadata(json);
const newExtrinsics = fromV5(metadata);
const newExtrinsics = fromV6(metadata);

describe('fromMetadata', () => {
it('should throw if an incorrect number of args is supplied', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/type-extrinsics/src/fromMetadata/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

import { ModuleMetadata } from '@polkadot/types/Metadata/v5/Metadata';
import { ModuleMetadata } from '@polkadot/types/Metadata/v6/Metadata';
import { Methods, ModulesWithMethods } from '@polkadot/types/primitive/Method';
import Metadata from '@polkadot/types/Metadata';
import { stringCamelCase } from '@polkadot/util';
Expand All @@ -18,7 +18,7 @@ import createUnchecked from './createUnchecked';
* @param metadata - The metadata to extend the storage object against.
*/
export default function fromMetadata (metadata: Metadata): ModulesWithMethods {
return metadata.asV5.modules
return metadata.asV6.modules
.filter((modul) => modul.calls.isSome)
.reduce((result, modul: ModuleMetadata, sectionIndex) => {
const section = stringCamelCase(modul.name.toString());
Expand Down
2 changes: 1 addition & 1 deletion packages/type-extrinsics/src/static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.

import { Metadata } from '@polkadot/types';
import metadataRpc from '@polkadot/types/Metadata/v5/static';
import metadataRpc from '@polkadot/types/Metadata/v6/static';
import { ModulesWithMethods } from '@polkadot/types/primitive/Method';

import fromMetadata from './fromMetadata';
Expand Down
4 changes: 2 additions & 2 deletions packages/type-storage/src/fromMetadata/createFunction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details.

import { StorageHasher, Text } from '@polkadot/types';
import { StorageFunction } from '@polkadot/types/primitive/StorageKey';
import { StorageEntry } from '@polkadot/types/primitive/StorageKey';
import { stringToU8a, u8aConcat, u8aToHex } from '@polkadot/util';

import createFunction from './createFunction';
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('createFunction', () => {
});

describe('the created double map function', () => {
let storageFn: StorageFunction;
let storageFn: StorageEntry;
beforeAll(() => {
storageFn = createFunction(
'GenericAsset',
Expand Down
14 changes: 7 additions & 7 deletions packages/type-storage/src/fromMetadata/createFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// of the Apache-2.0 license. See the LICENSE file for details.

import { createType, Bytes, Compact, StorageKey, Text, U8a } from '@polkadot/types';
import { PlainType, StorageFunctionMetadata, StorageFunctionModifier, StorageFunctionType } from '@polkadot/types/Metadata/v5/Storage';
import { StorageFunction } from '@polkadot/types/primitive/StorageKey';
import { PlainType, StorageEntryMetadata, StorageEntryModifier, StorageEntryType } from '@polkadot/types/Metadata/v6/Storage';
import { StorageEntry } from '@polkadot/types/primitive/StorageKey';
import { assert, isNull, isUndefined, stringLowerFirst, stringToU8a, u8aConcat } from '@polkadot/util';

import getHasher, { HasherFunction } from './getHasher';
Expand All @@ -24,7 +24,7 @@ export interface CreateItemOptions {
* are not known at runtime (from state_getMetadata), they need to be supplied
* by us manually at compile time.
*/
export default function createFunction (section: Text | string, method: Text | string, meta: StorageFunctionMetadata, options: CreateItemOptions = {}): StorageFunction {
export default function createFunction (section: Text | string, method: Text | string, meta: StorageEntryMetadata, options: CreateItemOptions = {}): StorageEntry {
const stringKey = options.key
? options.key
: `${section.toString()} ${method.toString()}`;
Expand Down Expand Up @@ -81,10 +81,10 @@ export default function createFunction (section: Text | string, method: Text | s
if (meta.type.isMap && meta.type.asMap.isLinked) {
const keyHash = new U8a(hasher(`head of ${stringKey}`));
const keyFn: any = () => keyHash;
keyFn.meta = new StorageFunctionMetadata({
keyFn.meta = new StorageEntryMetadata({
name: meta.name,
modifier: new StorageFunctionModifier('Required'),
type: new StorageFunctionType(new PlainType(meta.type.asMap.key), 0),
modifier: new StorageEntryModifier('Required'),
type: new StorageEntryType(new PlainType(meta.type.asMap.key), 0),
fallback: new Bytes(),
documentation: meta.documentation
});
Expand All @@ -96,5 +96,5 @@ export default function createFunction (section: Text | string, method: Text | s
storageFn.section = stringLowerFirst(section.toString());
storageFn.toJSON = (): any => meta.toJSON();

return storageFn as StorageFunction;
return storageFn as StorageEntry;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import testingPairs from '@polkadot/keyring/testingPairs';
import { Metadata } from '@polkadot/types';
import json from '@polkadot/types/Metadata/v5/static';
import json from '@polkadot/types/Metadata/v6/static';
import { u8aToHex } from '@polkadot/util';

import fromMetadata from './';
Expand Down
2 changes: 1 addition & 1 deletion packages/type-storage/src/fromMetadata/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { storage } from './storage';
* @param metadata - The metadata to extend the storage object against.
*/
export default function fromMetadata (metadata: Metadata): Storage {
return metadata.asV5.modules.reduce((result, moduleMetadata) => {
return metadata.asV6.modules.reduce((result, moduleMetadata) => {
if (moduleMetadata.storage.isNone) {
return result;
}
Expand Down
Loading