aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2017-12-05 23:05:03 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2017-12-05 23:05:03 +0800
commitcde52b10b161e0672519fe77817e3b7486137e72 (patch)
tree41461e0340ffe25def3f96e3f77a79a914401e64
parent43983f1bb3660a7a7650d947de4bc1a2a9fb8c09 (diff)
downloaddexon-sol-tools-cde52b10b161e0672519fe77817e3b7486137e72.tar.gz
dexon-sol-tools-cde52b10b161e0672519fe77817e3b7486137e72.tar.zst
dexon-sol-tools-cde52b10b161e0672519fe77817e3b7486137e72.zip
Introduce separate ContextData type and rework it
-rw-r--r--packages/typed-contracts-templates/contract.mustache2
-rw-r--r--packages/typed-contracts-templates/partials/return_type.mustache8
-rw-r--r--packages/typed-contracts/src/index.ts18
-rw-r--r--packages/typed-contracts/src/types.ts11
4 files changed, 27 insertions, 12 deletions
diff --git a/packages/typed-contracts-templates/contract.mustache b/packages/typed-contracts-templates/contract.mustache
index 057088e8b..cb3bb3631 100644
--- a/packages/typed-contracts-templates/contract.mustache
+++ b/packages/typed-contracts-templates/contract.mustache
@@ -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}}
diff --git a/packages/typed-contracts-templates/partials/return_type.mustache b/packages/typed-contracts-templates/partials/return_type.mustache
index 5366c8fb7..383961a40 100644
--- a/packages/typed-contracts-templates/partials/return_type.mustache
+++ b/packages/typed-contracts-templates/partials/return_type.mustache
@@ -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}}
diff --git a/packages/typed-contracts/src/index.ts b/packages/typed-contracts/src/index.ts
index 9461ca8ca..19d289e49 100644
--- a/packages/typed-contracts/src/index.ts
+++ b/packages/typed-contracts/src/index.ts
@@ -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);
}
diff --git a/packages/typed-contracts/src/types.ts b/packages/typed-contracts/src/types.ts
index 3331588da..1dc039c83 100644
--- a/packages/typed-contracts/src/types.ts
+++ b/packages/typed-contracts/src/types.ts
@@ -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[];
+}