Contracts attachment (#818)
* WIP... just getting ti to work * Export ContractAbi from types * e2e testing * Add selector encoding (calls) * Linting * Exapnd e2e tests * TODO comments * Add ABI types & isConstant flag * Updated with actual steps (progress) * Linting * Cleanup event code * Bump deps * Bump deps (keyring mnemonic fix)
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
// Copyright 2017-2019 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { CodecArg, Constructor } from './types';
|
||||
|
||||
import { assert, isNumber, isNull, isString, isUndefined, stringCamelCase } from '@polkadot/util';
|
||||
|
||||
import Compact from './codec/Compact';
|
||||
import { createClass } from './codec/createType';
|
||||
|
||||
export type ContractABIArgs = Array<{
|
||||
name: string,
|
||||
type: string
|
||||
}>;
|
||||
|
||||
export type ContractABIMethodBase = {
|
||||
args: ContractABIArgs
|
||||
};
|
||||
|
||||
export type ContractABIMethod = ContractABIMethodBase & {
|
||||
mutates?: boolean,
|
||||
name: string,
|
||||
selector: number,
|
||||
return_type: string | null
|
||||
};
|
||||
|
||||
export type ContractABI = {
|
||||
deploy: ContractABIMethodBase,
|
||||
messages: Array<ContractABIMethod>,
|
||||
name: string
|
||||
};
|
||||
|
||||
export interface ContractABIFn {
|
||||
(...args: Array<CodecArg>): Uint8Array;
|
||||
args: ContractABIArgs;
|
||||
isConstant: boolean;
|
||||
type: string | null;
|
||||
}
|
||||
|
||||
export interface Contract {
|
||||
abi: ContractABI;
|
||||
deploy: ContractABIFn;
|
||||
messages: {
|
||||
[index: string]: ContractABIFn
|
||||
};
|
||||
}
|
||||
|
||||
export function validateArgs (name: string, args: ContractABIArgs): void {
|
||||
assert(Array.isArray(args), `Expected 'args' to exist on ${name}`);
|
||||
|
||||
args.forEach((arg) => {
|
||||
const unknownKeys = Object.keys(arg).filter((key) => !['name', 'type'].includes(key));
|
||||
|
||||
assert(unknownKeys.length === 0, `Unknown keys ${unknownKeys.join(', ')} found in ABI args for ${name}`);
|
||||
assert(isString(arg.name) && isString(arg.type), `${name} args should have valid name & type values`);
|
||||
});
|
||||
}
|
||||
|
||||
export function validateDeploy ({ deploy }: ContractABI): void {
|
||||
const unknownKeys = Object.keys(deploy).filter((key) => !['args'].includes(key));
|
||||
|
||||
assert(unknownKeys.length === 0, `Unknown keys ${unknownKeys.join(', ')} found in ABI deploy`);
|
||||
|
||||
validateArgs('deploy', deploy.args);
|
||||
}
|
||||
|
||||
export function validateMethods ({ messages }: ContractABI): void {
|
||||
messages.forEach((method) => {
|
||||
const unknownKeys = Object.keys(method).filter((key) => !['args', 'mutates', 'name', 'selector', 'return_type'].includes(key));
|
||||
|
||||
assert(unknownKeys.length === 0, `Unknown keys ${unknownKeys.join(', ')} found in ABI args for messages.${name}`);
|
||||
assert(isString(method.name) && isNumber(method.selector) && (isNull(method.return_type) || isString(method.return_type)), `Expected name, selector & return_type specified for messages.${method.name}`);
|
||||
|
||||
validateArgs(`messages.${method.name}`, method.args);
|
||||
});
|
||||
}
|
||||
|
||||
export function validateAbi (abi: ContractABI): void {
|
||||
const unknownKeys = Object.keys(abi).filter((key) => !['deploy', 'messages', 'name'].includes(key));
|
||||
|
||||
assert(unknownKeys.length === 0, `Unknown fields ${unknownKeys.join(', ')} found in ABI`);
|
||||
assert(abi.deploy && abi.messages && isString(abi.name), 'ABI should have deploy, messages & name sections');
|
||||
|
||||
validateDeploy(abi);
|
||||
validateMethods(abi);
|
||||
}
|
||||
|
||||
export default class ContractAbi implements Contract {
|
||||
abi: ContractABI;
|
||||
deploy: ContractABIFn;
|
||||
messages: { [index: string]: ContractABIFn } = {};
|
||||
|
||||
constructor (abi: ContractABI) {
|
||||
validateAbi(abi);
|
||||
|
||||
this.abi = abi;
|
||||
this.deploy = this._createEncoded('deploy', abi.deploy);
|
||||
|
||||
abi.messages.forEach((method) => {
|
||||
const name = stringCamelCase(method.name);
|
||||
|
||||
this.messages[name] = this._createEncoded(`messages.${name}`, method);
|
||||
});
|
||||
}
|
||||
|
||||
private _createClazz (args: ContractABIArgs, isDeploy: boolean): Constructor {
|
||||
return createClass(
|
||||
JSON.stringify(
|
||||
args.reduce((base: { [index: string]: string }, { name, type }) => {
|
||||
base[name] = type;
|
||||
|
||||
return base;
|
||||
}, isDeploy ? {} : { __selector: 'u32' })
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private _createEncoded (name: string, method: Partial<ContractABIMethod> & ContractABIMethodBase): ContractABIFn {
|
||||
const args = method.args.map(({ name, type }) => ({
|
||||
name: stringCamelCase(name),
|
||||
type
|
||||
}));
|
||||
const Clazz = this._createClazz(args, isUndefined(method.selector));
|
||||
const base: { [index: string]: any } = { __selector: method.selector };
|
||||
const encoder = (...params: Array<CodecArg>): Uint8Array => {
|
||||
assert(params.length === args.length, `Expected ${args.length} arguments to contract ${name}, found ${params.length}`);
|
||||
|
||||
return Compact.addLengthPrefix(
|
||||
new Clazz(
|
||||
args.reduce((mapped, { name }, index) => {
|
||||
mapped[name] = params[index];
|
||||
|
||||
return mapped;
|
||||
}, { ...base })
|
||||
).toU8a()
|
||||
);
|
||||
};
|
||||
|
||||
const fn = (encoder as ContractABIFn);
|
||||
|
||||
fn.args = args;
|
||||
fn.isConstant = !(method.mutates || false);
|
||||
fn.type = method.return_type || null;
|
||||
|
||||
return fn;
|
||||
}
|
||||
}
|
||||
@@ -143,9 +143,11 @@ export function getTypeDef (_type: Text | string, name?: string): TypeDef {
|
||||
// Returns the type Class for construction
|
||||
export function getTypeClass (value: TypeDef): Constructor {
|
||||
const Type = getRegistry().get(value.type);
|
||||
|
||||
if (Type) {
|
||||
return Type;
|
||||
}
|
||||
|
||||
switch (value.info) {
|
||||
case TypeDefInfo.Compact:
|
||||
assert(value.sub && !Array.isArray(value.sub), 'Expected subtype for Compact');
|
||||
@@ -201,7 +203,7 @@ export function getTypeClass (value: TypeDef): Constructor {
|
||||
throw new Error(`Unable to determine type from '${value.type}'`);
|
||||
}
|
||||
|
||||
export function createClass (type: Text | string, value?: any): Constructor {
|
||||
export function createClass (type: Text | string): Constructor {
|
||||
return getTypeClass(
|
||||
getTypeDef(type)
|
||||
);
|
||||
|
||||
@@ -9,5 +9,6 @@ import { assertSingletonPackage } from '@polkadot/util';
|
||||
|
||||
assertSingletonPackage('@polkadot/types');
|
||||
|
||||
export { default as ContractAbi } from './ContractAbi';
|
||||
export * from './codec';
|
||||
export * from './index.types';
|
||||
|
||||
@@ -50,6 +50,20 @@ export class Phase extends EnumType<ApplyExtrinsic | Finalization> {
|
||||
get asFinalization (): Finalization {
|
||||
return this.value as Finalization;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description true when this is a ApplyExtrinsic
|
||||
*/
|
||||
get isApplyExtrinsic (): boolean {
|
||||
return this.type === 'ApplyExtrinsic';
|
||||
}
|
||||
|
||||
/**
|
||||
* @description true when this is a ApplyExtrinsic
|
||||
*/
|
||||
get isFinalization (): boolean {
|
||||
return this.type === 'Finalization';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user