From 31c9c82f6c00c890c4676d7fc8744d17d810db8b Mon Sep 17 00:00:00 2001 From: Nadav Hollander Date: Sun, 24 Dec 2017 10:05:52 -0800 Subject: Fix typing generation for arrays in which types separated by |s --- packages/abi-gen/src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/abi-gen/src') diff --git a/packages/abi-gen/src/utils.ts b/packages/abi-gen/src/utils.ts index eaf5a30cc..b3f60a02a 100644 --- a/packages/abi-gen/src/utils.ts +++ b/packages/abi-gen/src/utils.ts @@ -10,7 +10,7 @@ export const utils = { if (solType.match(trailingArrayRegex)) { const arrayItemSolType = solType.replace(trailingArrayRegex, ''); const arrayItemTsType = utils.solTypeToTsType(paramKind, arrayItemSolType); - const arrayTsType = `${arrayItemTsType}[]`; + const arrayTsType = `(${arrayItemTsType})[]`; return arrayTsType; } else { const solTypeRegexToTsType = [ -- cgit v1.2.3 From 161a9d6ad20d7b127ef41a55dc12892e809127e5 Mon Sep 17 00:00:00 2001 From: Olaf Tomalka Date: Sat, 30 Dec 2017 16:13:32 +0100 Subject: Added Event generation to abi-gen --- packages/abi-gen/src/index.ts | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'packages/abi-gen/src') diff --git a/packages/abi-gen/src/index.ts b/packages/abi-gen/src/index.ts index 19d289e49..6bd8bfe94 100644 --- a/packages/abi-gen/src/index.ts +++ b/packages/abi-gen/src/index.ts @@ -15,6 +15,7 @@ import {ContextData, ParamKind} from './types'; import {utils} from './utils'; const ABI_TYPE_METHOD = 'function'; +const ABI_TYPE_EVENT = 'event'; const MAIN_TEMPLATE_NAME = 'contract.mustache'; const args = yargs @@ -74,6 +75,7 @@ for (const abiFileName of abiFileNames) { utils.log(`Please make sure your ABI file is either an array with ABI entries or an object with the abi key`); process.exit(1); } + const methodAbis = ABI.filter((abi: Web3.AbiDefinition) => abi.type === ABI_TYPE_METHOD) as Web3.MethodAbi[]; const methodsData = _.map(methodAbis, methodAbi => { _.map(methodAbi.inputs, input => { @@ -89,9 +91,13 @@ for (const abiFileName of abiFileNames) { }; return methodData; }); + + const eventAbis = ABI.filter((abi: Web3.AbiDefinition) => abi.type === ABI_TYPE_EVENT) as Web3.MethodAbi[]; + const contextData = { contractName: namedContent.name, methods: methodsData, + events: eventAbis, }; const renderedTsCode = template(contextData); writeOutputFile(namedContent.name, renderedTsCode); -- cgit v1.2.3 From e744e4cd989bd3ae1070c59f7baa8097f18b8b06 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 22 Dec 2017 15:05:32 +0100 Subject: Apply prettier config --- packages/abi-gen/src/index.ts | 15 +++++++-------- packages/abi-gen/src/utils.ts | 21 ++++++++++++--------- 2 files changed, 19 insertions(+), 17 deletions(-) (limited to 'packages/abi-gen/src') diff --git a/packages/abi-gen/src/index.ts b/packages/abi-gen/src/index.ts index 19d289e49..a74bdf8c4 100644 --- a/packages/abi-gen/src/index.ts +++ b/packages/abi-gen/src/index.ts @@ -2,7 +2,7 @@ import chalk from 'chalk'; import * as fs from 'fs'; -import {sync as globSync} from 'glob'; +import { sync as globSync } from 'glob'; import * as Handlebars from 'handlebars'; import * as _ from 'lodash'; import * as mkdirp from 'mkdirp'; @@ -11,8 +11,8 @@ import * as yargs from 'yargs'; import toSnakeCase = require('to-snake-case'); import * as Web3 from 'web3'; -import {ContextData, ParamKind} from './types'; -import {utils} from './utils'; +import { ContextData, ParamKind } from './types'; +import { utils } from './utils'; const ABI_TYPE_METHOD = 'function'; const MAIN_TEMPLATE_NAME = 'contract.mustache'; @@ -32,8 +32,7 @@ const args = yargs describe: 'Folder where to put the output files', type: 'string', demand: true, - }) - .argv; + }).argv; function writeOutputFile(name: string, renderedTsCode: string): void { const fileName = toSnakeCase(name); @@ -66,9 +65,9 @@ for (const abiFileName of abiFileNames) { const namedContent = utils.getNamedContent(abiFileName); utils.log(`Processing: ${chalk.bold(namedContent.name)}...`); const parsedContent = JSON.parse(namedContent.content); - const ABI = _.isArray(parsedContent) ? - parsedContent : // ABI file - parsedContent.abi; // Truffle contracts file + const ABI = _.isArray(parsedContent) + ? parsedContent // ABI file + : parsedContent.abi; // Truffle contracts file if (_.isUndefined(ABI)) { utils.log(`${chalk.red(`ABI not found in ${abiFileName}.`)}`); utils.log(`Please make sure your ABI file is either an array with ABI entries or an object with the abi key`); diff --git a/packages/abi-gen/src/utils.ts b/packages/abi-gen/src/utils.ts index eaf5a30cc..55e77f27d 100644 --- a/packages/abi-gen/src/utils.ts +++ b/packages/abi-gen/src/utils.ts @@ -2,7 +2,7 @@ import * as fs from 'fs'; import * as _ from 'lodash'; import * as path from 'path'; -import {ParamKind} from './types'; +import { ParamKind } from './types'; export const utils = { solTypeToTsType(paramKind: ParamKind, solType: string): string { @@ -14,19 +14,22 @@ export const utils = { return arrayTsType; } else { const solTypeRegexToTsType = [ - {regex: '^string$', tsType: 'string'}, - {regex: '^address$', tsType: 'string'}, - {regex: '^bool$', tsType: 'boolean'}, - {regex: '^u?int\\d*$', tsType: 'BigNumber'}, - {regex: '^bytes\\d*$', tsType: 'string'}, + { regex: '^string$', tsType: 'string' }, + { regex: '^address$', tsType: 'string' }, + { regex: '^bool$', tsType: 'boolean' }, + { regex: '^u?int\\d*$', tsType: 'BigNumber' }, + { regex: '^bytes\\d*$', tsType: 'string' }, ]; if (paramKind === ParamKind.Input) { // web3 allows to pass those an non-bignumbers and that's nice // but it always returns stuff as BigNumbers - solTypeRegexToTsType.unshift({regex: '^u?int(8|16|32)?$', tsType: 'number|BigNumber'}); + solTypeRegexToTsType.unshift({ + regex: '^u?int(8|16|32)?$', + tsType: 'number|BigNumber', + }); } for (const regexAndTxType of solTypeRegexToTsType) { - const {regex, tsType} = regexAndTxType; + const { regex, tsType } = regexAndTxType; if (solType.match(regex)) { return tsType; } @@ -41,7 +44,7 @@ export const utils = { const name = path.parse(filename).name; return name; }, - getNamedContent(filename: string): {name: string; content: string} { + getNamedContent(filename: string): { name: string; content: string } { const name = utils.getPartialNameFromFileName(filename); try { const content = fs.readFileSync(filename).toString(); -- cgit v1.2.3 From 94c75b5262a4837490de33d0968462d6b46546b6 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 3 Jan 2018 12:05:44 +0100 Subject: Add #302 description to changelog --- packages/abi-gen/src/index.ts | 2 +- packages/abi-gen/src/types.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'packages/abi-gen/src') diff --git a/packages/abi-gen/src/index.ts b/packages/abi-gen/src/index.ts index bcb4a468b..ed71087b2 100644 --- a/packages/abi-gen/src/index.ts +++ b/packages/abi-gen/src/index.ts @@ -91,7 +91,7 @@ for (const abiFileName of abiFileNames) { return methodData; }); - const eventAbis = ABI.filter((abi: Web3.AbiDefinition) => abi.type === ABI_TYPE_EVENT) as Web3.MethodAbi[]; + const eventAbis = ABI.filter((abi: Web3.AbiDefinition) => abi.type === ABI_TYPE_EVENT) as Web3.EventAbi[]; const contextData = { contractName: namedContent.name, diff --git a/packages/abi-gen/src/types.ts b/packages/abi-gen/src/types.ts index 1dc039c83..8b158d77a 100644 --- a/packages/abi-gen/src/types.ts +++ b/packages/abi-gen/src/types.ts @@ -12,4 +12,5 @@ export interface Method extends Web3.MethodAbi { export interface ContextData { contractName: string; methods: Method[]; + events: Web3.EventAbi[]; } -- cgit v1.2.3 From f3b8bac47787a13ea4360b992bbe550bec2c3e19 Mon Sep 17 00:00:00 2001 From: Olaf Tomalka Date: Wed, 3 Jan 2018 15:48:29 +0100 Subject: Added constructor ABIs to abi-gen Additionally if the constructor is not existent in JSON, meaning it's implcite with no parameters, we're explicitly creating one, with actual JSON parameters that it should have. --- packages/abi-gen/src/index.ts | 7 +++++++ packages/abi-gen/src/utils.ts | 9 +++++++++ 2 files changed, 16 insertions(+) (limited to 'packages/abi-gen/src') diff --git a/packages/abi-gen/src/index.ts b/packages/abi-gen/src/index.ts index ed71087b2..65dc1c607 100644 --- a/packages/abi-gen/src/index.ts +++ b/packages/abi-gen/src/index.ts @@ -14,6 +14,7 @@ import * as Web3 from 'web3'; import { ContextData, ParamKind } from './types'; import { utils } from './utils'; +const ABI_TYPE_CONSTRUCTOR = 'constructor'; const ABI_TYPE_METHOD = 'function'; const ABI_TYPE_EVENT = 'event'; const MAIN_TEMPLATE_NAME = 'contract.mustache'; @@ -75,6 +76,11 @@ for (const abiFileName of abiFileNames) { process.exit(1); } + let constructor = ABI.find((abi: Web3.AbiDefinition) => abi.type === ABI_TYPE_CONSTRUCTOR) as Web3.ConstructorAbi; + if (!constructor) { + constructor = utils.getEmptyConstructor(); // The constructor exists, but it's implicit in JSON's ABI definition + } + const methodAbis = ABI.filter((abi: Web3.AbiDefinition) => abi.type === ABI_TYPE_METHOD) as Web3.MethodAbi[]; const methodsData = _.map(methodAbis, methodAbi => { _.map(methodAbi.inputs, input => { @@ -95,6 +101,7 @@ for (const abiFileName of abiFileNames) { const contextData = { contractName: namedContent.name, + constructor, methods: methodsData, events: eventAbis, }; diff --git a/packages/abi-gen/src/utils.ts b/packages/abi-gen/src/utils.ts index 764daa142..524c54a5e 100644 --- a/packages/abi-gen/src/utils.ts +++ b/packages/abi-gen/src/utils.ts @@ -1,6 +1,7 @@ import * as fs from 'fs'; import * as _ from 'lodash'; import * as path from 'path'; +import * as Web3 from 'web3'; import { ParamKind } from './types'; @@ -56,4 +57,12 @@ export const utils = { throw new Error(`Failed to read ${filename}: ${err}`); } }, + getEmptyConstructor(): Web3.ConstructorAbi { + return { + type: 'constructor', + stateMutability: 'nonpayable', + payable: false, + inputs: [], + }; + }, }; -- cgit v1.2.3 From c49f68ef3ebb11f779868b35f938c1d1cb08c665 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 4 Jan 2018 14:53:38 +0100 Subject: Fix linter errors --- packages/abi-gen/src/utils.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'packages/abi-gen/src') diff --git a/packages/abi-gen/src/utils.ts b/packages/abi-gen/src/utils.ts index 764daa142..f407ae23d 100644 --- a/packages/abi-gen/src/utils.ts +++ b/packages/abi-gen/src/utils.ts @@ -10,7 +10,7 @@ export const utils = { if (solType.match(trailingArrayRegex)) { const arrayItemSolType = solType.replace(trailingArrayRegex, ''); const arrayItemTsType = utils.solTypeToTsType(paramKind, arrayItemSolType); - const arrayTsType = `(${arrayItemTsType})[]`; + const arrayTsType = utils.isUnionType(arrayItemTsType) ? `Array<${arrayItemTsType}>` : `${arrayItemTsType}[]`; return arrayTsType; } else { const solTypeRegexToTsType = [ @@ -37,6 +37,9 @@ export const utils = { throw new Error(`Unknown Solidity type found: ${solType}`); } }, + isUnionType(tsType: string): boolean { + return tsType === 'number|BigNumber'; + }, log(...args: any[]): void { console.log(...args); // tslint:disable-line:no-console }, -- cgit v1.2.3 From 326a6b729fe6cf96f0b4787d84903282738b6863 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Thu, 4 Jan 2018 16:31:46 +0100 Subject: Fix prettier --- packages/abi-gen/src/utils.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'packages/abi-gen/src') diff --git a/packages/abi-gen/src/utils.ts b/packages/abi-gen/src/utils.ts index f407ae23d..edda016b5 100644 --- a/packages/abi-gen/src/utils.ts +++ b/packages/abi-gen/src/utils.ts @@ -10,7 +10,9 @@ export const utils = { if (solType.match(trailingArrayRegex)) { const arrayItemSolType = solType.replace(trailingArrayRegex, ''); const arrayItemTsType = utils.solTypeToTsType(paramKind, arrayItemSolType); - const arrayTsType = utils.isUnionType(arrayItemTsType) ? `Array<${arrayItemTsType}>` : `${arrayItemTsType}[]`; + const arrayTsType = utils.isUnionType(arrayItemTsType) + ? `Array<${arrayItemTsType}>` + : `${arrayItemTsType}[]`; return arrayTsType; } else { const solTypeRegexToTsType = [ -- cgit v1.2.3 From e019ae4aed610c4645894c2999871fd3c0b57388 Mon Sep 17 00:00:00 2001 From: Olaf Tomalka Date: Fri, 5 Jan 2018 02:07:25 +0100 Subject: Changes to abi-gen after code review * Added change to CHANGELOG * Renamed variable and context to ctor to avoid keyword * Used lodash' isUndefined to better check ctor --- packages/abi-gen/src/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'packages/abi-gen/src') diff --git a/packages/abi-gen/src/index.ts b/packages/abi-gen/src/index.ts index 65dc1c607..527af32b1 100644 --- a/packages/abi-gen/src/index.ts +++ b/packages/abi-gen/src/index.ts @@ -76,9 +76,9 @@ for (const abiFileName of abiFileNames) { process.exit(1); } - let constructor = ABI.find((abi: Web3.AbiDefinition) => abi.type === ABI_TYPE_CONSTRUCTOR) as Web3.ConstructorAbi; - if (!constructor) { - constructor = utils.getEmptyConstructor(); // The constructor exists, but it's implicit in JSON's ABI definition + let ctor = ABI.find((abi: Web3.AbiDefinition) => abi.type === ABI_TYPE_CONSTRUCTOR) as Web3.ConstructorAbi; + if (_.isUndefined(ctor)) { + ctor = utils.getEmptyConstructor(); // The constructor exists, but it's implicit in JSON's ABI definition } const methodAbis = ABI.filter((abi: Web3.AbiDefinition) => abi.type === ABI_TYPE_METHOD) as Web3.MethodAbi[]; @@ -101,7 +101,7 @@ for (const abiFileName of abiFileNames) { const contextData = { contractName: namedContent.name, - constructor, + ctor, methods: methodsData, events: eventAbis, }; -- cgit v1.2.3 From 0f8b2703d19f41adf96d2ec351b4da30d4ce636e Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 10 Jan 2018 11:28:43 +0100 Subject: Fix a compiler error with constructor enum --- packages/abi-gen/src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/abi-gen/src') diff --git a/packages/abi-gen/src/utils.ts b/packages/abi-gen/src/utils.ts index f6291d98d..b9149e850 100644 --- a/packages/abi-gen/src/utils.ts +++ b/packages/abi-gen/src/utils.ts @@ -64,7 +64,7 @@ export const utils = { }, getEmptyConstructor(): Web3.ConstructorAbi { return { - type: 'constructor', + type: Web3.AbiType.Constructor, stateMutability: 'nonpayable', payable: false, inputs: [], -- cgit v1.2.3 From fa7237fde7acea32c5492f0dcdef4d55b3ade6e1 Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Wed, 10 Jan 2018 12:00:39 +0100 Subject: Fix a compiler error in abi-gen --- packages/abi-gen/src/types.ts | 7 +++++++ packages/abi-gen/src/utils.ts | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'packages/abi-gen/src') diff --git a/packages/abi-gen/src/types.ts b/packages/abi-gen/src/types.ts index 8b158d77a..e82ab824b 100644 --- a/packages/abi-gen/src/types.ts +++ b/packages/abi-gen/src/types.ts @@ -5,6 +5,13 @@ export enum ParamKind { Output = 'output', } +export enum AbiType { + Function = 'function', + Constructor = 'constructor', + Event = 'event', + Fallback = 'fallback', +} + export interface Method extends Web3.MethodAbi { singleReturnValue: boolean; } diff --git a/packages/abi-gen/src/utils.ts b/packages/abi-gen/src/utils.ts index b9149e850..14255643a 100644 --- a/packages/abi-gen/src/utils.ts +++ b/packages/abi-gen/src/utils.ts @@ -3,7 +3,7 @@ import * as _ from 'lodash'; import * as path from 'path'; import * as Web3 from 'web3'; -import { ParamKind } from './types'; +import { AbiType, ParamKind } from './types'; export const utils = { solTypeToTsType(paramKind: ParamKind, solType: string): string { @@ -64,7 +64,7 @@ export const utils = { }, getEmptyConstructor(): Web3.ConstructorAbi { return { - type: Web3.AbiType.Constructor, + type: AbiType.Constructor, stateMutability: 'nonpayable', payable: false, inputs: [], -- cgit v1.2.3