Skip to content
This repository was archived by the owner on Sep 20, 2019. It is now read-only.
Open
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
12 changes: 1 addition & 11 deletions contracts/Exchange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ contract Exchange is Ownable, ExchangeInterface {

address constant public ETH = 0x0;

uint256 constant public MAX_FEE = 5000000000000000; // 0.5% ((0.5 / 100) * 10**18)

ExchangeLibrary.Exchange public exchange;

function Exchange(uint _takerFee, address _feeAccount, VaultInterface _vault) public {
function Exchange(address _feeAccount, VaultInterface _vault) public {
require(address(_vault) != 0x0);
setFees(_takerFee);
setFeeAccount(_feeAccount);
exchange.vault = _vault;
}
Expand Down Expand Up @@ -144,13 +141,6 @@ contract Exchange is Ownable, ExchangeInterface {
return exchange.fills[hash];
}

/// @dev Sets the taker fee.
/// @param _takerFee New taker fee.
function setFees(uint _takerFee) public onlyOwner {
require(_takerFee <= MAX_FEE);
exchange.takerFee = _takerFee;
}

/// @dev Sets the account where fees will be transferred to.
/// @param _feeAccount Address for the account.
function setFeeAccount(address _feeAccount) public onlyOwner {
Expand Down
7 changes: 7 additions & 0 deletions contracts/Fees/FeeInterface.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pragma solidity ^0.4.18;

interface FeeInterface {

function fees(address user) external view returns (uint);

}
25 changes: 25 additions & 0 deletions contracts/Fees/FeeManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pragma solidity ^0.4.18;

import "./FeeInterface.sol";
import "./../Ownership/Ownable.sol";

contract FeeManager is Ownable, FeeInterface {

uint256 constant public MAX_FEE = 5000000000000000; // 0.5% ((0.5 / 100) * 10**18)

mapping (uint => uint) public tiers;
mapping (address => uint) public level;

function setTier(uint tier, uint fee) external onlyOwner {
require(fee <= MAX_FEE);
tiers[tier] = fee;
}

function setUserLevel(address user, uint tier) external onlyOwner {
level[user] = tier;
}

function fees(address user) external view returns (uint) {
return tiers[level[user]];
}
}
20 changes: 18 additions & 2 deletions contracts/Libraries/ExchangeLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import "./SafeMath.sol";
import "@dexyproject/signature-validator/contracts/SignatureValidator.sol";
import "./../Vault/VaultInterface.sol";
import "./../HookSubscriber.sol";
import "./../Fees/FeeInterface.sol";

library ExchangeLibrary {

using SafeMath for *;
using OrderLibrary for OrderLibrary.Order;
using ExchangeLibrary for ExchangeLibrary.Exchange;

event Traded(
bytes32 indexed hash,
Expand All @@ -23,7 +25,7 @@ library ExchangeLibrary {

struct Exchange {
VaultInterface vault;
uint takerFee;
FeeInterface feeManager;
address feeAccount;
mapping (address => mapping (bytes32 => bool)) orders;
mapping (bytes32 => uint) fills;
Expand Down Expand Up @@ -61,7 +63,7 @@ library ExchangeLibrary {
require(self.vault.balanceOf(order.takerToken, taker) >= fillAmount);

uint makeAmount = order.makerTokenAmount.mul(fillAmount).div(order.takerTokenAmount);
uint tradeTakerFee = makeAmount.mul(self.takerFee).div(1 ether);
uint tradeTakerFee = self.calculateFee(makeAmount, taker);

if (tradeTakerFee > 0) {
self.vault.transfer(order.makerToken, order.maker, self.feeAccount, tradeTakerFee);
Expand Down Expand Up @@ -165,4 +167,18 @@ library ExchangeLibrary {

return remainder.mul(1000000).div(numerator.mul(target));
}

/// @dev Returns the fee for a specific amount.
/// @param self Exchange storage.
/// @param takeAmount Amount of order to be taken.
/// @param taker Address of the taker.
/// @return Fee amount.
function calculateFee(Exchange storage self, uint takeAmount, address taker) internal view returns (uint) {
uint feeAmount = self.feeManager.fees(taker);
if (feeAmount == 0) {
return 0;
}

return takeAmount.mul(feeAmount).div(1 ether);
}
}
2 changes: 1 addition & 1 deletion test/TestExchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ contract('Exchange', function (accounts) {
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545/"));

vault = await Vault.new(erc820Registry.$address);
exchange = await Exchange.new(2500000000000000, feeAccount, vault.address);
exchange = await Exchange.new(feeAccount, vault.address);
await vault.addSpender(exchange.address)
});

Expand Down