-
Notifications
You must be signed in to change notification settings - Fork 38
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
Conversation
package.json
Outdated
@@ -33,6 +33,7 @@ | |||
}, | |||
"dependencies": { | |||
"bn.js": "4.11.6", | |||
"moment": "^2.22.2", |
There was a problem hiding this comment.
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.
src/helpers/lib/token.js
Outdated
"payable":false, | ||
"stateMutability":"view", | ||
"type":"function" | ||
}, |
There was a problem hiding this comment.
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.
For Finance and TokenManager. Using helpers in aragon/radspec#42
Hmm, just did |
src/helpers/transformTime.js
Outdated
} | ||
|
||
const capitalize = s => s.charAt(0).toUpperCase() + s.slice(1) | ||
const add = require(`date-fns/add${capitalize(fromUnit)}`) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
@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. |
Adapt to last changes in aragon/radspec#42
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 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. |
@izqui What Epic(s) should this be added to? |
@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 |
src/helpers/lib/formatBN.js
Outdated
fraction = `${zeros}${fraction}` | ||
const whole = value.div(base).toString() | ||
|
||
return `${whole}${parseInt(fraction) === 0 ? '' : `.${fraction.slice(0, precision)}`}` |
There was a problem hiding this comment.
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`) |
There was a problem hiding this comment.
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?
@izqui I've updated this PR:
|
src/helpers/transformTime.js
Outdated
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)) { |
There was a problem hiding this comment.
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)
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