diff options
author | Remco Bloemen <remco@wicked.ventures> | 2018-02-07 09:22:29 +0800 |
---|---|---|
committer | Amir Bandeali <abandeali1@gmail.com> | 2018-04-21 04:56:15 +0800 |
commit | 914db52c4da9f6824666c359df5efceabee51ff0 (patch) | |
tree | bc72011c6cb831d7ea8d17a78242725f8a881b58 | |
parent | fe51ec906ea4da7c2a749422e874ab2fef6f3cb8 (diff) | |
download | dexon-sol-tools-914db52c4da9f6824666c359df5efceabee51ff0.tar dexon-sol-tools-914db52c4da9f6824666c359df5efceabee51ff0.tar.gz dexon-sol-tools-914db52c4da9f6824666c359df5efceabee51ff0.tar.bz2 dexon-sol-tools-914db52c4da9f6824666c359df5efceabee51ff0.tar.lz dexon-sol-tools-914db52c4da9f6824666c359df5efceabee51ff0.tar.xz dexon-sol-tools-914db52c4da9f6824666c359df5efceabee51ff0.tar.zst dexon-sol-tools-914db52c4da9f6824666c359df5efceabee51ff0.zip |
Move order settlement to MSettlement
4 files changed, 68 insertions, 59 deletions
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/Exchange.sol b/packages/contracts/src/contracts/current/protocol/Exchange/Exchange.sol index 9c6883ebf..8f0bb45c5 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/Exchange.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/Exchange.sol @@ -33,7 +33,7 @@ contract Exchange is function Exchange(address _zrxToken, address _tokenTransferProxy) public - MixinExchangeCore(_zrxToken) + MixinExchangeCore() MixinSignatureValidatorEcrecover() MixinSettlementProxy(_tokenTransferProxy, _zrxToken) MixinWrapperFunctions() diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/MixinExchangeCore.sol b/packages/contracts/src/contracts/current/protocol/Exchange/MixinExchangeCore.sol index fc22f1292..836e9dd9b 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/MixinExchangeCore.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinExchangeCore.sol @@ -38,8 +38,6 @@ contract MixinExchangeCore is LibErrors, LibPartialAmount { - address public ZRX_TOKEN_CONTRACT; - // Mappings of orderHash => amounts of takerTokenAmount filled or cancelled. mapping (bytes32 => uint) public filled; mapping (bytes32 => uint) public cancelled; @@ -69,12 +67,6 @@ contract MixinExchangeCore is bytes32 orderHash ); - function MixinExchangeCore(address _zrxToken) - public - { - ZRX_TOKEN_CONTRACT = _zrxToken; - } - /* * Core exchange functions */ @@ -144,44 +136,18 @@ contract MixinExchangeCore is LogError(uint8(Errors.INSUFFICIENT_BALANCE_OR_ALLOWANCE), order.orderHash); return 0; } - - uint filledMakerTokenAmount = getPartialAmount(filledTakerTokenAmount, order.takerTokenAmount, order.makerTokenAmount); - uint paidMakerFee; - uint paidTakerFee; + + // Update state filled[order.orderHash] = safeAdd(filled[order.orderHash], filledTakerTokenAmount); - require(transferViaTokenTransferProxy( - order.makerToken, - order.maker, - msg.sender, - filledMakerTokenAmount - )); - require(transferViaTokenTransferProxy( - order.takerToken, - msg.sender, - order.maker, - filledTakerTokenAmount - )); - if (order.feeRecipient != address(0)) { - if (order.makerFee > 0) { - paidMakerFee = getPartialAmount(filledTakerTokenAmount, order.takerTokenAmount, order.makerFee); - require(transferViaTokenTransferProxy( - ZRX_TOKEN_CONTRACT, - order.maker, - order.feeRecipient, - paidMakerFee - )); - } - if (order.takerFee > 0) { - paidTakerFee = getPartialAmount(filledTakerTokenAmount, order.takerTokenAmount, order.takerFee); - require(transferViaTokenTransferProxy( - ZRX_TOKEN_CONTRACT, - msg.sender, - order.feeRecipient, - paidTakerFee - )); - } - } - + + // Settle order + uint256 filledMakerTokenAmount; + uint256 paidMakerFee; + uint256 paidTakerFee; + (filledMakerTokenAmount, paidMakerFee, paidTakerFee) = + settleOrder(order, msg.sender, filledTakerTokenAmount); + + // Log order LogFill( order.maker, msg.sender, diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/MixinSettlementProxy.sol b/packages/contracts/src/contracts/current/protocol/Exchange/MixinSettlementProxy.sol index 906020d26..d66223b4d 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/MixinSettlementProxy.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinSettlementProxy.sol @@ -41,6 +41,52 @@ contract MixinSettlementProxy is ZRX_TOKEN_CONTRACT = zrxToken; TOKEN_TRANSFER_PROXY_CONTRACT = proxyContract; } + + function settleOrder( + Order order, + address taker, + uint filledTakerTokenAmount) + internal + returns ( + uint filledMakerTokenAmount, + uint paidMakerFee, + uint paidTakerFee + ) + { + filledMakerTokenAmount = getPartialAmount(filledTakerTokenAmount, order.takerTokenAmount, order.makerTokenAmount); + require(transferViaTokenTransferProxy( + order.makerToken, + order.maker, + taker, + filledMakerTokenAmount + )); + require(transferViaTokenTransferProxy( + order.takerToken, + taker, + order.maker, + filledTakerTokenAmount + )); + if (order.feeRecipient != address(0)) { + if (order.makerFee > 0) { + paidMakerFee = getPartialAmount(filledTakerTokenAmount, order.takerTokenAmount, order.makerFee); + require(transferViaTokenTransferProxy( + ZRX_TOKEN_CONTRACT, + order.maker, + order.feeRecipient, + paidMakerFee + )); + } + if (order.takerFee > 0) { + paidTakerFee = getPartialAmount(filledTakerTokenAmount, order.takerTokenAmount, order.takerFee); + require(transferViaTokenTransferProxy( + ZRX_TOKEN_CONTRACT, + taker, + order.feeRecipient, + paidTakerFee + )); + } + } + } /// @dev Transfers a token using TokenTransferProxy transferFrom function. /// @param token Address of token to transferFrom. diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MSettlement.sol b/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MSettlement.sol index b6506dfab..0988050af 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MSettlement.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/mixins/MSettlement.sol @@ -21,20 +21,17 @@ pragma solidity ^0.4.19; import "../LibOrder.sol"; contract MSettlement is LibOrder { - - /// @dev Transfers a token using TokenTransferProxy transferFrom function. - /// @param token Address of token to transferFrom. - /// @param from Address transfering token. - /// @param to Address receiving token. - /// @param value Amount of token to transfer. - /// @return Success of token transfer. - function transferViaTokenTransferProxy( - address token, - address from, - address to, - uint value) + + function settleOrder( + Order order, + address taker, + uint filledTakerTokenAmount) internal - returns (bool); + returns ( + uint filledMakerTokenAmount, + uint paidMakerFee, + uint paidTakerFee + ); /// @dev Checks if any order transfers will fail. /// @param order Order struct of params that will be checked. |