This repository has been archived by the owner on Sep 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
swap.sol
77 lines (62 loc) · 2.47 KB
/
swap.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
pragma solidity 0.4.24;
contract ERC20 {
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
}
contract Exchange {
function fillOrder(address[5], uint[6], uint, bool, uint8, bytes32, bytes32) public returns (uint);
}
contract WETH {
function deposit() public payable;
function withdraw(uint) public;
}
contract HydroSwap {
address exchangeAddress;
address tokenProxyAddress;
address wethAddress;
uint256 constant MAX_UINT = 2 ** 256 - 1;
event LogSwapSuccess(bytes32 indexed id);
constructor(address _exchangeAddress, address _tokenProxyAddress, address _wethAddress) public {
exchangeAddress = _exchangeAddress;
tokenProxyAddress = _tokenProxyAddress;
wethAddress = _wethAddress;
}
function swap(
bytes32 id,
address[5] orderAddresses,
uint[6] orderValues,
uint8 v,
bytes32 r,
bytes32 s)
external
payable
returns (uint256 takerTokenFilledAmount)
{
address makerTokenAddress = orderAddresses[2];
address takerTokenAddress = orderAddresses[3];
uint makerTokenAmount = orderValues[0];
uint takerTokenAmount = orderValues[1];
if (takerTokenAddress == wethAddress) {
require(takerTokenAmount == msg.value, "WRONG_ETH_AMOUNT");
WETH(wethAddress).deposit.value(takerTokenAmount)();
} else {
require(ERC20(takerTokenAddress).transferFrom(msg.sender, this, takerTokenAmount), "TOKEN_TRANSFER_FROM_ERROR");
}
require(ERC20(takerTokenAddress).approve(tokenProxyAddress, takerTokenAmount), "TOKEN_APPROVE_ERROR");
require(
Exchange(exchangeAddress).fillOrder(orderAddresses, orderValues, takerTokenAmount, true, v, r, s) == takerTokenAmount,
"FILL_ORDER_ERROR"
);
if (makerTokenAddress == wethAddress) {
WETH(wethAddress).withdraw(makerTokenAmount);
msg.sender.transfer(makerTokenAmount);
} else {
require(ERC20(makerTokenAddress).transfer(msg.sender, makerTokenAmount), "TOKEN_TRANSFER_ERROR");
}
emit LogSwapSuccess(id);
return takerTokenAmount;
}
// Need payable fallback function to accept the WETH withdraw funds.
function() public payable {}
}