Allow for contract Vec, Option and Tuple arg/return (#997)
* Allow for contract Vec & Option arg/return * Support nesting, fixed vecs and tuples * Support Result * Update CHANGELOG * Add additional types added to CHANGELOG (& re-order) * Add metadata entry (ready to go)
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
// 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 TypesAbi from '../test/abi/types.json';
|
||||
|
||||
import ContractAbi, { ContractABIFn$Arg } from './ContractAbi';
|
||||
|
||||
describe('ContractAbi', () => {
|
||||
let abi: ContractAbi;
|
||||
|
||||
beforeAll(() => {
|
||||
abi = new ContractAbi(TypesAbi);
|
||||
});
|
||||
|
||||
function check (method: string, args: Array<ContractABIFn$Arg>, type: string | null): void {
|
||||
const fn = abi.messages[method];
|
||||
|
||||
expect(fn.args).toEqual(args);
|
||||
expect(fn.type).toEqual(type);
|
||||
}
|
||||
|
||||
it('has the primitive method with args & return', () => {
|
||||
check(
|
||||
'primitive',
|
||||
[
|
||||
{ name: 'bool', type: 'bool' },
|
||||
{ name: 'u32', type: 'u32' }
|
||||
],
|
||||
'bool'
|
||||
);
|
||||
});
|
||||
|
||||
it('has the vector method with args & return', () => {
|
||||
check(
|
||||
'vector',
|
||||
[
|
||||
{ name: 'vecU8', type: 'Vec<u8>' },
|
||||
{ name: 'vecU32', type: 'Vec<u32>' }
|
||||
],
|
||||
'Vec<bool>'
|
||||
);
|
||||
});
|
||||
|
||||
it('has the vector_fixed method with args & return', () => {
|
||||
check(
|
||||
'vectorFixed',
|
||||
[
|
||||
{ name: 'vecU8Length32', type: '[u8;32]' }
|
||||
],
|
||||
'[u32;8]'
|
||||
);
|
||||
});
|
||||
|
||||
it('has the option method with args & return', () => {
|
||||
check(
|
||||
'option',
|
||||
[
|
||||
{ name: 'optionU32', type: 'Option<u32>' }
|
||||
],
|
||||
'Option<bool>'
|
||||
);
|
||||
});
|
||||
|
||||
it('has the result method with return (empty)', () => {
|
||||
check(
|
||||
'result',
|
||||
[],
|
||||
'()'
|
||||
);
|
||||
});
|
||||
|
||||
it('allows for nested args', () => {
|
||||
check(
|
||||
'nested',
|
||||
[
|
||||
{ name: 'optionVec', type: 'Option<Vec<u32>>' }
|
||||
],
|
||||
'Vec<(u32,u64)>'
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -4,25 +4,44 @@
|
||||
|
||||
import { CodecArg, Constructor } from './types';
|
||||
|
||||
import { assert, isNumber, isNull, isString, isUndefined, stringCamelCase } from '@polkadot/util';
|
||||
import { assert, isNumber, isNull, isString, isUndefined, stringCamelCase, isObject } from '@polkadot/util';
|
||||
|
||||
import Compact from './codec/Compact';
|
||||
import { createClass } from './codec/createType';
|
||||
|
||||
export type ContractABIArgs = Array<{
|
||||
export type ContractABITypes$Struct = {
|
||||
'Option<T>'?: {
|
||||
T: ContractABITypes
|
||||
},
|
||||
'Result<T,E>'?: {
|
||||
T: ContractABITypes,
|
||||
E: ContractABITypes
|
||||
},
|
||||
'Vec<T>'?: {
|
||||
T: ContractABITypes
|
||||
},
|
||||
'[T;n]'?: {
|
||||
T: ContractABITypes,
|
||||
n: number
|
||||
}
|
||||
};
|
||||
|
||||
export type ContractABITypes = string | ContractABITypes$Struct | Array<string | ContractABITypes$Struct>;
|
||||
|
||||
export type ContractABIArg = {
|
||||
name: string,
|
||||
type: string
|
||||
}>;
|
||||
type: ContractABITypes
|
||||
};
|
||||
|
||||
export type ContractABIMethodBase = {
|
||||
args: ContractABIArgs
|
||||
args: Array<ContractABIArg>
|
||||
};
|
||||
|
||||
export type ContractABIMethod = ContractABIMethodBase & {
|
||||
mutates?: boolean,
|
||||
name: string,
|
||||
selector: number,
|
||||
return_type: string | null
|
||||
return_type: ContractABITypes | null
|
||||
};
|
||||
|
||||
export type ContractABI = {
|
||||
@@ -31,9 +50,14 @@ export type ContractABI = {
|
||||
name: string
|
||||
};
|
||||
|
||||
export interface ContractABIFn$Arg {
|
||||
name: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
export interface ContractABIFn {
|
||||
(...args: Array<CodecArg>): Uint8Array;
|
||||
args: ContractABIArgs;
|
||||
args: Array<ContractABIFn$Arg>;
|
||||
isConstant: boolean;
|
||||
type: string | null;
|
||||
}
|
||||
@@ -46,14 +70,15 @@ export interface Contract {
|
||||
};
|
||||
}
|
||||
|
||||
export function validateArgs (name: string, args: ContractABIArgs): void {
|
||||
export function validateArgs (name: string, args: Array<ContractABIArg>): 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`);
|
||||
assert(isString(arg.name), `${name} args should have valid name `);
|
||||
assert(isString(arg.type) || isObject(arg.type), `${name} args should have valid type`);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -70,7 +95,9 @@ export function validateMethods ({ messages }: ContractABI): void {
|
||||
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.${method.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}`);
|
||||
assert(isString(method.name), `Expected name for messages.${method.name}`);
|
||||
assert(isNumber(method.selector), `Expected selector for messages.${method.name}`);
|
||||
assert(isNull(method.return_type) || isString(method.return_type) || isObject(method.return_type), `Expected return_type for messages.${method.name}`);
|
||||
|
||||
validateArgs(`messages.${method.name}`, method.args);
|
||||
});
|
||||
@@ -104,7 +131,25 @@ export default class ContractAbi implements Contract {
|
||||
});
|
||||
}
|
||||
|
||||
private _createClazz (args: ContractABIArgs, baseDef: { [index: string]: string }): Constructor {
|
||||
private _convertType (type: ContractABITypes): string {
|
||||
if (isString(type)) {
|
||||
return type;
|
||||
} else if (Array.isArray(type)) {
|
||||
return `(${type.map((type) => this._convertType(type)).join(',')})`;
|
||||
} else if (type['Option<T>']) {
|
||||
return `Option<${this._convertType(type['Option<T>'].T)}>`;
|
||||
} else if (type['Result<T,E>']) {
|
||||
return `()`; // Result is not supported, but only applicable for returns
|
||||
} else if (type['Vec<T>']) {
|
||||
return `Vec<${this._convertType(type['Vec<T>'].T)}>`;
|
||||
} else if (type['[T;n]']) {
|
||||
return `[${this._convertType(type['[T;n]'].T)};${type['[T;n]'].n}]`;
|
||||
}
|
||||
|
||||
throw new Error(`Unknown type specified ${JSON.stringify(type)}`);
|
||||
}
|
||||
|
||||
private _createClazz (args: Array<ContractABIFn$Arg>, baseDef: { [index: string]: string }): Constructor {
|
||||
return createClass(
|
||||
JSON.stringify(
|
||||
args.reduce((base: { [index: string]: string }, { name, type }) => {
|
||||
@@ -117,9 +162,9 @@ export default class ContractAbi implements Contract {
|
||||
}
|
||||
|
||||
private _createEncoded (name: string, method: Partial<ContractABIMethod> & ContractABIMethodBase): ContractABIFn {
|
||||
const args = method.args.map(({ name, type }) => ({
|
||||
const args: Array<ContractABIFn$Arg> = method.args.map(({ name, type }) => ({
|
||||
name: stringCamelCase(name),
|
||||
type
|
||||
type: this._convertType(type)
|
||||
}));
|
||||
const Clazz = this._createClazz(args, isUndefined(method.selector) ? {} : { __selector: 'u32' });
|
||||
const baseStruct: { [index: string]: any } = { __selector: method.selector };
|
||||
@@ -141,7 +186,7 @@ export default class ContractAbi implements Contract {
|
||||
|
||||
fn.args = args;
|
||||
fn.isConstant = !(method.mutates || false);
|
||||
fn.type = method.return_type || null;
|
||||
fn.type = method.return_type ? this._convertType(method.return_type) : null;
|
||||
|
||||
return fn;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user