Skip to content

Commit

Permalink
Make helpers a class and provide eth object to helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
izqui committed Sep 21, 2018
1 parent 8ff3081 commit 0d7c770
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/evaluator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Eth = require('web3-eth')
const Web3Utils = require('web3-utils')
const BN = require('bn.js')
const types = require('../types')
const helpers = require('../helpers')
const { Helpers } = require('../helpers')

/**
* A value coupled with a type
Expand Down Expand Up @@ -64,6 +64,7 @@ class Evaluator {
this.bindings = bindings
this.eth = new Eth(ethNode || 'https://mainnet.infura.io')
this.to = to && new TypedValue('address', to)
this.helpers = new Helpers(this.eth)
}

/**
Expand Down Expand Up @@ -243,12 +244,12 @@ class Evaluator {
if (node.type === 'HelperFunction') {
const helperName = node.name

if (!helpers.exists(helperName)) {
if (!this.helpers.exists(helperName)) {
this.panic(`${helperName} helper function is not defined`)
}

const inputs = await this.evaluateNodes(node.inputs)
const { type, value } = await helpers.execute(helperName, inputs)
const { type, value } = await this.helpers.execute(helperName, inputs)

return new TypedValue(type, value)
}
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/echo.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = async (node, repeat = 1) => {
return { type: 'string', value: node.value.repeat(repeat) }
module.exports = () => async (echo, repeat = 1) => {
return { type: 'string', value: echo.repeat(repeat) }
}
2 changes: 1 addition & 1 deletion src/helpers/formatDate.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = async (timestamp, format='MM-DD-YYYY') => {
module.exports = () => async (timestamp, format='MM-DD-YYYY') => {
return { type: 'string', value: timestamp } // TODO
}
28 changes: 21 additions & 7 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
const formatDate = require('./formatDate')
const echo = require('./echo')
const tokenAmount = require('./tokenAmount')

module.exports = {
helpers: {
formatDate,
echo,
},
const defaultHelpers = {
formatDate,
echo,
tokenAmount,
}

class Helpers {
constructor (eth, userHelpers = {}) {
this.eth = eth
this.helpers = { ...defaultHelpers, ...userHelpers }
}

exists (helper) {
return !!this.helpers[helper]
},
}

execute (helper, inputs) {
return this.helpers[helper](...inputs)
inputs = inputs.map(input => input.value) // pass values directly
return this.helpers[helper](this.eth)(...inputs)
}
}

module.exports = {
Helpers,

defaultHelpers
}

0 comments on commit 0d7c770

Please sign in to comment.