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

[PoC] Downcasting from u256 to u128 #212

Closed
josojo opened this issue Sep 25, 2019 · 4 comments · Fixed by #267
Closed

[PoC] Downcasting from u256 to u128 #212

josojo opened this issue Sep 25, 2019 · 4 comments · Fixed by #267
Assignees

Comments

@josojo
Copy link
Contributor

josojo commented Sep 25, 2019

Currently this downcasting could corrupt the numbers. We need to make sure that this is not happening.

@bh2smith is currently writing a library for it

@fleupold
Copy link
Contributor

Which downcasting are you talking about? Can you provide a link or more context?

@bh2smith
Copy link
Contributor

Which downcasting are you talking about?

I believe this refers explicitly to the require statement at the bottom of the following function

function getExecutedSellAmount(
uint128 executedBuyAmount,
uint128 buyTokenPrice,
uint128 sellTokenPrice
) internal view returns (uint128) {
// executedSellAmount = sellAmount * (1 - (1/feeDenominator))
// = sellAmount - sellAmount/feeDenominator
// = (sellAmount * feeDenominator) / feeDenominator - sellAmount/feeDenominator
// = (sellAmount * feeDenominator - sellAmount) / feeDenominator
// = (sellAmount * (feeDenominator - 1)) /feeDenominator
// = (executedBuyAmount * buyTokenPrice / sellTokenPrice) * (feeDenominator - 1) / feeDenominator
// in order to minimize rounding errors, the order is switched
// = (executedBuyAmount * buyTokenPrice / feeDenominator) * (feeDenominator - 1) / sellTokenPrice
uint256 sellAmount = (uint256(executedBuyAmount).mul(buyTokenPrice) / (feeDenominator - 1))
.mul(feeDenominator) / sellTokenPrice;
require(sellAmount < MAX_UINT128, "sellAmount too large");
return uint128(sellAmount);
}

I have opened a PR in OpenZeppelin repo that circumvents having to do this in a much simpler way. Take a look here!

OpenZeppelin/openzeppelin-contracts#1926

@bh2smith bh2smith changed the title Downcasting from u256 to u128 [PoC] Downcasting from u256 to u128 Oct 30, 2019
@bh2smith
Copy link
Contributor

The decision for this sprint was to determine what the best approach is here while waiting for the next release (since OpenZeppelin.SafeCast wont appear till v2.5).

@fleupold
Copy link
Contributor

I think we suggested to use OpenZeppelin as a npm git dependency (https://docs.npmjs.com/files/package.json#git-urls-as-dependencies) pinned to a tag/commit that includes the new SafeCast library.

@bh2smith bh2smith self-assigned this Nov 2, 2019
bh2smith added a commit that referenced this issue Nov 4, 2019
Closes #212 - by using specific commit hash of OpenZeppelin dependency and using its new SafeCast security tool!

TestPlan: checkout this branch, run `npm install` followed by `truffle test` (with an instance of ganache–cli running).
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 a pull request may close this issue.

3 participants