diff options
author | Amir Bandeali <abandeali1@gmail.com> | 2018-03-30 02:08:52 +0800 |
---|---|---|
committer | Amir Bandeali <abandeali1@gmail.com> | 2018-04-21 04:56:17 +0800 |
commit | 75f77f3e8675c468d88456aec02561732c7646fb (patch) | |
tree | 6cbc55dca9fda65c7872e078ff134cf86107356c /packages/contracts/src | |
parent | 7a66da5a51603d4d87d644f3635f8c7f1d00bb36 (diff) | |
download | dexon-sol-tools-75f77f3e8675c468d88456aec02561732c7646fb.tar dexon-sol-tools-75f77f3e8675c468d88456aec02561732c7646fb.tar.gz dexon-sol-tools-75f77f3e8675c468d88456aec02561732c7646fb.tar.bz2 dexon-sol-tools-75f77f3e8675c468d88456aec02561732c7646fb.tar.lz dexon-sol-tools-75f77f3e8675c468d88456aec02561732c7646fb.tar.xz dexon-sol-tools-75f77f3e8675c468d88456aec02561732c7646fb.tar.zst dexon-sol-tools-75f77f3e8675c468d88456aec02561732c7646fb.zip |
Add marketBuyOrders, rename variables, and fx old tests
Diffstat (limited to 'packages/contracts/src')
8 files changed, 326 insertions, 226 deletions
diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/LibOrder.sol b/packages/contracts/src/contracts/current/protocol/Exchange/LibOrder.sol index e966f9ffc..4394e26c8 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/LibOrder.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/LibOrder.sol @@ -28,10 +28,10 @@ contract LibOrder { "address makerTokenAddress", "address takerTokenAddress", "address feeRecipientAddress", - "uint256 makerTokenAmount", - "uint256 takerTokenAmount", - "uint256 makerFeeAmount", - "uint256 takerFeeAmount", + "uint256 makerSellAmount", + "uint256 makerBuyAmount", + "uint256 makerFee", + "uint256 takerFee", "uint256 expirationTimeSeconds", "uint256 salt" ); @@ -42,10 +42,10 @@ contract LibOrder { address makerTokenAddress; address takerTokenAddress; address feeRecipientAddress; - uint256 makerTokenAmount; - uint256 takerTokenAmount; - uint256 makerFeeAmount; - uint256 takerFeeAmount; + uint256 makerSellAmount; + uint256 makerBuyAmount; + uint256 makerFee; + uint256 takerFee; uint256 expirationTimeSeconds; uint256 salt; } @@ -68,10 +68,10 @@ contract LibOrder { order.makerTokenAddress, order.takerTokenAddress, order.feeRecipientAddress, - order.makerTokenAmount, - order.takerTokenAmount, - order.makerFeeAmount, - order.takerFeeAmount, + order.makerSellAmount, + order.makerBuyAmount, + order.makerFee, + order.takerFee, order.expirationTimeSeconds, order.salt ) diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/MixinExchangeCore.sol b/packages/contracts/src/contracts/current/protocol/Exchange/MixinExchangeCore.sol index 82c1cc33a..375848ed1 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/MixinExchangeCore.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinExchangeCore.sol @@ -39,8 +39,10 @@ contract MixinExchangeCore is LibErrors, LibPartialAmount { - // Mappings of orderHash => amounts of takerTokenAmount filled or cancelled. + // Mapping of orderHash => amount of takerToken already bought by maker mapping (bytes32 => uint256) public filled; + + // Mapping of orderHash => cancelled mapping (bytes32 => bool) public cancelled; // Mapping of makerAddress => lowest salt an order can have in order to be fillable @@ -53,10 +55,10 @@ contract MixinExchangeCore is address indexed feeRecipientAddress, address makerTokenAddress, address takerTokenAddress, - uint256 makerTokenFilledAmount, - uint256 takerTokenFilledAmount, - uint256 makerFeeAmountPaid, - uint256 takerFeeAmountPaid, + uint256 makerAmountSold, + uint256 makerAmountBought, + uint256 makerFeePaid, + uint256 takerFeePaid, bytes32 indexed orderHash ); @@ -69,7 +71,7 @@ contract MixinExchangeCore is ); event LogCancelUpTo( - address indexed maker, + address indexed makerAddress, uint256 makerEpoch ); @@ -79,15 +81,15 @@ contract MixinExchangeCore is /// @dev Fills the input order. /// @param order Order struct containing order specifications. - /// @param takerTokenFillAmount Desired amount of takerToken to fill. + /// @param takerSellAmount Desired amount of takerToken to sell. /// @param signature Proof of signing order by maker. /// @return Total amount of takerToken filled in trade. function fillOrder( - Order order, - uint256 takerTokenFillAmount, - bytes signature) + Order memory order, + uint256 takerSellAmount, + bytes memory signature) public - returns (uint256 takerTokenFilledAmount) + returns (uint256 takerAmountSold) { // Compute the order hash bytes32 orderHash = getOrderHash(order); @@ -101,8 +103,6 @@ contract MixinExchangeCore is // Validate order and maker only if first time seen // TODO: Read filled and cancelled only once if (filled[orderHash] == 0) { - require(order.makerTokenAmount > 0); - require(order.takerTokenAmount > 0); require(isValidSignature(orderHash, order.makerAddress, signature)); } @@ -110,7 +110,6 @@ contract MixinExchangeCore is if (order.takerAddress != address(0)) { require(order.takerAddress == msg.sender); } - require(takerTokenFillAmount > 0); // Validate order expiration if (block.timestamp >= order.expirationTimeSeconds) { @@ -119,15 +118,15 @@ contract MixinExchangeCore is } // Validate order availability - uint256 remainingTakerTokenAmount = safeSub(order.takerTokenAmount, filled[orderHash]); - if (remainingTakerTokenAmount == 0) { + uint256 remainingMakerBuyAmount = safeSub(order.makerBuyAmount, filled[orderHash]); + if (remainingMakerBuyAmount == 0) { LogError(uint8(Errors.ORDER_FULLY_FILLED), orderHash); return 0; } // Validate fill order rounding - takerTokenFilledAmount = min256(takerTokenFillAmount, remainingTakerTokenAmount); - if (isRoundingError(takerTokenFilledAmount, order.takerTokenAmount, order.makerTokenAmount)) { + takerAmountSold = min256(takerSellAmount, remainingMakerBuyAmount); + if (isRoundingError(takerAmountSold, order.makerBuyAmount, order.makerSellAmount)) { LogError(uint8(Errors.ROUNDING_ERROR_TOO_LARGE), orderHash); return 0; } @@ -139,11 +138,11 @@ contract MixinExchangeCore is } // Update state - filled[orderHash] = safeAdd(filled[orderHash], takerTokenFilledAmount); + filled[orderHash] = safeAdd(filled[orderHash], takerAmountSold); // Settle order - var (makerTokenFilledAmount, makerFeeAmountPaid, takerFeeAmountPaid) = - settleOrder(order, msg.sender, takerTokenFilledAmount); + var (makerAmountSold, makerFeePaid, takerFeePaid) = + settleOrder(order, msg.sender, takerAmountSold); // Log order LogFill( @@ -152,19 +151,19 @@ contract MixinExchangeCore is order.feeRecipientAddress, order.makerTokenAddress, order.takerTokenAddress, - makerTokenFilledAmount, - takerTokenFilledAmount, - makerFeeAmountPaid, - takerFeeAmountPaid, + makerAmountSold, + takerAmountSold, + makerFeePaid, + takerFeePaid, orderHash ); - return takerTokenFilledAmount; + return takerAmountSold; } /// @dev After calling, the order can not be filled anymore. /// @param order Order struct containing order specifications. /// @return True if the order state changed to cancelled. False if the transaction was already cancelled or expired. - function cancelOrder(Order order) + function cancelOrder(Order memory order) public returns (bool) { @@ -172,8 +171,6 @@ contract MixinExchangeCore is bytes32 orderHash = getOrderHash(order); // Validate the order - require(order.makerTokenAmount > 0); - require(order.takerTokenAmount > 0); require(order.makerAddress == msg.sender); if (block.timestamp >= order.expirationTimeSeconds) { diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/MixinSettlementProxy.sol b/packages/contracts/src/contracts/current/protocol/Exchange/MixinSettlementProxy.sol index f4139fe65..772ea3d9f 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/MixinSettlementProxy.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinSettlementProxy.sol @@ -48,32 +48,32 @@ contract MixinSettlementProxy is } function MixinSettlementProxy( - ITokenTransferProxy proxyContract, - IToken zrxToken) + ITokenTransferProxy _proxyContract, + IToken _zrxToken) public { - ZRX_TOKEN = zrxToken; - TRANSFER_PROXY = proxyContract; + ZRX_TOKEN = _zrxToken; + TRANSFER_PROXY = _proxyContract; } function settleOrder( - Order order, + Order memory order, address takerAddress, - uint256 takerTokenFilledAmount) + uint256 takerAmountSold) internal returns ( - uint256 makerTokenFilledAmount, - uint256 makerFeeAmountPaid, - uint256 takerFeeAmountPaid + uint256 makerAmountSold, + uint256 makerFeePaid, + uint256 takerFeePaid ) { - makerTokenFilledAmount = getPartialAmount(takerTokenFilledAmount, order.takerTokenAmount, order.makerTokenAmount); + makerAmountSold = getPartialAmount(takerAmountSold, order.makerBuyAmount, order.makerSellAmount); require( TRANSFER_PROXY.transferFrom( order.makerTokenAddress, order.makerAddress, takerAddress, - makerTokenFilledAmount + makerAmountSold ) ); require( @@ -81,33 +81,33 @@ contract MixinSettlementProxy is order.takerTokenAddress, takerAddress, order.makerAddress, - takerTokenFilledAmount + takerAmountSold ) ); if (order.feeRecipientAddress != address(0)) { - if (order.makerFeeAmount > 0) { - makerFeeAmountPaid = getPartialAmount(takerTokenFilledAmount, order.takerTokenAmount, order.makerFeeAmount); + if (order.makerFee > 0) { + makerFeePaid = getPartialAmount(takerAmountSold, order.makerBuyAmount, order.makerFee); require( TRANSFER_PROXY.transferFrom( ZRX_TOKEN, order.makerAddress, order.feeRecipientAddress, - makerFeeAmountPaid + makerFeePaid ) ); } - if (order.takerFeeAmount > 0) { - takerFeeAmountPaid = getPartialAmount(takerTokenFilledAmount, order.takerTokenAmount, order.takerFeeAmount); + if (order.takerFee > 0) { + takerFeePaid = getPartialAmount(takerAmountSold, order.makerBuyAmount, order.takerFee); require( TRANSFER_PROXY.transferFrom( ZRX_TOKEN, takerAddress, order.feeRecipientAddress, - takerFeeAmountPaid + takerFeePaid ) ); } } - return (makerTokenFilledAmount, makerFeeAmountPaid, takerFeeAmountPaid); + return (makerAmountSold, makerFeePaid, takerFeePaid); } } diff --git a/packages/contracts/src/contracts/current/protocol/Exchange/MixinWrapperFunctions.sol b/packages/contracts/src/contracts/current/protocol/Exchange/MixinWrapperFunctions.sol index 39f4784cb..cfd52f71d 100644 --- a/packages/contracts/src/contracts/current/protocol/Exchange/MixinWrapperFunctions.sol +++ b/packages/contracts/src/contracts/current/protocol/Exchange/MixinWrapperFunctions.sol @@ -20,42 +20,46 @@ pragma solidity ^0.4.21; pragma experimental ABIEncoderV2; import "./mixins/MExchangeCore.sol"; +import "./LibPartialAmount.sol"; import "../../utils/SafeMath/SafeMath.sol"; /// @dev Consumes MExchangeCore contract MixinWrapperFunctions is MExchangeCore, - SafeMath + SafeMath, + LibPartialAmount { + /// @dev Fills the input order. Reverts if exact takerSellAmount not filled. /// @param order Order struct containing order specifications. - /// @param takerTokenFillAmount Desired amount of takerToken to fill. + /// @param takerSellAmount Desired amount of takerToken to fill. /// @param signature Maker's signature of the order. function fillOrKillOrder( - Order order, - uint256 takerTokenFillAmount, - bytes signature) + Order memory order, + uint256 takerSellAmount, + bytes memory signature) public { require( fillOrder( order, - takerTokenFillAmount, + takerSellAmount, signature - ) == takerTokenFillAmount + ) == takerSellAmount ); } - /// @dev Fills an order with specified parameters and ECDSA signature. Returns false if the transaction would otherwise revert. + /// @dev Fills an order with specified parameters and ECDSA signature. + /// Returns false if the transaction would otherwise revert. /// @param order Order struct containing order specifications. - /// @param takerTokenFillAmount Desired amount of takerToken to fill. + /// @param takerSellAmount Desired amount of takerToken to fill. /// @param signature Maker's signature of the order. /// @return Total amount of takerToken filled in trade. function fillOrderNoThrow( - Order order, - uint256 takerTokenFillAmount, - bytes signature) + Order memory order, + uint256 takerSellAmount, + bytes memory signature) public - returns (uint256 takerTokenFilledAmount) + returns (uint256 takerAmountSold) { // We need to call MExchangeCore.fillOrder using a delegatecall in // assembly so that we can intercept a call that throws. For this, we @@ -101,8 +105,8 @@ contract MixinWrapperFunctions is mstore(add(start, 292), mload(add(order, 288))) // expirationTimeSeconds mstore(add(start, 324), mload(add(order, 320))) // salt - // Write takerTokenFillAmount - mstore(add(start, 356), takerTokenFillAmount) + // Write takerSellAmount + mstore(add(start, 356), takerSellAmount) // Write signature offset mstore(add(start, 388), 416) @@ -133,30 +137,30 @@ contract MixinWrapperFunctions is ) switch success case 0 { - takerTokenFilledAmount := 0 + takerAmountSold := 0 } case 1 { - takerTokenFilledAmount := mload(start) + takerAmountSold := mload(start) } } - return takerTokenFilledAmount; + return takerAmountSold; } /// @dev Synchronously executes multiple calls of fillOrder in a single transaction. /// @param orders Array of orders. - /// @param takerTokenFillAmounts Array of desired amounts of takerToken to fill in orders. + /// @param takerSellAmounts Array of desired amounts of takerToken to fill in orders. /// @param signatures Maker's signatures of the orders. function batchFillOrders( - Order[] orders, - uint256[] takerTokenFillAmounts, - bytes[] signatures) + Order[] memory orders, + uint256[] memory takerSellAmounts, + bytes[] memory signatures) public { for (uint256 i = 0; i < orders.length; i++) { fillOrder( orders[i], - takerTokenFillAmounts[i], + takerSellAmounts[i], signatures[i] ); } @@ -164,18 +168,18 @@ contract MixinWrapperFunctions is /// @dev Synchronously executes multiple calls of fillOrKill in a single transaction. /// @param orders Array of orders. - /// @param takerTokenFillAmounts Array of desired amounts of takerToken to fill in orders. + /// @param takerSellAmounts Array of desired amounts of takerToken to fill in orders. /// @param signatures Maker's signatures of the orders. function batchFillOrKillOrders( - Order[] orders, - uint256[] takerTokenFillAmounts, - bytes[] signatures) + Order[] memory orders, + uint256[] memory takerSellAmounts, + bytes[] memory signatures) public { for (uint256 i = 0; i < orders.length; i++) { fillOrKillOrder( orders[i], - takerTokenFillAmounts[i], + takerSellAmounts[i], signatures[i] ); } @@ -183,86 +187,168 @@ contract MixinWrapperFunctions is /// @dev Fills an order with specified parameters and ECDSA signature. Returns false if the transaction would otherwise revert. /// @param orders Array of orders. - /// @param takerTokenFillAmounts Array of desired amounts of takerToken to fill in orders. + /// @param takerSellAmounts Array of desired amounts of takerToken to fill in orders. /// @param signatures Maker's signatures of the orders. function batchFillOrdersNoThrow( - Order[] orders, - uint256[] takerTokenFillAmounts, - bytes[] signatures) + Order[] memory orders, + uint256[] memory takerSellAmounts, + bytes[] memory signatures) public { for (uint256 i = 0; i < orders.length; i++) { fillOrderNoThrow( orders[i], - takerTokenFillAmounts[i], + takerSellAmounts[i], signatures[i] ); } } - /// @dev Synchronously executes multiple fill orders in a single transaction until total takerTokenFillAmount filled. + /// @dev Synchronously executes multiple fill orders in a single transaction until total amount is sold by taker. /// @param orders Array of orders. - /// @param takerTokenFillAmount Desired amount of takerToken to fill. + /// @param takerSellAmount Desired amount of takerToken to sell. /// @param signatures Maker's signatures of the orders. - /// @return Total amount of takerTokenFillAmount filled in orders. - function marketFillOrders( - Order[] orders, - uint256 takerTokenFillAmount, - bytes[] signatures) + /// @return Total amount of tokens sold by taker in orders. + function marketSellOrders( + Order[] memory orders, + uint256 takerSellAmount, + bytes[] memory signatures) public - returns (uint256 totalTakerTokenFilledAmount) + returns (uint256 takerAmountSold) { for (uint256 i = 0; i < orders.length; i++) { require(orders[i].takerTokenAddress == orders[0].takerTokenAddress); - uint256 remainingTakerTokenFillAmount = safeSub(takerTokenFillAmount, totalTakerTokenFilledAmount); - totalTakerTokenFilledAmount = safeAdd( - totalTakerTokenFilledAmount, + uint256 remainingTakerSellAmount = safeSub(takerSellAmount, takerAmountSold); + takerAmountSold = safeAdd( + takerAmountSold, fillOrder( orders[i], - remainingTakerTokenFillAmount, + remainingTakerSellAmount, signatures[i] ) ); - if (totalTakerTokenFilledAmount == takerTokenFillAmount) { + if (takerAmountSold == takerSellAmount) { break; } } - return totalTakerTokenFilledAmount; + return takerAmountSold; } - /// @dev Synchronously executes multiple calls of fillOrderNoThrow in a single transaction until total takerTokenFillAmount filled. + /// @dev Synchronously executes multiple calls of fillOrderNoThrow in a single transaction until total amount is sold by taker. + /// Returns false if the transaction would otherwise revert. /// @param orders Array of orders. - /// @param takerTokenFillAmount Desired total amount of takerToken to fill in orders. + /// @param takerSellAmount Desired amount of takerToken to sell. /// @param signatures Maker's signatures of the orders. - /// @return Total amount of takerTokenFillAmount filled in orders. - function marketFillOrdersNoThrow( - Order[] orders, - uint256 takerTokenFillAmount, - bytes[] signatures) + /// @return Total amount of tokens sold by taker in orders. + function marketSellOrdersNoThrow( + Order[] memory orders, + uint256 takerSellAmount, + bytes[] memory signatures) public - returns (uint256 totalTakerTokenFilledAmount) + returns (uint256 takerAmountSold) { for (uint256 i = 0; i < orders.length; i++) { require(orders[i].takerTokenAddress == orders[0].takerTokenAddress); - uint256 remainingTakerTokenFillAmount = safeSub(takerTokenFillAmount, totalTakerTokenFilledAmount); - totalTakerTokenFilledAmount = safeAdd( - totalTakerTokenFilledAmount, + uint256 remainingTakerSellAmount = safeSub(takerSellAmount, takerAmountSold); + takerAmountSold = safeAdd( + takerAmountSold, fillOrderNoThrow( orders[i], - remainingTakerTokenFillAmount, + remainingTakerSellAmount, + signatures[i] + ) + ); + if (takerAmountSold == takerSellAmount) { + break; + } + } + return takerAmountSold; + } + + /// @dev Synchronously executes multiple fill orders in a single transaction until total amount is bought by taker. + /// @param orders Array of orders. + /// @param takerBuyAmount Desired amount of makerToken to buy. + /// @param signatures Maker's signatures of the orders. + /// @return Total amount of takerTokenFillAmount filled in orders. + function marketBuyOrders( + Order[] memory orders, + uint256 takerBuyAmount, + bytes[] memory signatures) + public + returns (uint256 takerAmountBought) + { + for (uint256 i = 0; i < orders.length; i++) { + require(orders[i].takerTokenAddress == orders[0].takerTokenAddress); + uint256 remainingTakerBuyAmount = safeSub(takerBuyAmount, takerAmountBought); + uint256 takerSellAmount = getPartialAmount( + orders[i].makerBuyAmount, + orders[i].makerSellAmount, + remainingTakerBuyAmount + ); + uint256 takerAmountSold = fillOrder( + orders[i], + takerSellAmount, signatures[i] + ); + takerAmountBought = safeAdd( + takerAmountBought, + getPartialAmount( + orders[i].makerSellAmount, + orders[i].makerBuyAmount, + takerAmountSold + ) + ); + if (takerAmountBought == takerBuyAmount) { + break; + } + } + return takerAmountBought; + } + + /// @dev Synchronously executes multiple fill orders in a single transaction until total amount is bought by taker. + /// Returns false if the transaction would otherwise revert. + /// @param orders Array of orders. + /// @param takerBuyAmount Desired amount of makerToken to fill. + /// @param signatures Maker's signatures of the orders. + /// @return Total amount of takerTokenFillAmount filled in orders. + function marketBuyOrdersNoThrow( + Order[] memory orders, + uint256 takerBuyAmount, + bytes[] memory signatures) + public + returns (uint256 takerAmountBought) + { + for (uint256 i = 0; i < orders.length; i++) { + require(orders[i].takerTokenAddress == orders[0].takerTokenAddress); + uint256 remainingTakerBuyAmount = safeSub(takerBuyAmount, takerAmountBought); + uint256 takerSellAmount = getPartialAmount( + orders[i].makerBuyAmount, + orders[i].makerSellAmount, + remainingTakerBuyAmount + ); + uint256 takerAmountSold = fillOrderNoThrow( + orders[i], + takerSellAmount, + signatures[i] + ); + takerAmountBought = safeAdd( + takerAmountBought, + getPartialAmount( + orders[i].makerSellAmount, + orders[i].makerBuyAmount, + takerAmountSold ) ); - if (totalTakerTokenFilledAmount == takerTokenFillAmount) { + if (takerAmountBought == takerBuyAmount) { break; } } - return totalTakerTokenFilledAmount; + return takerAmountBought; } /// @dev Synchronously cancels multiple orders in a single transaction. /// @param orders Array of orders. - function batchCancelOrders(Order[] orders) + function batchCancelOrders(Order[] memory orders) public { for (uint256 i = 0; i < orders.length; i++) { diff --git a/packages/contracts/src/utils/exchange_wrapper.ts b/packages/contracts/src/utils/exchange_wrapper.ts index 179fe44a5..743a9c496 100644 --- a/packages/contracts/src/utils/exchange_wrapper.ts +++ b/packages/contracts/src/utils/exchange_wrapper.ts @@ -22,12 +22,12 @@ export class ExchangeWrapper { public async fillOrderAsync( signedOrder: SignedOrder, from: string, - opts: { takerTokenFillAmount?: BigNumber } = {}, + opts: { takerSellAmount?: BigNumber } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { - const params = orderUtils.createFill(signedOrder, opts.takerTokenFillAmount); + const params = orderUtils.createFill(signedOrder, opts.takerSellAmount); const txHash = await this._exchange.fillOrder.sendTransactionAsync( params.order, - params.takerTokenFillAmount, + params.takerSellAmount, params.signature, { from }, ); @@ -43,12 +43,12 @@ export class ExchangeWrapper { public async fillOrKillOrderAsync( signedOrder: SignedOrder, from: string, - opts: { takerTokenFillAmount?: BigNumber } = {}, + opts: { takerSellAmount?: BigNumber } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { - const params = orderUtils.createFill(signedOrder, opts.takerTokenFillAmount); + const params = orderUtils.createFill(signedOrder, opts.takerSellAmount); const txHash = await this._exchange.fillOrKillOrder.sendTransactionAsync( params.order, - params.takerTokenFillAmount, + params.takerSellAmount, params.signature, { from }, ); @@ -58,12 +58,12 @@ export class ExchangeWrapper { public async fillOrderNoThrowAsync( signedOrder: SignedOrder, from: string, - opts: { takerTokenFillAmount?: BigNumber } = {}, + opts: { takerSellAmount?: BigNumber } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { - const params = orderUtils.createFill(signedOrder, opts.takerTokenFillAmount); + const params = orderUtils.createFill(signedOrder, opts.takerSellAmount); const txHash = await this._exchange.fillOrderNoThrow.sendTransactionAsync( params.order, - params.takerTokenFillAmount, + params.takerSellAmount, params.signature, { from }, ); @@ -73,12 +73,12 @@ export class ExchangeWrapper { public async batchFillOrdersAsync( orders: SignedOrder[], from: string, - opts: { takerTokenFillAmounts?: BigNumber[] } = {}, + opts: { takerSellAmounts?: BigNumber[] } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { - const params = formatters.createBatchFill(orders, opts.takerTokenFillAmounts); + const params = formatters.createBatchFill(orders, opts.takerSellAmounts); const txHash = await this._exchange.batchFillOrders.sendTransactionAsync( params.orders, - params.takerTokenFillAmounts, + params.takerSellAmounts, params.signatures, { from }, ); @@ -88,12 +88,12 @@ export class ExchangeWrapper { public async batchFillOrKillOrdersAsync( orders: SignedOrder[], from: string, - opts: { takerTokenFillAmounts?: BigNumber[] } = {}, + opts: { takerSellAmounts?: BigNumber[] } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { - const params = formatters.createBatchFill(orders, opts.takerTokenFillAmounts); + const params = formatters.createBatchFill(orders, opts.takerSellAmounts); const txHash = await this._exchange.batchFillOrKillOrders.sendTransactionAsync( params.orders, - params.takerTokenFillAmounts, + params.takerSellAmounts, params.signatures, { from }, ); @@ -103,42 +103,72 @@ export class ExchangeWrapper { public async batchFillOrdersNoThrowAsync( orders: SignedOrder[], from: string, - opts: { takerTokenFillAmounts?: BigNumber[] } = {}, + opts: { takerSellAmounts?: BigNumber[] } = {}, ): Promise<TransactionReceiptWithDecodedLogs> { - const params = formatters.createBatchFill(orders, opts.takerTokenFillAmounts); + const params = formatters.createBatchFill(orders, opts.takerSellAmounts); const txHash = await this._exchange.batchFillOrdersNoThrow.sendTransactionAsync( params.orders, - params.takerTokenFillAmounts, + params.takerSellAmounts, params.signatures, { from }, ); const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); return tx; } - public async marketFillOrdersAsync( + public async marketSellOrdersAsync( orders: SignedOrder[], from: string, - opts: { takerTokenFillAmount: BigNumber }, + opts: { takerSellAmount: BigNumber }, ): Promise<TransactionReceiptWithDecodedLogs> { - const params = formatters.createMarketFillOrders(orders, opts.takerTokenFillAmount); - const txHash = await this._exchange.marketFillOrders.sendTransactionAsync( + const params = formatters.createMarketSellOrders(orders, opts.takerSellAmount); + const txHash = await this._exchange.marketSellOrders.sendTransactionAsync( params.orders, - params.takerTokenFillAmount, + params.takerSellAmount, params.signatures, { from }, ); const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); return tx; } - public async marketFillOrdersNoThrowAsync( + public async marketSellOrdersNoThrowAsync( orders: SignedOrder[], from: string, - opts: { takerTokenFillAmount: BigNumber }, + opts: { takerSellAmount: BigNumber }, ): Promise<TransactionReceiptWithDecodedLogs> { - const params = formatters.createMarketFillOrders(orders, opts.takerTokenFillAmount); - const txHash = await this._exchange.marketFillOrdersNoThrow.sendTransactionAsync( + const params = formatters.createMarketSellOrders(orders, opts.takerSellAmount); + const txHash = await this._exchange.marketSellOrdersNoThrow.sendTransactionAsync( params.orders, - params.takerTokenFillAmount, + params.takerSellAmount, + params.signatures, + { from }, + ); + const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + return tx; + } + public async marketBuyOrdersAsync( + orders: SignedOrder[], + from: string, + opts: { takerBuyAmount: BigNumber }, + ): Promise<TransactionReceiptWithDecodedLogs> { + const params = formatters.createMarketBuyOrders(orders, opts.takerBuyAmount); + const txHash = await this._exchange.marketBuyOrders.sendTransactionAsync( + params.orders, + params.takerBuyAmount, + params.signatures, + { from }, + ); + const tx = await this._getTxWithDecodedExchangeLogsAsync(txHash); + return tx; + } + public async marketBuyOrdersNoThrowAsync( + orders: SignedOrder[], + from: string, + opts: { takerBuyAmount: BigNumber }, + ): Promise<TransactionReceiptWithDecodedLogs> { + const params = formatters.createMarketBuyOrders(orders, opts.takerBuyAmount); + const txHash = await this._exchange.marketBuyOrdersNoThrow.sendTransactionAsync( + params.orders, + params.takerBuyAmount, params.signatures, { from }, ); @@ -191,7 +221,7 @@ export class ExchangeWrapper { ); return partialAmount; } - public async getFilledTakerTokenAmountAsync(orderHashHex: string): Promise<BigNumber> { + public async getMakerAmountBoughtAsync(orderHashHex: string): Promise<BigNumber> { const filledAmount = new BigNumber(await this._exchange.filled.callAsync(orderHashHex)); return filledAmount; } diff --git a/packages/contracts/src/utils/formatters.ts b/packages/contracts/src/utils/formatters.ts index 2b261f967..d70c03cb8 100644 --- a/packages/contracts/src/utils/formatters.ts +++ b/packages/contracts/src/utils/formatters.ts @@ -1,78 +1,59 @@ import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; -import { BatchCancelOrders, BatchFillOrders, MarketFillOrders, SignedOrder } from './types'; +import { orderUtils } from './order_utils'; +import { BatchCancelOrders, BatchFillOrders, MarketBuyOrders, MarketSellOrders, SignedOrder } from './types'; export const formatters = { - createBatchFill(signedOrders: SignedOrder[], takerTokenFillAmounts: BigNumber[] = []) { + createBatchFill(signedOrders: SignedOrder[], takerSellAmounts: BigNumber[] = []) { const batchFill: BatchFillOrders = { orders: [], signatures: [], - takerTokenFillAmounts, + takerSellAmounts, }; _.forEach(signedOrders, signedOrder => { - batchFill.orders.push({ - makerAddress: signedOrder.makerAddress, - takerAddress: signedOrder.takerAddress, - makerTokenAddress: signedOrder.makerTokenAddress, - takerTokenAddress: signedOrder.takerTokenAddress, - feeRecipientAddress: signedOrder.feeRecipientAddress, - makerTokenAmount: signedOrder.makerTokenAmount, - takerTokenAmount: signedOrder.takerTokenAmount, - makerFeeAmount: signedOrder.makerFeeAmount, - takerFeeAmount: signedOrder.takerFeeAmount, - expirationTimeSeconds: signedOrder.expirationTimeSeconds, - salt: signedOrder.salt, - }); + const orderStruct = orderUtils.getOrderStruct(signedOrder); + batchFill.orders.push(orderStruct); batchFill.signatures.push(signedOrder.signature); - if (takerTokenFillAmounts.length < signedOrders.length) { - batchFill.takerTokenFillAmounts.push(signedOrder.takerTokenAmount); + if (takerSellAmounts.length < signedOrders.length) { + batchFill.takerSellAmounts.push(signedOrder.makerBuyAmount); } }); return batchFill; }, - createMarketFillOrders(signedOrders: SignedOrder[], takerTokenFillAmount: BigNumber) { - const marketFillOrders: MarketFillOrders = { + createMarketSellOrders(signedOrders: SignedOrder[], takerSellAmount: BigNumber) { + const marketSellOrders: MarketSellOrders = { orders: [], signatures: [], - takerTokenFillAmount, + takerSellAmount, }; _.forEach(signedOrders, signedOrder => { - marketFillOrders.orders.push({ - makerAddress: signedOrder.makerAddress, - takerAddress: signedOrder.takerAddress, - makerTokenAddress: signedOrder.makerTokenAddress, - takerTokenAddress: signedOrder.takerTokenAddress, - feeRecipientAddress: signedOrder.feeRecipientAddress, - makerTokenAmount: signedOrder.makerTokenAmount, - takerTokenAmount: signedOrder.takerTokenAmount, - makerFeeAmount: signedOrder.makerFeeAmount, - takerFeeAmount: signedOrder.takerFeeAmount, - expirationTimeSeconds: signedOrder.expirationTimeSeconds, - salt: signedOrder.salt, - }); - marketFillOrders.signatures.push(signedOrder.signature); + const orderStruct = orderUtils.getOrderStruct(signedOrder); + marketSellOrders.orders.push(orderStruct); + marketSellOrders.signatures.push(signedOrder.signature); }); - return marketFillOrders; + return marketSellOrders; + }, + createMarketBuyOrders(signedOrders: SignedOrder[], takerBuyAmount: BigNumber) { + const marketBuyOrders: MarketBuyOrders = { + orders: [], + signatures: [], + takerBuyAmount, + }; + _.forEach(signedOrders, signedOrder => { + const orderStruct = orderUtils.getOrderStruct(signedOrder); + marketBuyOrders.orders.push(orderStruct); + marketBuyOrders.signatures.push(signedOrder.signature); + }); + return marketBuyOrders; }, createBatchCancel(signedOrders: SignedOrder[]) { const batchCancel: BatchCancelOrders = { orders: [], }; _.forEach(signedOrders, signedOrder => { - batchCancel.orders.push({ - makerAddress: signedOrder.makerAddress, - takerAddress: signedOrder.takerAddress, - makerTokenAddress: signedOrder.makerTokenAddress, - takerTokenAddress: signedOrder.takerTokenAddress, - feeRecipientAddress: signedOrder.feeRecipientAddress, - makerTokenAmount: signedOrder.makerTokenAmount, - takerTokenAmount: signedOrder.takerTokenAmount, - makerFeeAmount: signedOrder.makerFeeAmount, - takerFeeAmount: signedOrder.takerFeeAmount, - expirationTimeSeconds: signedOrder.expirationTimeSeconds, - salt: signedOrder.salt, - }); + const orderStruct = orderUtils.getOrderStruct(signedOrder); + batchCancel.orders.push(orderStruct); }); return batchCancel; }, diff --git a/packages/contracts/src/utils/order_utils.ts b/packages/contracts/src/utils/order_utils.ts index c14dc4e80..1f808024a 100644 --- a/packages/contracts/src/utils/order_utils.ts +++ b/packages/contracts/src/utils/order_utils.ts @@ -7,10 +7,10 @@ import { crypto } from './crypto'; import { OrderStruct, SignatureType, SignedOrder, UnsignedOrder } from './types'; export const orderUtils = { - createFill: (signedOrder: SignedOrder, takerTokenFillAmount?: BigNumber) => { + createFill: (signedOrder: SignedOrder, takerSellAmount?: BigNumber) => { const fill = { order: orderUtils.getOrderStruct(signedOrder), - takerTokenFillAmount: takerTokenFillAmount || signedOrder.takerTokenAmount, + takerSellAmount: takerSellAmount || signedOrder.makerBuyAmount, signature: signedOrder.signature, }; return fill; @@ -18,7 +18,7 @@ export const orderUtils = { createCancel(signedOrder: SignedOrder, takerTokenCancelAmount?: BigNumber) { const cancel = { order: orderUtils.getOrderStruct(signedOrder), - takerTokenCancelAmount: takerTokenCancelAmount || signedOrder.takerTokenAmount, + takerTokenCancelAmount: takerTokenCancelAmount || signedOrder.makerBuyAmount, }; return cancel; }, @@ -29,10 +29,10 @@ export const orderUtils = { makerTokenAddress: signedOrder.makerTokenAddress, takerTokenAddress: signedOrder.takerTokenAddress, feeRecipientAddress: signedOrder.feeRecipientAddress, - makerTokenAmount: signedOrder.makerTokenAmount, - takerTokenAmount: signedOrder.takerTokenAmount, - makerFeeAmount: signedOrder.makerFeeAmount, - takerFeeAmount: signedOrder.takerFeeAmount, + makerSellAmount: signedOrder.makerSellAmount, + makerBuyAmount: signedOrder.makerBuyAmount, + makerFee: signedOrder.makerFee, + takerFee: signedOrder.takerFee, expirationTimeSeconds: signedOrder.expirationTimeSeconds, salt: signedOrder.salt, }; @@ -46,10 +46,10 @@ export const orderUtils = { 'address makerTokenAddress', 'address takerTokenAddress', 'address feeRecipientAddress', - 'uint256 makerTokenAmount', - 'uint256 takerTokenAmount', - 'uint256 makerFeeAmount', - 'uint256 takerFeeAmount', + 'uint256 makerSellAmount', + 'uint256 makerBuyAmount', + 'uint256 makerFee', + 'uint256 takerFee', 'uint256 expirationTimeSeconds', 'uint256 salt', ]); @@ -60,10 +60,10 @@ export const orderUtils = { order.makerTokenAddress, order.takerTokenAddress, order.feeRecipientAddress, - order.makerTokenAmount, - order.takerTokenAmount, - order.makerFeeAmount, - order.takerFeeAmount, + order.makerSellAmount, + order.makerBuyAmount, + order.makerFee, + order.takerFee, order.expirationTimeSeconds, order.salt, ]); diff --git a/packages/contracts/src/utils/types.ts b/packages/contracts/src/utils/types.ts index ce87e9851..570f664e0 100644 --- a/packages/contracts/src/utils/types.ts +++ b/packages/contracts/src/utils/types.ts @@ -14,13 +14,19 @@ export interface SubmissionContractEventArgs { export interface BatchFillOrders { orders: OrderStruct[]; signatures: string[]; - takerTokenFillAmounts: BigNumber[]; + takerSellAmounts: BigNumber[]; } -export interface MarketFillOrders { +export interface MarketSellOrders { orders: OrderStruct[]; signatures: string[]; - takerTokenFillAmount: BigNumber; + takerSellAmount: BigNumber; +} + +export interface MarketBuyOrders { + orders: OrderStruct[]; + signatures: string[]; + takerBuyAmount: BigNumber; } export interface BatchCancelOrders { @@ -37,10 +43,10 @@ export interface DefaultOrderParams { feeRecipientAddress: string; makerTokenAddress: string; takerTokenAddress: string; - makerTokenAmount: BigNumber; - takerTokenAmount: BigNumber; - makerFeeAmount: BigNumber; - takerFeeAmount: BigNumber; + makerSellAmount: BigNumber; + makerBuyAmount: BigNumber; + makerFee: BigNumber; + takerFee: BigNumber; } export interface TransactionDataParams { @@ -122,10 +128,10 @@ export interface OrderStruct { makerTokenAddress: string; takerTokenAddress: string; feeRecipientAddress: string; - makerTokenAmount: BigNumber; - takerTokenAmount: BigNumber; - makerFeeAmount: BigNumber; - takerFeeAmount: BigNumber; + makerSellAmount: BigNumber; + makerBuyAmount: BigNumber; + makerFee: BigNumber; + takerFee: BigNumber; expirationTimeSeconds: BigNumber; salt: BigNumber; } |