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

(Fix) floorToDecimals function updating to cover all use cases #281

Merged
merged 15 commits into from
Oct 22, 2017
Merged

Conversation

vbaranov
Copy link
Collaborator

Related to #274
Function roundToDecimals, which truncates decimals to max 18, is improved to cover all use cases.

(Fix) roundToDecimals function updating to cover all use case
@igorbarinov
Copy link
Member

@15chrjef please take a look at the PR

Copy link
Contributor

@mark-antony1 mark-antony1 left a 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.

  1. 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)
  2. all string or number literals should be extracted as constants to a local or global constant file
  3. 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.

@@ -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)
Copy link
Contributor

@mark-antony1 mark-antony1 Oct 19, 2017

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.

@@ -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)
Copy link
Contributor

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

export const roundToDecimals = (n, input) => Math.floor10(input, n)

const decimalAdjust = (type, value, exp) => {
if (typeof exp === 'undefined' || +exp === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. why would exp be equal to the string 'undefined'? This should most likely be the value undefined.
  2. what are you trying to do with +exp? if its number coersion, why not make it a number when it is passed in initially?
  3. 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.

Copy link
Collaborator Author

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


const decimalAdjust = (type, value, exp) => {
if (typeof exp === 'undefined' || +exp === 0) {
return Math[type](value);
Copy link
Contributor

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?

Copy link
Collaborator Author

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

if (typeof exp === 'undefined' || +exp === 0) {
return Math[type](value);
}
value = +value;
Copy link
Contributor

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.

Copy link
Collaborator Author

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

Copy link
Contributor

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

}
value = +value;
exp = +exp;
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
Copy link
Contributor

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)

Copy link
Collaborator Author

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

Copy link
Contributor

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, done

if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
return NaN;
}
value = value.toString().split('e');
Copy link
Contributor

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'

Copy link
Collaborator Author

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

Copy link
Contributor

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

value -> inputNumber

@vbaranov vbaranov closed this Oct 19, 2017
@vbaranov vbaranov reopened this Oct 19, 2017
Updates regarding to reviewer comments
Emergency (Fix) Change gas price const to 5GWei
@@ -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);
Copy link
Contributor

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ok, done

Copy link
Contributor

@mark-antony1 mark-antony1 left a 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.

@vbaranov
Copy link
Collaborator Author

@15chrjef, is it time to approve?

return 1.0 / n * Math.ceil(n * input)
export const roundToDecimals = (n, input) => Math.floor10(input, n)

const decimalAdjust = (type, value, exp) => {
Copy link
Member

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@vbaranov vbaranov changed the title (Fix) roundToDecimals function updating to cover all use cases (Fix) floorToDecimals function updating to cover all use cases Oct 22, 2017
@vbaranov vbaranov merged commit 26131b0 into master Oct 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants