Skip to content

Commit

Permalink
Using SafeCast for all downcasting (#267)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
bh2smith authored Nov 4, 2019
1 parent 0c0f7b7 commit d388f8e
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 98 deletions.
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/utils/SafeCast.sol";
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 @@ -247,10 +249,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 @@ -263,10 +263,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 @@ -303,25 +302,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
Loading

0 comments on commit d388f8e

Please sign in to comment.