Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement helper functions #42

Merged
merged 26 commits into from
Oct 23, 2018
Merged

Implement helper functions #42

merged 26 commits into from
Oct 23, 2018

Conversation

izqui
Copy link
Contributor

@izqui izqui commented Sep 20, 2018

Closes #40

Helper function structure is ready, need to implement the initial helpers we need now:

  • @tokenAmount(tokenAddr, amount, [showSymbol=true], [precision=2]): formats the token amount based on the token decimals.
  • @transformTime(time, toUnit, [fromUnit='seconds']): Transforms between time units.
  • @formatDate(timestamp, [dateFormat='MM-DD-YYYY']): Formats timestamp as a human readable date, supports passing the format for displaying the date.
  • @formatPct(number, [percentageBase=10^18], [precision=2]): Formats a number as a percentage

@izqui izqui self-assigned this Sep 20, 2018
@izqui izqui added this to the sprint: 1.3 milestone Sep 20, 2018
@coveralls
Copy link

coveralls commented Sep 21, 2018

Coverage Status

Coverage increased (+0.4%) to 91.119% when pulling 035d1a2 on helpers into 80aeeb0 on master.

@izqui izqui changed the title [WIP] Implement helper functions Implement helper functions Sep 24, 2018
@izqui izqui requested a review from sohkai September 24, 2018 17:21
package.json Outdated
@@ -33,6 +33,7 @@
},
"dependencies": {
"bn.js": "4.11.6",
"moment": "^2.22.2",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use date-fns@2.0.0-alpha.7 for now since that's what our other frontend projects are using.

moment.js is yuge.

"payable":false,
"stateMutability":"view",
"type":"function"
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we only use the .decimals() and .symbol() parts of the token, we could strip this down into just those.

See the Finance app as an example; it just creates an object out of those and gives it to web3.eth.Contract() as the ABI.

bingen pushed a commit to aragon/aragon-apps that referenced this pull request Sep 27, 2018
For Finance and TokenManager.
Using helpers in aragon/radspec#42
@izqui
Copy link
Contributor Author

izqui commented Sep 27, 2018

@sohkai Moved to date-fns in c6cd403. I pinned to 2.0.0-alpha.7 as it seems that they are making changes to the formatting tokens in the newest alphas and those seem less standard than the ones used in alpha.7

@sohkai
Copy link
Contributor

sohkai commented Sep 27, 2018

Hmm, just did npm ls date-fns on aragon/aragon and it looks like it's picking up 2.0.0-alpha.18 :(. @bpierre do you think we should pin everything to alpha.7 for now and just wait until the official 2.0.0 lands?

}

const capitalize = s => s.charAt(0).toUpperCase() + s.slice(1)
const add = require(`date-fns/add${capitalize(fromUnit)}`)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure if this dynamic require could cause us trouble

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure bundlers aren't going to like this :(. Best to avoid it and add it to a map or similar.

@izqui
Copy link
Contributor Author

izqui commented Sep 28, 2018

@sohkai I haven't checked what the motivation behind changing the formatting tokens is, but given that they are changing those in alpha versions we definitely need to pin it.

See the diff between alpha 7 and alpha 18. Updating how the formatting works will be quite disruptive as it will potentially break radspec strings.

bingen pushed a commit to aragon/aragon-apps that referenced this pull request Sep 28, 2018
Adapt to last changes in aragon/radspec#42
@sohkai
Copy link
Contributor

sohkai commented Oct 1, 2018

Yeah I'm worried that aragon-ui or aragon/aragon's time based helpers are also broken due to the change :(.

It looks like it was mostly motivated by the unicode standard (TR35), but this looks like it's still getting updated.

Edit: Can confirm aragon-ui's Countdown element's formatting is broken (not really in a user-visible way though) due to this update, but nothing else should be (they don't use format or parse a lot).

I'm siding with moving to the TR35 notation, since it seems more standard across different languages, is a real spec, and moment's API is actually non-standard.

@cfl0ws
Copy link

cfl0ws commented Oct 1, 2018

@izqui What Epic(s) should this be added to?

@izqui
Copy link
Contributor Author

izqui commented Oct 2, 2018

@chris-remus all of the apps will benefit from this changes, specially Finance and the Token Manager, but i wouldn't say it is specific to any of the sprints we have

@cfl0ws
Copy link

cfl0ws commented Oct 5, 2018

@sohkai and @bpierre To make decision on "Yeah I'm worried that aragon-ui or aragon/aragon's time based helpers are also broken due to the change :( "

fraction = `${zeros}${fraction}`
const whole = value.div(base).toString()

return `${whole}${parseInt(fraction) === 0 ? '' : `.${fraction.slice(0, precision)}`}`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn’t be a problem in modern JS engines, but it can still be a good idea to specify the radix as a second parameter of parseInt() for a while, just in case scripts are run on unsupported engines (e.g. old mobile browser or Node.js version) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt#Octal_interpretations_with_no_radix

node.inputs = this.functionInputs(astBody)
} else {
// There is actually no good reason not to allow calling a helper without ()
// this.report(`Expected '(' for executing helper function`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@izqui should this be uncommented?

@sohkai
Copy link
Contributor

sohkai commented Oct 21, 2018

@izqui I've updated this PR:

  • 8a2e04c: Updates to use date-fns@2.0.0-beta.22, and use a more "natural" and unambiguous default time format (1/2/2018 could be read as "Jan. 2, 2018" or "Feb. 1, 2018" depending on your locale)
  • 06c6811: I noticed a lot of different number and input assumptions in the helpers; while not a problem I hope this provides some sanity. Values required to be numbers are explicitly coerced using Number() and values required to be BN explicitly create new instances of BN. Note that bn.js applies a base of 10 by default as well.
  • 6bc383f: Removes dynamic requires from transformTime(), and simplifies its implementation. (API change) also changes the unit tokens to just use the date-fns units.
  • b390a29: (API change) exports the default helpers directly onto the module, rather than under a defaultHelpers namespace; seems to better reflect how the rest of the modules are exported.

throw new Error(`@transformTime: Time unit ${fromUnit} is not supported as a fromUnit`)
}

if (!SUPPORTED_TO_UNITS.has(toUnit)) {
if (toUnit && !ADD_UNIT_FN.has(toUnit)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have a special identifier for the 'best' unit, otherwise it won't be possible to format using that unit if a fromUnit is provided as well (as we cannot pass null from radspec)

@sohkai sohkai merged commit e1decc6 into master Oct 23, 2018
@sohkai sohkai deleted the helpers branch October 23, 2018 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants