Create api-contract (baseline for useful interactions) (#1007)

* Create api-contract (baseline for useful interactions)

* Add CHANGELOG entry (breaking)
This commit is contained in:
Jaco Greeff
2019-06-14 13:48:53 +02:00
committed by GitHub
parent 6e7a126f4d
commit 4cba24c134
25 changed files with 614 additions and 312 deletions
+101
View File
@@ -0,0 +1,101 @@
// Copyright 2017-2019 @polkadot/api-contract 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 { ContractABIFn$Arg } from './types';
import typesAbi from '../test/abi/types.json';
import erc20Abi from '../test/contracts/erc20.json';
import { Abi } from '.';
describe('Abi', () => {
describe('types encoding', () => {
let abi: Abi;
beforeAll(() => {
abi = new Abi(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)>'
);
});
});
describe('erc20', () => {
let abi: Abi;
beforeEach(() => {
abi = new Abi(erc20Abi);
});
it('has the attached methods', () => {
expect(Object.keys(abi.messages)).toEqual(
['totalSupply', 'balanceOf', 'allowance', 'transfer', 'approve', 'transferFrom']
);
});
});
});