Introduce separate ContextData type and rework it

This commit is contained in:
Leonid Logvinov
2017-12-05 18:05:03 +03:00
parent 43983f1bb3
commit cde52b10b1
4 changed files with 27 additions and 12 deletions

View File

@@ -12,7 +12,7 @@ import {promisify} from '../../utils/promisify';
import {BaseContract} from './base_contract';
export class {{contractName}}Contract extends BaseContract {
{{#each methodAbis}}
{{#each methods}}
{{#this.constant}}
{{> call contractName=../contractName}}
{{/this.constant}}

View File

@@ -1,6 +1,6 @@
{{#outputs.singleReturnValue}}
{{#singleReturnValue}}
{{#returnType outputs.0.type}}{{/returnType}}
{{/outputs.singleReturnValue}}
{{^outputs.singleReturnValue}}
{{/singleReturnValue}}
{{^singleReturnValue}}
[{{#each outputs}}{{#returnType type}}{{/returnType}}{{#unless @last}}, {{/unless}}{{/each}}]
{{/outputs.singleReturnValue}}
{{/singleReturnValue}}

View File

@@ -11,7 +11,7 @@ import * as yargs from 'yargs';
import toSnakeCase = require('to-snake-case');
import * as Web3 from 'web3';
import {ParamKind} from './types';
import {ContextData, ParamKind} from './types';
import {utils} from './utils';
const ABI_TYPE_METHOD = 'function';
@@ -51,7 +51,7 @@ for (const partialTemplateFileName of partialTemplateFileNames) {
}
const mainTemplate = utils.getNamedContent(`${args.templates}/${MAIN_TEMPLATE_NAME}`);
const template = Handlebars.compile(mainTemplate.content);
const template = Handlebars.compile<ContextData>(mainTemplate.content);
const abiFileNames = globSync(args.abiGlob);
if (_.isEmpty(abiFileNames)) {
utils.log(`${chalk.red(`No ABI files found.`)}`);
@@ -75,7 +75,7 @@ for (const abiFileName of abiFileNames) {
process.exit(1);
}
const methodAbis = ABI.filter((abi: Web3.AbiDefinition) => abi.type === ABI_TYPE_METHOD) as Web3.MethodAbi[];
_.map(methodAbis, methodAbi => {
const methodsData = _.map(methodAbis, methodAbi => {
_.map(methodAbi.inputs, input => {
if (_.isEmpty(input.name)) {
// Auto-generated getters don't have parameter names
@@ -83,12 +83,16 @@ for (const abiFileName of abiFileNames) {
}
});
// This will make templates simpler
(methodAbi.outputs as any).singleReturnValue = methodAbi.outputs.length === 1;
const methodData = {
...methodAbi,
singleReturnValue: methodAbi.outputs.length === 1,
};
return methodData;
});
const templateData = {
const contextData = {
contractName: namedContent.name,
methodAbis,
methods: methodsData,
};
const renderedTsCode = template(templateData);
const renderedTsCode = template(contextData);
writeOutputFile(namedContent.name, renderedTsCode);
}

View File

@@ -1,4 +1,15 @@
import * as Web3 from 'web3';
export enum ParamKind {
Input = 'input',
Output = 'output',
}
export interface Method extends Web3.MethodAbi {
singleReturnValue: boolean;
}
export interface ContextData {
contractName: string;
methods: Method[];
}