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

Using SafeCast for all downcasting #267

Merged
merged 7 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions contracts/StablecoinConverter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ pragma solidity ^0.5.0;
import "./EpochTokenLocker.sol";
import "@gnosis.pm/solidity-data-structures/contracts/libraries/IdToAddressBiMap.sol";
import "@gnosis.pm/solidity-data-structures/contracts/libraries/IterableAppendOnlySet.sol";
import "openzeppelin-solidity/contracts/util/SafeCast.sol";
bh2smith marked this conversation as resolved.
Show resolved Hide resolved
import "solidity-bytes-utils/contracts/BytesLib.sol";
import "./libraries/TokenConservation.sol";


contract StablecoinConverter is EpochTokenLocker {
using SafeCast for uint;
using SafeMath for uint128;
using BytesLib for bytes32;
using BytesLib for bytes;
Expand Down Expand Up @@ -246,10 +248,8 @@ contract StablecoinConverter is EpochTokenLocker {
currentPrices[order.buyToken],
currentPrices[order.sellToken]
);
return uint128(
execBuy.sub(execSell.mul(order.priceNumerator)
.div(order.priceDenominator)).mul(currentPrices[order.buyToken])
);
return execBuy.sub(execSell.mul(order.priceNumerator)
.div(order.priceDenominator)).mul(currentPrices[order.buyToken]).toUint128();
}

function evaluateDisregardedUtility(Order memory order, address user) internal view returns(uint128) {
Expand All @@ -262,10 +262,9 @@ contract StablecoinConverter is EpochTokenLocker {
uint256(order.remainingAmount),
getBalance(user, tokenIdToAddressMap(order.sellToken))
);
// TODO - use SafeCast
uint256 limitTerm = currentPrices[order.sellToken].mul(order.priceDenominator)
.sub(currentPrices[order.buyToken].mul(order.priceNumerator));
return uint128(leftoverSellAmount.mul(limitTerm).div(order.priceDenominator));
return leftoverSellAmount.mul(limitTerm).div(order.priceDenominator).toUint128();
}

function grantRewardToSolutionSubmitter(uint feeReward) internal {
Expand Down Expand Up @@ -302,25 +301,23 @@ contract StablecoinConverter is EpochTokenLocker {
// = ((executedBuyAmount * buyTokenPrice) / (feeDenominator - 1)) * feeDenominator) / sellTokenPrice
uint256 sellAmount = uint256(executedBuyAmount).mul(buyTokenPrice).div(feeDenominator - 1)
.mul(feeDenominator).div(sellTokenPrice);
// TODO - use SafeCast here.
require(sellAmount < MAX_UINT128, "sellAmount too large");
return uint128(sellAmount);
return sellAmount.toUint128();
}

function updateRemainingOrder(
address owner,
uint orderId,
uint128 executedAmount
) internal {
orders[owner][orderId].remainingAmount = uint128(orders[owner][orderId].remainingAmount.sub(executedAmount));
orders[owner][orderId].remainingAmount = orders[owner][orderId].remainingAmount.sub(executedAmount).toUint128();
}

function revertRemainingOrder(
address owner,
uint orderId,
uint128 executedAmount
) internal {
orders[owner][orderId].remainingAmount = uint128(orders[owner][orderId].remainingAmount.add(executedAmount));
orders[owner][orderId].remainingAmount = orders[owner][orderId].remainingAmount.add(executedAmount).toUint128();
}

function documentTrades(
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"ethereumjs-util": "^6.0.0",
"fast-memoize": "^2.5.1",
"merkletreejs": "0.0.22",
"openzeppelin-solidity": "2.1.1",
"openzeppelin-solidity": "OpenZeppelin/openzeppelin-contracts#2c11ed59faaac4709ab1dc18244dbad3970609e6",
bh2smith marked this conversation as resolved.
Show resolved Hide resolved
"solidity-bytes-utils": "0.0.8",
"truffle-contract": "^4.0.31",
"yargs": "^14.0.0"
Expand Down