Skip to content

Commit

Permalink
Merge pull request #827 from TrooperCrypto/feat/contract-updates
Browse files Browse the repository at this point in the history
remove fees, add order hash to swap
  • Loading branch information
0xtrooper authored Mar 25, 2023
2 parents 1498574 + 6550664 commit 4dda6d1
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 1,140 deletions.
74 changes: 11 additions & 63 deletions evm_contracts/contracts/ZigZagExchange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ contract ZigZagExchange is EIP712 {
address indexed makerSellToken,
address indexed takerSellToken,
uint256 makerSellAmount,
uint256 takerSellAmount,
uint256 makerVolumeFee,
uint256 takerVolumeFee
uint256 takerSellAmount
);

event CancelOrder(bytes32 indexed orderHash);
Expand All @@ -35,20 +33,12 @@ contract ZigZagExchange is EIP712 {

mapping(bytes32 => bool) public cancelled;

// fees
address immutable FEE_ADDRESS;
address immutable WETH_ADDRESS;
address immutable EXCHANGE_ADDRESS;
address constant ETH_ADDRESS = address(0);

uint256 maker_fee_numerator = 0;
uint256 maker_fee_denominator = 10000;
uint256 taker_fee_numerator = 5;
uint256 taker_fee_denominator = 10000;

// initialize fee address
constructor(string memory name, string memory version, address fee_address, address weth_address) EIP712(name, version) {
FEE_ADDRESS = fee_address;
constructor(string memory name, string memory version, address weth_address) EIP712(name, version) {
WETH_ADDRESS = weth_address;
EXCHANGE_ADDRESS = address(this);
}
Expand Down Expand Up @@ -102,9 +92,6 @@ contract ZigZagExchange is EIP712 {
fillAvailable
);
}

// adjust the takerAmountOut by the tx fee paid by the taker
takerAmount = takerAmount - (takerAmount * taker_fee_numerator) / taker_fee_denominator;
}

_refundETH();
Expand Down Expand Up @@ -141,9 +128,6 @@ contract ZigZagExchange is EIP712 {
takerAmount,
fillAvailable
);

// adjust the takerAmountOut by the tx fee paid by the taker
takerAmount = takerAmount - (takerAmount * taker_fee_numerator) / taker_fee_denominator;
}

