diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-02-24 04:20:59 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-02-28 04:05:23 +0800 |
commit | fe8f2d8d898e9909636366c9ceee37a5e9481573 (patch) | |
tree | 16be81a7d12e527c65084b972b39a4fe27c3267f /packages/abi-gen/src | |
parent | 2d561bc8a05e8d1fca91cde93bae2080d87be926 (diff) | |
download | dexon-sol-tools-fe8f2d8d898e9909636366c9ceee37a5e9481573.tar.gz dexon-sol-tools-fe8f2d8d898e9909636366c9ceee37a5e9481573.tar.zst dexon-sol-tools-fe8f2d8d898e9909636366c9ceee37a5e9481573.zip |
Add support for ABIv2 to abi-gen
Diffstat (limited to 'packages/abi-gen/src')
-rw-r--r-- | packages/abi-gen/src/utils.ts | 34 |
1 files changed, 29 insertions, 5 deletions
diff --git a/packages/abi-gen/src/utils.ts b/packages/abi-gen/src/utils.ts index dc2c5390e..349604aec 100644 --- a/packages/abi-gen/src/utils.ts +++ b/packages/abi-gen/src/utils.ts @@ -6,14 +6,20 @@ import * as Web3 from 'web3'; import { AbiType, ContractsBackend, ParamKind } from './types'; export const utils = { - solTypeToTsType(paramKind: ParamKind, backend: ContractsBackend, solType: string): string { + solTypeToTsType( + paramKind: ParamKind, + backend: ContractsBackend, + solType: string, + components?: Web3.DataItem[], + ): string { const trailingArrayRegex = /\[\d*\]$/; if (solType.match(trailingArrayRegex)) { const arrayItemSolType = solType.replace(trailingArrayRegex, ''); - const arrayItemTsType = utils.solTypeToTsType(paramKind, backend, arrayItemSolType); - const arrayTsType = utils.isUnionType(arrayItemTsType) - ? `Array<${arrayItemTsType}>` - : `${arrayItemTsType}[]`; + const arrayItemTsType = utils.solTypeToTsType(paramKind, backend, arrayItemSolType, components); + const arrayTsType = + utils.isUnionType(arrayItemTsType) || utils.isObjectType(arrayItemTsType) + ? `Array<${arrayItemTsType}>` + : `${arrayItemTsType}[]`; return arrayTsType; } else { const solTypeRegexToTsType = [ @@ -45,12 +51,30 @@ export const utils = { return tsType; } } + const TUPLE_TYPE_REGEX = '^tuple$'; + if (solType.match(TUPLE_TYPE_REGEX)) { + const componentsType = _.map(components, component => { + const componentValueType = utils.solTypeToTsType( + paramKind, + backend, + component.type, + component.components, + ); + const componentType = `${component.name}: ${componentValueType}`; + return componentType; + }); + const tsType = `{${componentsType}}`; + return tsType; + } throw new Error(`Unknown Solidity type found: ${solType}`); } }, isUnionType(tsType: string): boolean { return tsType === 'number|BigNumber'; }, + isObjectType(tsType: string): boolean { + return /^{.*}$/.test(tsType); + }, log(...args: any[]): void { console.log(...args); // tslint:disable-line:no-console }, |