-
Notifications
You must be signed in to change notification settings - Fork 215
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
(Fix) floorToDecimals function updating to cover all use cases #281
Conversation
(Fix) roundToDecimals function updating to cover all use case
@15chrjef please take a look at the PR |
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.
Looks like a much-needed commit :)
A few comments regarding code quality.
- As a general rule, any logic that is not clear to someone who has never seen our codebase should be extracted into a variable or function (this can lead to a bunch of variables and utils, but also should encourage cleaner and conciser code)
- all string or number literals should be extracted as constants to a local or global constant file
- make sure to keep the same level of abstraction in a function/ make sure a function only does one thing. If a function has more than one responsibility, create a new function for this.
src/components/stepFour/index.js
Outdated
@@ -267,7 +267,7 @@ export class stepFour extends stepTwo { | |||
//FlatPricing | |||
getPricingStrategyParams = (web3, pricingStrategy, i, token) => { | |||
console.log(pricingStrategy); | |||
let oneTokenInETH = roundToDecimals(1000000000000000000, 1/pricingStrategy.rate) | |||
let oneTokenInETH = roundToDecimals(-18, 1/pricingStrategy.rate) |
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.
what is -18? We should not have plain strings or numbers, these values should all be extracted to constants and imported. I would also make '1/pricingStrategy.rate' a variable.
src/utils/utils.js
Outdated
@@ -283,7 +283,7 @@ export const getconstructorParams = (abiConstructor, state, vals, crowdsaleNum) | |||
//params.vals.push(state.web3.toWei(1/state.pricingStrategy[crowdsaleNum].rate/10**state.token.decimals, "ether")); | |||
|
|||
let oneTokenInETHRaw = 1/state.pricingStrategy[crowdsaleNum].rate | |||
let oneTokenInETH = roundToDecimals(1000000000000000000, oneTokenInETHRaw) | |||
let oneTokenInETH = roundToDecimals(-18, oneTokenInETHRaw) |
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.
same as above for -18
src/utils/utils.js
Outdated
export const roundToDecimals = (n, input) => Math.floor10(input, n) | ||
|
||
const decimalAdjust = (type, value, exp) => { | ||
if (typeof exp === 'undefined' || +exp === 0) { |
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.
- why would exp be equal to the string 'undefined'? This should most likely be the value undefined.
- what are you trying to do with +exp? if its number coersion, why not make it a number when it is passed in initially?
- what does having an undefined exp or an exp equal to 0 mean in words? The logic contained within the if conditional should be extracted to a variable as well.
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 Math.floor10 and decimalAdjust. It is well-known solution: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
src/utils/utils.js
Outdated
|
||
const decimalAdjust = (type, value, exp) => { | ||
if (typeof exp === 'undefined' || +exp === 0) { | ||
return Math[type](value); |
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.
what is this trying to do here?
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 Math.floor10 and decimalAdjust. It is well-known solution: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
src/utils/utils.js
Outdated
if (typeof exp === 'undefined' || +exp === 0) { | ||
return Math[type](value); | ||
} | ||
value = +value; |
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.
why all the '+value's? There should be a clearer way of accomplishing your intent here.
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 Math.floor10 and decimalAdjust. It is well-known solution: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
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 agree. All the '+' signs in the expressions will be replaced by Math.round? Or other methods on the Math object?
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 is the numeric representation: https://stackoverflow.com/questions/6682997/javascript-plus-symbol-before-variable
src/utils/utils.js
Outdated
} | ||
value = +value; | ||
exp = +exp; | ||
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) { |
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 would extract the clause in the if conditional to a variable as well as !(typeof exp === 'number' && exp % 1 === 0)
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 Math.floor10 and decimalAdjust. It is well-known solution: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
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.
Sounds good. Please extract line 306 to a variable
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.
Ok, done
src/utils/utils.js
Outdated
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) { | ||
return NaN; | ||
} | ||
value = value.toString().split('e'); |
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.
Instead of these three variable assignments that are self referencing, build a util function to extract this logic and return the final value for 'value'
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 Math.floor10 and decimalAdjust. It is well-known solution: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
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.
sure. Can you rename 'value' to something more descriptive?
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.
value -> inputNumber
Updates regarding to reviewer comments
Emergency (Fix) Change gas price const to 5GWei
src/components/stepFour/utils.js
Outdated
@@ -220,7 +221,7 @@ export function transferOwnership(web3, abi, addr, finalizeAgentAddr, gasLimit, | |||
} | |||
if (!tokenContract) return noContractAlert(); | |||
|
|||
sendTXToContract(web3, tokenContract.methods.transferOwnership(finalizeAgentAddr).send({gasLimit: gasLimit, gasPrice: 21000000000}), cb); | |||
sendTXToContract(web3, tokenContract.methods.transferOwnership(finalizeAgentAddr).send({gasLimit: gasLimit, gasPrice: GAS_PRICE}), cb); |
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.
What is
tokenContract.methods.transferOwnership(finalizeAgentAddr).send({gasLimit: gasLimit, gasPrice: GAS_PRICE})
I would extract this to a variable
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.
ok, done
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.
Few comments and questions on semantics. Math methods look good.
@15chrjef, is it time to approve? |
src/utils/utils.js
Outdated
return 1.0 / n * Math.ceil(n * input) | ||
export const roundToDecimals = (n, input) => Math.floor10(input, n) | ||
|
||
const decimalAdjust = (type, value, exp) => { |
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.
where did we get this function from?
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.
Related to #274
Function roundToDecimals, which truncates decimals to max 18, is improved to cover all use cases.