return true;
Expand Down Expand Up @@ -178,8 +162,6 @@ contract ZigZagExchange is EIP712 {
uint takerBuyAmount,
bool fillAvailable
) public payable returns (bool) {
// add the takerFee to the buy amount to recive the exact amount after fees
takerBuyAmount = (takerBuyAmount * taker_fee_denominator) / (taker_fee_denominator - taker_fee_numerator);
_fillOrderETH(makerOrder, makerSignature, msg.sender, msg.sender, takerBuyAmount, fillAvailable);
_refundETH();
return true;
Expand Down Expand Up @@ -239,8 +221,6 @@ contract ZigZagExchange is EIP712 {
uint takerBuyAmount,
bool fillAvailable
) public returns (bool) {
// add the takerFee to the buy amount to recive the exact amount after fees
takerBuyAmount = (takerBuyAmount * taker_fee_denominator) / (taker_fee_denominator - taker_fee_numerator);
_fillOrder(
makerOrder,
makerSignature,
Expand Down Expand Up @@ -283,22 +263,14 @@ contract ZigZagExchange is EIP712 {
uint makerOrderFilled = makerOrderInfo.orderSellFilledAmount + takerBuyAmountAdjusted;
filled[makerOrderInfo.orderHash] = makerOrderFilled;

// The fee gets subtracted from the buy amounts so they deduct from the total instead of adding on to it
// The maker fee comes out of the taker sell quantity, so the maker ends up with less
// The taker fee comes out of the maker sell quantity, so the taker ends up with less
// makerFee = (takerSellAmount * maker_fee_numerator) / maker_fee_denominator
// takerFee = (takerBuyAmountAdjusted * taker_fee_numerator) / taker_fee_denominator

_settleMatchedOrders(
makerOrder.user,
taker,
takerReciver,
sellToken,
buyToken,
takerBuyAmountAdjusted,
takerSellAmount,
(takerSellAmount * maker_fee_numerator) / maker_fee_denominator,
(takerBuyAmountAdjusted * taker_fee_numerator) / taker_fee_denominator
takerSellAmount
);

emit OrderStatus(makerOrderInfo.orderHash, makerOrderFilled, makerOrder.sellAmount - makerOrderFilled);
Expand All @@ -311,9 +283,7 @@ contract ZigZagExchange is EIP712 {
address makerSellToken,
address takerSellToken,
uint makerSellAmount,
uint takerSellAmount,
uint makerFee,
uint takerFee
uint takerSellAmount
) internal {
if (takerSellToken == ETH_ADDRESS) {
require(msg.value >= takerSellAmount, 'msg value not high enough');
Expand All @@ -330,46 +300,24 @@ contract ZigZagExchange is EIP712 {
require(IERC20(makerSellToken).allowance(maker, EXCHANGE_ADDRESS) >= makerSellAmount, 'maker order not enough allowance');
}

// Taker fee -> fee recipient
// taker fee is collected in takerBuyToken
if (takerFee > 0) {
if (makerSellToken == ETH_ADDRESS) {
IERC20(WETH_ADDRESS).transferFrom(maker, FEE_ADDRESS, takerFee);
} else {
IERC20(makerSellToken).transferFrom(maker, FEE_ADDRESS, takerFee);
}
}

// Maker fee -> fee recipient
// Maker fee is collected in makerBuyToken
if (makerFee > 0) {
if (takerSellToken == ETH_ADDRESS) {
IWETH9(WETH_ADDRESS).depositTo{ value: makerFee }(FEE_ADDRESS);
} else if (taker == EXCHANGE_ADDRESS) {
IERC20(takerSellToken).transfer(FEE_ADDRESS, makerFee);
} else {
IERC20(takerSellToken).transferFrom(taker, FEE_ADDRESS, makerFee);
}
}

// taker -> maker
if (takerSellToken == ETH_ADDRESS) {
IWETH9(WETH_ADDRESS).depositTo{ value: takerSellAmount - makerFee }(maker);
IWETH9(WETH_ADDRESS).depositTo{ value: takerSellAmount }(maker);
} else if (taker == EXCHANGE_ADDRESS) {
IERC20(takerSellToken).transfer(maker, takerSellAmount - makerFee);
IERC20(takerSellToken).transfer(maker, takerSellAmount);
} else {
IERC20(takerSellToken).transferFrom(taker, maker, takerSellAmount - makerFee);
IERC20(takerSellToken).transferFrom(taker, maker, takerSellAmount);
}

// maker -> taker
if (makerSellToken == ETH_ADDRESS) {
IERC20(WETH_ADDRESS).transferFrom(maker, EXCHANGE_ADDRESS, makerSellAmount - takerFee);
IWETH9(WETH_ADDRESS).withdrawTo(takerReciver, makerSellAmount - takerFee);
IERC20(WETH_ADDRESS).transferFrom(maker, EXCHANGE_ADDRESS, makerSellAmount);
IWETH9(WETH_ADDRESS).withdrawTo(takerReciver, makerSellAmount);
} else {
IERC20(makerSellToken).transferFrom(maker, takerReciver, makerSellAmount - takerFee);
IERC20(makerSellToken).transferFrom(maker, takerReciver, makerSellAmount);
}

emit Swap(maker, taker, makerSellToken, takerSellToken, makerSellAmount, takerSellAmount, makerFee, takerFee);
emit Swap(maker, taker, makerSellToken, takerSellToken, makerSellAmount, takerSellAmount);
}

function getOpenOrder(LibOrder.Order calldata order) public view returns (LibOrder.OrderInfo memory orderInfo) {
Expand Down
3 changes: 1 addition & 2 deletions evm_contracts/scripts/deployExchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ const hre = require('hardhat')

async function main() {
const Exchange = await hre.ethers.getContractFactory('ZigZagExchange')
const fee_address = "0xF4BBA1e2a5024a2754225b981e9A0DB7d2c33EE9";
const weth_address = "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1";
const exchange = await Exchange.deploy("ZigZag", "2.1", fee_address, weth_address);
const exchange = await Exchange.deploy("ZigZag", "2.1", weth_address);

await exchange.deployed()

Expand Down
92 changes: 0 additions & 92 deletions evm_contracts/test/BTCBridge.ts

This file was deleted.

Loading

0 comments on commit 4dda6d1

Please sign in to comment.