Support ABI files with wasm (.contract output) (#2841)

* Support ABI files with wasm (.contract output)

* AbiBase exports

* Additional default exports

* Remove any

* Fix build
This commit is contained in:
Jaco Greeff
2020-11-12 12:23:28 +01:00
committed by GitHub
parent 999e69b031
commit 98a2ef9d72
43 changed files with 228 additions and 68 deletions
+1 -1
View File
@@ -4,7 +4,7 @@
import { ApiTypes, DecorateMethod } from '@polkadot/api/types';
import { AnyJson, Registry } from '@polkadot/types/types';
import ApiBase from '@polkadot/api/base';
import { ApiBase } from '@polkadot/api/base';
import { assert } from '@polkadot/util';
import { Abi } from '../Abi';
+1 -1
View File
@@ -10,7 +10,7 @@ import { MapConstructorExec } from './types';
import BN from 'bn.js';
import { SubmittableResult } from '@polkadot/api';
import ApiBase from '@polkadot/api/base';
import { ApiBase } from '@polkadot/api/base';
import { isUndefined, stringCamelCase } from '@polkadot/util';
import { Abi } from '../Abi';
@@ -0,0 +1,27 @@
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
// SPDX-License-Identifier: Apache-2.0
import fs from 'fs';
import path from 'path';
import { decorateMethod } from '@polkadot/api/promise';
import contractFlipper from '../../test/contracts/ink/flipper.contract.json';
import abiFlipper from '../../test/contracts/ink/flipper.json';
import { Code } from './Code';
import { mockApi } from './mock';
const wasmFlipper = fs.readFileSync(path.join(__dirname, '../../test/contracts/ink/flipper.wasm'));
describe('Code', (): void => {
it('can construct with an individual ABI/WASM combo', (): void => {
expect(
() => new Code(mockApi, abiFlipper, wasmFlipper, decorateMethod)
).not.toThrow();
});
it('can construct with an .contract ABI', (): void => {
expect(
() => new Code(mockApi, contractFlipper, null, decorateMethod)
).not.toThrow();
});
});
+8 -4
View File
@@ -7,8 +7,8 @@ import { EventRecord, Hash } from '@polkadot/types/interfaces';
import { AnyJson, ISubmittableResult } from '@polkadot/types/types';
import { SubmittableResult } from '@polkadot/api';
import ApiBase from '@polkadot/api/base';
import { compactAddLength, u8aToU8a } from '@polkadot/util';
import { ApiBase } from '@polkadot/api/base';
import { assert, compactAddLength, isWasm, u8aToU8a } from '@polkadot/util';
import { Abi } from '../Abi';
import { applyOnEvent } from '../util';
@@ -28,10 +28,14 @@ export class CodeSubmittableResult<ApiType extends ApiTypes> extends Submittable
export class Code<ApiType extends ApiTypes> extends Base<ApiType> {
public readonly code: Uint8Array;
constructor (api: ApiBase<ApiType>, abi: AnyJson | Abi, wasm: Uint8Array | string | Buffer, decorateMethod: DecorateMethod<ApiType>) {
constructor (api: ApiBase<ApiType>, abi: AnyJson | Abi, wasm: Uint8Array | string | Buffer | null | undefined, decorateMethod: DecorateMethod<ApiType>) {
super(api, abi, decorateMethod);
this.code = u8aToU8a(wasm);
this.code = isWasm(this.abi.project.source.wasm)
? this.abi.project.source.wasm
: u8aToU8a(wasm);
assert(isWasm(this.code), 'No WASM code provided');
}
public createBlueprint (): SubmittableExtrinsic<ApiType, CodeSubmittableResult<ApiType>> {
+1 -1
View File
@@ -11,7 +11,7 @@ import { ContractRead, MapMessageExec, MapMessageRead } from './types';
import BN from 'bn.js';
import { map } from 'rxjs/operators';
import { SubmittableResult } from '@polkadot/api';
import ApiBase from '@polkadot/api/base';
import { ApiBase } from '@polkadot/api/base';
import { assert, bnToBn, isFunction, isUndefined, logger, stringCamelCase } from '@polkadot/util';
import { Abi } from '../Abi';
+20
View File
@@ -0,0 +1,20 @@
// Copyright 2017-2020 @polkadot/api-contract authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { ApiBase } from '@polkadot/api/base';
import { TypeRegistry } from '@polkadot/types';
const registry = new TypeRegistry();
export const mockApi = {
registry,
rx: {
tx: {
contracts: {
putCode: (): never => {
throw new Error('mock');
}
}
}
}
} as unknown as ApiBase<'promise'>;
+1 -1
View File
@@ -9,7 +9,7 @@ import { Abi } from '../Abi';
import { Code as BaseCode } from '../base';
export class Code extends BaseCode<'promise'> {
constructor (api: ApiPromise, abi: AnyJson | Abi, wasm: Uint8Array | string | Buffer) {
constructor (api: ApiPromise, abi: AnyJson | Abi, wasm: Uint8Array | string | Buffer | null | undefined) {
super(api, abi, wasm, decorateMethod);
}
}
+1 -1
View File
@@ -10,7 +10,7 @@ import { Abi } from '../Abi';
import { Code as BaseCode } from '../base';
export class Code extends BaseCode<'rxjs'> {
constructor (api: ApiRx, abi: AnyJson | Abi, wasm: Uint8Array | string | Buffer) {
constructor (api: ApiRx, abi: AnyJson | Abi, wasm: Uint8Array | string | Buffer | null | undefined) {
super(api, abi, wasm, decorateMethod);
}
}
+1 -1
View File
@@ -5,7 +5,7 @@ import { ApiTypes } from '@polkadot/api/types';
import { ContractExecResultResult, ContractSelector } from '@polkadot/types/interfaces';
import { Codec, CodecArg, TypeDef } from '@polkadot/types/types';
import ApiBase from '@polkadot/api/base';
import { ApiBase } from '@polkadot/api/base';
import { Text, u64 } from '@polkadot/types';
import { Abi } from '.';
@@ -0,0 +1,107 @@
{
"metadataVersion": "0.1.0",
"source": {
"hash": "0x98086d5ccadd437459b812682380250f51a3034272cd1108e499acc0cafe9f42",
"language": "ink! 3.0.0-rc2",
"compiler": "rustc 1.49.0-nightly",
"wasm": "0x0061736d01000000012c0860027f7f0060037f7f7f0060017f017f60017f006000017f60037f7f7f017f60047f7f7f7f0060017f017e02880106057365616c30107365616c5f6765745f73746f726167650005057365616c30107365616c5f7365745f73746f726167650001057365616c30167365616c5f76616c75655f7472616e736665727265640000057365616c300a7365616c5f696e7075740000057365616c300b7365616c5f72657475726e000103656e76066d656d6f72790201021003121102020002000100070303060402040503010608017f01418080040b071102066465706c6f7900100463616c6c00120a8e1111c90101017f230041406a22012400200141206a200041186a290300370300200141186a200041106a290300370300200141106a200041086a2903003703002001420037032820012000290300370308200141086a1006200141808001360234200141a4800436023020014180800136023841a48004200141386a10002100200141306a20012802381007024002400240024020000e0401000002000b000b20012001290330370338200141386a100841ff017122004102470d01000b000b200141406b240020004100470b6001037e200029032021012000420137032020002001200029030022027c22013703002000200029030822032001200254ad7c22013703082000200029031022022001200354ad7c2201370310200020002903182001200254ad7c37031820000b4901037f230041106b22022400200028020421032000410036020420002802002104200041a48004360200200241086a200120042003100f20002002290308370200200241106a24000b4201027f230041106b22012400200141086a2000100b20012d0009210020012d00082102200141106a240041024101410220004101461b410020001b20024101711b0b8f0101017f230041406a22022400200241206a200141186a290300370300200241186a200141106a290300370300200241106a200141086a2903003703002002420037032820022001290300370308200241086a1006200241386a41808001360200200241a48004360234200241003602302002200241306a2000100a200228020020022802041001200241406b24000b9d0101037f230041106b22032400200141086a220428020021052004410036020020012802042104200141a48004360204200320023a000f2003410120042005100f024020032802044101460440200328020020032d000f3a000020014100360208200141a480043602042005450d0120012005417f6a3602082001200441016a3602042000410136020420002004360200200341106a24000f0b000b000b4801027f230041106b22022400200241003a000f024020012002410f6a4101101345044020022d000f21010c010b410121030b200020013a0001200020033a0000200241106a24000b890101027f230041106b22012400200142003c000c200142003e0208027f0340200241044604402001280208210241000c020b20012000100b20012d0000410171450440200141086a20026a20012d00013a00002001200241016a22023a000c0c010b0b200241ff01710440200141003a000c0b4100210241010b200141106a2400ad2002ad420886840b2201017f230041106b220124002001200036020c2001410c6a2802002d0000100e000b4601017f230041206b22012400200141186a41808001360200200141a4800436021420014100360210200141086a200141106a2000100a41002001280208200128020c1004000b2300410020014d0440200320014f044020002001360204200020023602000f0b000b000b15004100101141ff0171410274418080046a2802000bd80502057f017e230041306b220124000240027f024002402000044020014180800136020c200141a48004360208200141086a10142001200129030837031041012103200141106a100c2206a722054101710440410121020c030b200642ffffffffff1f832206422088a721002006421888a721042006421088a72102200541087641ff01712205411e470440200541c00147200041f3014772200241ff017141960147720d0241002102200441ff017141a501460d030c020b200041d60047200241ff017141dc0047720d014100210241002103200441ff017141a401470d010c020b20014180800136020c200141a48004360208200141086a101420012001290308370310410321020240200141106a100c2206a722044101710d00200642ffffffffff1f832206422088a721002006421888a721032006421088a7210202400240200441087641ff0171220441ea00470440200441d101472000412b4772200241ff017141830147720d02200341ff017141d100460d010c020b200041e20147200241ff0171413747720d0141022102200341ff01714112470d010c020b41032102200141106a100841ff017122004102460d01200041004721020c010b410321020b4106200241034622030d021a4106200220031b22024102460440200141286a4200370300200141206a4200370300200141186a4200370300200142003703104100200141106a100941080c030b200141286a4200370300200141206a4200370300200141186a4200370300200142003703102002410171200141106a100941080c020b41012102410121030b410620020d001a2003450d01200141286a4200370300200141206a4200370300200141186a420037030020014200370310200141106a1005410173200141106a100941080b200141306a24000f0b200141286a4200370300200141206a4200370300200141186a4200370300200142003703102001200141106a10053a0008200141086a100d000ba90102027f027e230041206b22002400200041808001360204200041a4800436020020004180800136021041a48004200041106a100220002000280210100720002000290300370308200041186a2201420037030020004200370310027f4101200041086a200041106a411010130d001a200129030021022000290310210341000b200220038450457245044041011011200041206a240041ff0171410274418080046a2802000f0b000b45000240200028020420024f047f2001200028020020021015200028020422012002490d012000200120026b3602042000200028020020026a36020041000541010b0f0b000b3301017f230041106b220124002001200028020436020c20002802002001410c6a10032000200128020c1007200141106a24000b2c01017f037f2002200346047f200005200020036a200120036a2d00003a0000200341016a21030c010b0b1a0b0b250100418080040b1d0100000002000000030000000400000005000000060000000700000008"
},
"contract": {
"name": "flipper",
"version": "3.0.0-rc2",
"authors": [
"Parity Technologies <admin@parity.io>"
]
},
"spec": {
"constructors": [
{
"args": [
{
"name": "init_value",
"type": {
"displayName": [
"bool"
],
"type": 1
}
}
],
"docs": [
" Creates a new flipper smart contract initialized with the given value."
],
"name": [
"new"
],
"selector": "0xd183512b"
},
{
"args": [],
"docs": [
" Creates a new flipper smart contract initialized to `false`."
],
"name": [
"default"
],
"selector": "0x6a3712e2"
}
],
"docs": [],
"events": [],
"messages": [
{
"args": [],
"docs": [
" Flips the current value of the Flipper's bool."
],
"mutates": true,
"name": [
"flip"
],
"payable": false,
"returnType": null,
"selector": "0xc096a5f3"
},
{
"args": [],
"docs": [
" Returns the current value of the Flipper's bool."
],
"mutates": false,
"name": [
"get"
],
"payable": false,
"returnType": {
"displayName": [
"bool"
],
"type": 1
},
"selector": "0x1e5ca456"
}
]
},
"storage": {
"struct": {
"fields": [
{
"layout": {
"cell": {
"key": "0x0000000000000000000000000000000000000000000000000000000000000000",
"ty": 1
}
},
"name": "value"
}
]
}
},
"types": [
{
"def": {
"primitive": "bool"
}
}
]
}
@@ -17,7 +17,7 @@ const _Header = Struct.with(runtimeTypes.types.Header as any) as Constructor<Hea
* @description
* A [[Block]] header with an additional `author` field that indicates the block author
*/
export default class HeaderExtended extends _Header {
export class HeaderExtended extends _Header {
readonly #author?: AccountId;
constructor (registry: Registry, header?: Header, sessionValidators?: AccountId[]) {
@@ -17,7 +17,7 @@ const _SignedBlock = Struct.with(runtimeTypes.types.SignedBlock as any) as Const
* @description
* A [[Block]] header with an additional `author` field that indicates the block author
*/
export default class BlockExtended extends _SignedBlock {
export class SignedBlockExtended extends _SignedBlock {
readonly #author?: AccountId;
constructor (registry: Registry, block?: SignedBlock, sessionValidators?: AccountId[]) {
+2 -2
View File
@@ -1,5 +1,5 @@
// Copyright 2017-2020 @polkadot/api-derive authors & contributors
// SPDX-License-Identifier: Apache-2.0
export { default as HeaderExtended } from './HeaderExtended';
export { default as SignedBlockExtended } from './SignedBlockExtended';
export { HeaderExtended } from './HeaderExtended';
export { SignedBlockExtended } from './SignedBlockExtended';
+3 -3
View File
@@ -24,10 +24,10 @@ import { unwrapStorageType } from '@polkadot/types/primitive/StorageKey';
import { assert, compactStripLength, logger, u8aToHex } from '@polkadot/util';
import { createSubmittable } from '../submittable';
import augmentObject from '../util/augmentObject';
import { augmentObject } from '../util/augmentObject';
import { decorateSections, DeriveAllSections } from '../util/decorate';
import { extractStorageArgs } from '../util/validate';
import Events from './Events';
import { Events } from './Events';
interface MetaDecoration {
callIndex?: Uint8Array;
@@ -44,7 +44,7 @@ const l = logger('api/init');
let instanceCounter = 0;
export default abstract class Decorate<ApiType extends ApiTypes> extends Events {
export abstract class Decorate<ApiType extends ApiTypes> extends Events {
readonly #instanceId: string;
#registry: Registry;
+1 -1
View File
@@ -5,7 +5,7 @@ import { ApiInterfaceEvents } from '../types';
import EventEmitter from 'eventemitter3';
export default class Events {
export class Events {
#eventemitter = new EventEmitter();
protected emit (type: ApiInterfaceEvents, ...args: any[]): boolean {
+2 -2
View File
@@ -9,7 +9,7 @@ import { Metadata } from '@polkadot/metadata';
import { Text } from '@polkadot/types';
import { assertReturn } from '@polkadot/util';
import Init from './Init';
import { Init } from './Init';
interface PkgJson {
name: string;
@@ -30,7 +30,7 @@ function assertResult<T> (value: T | undefined): T {
return assertReturn(value, 'Api needs to be initialized before using, listen on \'ready\'');
}
export default abstract class Getters<ApiType extends ApiTypes> extends Init<ApiType> {
export abstract class Getters<ApiType extends ApiTypes> extends Init<ApiType> {
/**
* @description Contains the parameter types (constants) of all modules.
*
+2 -2
View File
@@ -17,14 +17,14 @@ import { getSpecAlias, getSpecTypes, getUpgradeVersion } from '@polkadot/types-k
import { BN_ZERO, assert, logger, u8aEq, u8aToU8a } from '@polkadot/util';
import { cryptoWaitReady } from '@polkadot/util-crypto';
import Decorate from './Decorate';
import { Decorate } from './Decorate';
const KEEPALIVE_INTERVAL = 15000;
const DEFAULT_BLOCKNUMBER = { unwrap: () => BN_ZERO };
const l = logger('api/init');
export default abstract class Init<ApiType extends ApiTypes> extends Decorate<ApiType> {
export abstract class Init<ApiType extends ApiTypes> extends Decorate<ApiType> {
#healthTimer: NodeJS.Timeout | null = null;
#registries: VersionedRegistry[] = [];
+2 -2
View File
@@ -6,7 +6,7 @@ import { ApiOptions, ApiTypes, DecorateMethod, Signer } from '../types';
import { assert, isString, u8aToHex, u8aToU8a } from '@polkadot/util';
import Getters from './Getters';
import { Getters } from './Getters';
interface KeyringSigner {
sign (message: Uint8Array): Uint8Array;
@@ -16,7 +16,7 @@ interface SignerRawOptions {
signer?: Signer;
}
export default abstract class ApiBase<ApiType extends ApiTypes> extends Getters<ApiType> {
export abstract class ApiBase<ApiType extends ApiTypes> extends Getters<ApiType> {
/**
* @description Create an instance of the class
*
+1 -1
View File
@@ -8,7 +8,7 @@ import { EMPTY, Observable, Subscription } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';
import { isFunction, assert } from '@polkadot/util';
import ApiBase from '../base';
import { ApiBase } from '../base';
import { Combinator, CombinatorCallback, CombinatorFunction } from './Combinator';
interface Tracker<T> {
+1 -1
View File
@@ -6,7 +6,7 @@ import { ApiOptions, DecorateFn } from '../types';
import { from, Observable } from 'rxjs';
import ApiBase from '../base';
import { ApiBase } from '../base';
export function decorateMethod <Method extends DecorateFn<Codec>> (method: Method): Method {
return method;
+1 -1
View File
@@ -13,7 +13,7 @@ import { first, map, mapTo, mergeMap, switchMap, tap } from 'rxjs/operators';
import { assert, isBn, isFunction, isNumber, isString, isU8a } from '@polkadot/util';
import { filterEvents, isKeyringPair } from '../util';
import ApiBase from '../base';
import { ApiBase } from '../base';
import { SubmittableResult } from './Result';
interface SubmittableOptions<ApiType extends ApiTypes> {
@@ -5,7 +5,7 @@ import { Call, Extrinsic } from '@polkadot/types/interfaces';
import { ApiInterfaceRx, ApiTypes } from '../types';
import { SubmittableExtrinsic } from './types';
import ApiBase from '../base';
import { ApiBase } from '../base';
import { createClass } from './createClass';
type Creator<ApiType extends ApiTypes> = (extrinsic: Call | Uint8Array | string) => SubmittableExtrinsic<ApiType>;
+2 -2
View File
@@ -13,14 +13,14 @@ import { Hash, RuntimeVersion } from '@polkadot/types/interfaces';
import { DefinitionRpc, DefinitionRpcSub, Signer, SignatureOptions, Registry, RegisteredTypes } from '@polkadot/types/types';
import { DeriveAllSections } from '../util/decorate';
import ApiBase from '../base';
import { ApiBase } from '../base';
import { QueryableConsts } from './consts';
import { DecoratedRpc } from './rpc';
import { QueryableStorage, QueryableStorageMulti } from './storage';
import { SubmittableExtrinsics } from './submittable';
export { Signer, SignerResult } from '@polkadot/types/types';
export { default as ApiBase } from '../base';
export { ApiBase } from '../base';
export * from '../submittable/types';
export * from './base';
export * from './consts';
+1 -1
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2020 @polkadot/api authors & contributors
// SPDX-License-Identifier: Apache-2.0
import augmentObject from './augmentObject';
import { augmentObject } from './augmentObject';
describe('augmentObject', (): void => {
let spy: any;
+1 -1
View File
@@ -73,7 +73,7 @@ function extractMethods (src: Record<string, Record<string, any>>, dst: Record<s
* already available, but rather just adds new missing ites into the result object.
* @internal
*/
export default function augmentObject (prefix: string | null, src: Record<string, Record<string, unknown>>, dst: Record<string, Record<string, unknown>>, fromEmpty = false): Record<string, Record<string, any>> {
export function augmentObject (prefix: string | null, src: Record<string, Record<string, unknown>>, dst: Record<string, Record<string, unknown>>, fromEmpty = false): Record<string, Record<string, any>> {
if (fromEmpty) {
Object.keys(dst).forEach((key): void => {
delete dst[key];
@@ -7,7 +7,7 @@ import { AnyJson, CallFunction, Registry, RegistryMetadataCall } from '@polkadot
import { assert, stringCamelCase } from '@polkadot/util';
/** @internal */
export default function createUnchecked (registry: Registry, section: string, callIndex: Uint8Array, callMetadata: RegistryMetadataCall): CallFunction {
export function createUnchecked (registry: Registry, section: string, callIndex: Uint8Array, callMetadata: RegistryMetadataCall): CallFunction {
const expectedArgs = callMetadata.args;
const funcName = stringCamelCase(callMetadata.name);
@@ -5,7 +5,7 @@ import { Calls, ModulesWithCalls, Registry, RegistryMetadata, RegistryMetadataMo
import { stringCamelCase } from '@polkadot/util';
import createUnchecked from './createUnchecked';
import { createUnchecked } from './createUnchecked';
import extrinsics from '.';
/** @internal */
@@ -5,7 +5,7 @@ import { Text } from '@polkadot/types';
import { TypeRegistry } from '@polkadot/types/create';
import { u8aToHex } from '@polkadot/util';
import createFunction from './createFunction';
import { createFunction } from './createFunction';
describe('createFunction', (): void => {
const registry = new TypeRegistry();
@@ -174,7 +174,7 @@ function extendPrefixedMap (registry: Registry, itemFn: CreateItemFn, storageFn:
}
/** @internal */
export default function createFunction (registry: Registry, itemFn: CreateItemFn, options: CreateItemOptions): StorageEntry {
export function createFunction (registry: Registry, itemFn: CreateItemFn, options: CreateItemOptions): StorageEntry {
const { meta: { type } } = itemFn;
const [hasher, key2Hasher] = getHashers(itemFn);
@@ -7,8 +7,8 @@ import { ModuleStorage, Storage } from '../types';
import { stringCamelCase, stringLowerFirst } from '@polkadot/util';
import { Metadata } from '../../Metadata';
import createFunction from './createFunction';
import getStorage from './storage';
import { createFunction } from './createFunction';
import { getStorage } from './storage';
/** @internal */
export function storageFromMeta (registry: Registry, metadata: Metadata): Storage {
@@ -1,6 +1,6 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import getStorage from './storage';
import { getStorage } from './storage';
export default getStorage;
@@ -3,7 +3,7 @@
import { TypeRegistry } from '@polkadot/types/create';
import getStorage from './storage';
import { getStorage } from './storage';
describe('storage', (): void => {
const registry = new TypeRegistry();
@@ -8,7 +8,7 @@ import { Storage } from '../types';
import * as substrate from './substrate';
/** @internal */
export default function getStorage (registry: Registry, metaVersion: number): Storage {
export function getStorage (registry: Registry, metaVersion: number): Storage {
return {
substrate: Object
.entries(substrate)
@@ -5,7 +5,7 @@ import { StorageEntryMetadataLatest } from '@polkadot/types/interfaces';
import { StorageEntry } from '@polkadot/types/primitive/types';
import { Registry } from '@polkadot/types/types';
import createFunction from './createFunction';
import { createFunction } from './createFunction';
interface SubstrateMetadata {
documentation: string;
@@ -2,48 +2,48 @@
// SPDX-License-Identifier: Apache-2.0
import { JsonRpcResponse } from '../types';
import Coder from './';
import { RpcCoder } from '.';
describe('decodeResponse', (): void => {
let coder: Coder;
let coder: RpcCoder;
beforeEach((): void => {
coder = new Coder();
coder = new RpcCoder();
});
it('expects a non-empty input object', (): void => {
expect(
(): any => coder.decodeResponse(undefined as unknown as JsonRpcResponse)
() => coder.decodeResponse(undefined as unknown as JsonRpcResponse)
).toThrow(/Empty response/);
});
it('expects a valid jsonrpc field', (): void => {
expect(
(): any => coder.decodeResponse({} as JsonRpcResponse)
() => coder.decodeResponse({} as JsonRpcResponse)
).toThrow(/Invalid jsonrpc/);
});
it('expects a valid id field', (): void => {
expect(
(): any => coder.decodeResponse({ jsonrpc: '2.0' } as JsonRpcResponse)
() => coder.decodeResponse({ jsonrpc: '2.0' } as JsonRpcResponse)
).toThrow(/Invalid id/);
});
it('expects a valid result field', (): void => {
expect(
(): any => coder.decodeResponse({ id: 1, jsonrpc: '2.0' } as JsonRpcResponse)
() => coder.decodeResponse({ id: 1, jsonrpc: '2.0' } as JsonRpcResponse)
).toThrow(/No result/);
});
it('throws any error found', (): void => {
expect(
(): any => coder.decodeResponse({ error: { code: 123, message: 'test error' }, id: 1, jsonrpc: '2.0' } as JsonRpcResponse)
() => coder.decodeResponse({ error: { code: 123, message: 'test error' }, id: 1, jsonrpc: '2.0' } as JsonRpcResponse)
).toThrow(/123: test error/);
});
it('throws any error found, with data', (): void => {
expect(
(): any => coder.decodeResponse({ error: { code: 123, data: 'Error("Some random error description")', message: 'test error' }, id: 1, jsonrpc: '2.0' } as JsonRpcResponse)
() => coder.decodeResponse({ error: { code: 123, data: 'Error("Some random error description")', message: 'test error' }, id: 1, jsonrpc: '2.0' } as JsonRpcResponse)
).toThrow(/123: test error: Some random error description/);
});
@@ -1,13 +1,13 @@
// Copyright 2017-2020 @polkadot/rpc-provider authors & contributors
// SPDX-License-Identifier: Apache-2.0
import Coder from './';
import { RpcCoder } from '.';
describe('encodeJson', (): void => {
let coder: Coder;
let coder: RpcCoder;
beforeEach((): void => {
coder = new Coder();
coder = new RpcCoder();
});
it('encodes a valid JsonRPC JSON string', (): void => {
@@ -1,13 +1,13 @@
// Copyright 2017-2020 @polkadot/rpc-provider authors & contributors
// SPDX-License-Identifier: Apache-2.0
import Coder from './';
import { RpcCoder } from '.';
describe('encodeObject', (): void => {
let coder: Coder;
let coder: RpcCoder;
beforeEach((): void => {
coder = new Coder();
coder = new RpcCoder();
});
it('starts with id === 0 (nothing sent)', (): void => {
+1 -1
View File
@@ -22,7 +22,7 @@ function formatErrorData (data?: string | number): string {
}
/** @internal */
export default class RpcCoder {
export class RpcCoder {
#id = 0;
public decodeResponse (response: JsonRpcResponse): unknown {
+3 -3
View File
@@ -6,7 +6,7 @@ import { ProviderInterface, ProviderInterfaceCallback, ProviderInterfaceEmitted,
import { assert, logger } from '@polkadot/util';
import fetch from '@polkadot/x-fetch';
import Coder from '../coder';
import { RpcCoder } from '../coder';
import defaults from '../defaults';
const ERROR_SUBSCRIBE = 'HTTP Provider does not have subscriptions, use WebSockets instead';
@@ -34,7 +34,7 @@ const l = logger('api-http');
* @see [[WsProvider]]
*/
export class HttpProvider implements ProviderInterface {
readonly #coder: Coder;
readonly #coder: RpcCoder;
readonly #endpoint: string;
@@ -46,7 +46,7 @@ export class HttpProvider implements ProviderInterface {
constructor (endpoint: string = defaults.HTTP_URL, headers: Record<string, string> = {}) {
assert(/^(https|http):\/\//.test(endpoint), `Endpoint should start with 'http://', received '${endpoint}'`);
this.#coder = new Coder();
this.#coder = new RpcCoder();
this.#endpoint = endpoint;
this.#headers = headers;
}
+2 -2
View File
@@ -3,10 +3,10 @@
import { Logger } from '@polkadot/util/types';
import Coder from '../coder';
import { RpcCoder } from '../coder';
export interface HttpState {
coder: Coder;
coder: RpcCoder;
endpoint: string;
l: Logger;
}
+3 -3
View File
@@ -9,7 +9,7 @@ import EventEmitter from 'eventemitter3';
import { assert, isChildClass, isNull, isUndefined, logger } from '@polkadot/util';
import WS from '@polkadot/x-ws';
import Coder from '../coder';
import { RpcCoder } from '../coder';
import defaults from '../defaults';
import { getWSErrorString } from './errors';
@@ -61,7 +61,7 @@ const l = logger('api-ws');
* @see [[HttpProvider]]
*/
export class WsProvider implements ProviderInterface {
readonly #coder: Coder;
readonly #coder: RpcCoder;
readonly #endpoints: string[];
@@ -102,7 +102,7 @@ export class WsProvider implements ProviderInterface {
this.#eventemitter = new EventEmitter();
this.#autoConnectMs = autoConnectMs || 0;
this.#coder = new Coder();
this.#coder = new RpcCoder();
this.#endpointIndex = -1;
this.#endpoints = endpoints;
this.#headers = headers;
@@ -128,7 +128,8 @@ export default {
ContractProjectSource: {
hash: '[u8; 32]',
language: 'Text',
compiler: 'Text'
compiler: 'Text',
wasm: 'Raw'
}
}
} as Definitions;
@@ -1,7 +1,7 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
import { BTreeMap, Enum, Option, Struct, U8aFixed, Vec } from '@polkadot/types/codec';
import { BTreeMap, Enum, Option, Raw, Struct, U8aFixed, Vec } from '@polkadot/types/codec';
import { Bytes, Text, bool, u32, u64 } from '@polkadot/types/primitive';
import { SiLookupTypeId, SiPath, SiType } from '@polkadot/types/interfaces/scaleInfo';
@@ -140,6 +140,7 @@ export interface ContractProjectSource extends Struct {
readonly hash: U8aFixed;
readonly language: Text;
readonly compiler: Text;
readonly wasm: Raw;
}
/** @name ContractSelector */