Array handling
This commit is contained in:
parent
65c7428a7f
commit
16499e0bb0
|
@ -23,6 +23,14 @@ Handlebars.registerHelper('ifeq', function(elem, value, options){
|
||||||
return options.inverse(this);
|
return options.inverse(this);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Handlebars.registerHelper('ifarr', function(elem, options){
|
||||||
|
if(elem.indexOf('[]') > -1){
|
||||||
|
return options.fn(this);
|
||||||
|
}
|
||||||
|
return options.inverse(this);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
Handlebars.registerHelper('iflengthgt', function(arr, val, options) {
|
Handlebars.registerHelper('iflengthgt', function(arr, val, options) {
|
||||||
if (arr.length > val) {
|
if (arr.length > val) {
|
||||||
return options.fn(this);
|
return options.fn(this);
|
||||||
|
@ -34,6 +42,10 @@ Handlebars.registerHelper('emptyname', function(name, index) {
|
||||||
return name ? name : 'output' + index;
|
return name ? name : 'output' + index;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Handlebars.registerHelper('trim', function(name) {
|
||||||
|
return name.replace('[]', '');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
Handlebars.registerHelper('methodname', function(abiDefinition, functionName, inputs){
|
Handlebars.registerHelper('methodname', function(abiDefinition, functionName, inputs){
|
||||||
let funCount = abiDefinition.filter(x => x.name === functionName).length;
|
let funCount = abiDefinition.filter(x => x.name === functionName).length;
|
||||||
|
|
|
@ -14,7 +14,7 @@ class {{capitalize name}}Form{{@index}} extends Component {
|
||||||
{{#if inputs.length}}
|
{{#if inputs.length}}
|
||||||
input: {
|
input: {
|
||||||
{{#each inputs}}
|
{{#each inputs}}
|
||||||
{{#ifeq name ''}}field{{else}}{{name}}{{/ifeq}}: {{#ifeq type 'bool'}}false{{else}}''{{/ifeq}}{{#unless @last}},{{/unless}}
|
{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}: {{#ifeq type 'bool'}}false{{else}}''{{/ifeq}}{{#unless @last}},{{/unless}}
|
||||||
{{/each}}
|
{{/each}}
|
||||||
},
|
},
|
||||||
{{/if}}
|
{{/if}}
|
||||||
|
@ -50,7 +50,7 @@ class {{capitalize name}}Form{{@index}} extends Component {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
{{#ifview stateMutability}}
|
{{#ifview stateMutability}}
|
||||||
const result = await {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}input.{{#ifeq name ''}}field{{else}}{{name}}{{/ifeq}}{{#unless @last}}, {{/unless}}{{/each}}).call()
|
const result = await {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}input.{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}{{#unless @last}}, {{/unless}}{{/each}}).call()
|
||||||
{{#iflengthgt outputs 1}}
|
{{#iflengthgt outputs 1}}
|
||||||
this.setState({output: {
|
this.setState({output: {
|
||||||
{{#each outputs}}
|
{{#each outputs}}
|
||||||
|
@ -61,7 +61,13 @@ class {{capitalize name}}Form{{@index}} extends Component {
|
||||||
this.setState({output: result});
|
this.setState({output: result});
|
||||||
{{/iflengthgt}}
|
{{/iflengthgt}}
|
||||||
{{else}}
|
{{else}}
|
||||||
const toSend = {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}input.{{#ifeq name ''}}field{{else}}{{name}}{{/ifeq}}{{#unless @last}}, {{/unless}}{{/each}});
|
{{#each inputs}}
|
||||||
|
{{#ifarr type}}
|
||||||
|
input.{{trim name}} = input.{{trim name}}.split(',').map(x => trim(x.toString()));
|
||||||
|
{{/ifarr}}
|
||||||
|
{{/each}}
|
||||||
|
|
||||||
|
const toSend = {{../contractName}}.methods{{methodname ../functions name inputs}}({{#each inputs}}input.{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}{{#unless @last}}, {{/unless}}{{/each}});
|
||||||
|
|
||||||
const estimatedGas = await toSend.estimateGas({from: web3.eth.defaultAccount});
|
const estimatedGas = await toSend.estimateGas({from: web3.eth.defaultAccount});
|
||||||
|
|
||||||
|
@ -95,14 +101,14 @@ class {{capitalize name}}Form{{@index}} extends Component {
|
||||||
<ControlLabel>{{#ifeq name ''}}field{{else}}{{name}}{{/ifeq}}</ControlLabel>
|
<ControlLabel>{{#ifeq name ''}}field{{else}}{{name}}{{/ifeq}}</ControlLabel>
|
||||||
{{#ifeq type 'bool'}}
|
{{#ifeq type 'bool'}}
|
||||||
<Checkbox
|
<Checkbox
|
||||||
onClick={(e) => this.handleCheckbox(e, '{{#ifeq name ''}}field{{else}}{{name}}{{/ifeq}}')}
|
onClick={(e) => this.handleCheckbox(e, '{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}')}
|
||||||
/>
|
/>
|
||||||
{{else}}
|
{{else}}
|
||||||
<FormControl
|
<FormControl
|
||||||
type="text"
|
type="text"
|
||||||
defaultValue={ input.{{#ifeq name ''}}field{{else}}{{name}}{{/ifeq}} }
|
defaultValue={ input.{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}} }
|
||||||
placeholder="{{type}}"
|
placeholder="{{type}}"
|
||||||
onChange={(e) => this.handleChange(e, '{{#ifeq name ''}}field{{else}}{{name}}{{/ifeq}}')}
|
onChange={(e) => this.handleChange(e, '{{#ifeq name ''}}field{{else}}{{trim name}}{{/ifeq}}')}
|
||||||
/>
|
/>
|
||||||
{{/ifeq}}
|
{{/ifeq}}
|
||||||
</FormGroup>
|
</FormGroup>
|
||||||
|
|
|
@ -2,6 +2,8 @@
|
||||||
const Handlebars = require('handlebars');
|
const Handlebars = require('handlebars');
|
||||||
const fs = require('../../core/fs');
|
const fs = require('../../core/fs');
|
||||||
|
|
||||||
|
const capitalize = string => string.charAt(0).toUpperCase() + string.slice(1);
|
||||||
|
|
||||||
class ScaffoldingSolidity {
|
class ScaffoldingSolidity {
|
||||||
constructor(embark, options){
|
constructor(embark, options){
|
||||||
this.embark = embark;
|
this.embark = embark;
|
||||||
|
@ -27,7 +29,9 @@ class ScaffoldingSolidity {
|
||||||
|
|
||||||
build(contract, overwrite, cb){
|
build(contract, overwrite, cb){
|
||||||
try {
|
try {
|
||||||
const filename = contract.className.toLowerCase();
|
contract.className = capitalize(contract.className);
|
||||||
|
|
||||||
|
const filename = contract.className;
|
||||||
|
|
||||||
this._generateFile(contract, 'contract.sol.tpl', 'sol', {
|
this._generateFile(contract, 'contract.sol.tpl', 'sol', {
|
||||||
'contractName': contract.className,
|
'contractName': contract.className,
|
||||||
|
|
|
@ -16,11 +16,7 @@ class Scaffolding {
|
||||||
this.generate(options.contract, options.overwrite, false, cb);
|
this.generate(options.contract, options.overwrite, false, cb);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
isContract(contractName){
|
|
||||||
return this.engine.config.contractsConfig.contracts[contractName] !== undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
getScaffoldPlugin(framework){
|
getScaffoldPlugin(framework){
|
||||||
let dappGenerators = this.plugins.getPluginsFor('dappGenerator');
|
let dappGenerators = this.plugins.getPluginsFor('dappGenerator');
|
||||||
let builder;
|
let builder;
|
||||||
|
@ -49,21 +45,23 @@ class Scaffolding {
|
||||||
generate(contractName, overwrite, isContractGeneration, cb){
|
generate(contractName, overwrite, isContractGeneration, cb){
|
||||||
this.loadFrameworkModule();
|
this.loadFrameworkModule();
|
||||||
|
|
||||||
let build = this.getScaffoldPlugin(this.framework);
|
const build = this.getScaffoldPlugin(this.framework);
|
||||||
if(!build){
|
if(!build){
|
||||||
this.engine.logger.error("Could not find plugin for framework '" + this.framework + "'");
|
this.engine.logger.error("Could not find plugin for framework '" + this.framework + "'");
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!this.isContract(contractName) && Object.getOwnPropertyNames(this.fields).length === 0){
|
const hasFields = Object.getOwnPropertyNames(this.fields).length !== 0;
|
||||||
this.engine.logger.error("contract '" + contractName + "' does not exist");
|
|
||||||
|
if(isContractGeneration && !hasFields){
|
||||||
|
// This happens when you execute "scaffold ContractName",
|
||||||
|
// assuming the contract already exists in a .sol file
|
||||||
cb();
|
cb();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
let contract;
|
let contract;
|
||||||
if(isContractGeneration){
|
if(isContractGeneration && hasFields){
|
||||||
contract = {className: contractName, fields: this.fields};
|
contract = {className: contractName, fields: this.fields};
|
||||||
try {
|
try {
|
||||||
build(contract, overwrite, cb);
|
build(contract, overwrite, cb);
|
||||||
|
@ -75,6 +73,12 @@ class Scaffolding {
|
||||||
this.engine.events.request("contracts:list", (_err, contractsList) => {
|
this.engine.events.request("contracts:list", (_err, contractsList) => {
|
||||||
if(_err) throw new Error(_err);
|
if(_err) throw new Error(_err);
|
||||||
const contract = contractsList.find(x => x.className === contractName);
|
const contract = contractsList.find(x => x.className === contractName);
|
||||||
|
if(!contract){
|
||||||
|
this.engine.logger.error("contract '" + contractName + "' does not exist");
|
||||||
|
cb();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
build(contract, overwrite, cb);
|
build(contract, overwrite, cb);
|
||||||
} catch(err){
|
} catch(err){
|
||||||
|
|
Loading…
Reference in New Issue