Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .solcover.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
skipFiles: [
'Migrations.sol',
'test',
'external/LibBytes.sol',
'external',
'core/lib/ERC20Wrapper.sol' // TODO: Find a cleaner way to test state mutating functions, we will skip
],
};
107 changes: 107 additions & 0 deletions contracts/core/exchange-wrappers/ZeroExExchangeWrapper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
Copyright 2018 Set Labs Inc.

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.4.24;
pragma experimental "ABIEncoderV2";

import { SafeMath } from "zeppelin-solidity/contracts/math/SafeMath.sol";
import { ZeroExOrderDataHandler as OrderHandler } from "./lib/ZeroExOrderDataHandler.sol";
import { LibBytes } from "../../external/LibBytes.sol";
import { LibOrder as ZeroExOrder } from "../../external/0x/Exchange/libs/LibOrder.sol";
import { LibFillResults as ZeroExFillResults } from "../../external/0x/Exchange/libs/LibFillResults.sol";
import { IExchange as ZeroExExchange } from "../../external/0x/Exchange/interfaces/IExchange.sol";
import { ERC20Wrapper as ERC20 } from "../../core/lib/ERC20Wrapper.sol";



/**
* @title ZeroExExchangeWrapper
* @author Set Protocol
*
* The ZeroExExchangeWrapper contract wrapper to interface with 0x V2
*/
contract ZeroExExchangeWrapper
{
using SafeMath for uint256;

/* ============ State Variables ============ */

address public ZERO_EX_EXCHANGE;
address public ZERO_EX_PROXY;
address public SET_PROXY;


/* ============ Constructor ============ */

constructor(
address _zeroExExchange,
address _zeroExProxy,
address _setProxy
)
public
{
ZERO_EX_EXCHANGE = _zeroExExchange;
ZERO_EX_PROXY = _zeroExProxy;
SET_PROXY = _setProxy;
}


/* ============ Public Functions ============ */


// The purpose of this function is to decode the order data and execute the trade
// TODO - We are currently assuming no taker fee. Add in taker fee going forward
function exchange(
address _tradeOriginator,
bytes _orderData
)
external
// returns (uint256)
{
// Loop through order data and perform each order

// Approve the taker token for transfer to the Set Vault


// return 1;
}

/* ============ Getters ============ */

/* ============ Private ============ */
function fillZeroExOrder(
bytes _zeroExOrderData
)
private
returns (ZeroExFillResults.FillResults memory)
{
uint256 fillAmount = OrderHandler.parseFillAmount(_zeroExOrderData);
bytes memory signature = OrderHandler.sliceSignature(_zeroExOrderData);
ZeroExOrder.Order memory order = OrderHandler.parseZeroExOrder(_zeroExOrderData);

// Ensure the maker token is allowed to be approved to the ZeroEx proxy


ZeroExFillResults.FillResults memory fillResults =
ZeroExExchange(ZERO_EX_EXCHANGE).fillOrKillOrder(
order,
fillAmount,
signature
);

// Ensure the taker token is allowed to be approved to the TransferProxy
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pragma experimental "ABIEncoderV2";

import { SafeMath } from "zeppelin-solidity/contracts/math/SafeMath.sol";
import { LibBytes } from "../../../external/LibBytes.sol";
import { LibOrder } from "../../../external/0x/Exchange/libs/LibOrder.sol";


/**
Expand All @@ -33,28 +34,18 @@ library ZeroExOrderDataHandler {

// ============ Structs ============

struct Order {
address makerAddress; // Address that created the order.
address takerAddress; // Address that is allowed to fill the order.
address feeRecipientAddress; // Address that will recieve fees when order is filled.
address senderAddress; // Address that is allowed to call Exchange contract.
uint256 makerAssetAmount; // Amount of makerAsset being offered by maker.
uint256 takerAssetAmount; // Amount of takerAsset being bid on by maker.
uint256 makerFee; // Amount of ZRX paid to feeRecipient by maker
uint256 takerFee; // Amount of ZRX paid to feeRecipient by taker
uint256 expirationTimeSeconds; // Timestamp in seconds at which order expires.
uint256 salt; // Number to facilitate uniqueness of the order's hash.
bytes makerAssetData; // Encoded data when transferring makerAsset.
bytes takerAssetData; // Encoded data when transferring takerAsset.
}

struct ZeroExHeader {
uint256 signatureLength;
uint256 orderLength;
uint256 makerAssetDataLength;
uint256 takerAssetDataLength;
}

struct AssetDataAddresses {
address makerTokenAddress;
address takerTokenAddress;
}

// ============ Internal Functions ============

// We construct the following to allow calling fillOrder on ZeroEx V2 Exchange
Expand Down Expand Up @@ -111,12 +102,18 @@ library ZeroExOrderDataHandler {
return fillAmount;
}

function sliceSignature(bytes _orderData, uint _signatureLength)
function sliceSignature(bytes _orderData)
internal
pure
returns (bytes)
{
bytes memory signature = _orderData.slice(160, _signatureLength.add(160));
uint256 orderDataAddr = _orderData.contentAddress();
uint256 signatureLength;
assembly {
signatureLength := mload(orderDataAddr)
}

bytes memory signature = _orderData.slice(160, signatureLength.add(160));
return signature;
}

Expand All @@ -134,16 +131,16 @@ library ZeroExOrderDataHandler {
return order;
}

function parseZeroExOrder(
function constructZeroExOrder(
bytes _zeroExOrder,
uint _makerAssetDataLength,
uint _takerAssetDataLength
)
internal
pure
returns (Order memory)
returns (LibOrder.Order memory)
{
Order memory order;
LibOrder.Order memory order;
uint256 orderDataAddr = _zeroExOrder.contentAddress();

// | Data | Location | Length |
Expand Down Expand Up @@ -184,19 +181,35 @@ library ZeroExOrderDataHandler {
return order;
}

function parseZeroExOrderData(bytes _orderData)
function parseZeroExOrder(bytes _orderData)
internal
pure
returns(Order memory)
returns(LibOrder.Order memory)
{
ZeroExHeader memory header = parseOrderHeader(_orderData);

Order memory order = parseZeroExOrder(
LibOrder.Order memory order = constructZeroExOrder(
sliceZeroExOrder(_orderData, header.signatureLength, header.orderLength),
header.makerAssetDataLength,
header.takerAssetDataLength
);

return order;
}

function parseERC20TokenAddress(bytes _assetData)
internal
pure
returns(address)
{
bytes4 ERC20_SELECTOR = bytes4(keccak256("ERC20Token(address)"));
// Ensure that the asset is ERC20
bytes4 orderProxyId = _assetData.readBytes4(0);

require(ERC20_SELECTOR == orderProxyId);

address tokenAddress = address(_assetData.readBytes32(4));

return tokenAddress;
}
}
85 changes: 0 additions & 85 deletions contracts/core/external/ZeroExExchangeWrapper.sol

This file was deleted.

18 changes: 18 additions & 0 deletions contracts/core/lib/ERC20Wrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
pragma solidity 0.4.24;

import { IERC20 } from "../../lib/IERC20.sol";
import { CommonMath } from "../../lib/CommonMath.sol";


/**
Expand Down Expand Up @@ -106,6 +107,23 @@ library ERC20Wrapper {
);
}

function ensureAllowance(
address _token,
address _owner,
address _spender,
uint256 _quantity
)
private
{
if (allowance(_token, _owner, _spender) < _quantity) {
approve(
_token,
_spender,
CommonMath.maxUInt256()
);
}
}

// ============ Private Functions ============

/**
Expand Down
36 changes: 36 additions & 0 deletions contracts/external/0x/AssetProxy/interfaces/IAssetData.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*

Copyright 2018 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.4.23;

// @dev Interface of the asset proxy's assetData.
// The asset proxies take an ABI encoded `bytes assetData` as argument.
// This argument is ABI encoded as one of the methods of this interface.
interface IAssetData {

function ERC20Token(
address tokenContract)
external pure;

function ERC721Token(
address tokenContract,
uint256 tokenId,
bytes receiverData)
external pure;

}
Loading