Convert LibFillResults to library

This commit is contained in:
Amir Bandeali 2019-07-29 21:46:25 -05:00
parent 6ca9d4ee78
commit 28e781db15

View File

@ -19,13 +19,15 @@
pragma solidity ^0.5.9; pragma solidity ^0.5.9;
pragma experimental ABIEncoderV2; pragma experimental ABIEncoderV2;
import "@0x/contracts-utils/contracts/src/LibSafeMath.sol";
import "./LibMath.sol"; import "./LibMath.sol";
import "./LibOrder.sol"; import "./LibOrder.sol";
contract LibFillResults is library LibFillResults {
LibMath
{ using LibSafeMath for uint256;
struct BatchMatchedFillResults { struct BatchMatchedFillResults {
FillResults[] left; // Fill results for left orders FillResults[] left; // Fill results for left orders
FillResults[] right; // Fill results for right orders FillResults[] right; // Fill results for right orders
@ -55,23 +57,23 @@ contract LibFillResults is
LibOrder.Order memory order, LibOrder.Order memory order,
uint256 takerAssetFilledAmount uint256 takerAssetFilledAmount
) )
public internal
pure pure
returns (FillResults memory fillResults) returns (FillResults memory fillResults)
{ {
// Compute proportional transfer amounts // Compute proportional transfer amounts
fillResults.takerAssetFilledAmount = takerAssetFilledAmount; fillResults.takerAssetFilledAmount = takerAssetFilledAmount;
fillResults.makerAssetFilledAmount = _safeGetPartialAmountFloor( fillResults.makerAssetFilledAmount = LibMath.safeGetPartialAmountFloor(
takerAssetFilledAmount, takerAssetFilledAmount,
order.takerAssetAmount, order.takerAssetAmount,
order.makerAssetAmount order.makerAssetAmount
); );
fillResults.makerFeePaid = _safeGetPartialAmountFloor( fillResults.makerFeePaid = LibMath.safeGetPartialAmountFloor(
fillResults.makerAssetFilledAmount, fillResults.makerAssetFilledAmount,
order.makerAssetAmount, order.makerAssetAmount,
order.makerFee order.makerFee
); );
fillResults.takerFeePaid = _safeGetPartialAmountFloor( fillResults.takerFeePaid = LibMath.safeGetPartialAmountFloor(
takerAssetFilledAmount, takerAssetFilledAmount,
order.takerAssetAmount, order.takerAssetAmount,
order.takerFee order.takerFee
@ -98,19 +100,19 @@ contract LibFillResults is
uint256 rightOrderTakerAssetFilledAmount, uint256 rightOrderTakerAssetFilledAmount,
bool shouldMaximallyFillOrders bool shouldMaximallyFillOrders
) )
public internal
pure pure
returns (MatchedFillResults memory matchedFillResults) returns (MatchedFillResults memory matchedFillResults)
{ {
// Derive maker asset amounts for left & right orders, given store taker assert amounts // Derive maker asset amounts for left & right orders, given store taker assert amounts
uint256 leftTakerAssetAmountRemaining = _safeSub(leftOrder.takerAssetAmount, leftOrderTakerAssetFilledAmount); uint256 leftTakerAssetAmountRemaining = leftOrder.takerAssetAmount.safeSub(leftOrderTakerAssetFilledAmount);
uint256 leftMakerAssetAmountRemaining = _safeGetPartialAmountFloor( uint256 leftMakerAssetAmountRemaining = LibMath.safeGetPartialAmountFloor(
leftOrder.makerAssetAmount, leftOrder.makerAssetAmount,
leftOrder.takerAssetAmount, leftOrder.takerAssetAmount,
leftTakerAssetAmountRemaining leftTakerAssetAmountRemaining
); );
uint256 rightTakerAssetAmountRemaining = _safeSub(rightOrder.takerAssetAmount, rightOrderTakerAssetFilledAmount); uint256 rightTakerAssetAmountRemaining = rightOrder.takerAssetAmount.safeSub(rightOrderTakerAssetFilledAmount);
uint256 rightMakerAssetAmountRemaining = _safeGetPartialAmountFloor( uint256 rightMakerAssetAmountRemaining = LibMath.safeGetPartialAmountFloor(
rightOrder.makerAssetAmount, rightOrder.makerAssetAmount,
rightOrder.takerAssetAmount, rightOrder.takerAssetAmount,
rightTakerAssetAmountRemaining rightTakerAssetAmountRemaining
@ -138,24 +140,24 @@ contract LibFillResults is
} }
// Compute fees for left order // Compute fees for left order
matchedFillResults.left.makerFeePaid = _safeGetPartialAmountFloor( matchedFillResults.left.makerFeePaid = LibMath.safeGetPartialAmountFloor(
matchedFillResults.left.makerAssetFilledAmount, matchedFillResults.left.makerAssetFilledAmount,
leftOrder.makerAssetAmount, leftOrder.makerAssetAmount,
leftOrder.makerFee leftOrder.makerFee
); );
matchedFillResults.left.takerFeePaid = _safeGetPartialAmountFloor( matchedFillResults.left.takerFeePaid = LibMath.safeGetPartialAmountFloor(
matchedFillResults.left.takerAssetFilledAmount, matchedFillResults.left.takerAssetFilledAmount,
leftOrder.takerAssetAmount, leftOrder.takerAssetAmount,
leftOrder.takerFee leftOrder.takerFee
); );
// Compute fees for right order // Compute fees for right order
matchedFillResults.right.makerFeePaid = _safeGetPartialAmountFloor( matchedFillResults.right.makerFeePaid = LibMath.safeGetPartialAmountFloor(
matchedFillResults.right.makerAssetFilledAmount, matchedFillResults.right.makerAssetFilledAmount,
rightOrder.makerAssetAmount, rightOrder.makerAssetAmount,
rightOrder.makerFee rightOrder.makerFee
); );
matchedFillResults.right.takerFeePaid = _safeGetPartialAmountFloor( matchedFillResults.right.takerFeePaid = LibMath.safeGetPartialAmountFloor(
matchedFillResults.right.takerAssetFilledAmount, matchedFillResults.right.takerAssetFilledAmount,
rightOrder.takerAssetAmount, rightOrder.takerAssetAmount,
rightOrder.takerFee rightOrder.takerFee
@ -165,6 +167,23 @@ contract LibFillResults is
return matchedFillResults; return matchedFillResults;
} }
/// @dev Adds properties of both FillResults instances.
/// @param fillResults1 The first FillResults.
/// @param fillResults2 The second FillResults.
/// @return The sum of both fill results.
function addFillResults(FillResults memory fillResults1, FillResults memory fillResults2)
internal
pure
returns (FillResults memory totalFillResults)
{
totalFillResults.makerAssetFilledAmount = fillResults1.makerAssetFilledAmount.safeAdd(fillResults2.makerAssetFilledAmount);
totalFillResults.takerAssetFilledAmount = fillResults1.takerAssetFilledAmount.safeAdd(fillResults2.takerAssetFilledAmount);
totalFillResults.makerFeePaid = fillResults1.makerFeePaid.safeAdd(fillResults2.makerFeePaid);
totalFillResults.takerFeePaid = fillResults1.takerFeePaid.safeAdd(fillResults2.takerFeePaid);
return totalFillResults;
}
/// @dev Calculates part of the matched fill results for a given situation using the fill strategy that only /// @dev Calculates part of the matched fill results for a given situation using the fill strategy that only
/// awards profit denominated in the left maker asset. /// awards profit denominated in the left maker asset.
/// @param leftOrder The left order in the order matching situation. /// @param leftOrder The left order in the order matching situation.
@ -182,7 +201,7 @@ contract LibFillResults is
uint256 rightMakerAssetAmountRemaining, uint256 rightMakerAssetAmountRemaining,
uint256 rightTakerAssetAmountRemaining uint256 rightTakerAssetAmountRemaining
) )
internal private
pure pure
returns (MatchedFillResults memory matchedFillResults) returns (MatchedFillResults memory matchedFillResults)
{ {
@ -211,7 +230,7 @@ contract LibFillResults is
matchedFillResults.right.makerAssetFilledAmount = leftTakerAssetAmountRemaining; matchedFillResults.right.makerAssetFilledAmount = leftTakerAssetAmountRemaining;
// Round up to ensure the maker's exchange rate does not exceed the price specified by the order. // Round up to ensure the maker's exchange rate does not exceed the price specified by the order.
// We favor the maker when the exchange rate must be rounded. // We favor the maker when the exchange rate must be rounded.
matchedFillResults.right.takerAssetFilledAmount = _safeGetPartialAmountCeil( matchedFillResults.right.takerAssetFilledAmount = LibMath.safeGetPartialAmountCeil(
rightOrder.takerAssetAmount, rightOrder.takerAssetAmount,
rightOrder.makerAssetAmount, rightOrder.makerAssetAmount,
leftTakerAssetAmountRemaining // matchedFillResults.right.makerAssetFilledAmount leftTakerAssetAmountRemaining // matchedFillResults.right.makerAssetFilledAmount
@ -229,8 +248,7 @@ contract LibFillResults is
} }
// Calculate amount given to taker // Calculate amount given to taker
matchedFillResults.profitInLeftMakerAsset = _safeSub( matchedFillResults.profitInLeftMakerAsset = matchedFillResults.left.makerAssetFilledAmount.safeSub(
matchedFillResults.left.makerAssetFilledAmount,
matchedFillResults.right.takerAssetFilledAmount matchedFillResults.right.takerAssetFilledAmount
); );
@ -254,7 +272,7 @@ contract LibFillResults is
uint256 rightMakerAssetAmountRemaining, uint256 rightMakerAssetAmountRemaining,
uint256 rightTakerAssetAmountRemaining uint256 rightTakerAssetAmountRemaining
) )
internal private
pure pure
returns (MatchedFillResults memory matchedFillResults) returns (MatchedFillResults memory matchedFillResults)
{ {
@ -292,7 +310,7 @@ contract LibFillResults is
// Round down to ensure the right maker's exchange rate does not exceed the price specified by the order. // Round down to ensure the right maker's exchange rate does not exceed the price specified by the order.
// We favor the right maker when the exchange rate must be rounded and the profit is being paid in the // We favor the right maker when the exchange rate must be rounded and the profit is being paid in the
// right maker asset. // right maker asset.
matchedFillResults.right.makerAssetFilledAmount = _safeGetPartialAmountFloor( matchedFillResults.right.makerAssetFilledAmount = LibMath.safeGetPartialAmountFloor(
rightOrder.makerAssetAmount, rightOrder.makerAssetAmount,
rightOrder.takerAssetAmount, rightOrder.takerAssetAmount,
leftMakerAssetAmountRemaining leftMakerAssetAmountRemaining
@ -310,16 +328,14 @@ contract LibFillResults is
// Calculate amount given to taker in the left order's maker asset if the left spread will be part of the profit. // Calculate amount given to taker in the left order's maker asset if the left spread will be part of the profit.
if (doesLeftMakerAssetProfitExist) { if (doesLeftMakerAssetProfitExist) {
matchedFillResults.profitInLeftMakerAsset = _safeSub( matchedFillResults.profitInLeftMakerAsset = matchedFillResults.left.makerAssetFilledAmount.safeSub(
matchedFillResults.left.makerAssetFilledAmount,
matchedFillResults.right.takerAssetFilledAmount matchedFillResults.right.takerAssetFilledAmount
); );
} }
// Calculate amount given to taker in the right order's maker asset if the right spread will be part of the profit. // Calculate amount given to taker in the right order's maker asset if the right spread will be part of the profit.
if (doesRightMakerAssetProfitExist) { if (doesRightMakerAssetProfitExist) {
matchedFillResults.profitInRightMakerAsset = _safeSub( matchedFillResults.profitInRightMakerAsset = matchedFillResults.right.makerAssetFilledAmount.safeSub(
matchedFillResults.right.makerAssetFilledAmount,
matchedFillResults.left.takerAssetFilledAmount matchedFillResults.left.takerAssetFilledAmount
); );
} }
@ -341,7 +357,7 @@ contract LibFillResults is
uint256 rightMakerAssetAmountRemaining, uint256 rightMakerAssetAmountRemaining,
uint256 rightTakerAssetAmountRemaining uint256 rightTakerAssetAmountRemaining
) )
internal private
pure pure
returns (MatchedFillResults memory matchedFillResults) returns (MatchedFillResults memory matchedFillResults)
{ {
@ -366,7 +382,7 @@ contract LibFillResults is
uint256 rightMakerAssetAmountRemaining, uint256 rightMakerAssetAmountRemaining,
uint256 rightTakerAssetAmountRemaining uint256 rightTakerAssetAmountRemaining
) )
internal private
pure pure
returns (MatchedFillResults memory matchedFillResults) returns (MatchedFillResults memory matchedFillResults)
{ {
@ -376,7 +392,7 @@ contract LibFillResults is
// Round down to ensure the left maker's exchange rate does not exceed the price specified by the order. // Round down to ensure the left maker's exchange rate does not exceed the price specified by the order.
// We favor the left maker when the exchange rate must be rounded and the profit is being paid in the // We favor the left maker when the exchange rate must be rounded and the profit is being paid in the
// left maker asset. // left maker asset.
matchedFillResults.left.makerAssetFilledAmount = _safeGetPartialAmountFloor( matchedFillResults.left.makerAssetFilledAmount = LibMath.safeGetPartialAmountFloor(
leftOrder.makerAssetAmount, leftOrder.makerAssetAmount,
leftOrder.takerAssetAmount, leftOrder.takerAssetAmount,
rightMakerAssetAmountRemaining rightMakerAssetAmountRemaining
@ -384,21 +400,4 @@ contract LibFillResults is
return matchedFillResults; return matchedFillResults;
} }
/// @dev Adds properties of both FillResults instances.
/// @param fillResults1 The first FillResults.
/// @param fillResults2 The second FillResults.
/// @return The sum of both fill results.
function _addFillResults(FillResults memory fillResults1, FillResults memory fillResults2)
internal
pure
returns (FillResults memory totalFillResults)
{
totalFillResults.makerAssetFilledAmount = _safeAdd(fillResults1.makerAssetFilledAmount, fillResults2.makerAssetFilledAmount);
totalFillResults.takerAssetFilledAmount = _safeAdd(fillResults1.takerAssetFilledAmount, fillResults2.takerAssetFilledAmount);
totalFillResults.makerFeePaid = _safeAdd(fillResults1.makerFeePaid, fillResults2.makerFeePaid);
totalFillResults.takerFeePaid = _safeAdd(fillResults1.takerFeePaid, fillResults2.takerFeePaid);
return totalFillResults;
}
} }