* add LibERC721Order.sol * Add ERC721 interface to vendor/ * Add ERC721OrdersFeature interface * Storage lib for ERC721 orders feature * Implement basic functionality for ERC721 orders (buy, sell, cancel, etc) * Add isValidERC721OrderSignature to interface * implement onERC721Received * Implement batchBuyERC721s * left/right orders -> sell/buy orders * Add missing @return comments * Implement matching functions * Use SafeMath where necessary * add rich errors for ERC721OrdersFeature * Add comments * Add presign support for ERC721 orders * Cancel using just the order nonce * Add IERC721OrdersFeature to IZeroEx * Add taker callback * Assembly optimizations in LibERC721Order * Add ERC721Orders TS class * create zero-ex/contracts/test/integration/ and tokens/ directories * TestMintableERC721Token * tmp * address feedback from original PR (#391) * address feedback from original PR * Update contracts/zero-ex/contracts/src/features/ERC721OrdersFeature.sol Co-authored-by: Kim Persson <kimpers@users.noreply.github.com> * address review feedback and improve order parameter naming * Add batchCancel function * Emit order fields in preSign * Fix tests Co-authored-by: Lawrence Forman <me@merklejerk.com> Co-authored-by: Kim Persson <kimpers@users.noreply.github.com> Co-authored-by: Michael Zhu <mchl.zhu.96@gmail.com> * Remove revertIfIncomplete from batchMatch * Sanity check maker address in preSign * ERC1155OrdersFeature contracts * Commence refactor, abstract base contract * ERC721OrdersFeature inherits from NFTOrders * Refactor ERC1155OrdersFeature to inherit from NFTOrders * Fix order hashing * Fix ERC721OrdersFeature tests * Typos * Remove maker address from preSigned mapping * disable dex sampler tests * Refactor TS tooling * Address PR feedback * Rearrange event fields to better align with struct fields * Update comments * update AbiEncoder.create params * Add ERC1155Order to protocol-utils * Add ERC1155OrdersFeeature tests * Bump package versions and regenerate contract wrappers * Add ERC165Feature * NFT orders: address audit findings (#417) * CVF-1: use pragma solidity ^0.6 instead of ^0.6.5 * CVF-11: fix inaccurate comment * CVF-16: Enable taker callbacks for batchBuyERC1155s * CVF-17: use internal call if revertIfIncomplete is true * CVF-21: avoid duplicate SLOAD * CVF-23: merge if statements * CVF-24: Reorder status checks to be consistent with ERC721OrdersFeature * CVF-25: Update unclear comment (canonical hash -> EIP-712 hash) * CVF-31: Document keys of orderState mapping * CVF-45: DRY up fees/properties hashing * CVF-47, CVF-50, CVF-57: calculate properties.length once; hash propertyStructHashArray in-place using assembly * CVF-56: More descriptive names for assembly variables * CVF-71: Update confusing comment about rounding in _payFees * CVF-72: Move ETH assertions outside of loop in _payFees * CVF-74: Move property validation loop to else branch * CVF-82: Update inaccurate comment * CVF-86: Enable taker callbacks for batchBuyERC721s * CVF-87: use internal call if revertIfIncomplete is true * CVF-89: Perform token mismatch checks before stateful operations * CVF-90, CVF-91: Defer ERC20 token mismatch check * CVF-93: Add inline comments for _payFees parameters in matchERC721Orders * CVF-94: Fix comment (Step 7 -> Step 5) * CVF-98: Use binary & operator instead of mod * CVF-99: Update unclear comment (canonical hash -> EIP-712 hash) * CVF-65, CVF-66, CVF-67: Copy params.ethAvailable into local variable; check that ethSpent does not exceed ethAvailable; remove ethAvailable < erc20FillAmount check * CVF-52, CVF-55, CVF-59: calculate fees.length once; hash feeStructHashArray in-place using assembly * CVF-14, CVF-32: OrderState struct; separate storage mapping for 1155 cancellations so orders can be cancelled by nonce * Update changelogs, IZeroEx artifact/wrapper Co-authored-by: Lawrence Forman <lawrence@0xproject.com> Co-authored-by: Lawrence Forman <me@merklejerk.com> Co-authored-by: Kim Persson <kimpers@users.noreply.github.com>
144 lines
4.7 KiB
Solidity
144 lines
4.7 KiB
Solidity
// SPDX-License-Identifier: Apache-2.0
|
|
/*
|
|
|
|
Copyright 2020 ZeroEx Intl.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
pragma solidity ^0.6.5;
|
|
pragma experimental ABIEncoderV2;
|
|
|
|
import "@0x/contracts-utils/contracts/src/v06/LibBytesV06.sol";
|
|
import "@0x/contracts-utils/contracts/src/v06/LibMathV06.sol";
|
|
import "@0x/contracts-utils/contracts/src/v06/LibSafeMathV06.sol";
|
|
import "./tokens/TestMintableERC20Token.sol";
|
|
import "../src/features/libs/LibNativeOrder.sol";
|
|
import "../src/features/libs/LibSignature.sol";
|
|
|
|
|
|
contract TestFillQuoteTransformerExchange {
|
|
|
|
bytes32 public constant EIP712_EXCHANGE_DOMAIN_HASH = 0xaa81d881b1adbbf115e15b849cb9cdc643cad3c6a90f30eb505954af943247e6;
|
|
uint256 private constant REVERT_AMOUNT = 0xdeadbeef;
|
|
uint256 private constant PROTOCOL_FEE_MULTIPLIER = 1337;
|
|
|
|
using LibSafeMathV06 for uint256;
|
|
|
|
function fillLimitOrder(
|
|
LibNativeOrder.LimitOrder calldata order,
|
|
LibSignature.Signature calldata signature,
|
|
uint128 takerTokenFillAmount
|
|
)
|
|
external
|
|
payable
|
|
returns (uint128 takerTokenFilledAmount, uint128 makerTokenFilledAmount)
|
|
{
|
|
// The r field of the signature is the pre-filled amount.
|
|
uint128 takerTokenPreFilledAmount = uint128(uint256(signature.r));
|
|
if (REVERT_AMOUNT == takerTokenPreFilledAmount) {
|
|
revert("REVERT_AMOUNT");
|
|
}
|
|
if (takerTokenPreFilledAmount >= order.takerAmount) {
|
|
revert('FILLED');
|
|
}
|
|
uint256 protocolFee = PROTOCOL_FEE_MULTIPLIER * tx.gasprice;
|
|
// Return excess protocol fee.
|
|
msg.sender.transfer(msg.value - protocolFee);
|
|
takerTokenFilledAmount = LibSafeMathV06.min128(
|
|
order.takerAmount - takerTokenPreFilledAmount,
|
|
takerTokenFillAmount
|
|
);
|
|
|
|
// Take taker tokens.
|
|
order.takerToken.transferFrom(
|
|
msg.sender,
|
|
order.maker,
|
|
takerTokenFilledAmount
|
|
);
|
|
|
|
// Mint maker tokens.
|
|
makerTokenFilledAmount = LibSafeMathV06.safeDowncastToUint128(
|
|
uint256(takerTokenFilledAmount)
|
|
* uint256(order.makerAmount)
|
|
/ uint256(order.takerAmount)
|
|
);
|
|
TestMintableERC20Token(address(order.makerToken))
|
|
.mint(msg.sender, makerTokenFilledAmount);
|
|
|
|
// Take taker token fee.
|
|
uint128 takerFee = LibSafeMathV06.safeDowncastToUint128(
|
|
uint256(takerTokenFilledAmount)
|
|
* uint256(order.takerTokenFeeAmount)
|
|
/ uint256(order.takerAmount)
|
|
);
|
|
order.takerToken.transferFrom(msg.sender, order.feeRecipient, takerFee);
|
|
}
|
|
|
|
function fillRfqOrder(
|
|
LibNativeOrder.RfqOrder calldata order,
|
|
LibSignature.Signature calldata signature,
|
|
uint128 takerTokenFillAmount
|
|
)
|
|
external
|
|
payable
|
|
returns (uint128 takerTokenFilledAmount, uint128 makerTokenFilledAmount)
|
|
{
|
|
// The r field of the signature is the pre-filled amount.
|
|
uint128 takerTokenPreFilledAmount = uint128(uint256(signature.r));
|
|
if (REVERT_AMOUNT == takerTokenPreFilledAmount) {
|
|
revert("REVERT_AMOUNT");
|
|
}
|
|
if (takerTokenPreFilledAmount >= order.takerAmount) {
|
|
revert('FILLED');
|
|
}
|
|
takerTokenFilledAmount = LibSafeMathV06.min128(
|
|
order.takerAmount - takerTokenPreFilledAmount,
|
|
takerTokenFillAmount
|
|
);
|
|
|
|
// Take taker tokens.
|
|
order.takerToken.transferFrom(
|
|
msg.sender,
|
|
order.maker,
|
|
takerTokenFilledAmount
|
|
);
|
|
|
|
// Mint maker tokens.
|
|
makerTokenFilledAmount = LibSafeMathV06.safeDowncastToUint128(
|
|
uint256(takerTokenFilledAmount)
|
|
* uint256(order.makerAmount)
|
|
/ uint256(order.takerAmount)
|
|
);
|
|
TestMintableERC20Token(address(order.makerToken))
|
|
.mint(msg.sender, makerTokenFilledAmount);
|
|
}
|
|
|
|
function getProtocolFeeMultiplier()
|
|
external
|
|
pure
|
|
returns (uint256)
|
|
{
|
|
return PROTOCOL_FEE_MULTIPLIER;
|
|
}
|
|
|
|
function getLimitOrderHash(LibNativeOrder.LimitOrder calldata order)
|
|
external
|
|
pure
|
|
returns (bytes32)
|
|
{
|
|
return bytes32(order.salt);
|
|
}
|
|
}
|