Skip to content

Commit 179bb5d

Browse files
author
yakuhito
committed
update to support tokens
1 parent 1490b08 commit 179bb5d

File tree

4 files changed

+155
-77
lines changed

4 files changed

+155
-77
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
# yakuSwap-eth
22

3+
Gas consumption (estimate):
4+
5+
```
6+
·------------------------------|---------------------------|-------------|-----------------------------·
7+
| Solc version: 0.8.7 · Optimizer enabled: true · Runs: 200 · Block limit: 30000000 gas │
8+
·······························|···························|·············|······························
9+
| Methods · 100 gwei/gas · 3417.01 usd/eth │
10+
··············|················|·············|·············|·············|···············|··············
11+
| Contract · Method · Min · Max · Avg · # calls · usd (avg) │
12+
··············|················|·············|·············|·············|···············|··············
13+
| TestToken · approve · 46239 · 46251 · 46250 · 12 · 15.80 │
14+
··············|················|·············|·············|·············|···············|··············
15+
| YakuSwap · cancelSwap · - · - · 40105 · 2 · 13.70 │
16+
··············|················|·············|·············|·············|···············|··············
17+
| YakuSwap · completeSwap · 86298 · 86310 · 86306 · 6 · 29.49 │
18+
··············|················|·············|·············|·············|···············|··············
19+
| YakuSwap · createSwap · 84508 · 84520 · 84519 · 24 · 28.88 │
20+
··············|················|·············|·············|·············|···············|··············
21+
| YakuSwap · withdrawFees · 36483 · 52383 · 47083 · 3 · 16.09 │
22+
··············|················|·············|·············|·············|···············|··············
23+
| Deployments · · % of limit · │
24+
·······························|·············|·············|·············|···············|··············
25+
| TestToken · - · - · 641308 · 2.1 % · 219.14 │
26+
·······························|·············|·············|·············|···············|··············
27+
| YakuSwap · - · - · 902396 · 3 % · 308.35 │
28+
·------------------------------|-------------|-------------|-------------|---------------|-------------·
29+
```
30+
331
License
432
=======
533
Copyright 2021 Mihai Dancaescu

contracts/TestToken.sol

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//SPDX-License-Identifier: Apache-2.0
2+
pragma solidity ^0.8.0;
3+
4+
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
5+
6+
contract TestToken is ERC20 {
7+
constructor(address initialAccount, uint initialBalance) ERC20("TestToken", "TST") {
8+
_mint(initialAccount, initialBalance);
9+
}
10+
}

contracts/YakuSwap.sol

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
//SPDX-License-Identifier: MIT
1+
//SPDX-License-Identifier: Apache-2.0
22
pragma solidity ^0.8.0;
33

44
import "@openzeppelin/contracts/access/Ownable.sol";
55

6+
interface IERC20 {
7+
function transferFrom(address _from, address _to, uint256 _amount) external returns (bool);
8+
function transfer(address _to, uint256 _amount) external returns (bool);
9+
}
10+
611
contract YakuSwap is Ownable {
712

813
// Uninitialized - Default status (if swaps[index] doesn't exist, status will get this value)
@@ -12,32 +17,34 @@ contract YakuSwap is Ownable {
1217
enum SwapStatus {Uninitialized, Created, Completed, Cancelled}
1318

1419
mapping(bytes32 => SwapStatus) public swaps;
15-
16-
uint public totalFees = 0;
20+
mapping(address => uint) public totalFees;
1721

1822
uint constant public MAX_BLOCK_HEIGHT = 256;
1923

2024
event SwapCreated(
21-
bytes32 indexed swapHash,
25+
bytes32 swapHash,
26+
address indexed tokenAddress,
2227
address indexed fromAddress,
2328
address indexed toAddress,
24-
uint value,
29+
uint amount,
2530
bytes32 secretHash,
2631
uint blockNumber
2732
);
2833

2934
function _getSwapHash(
35+
address tokenAddress,
3036
address fromAddress,
3137
address toAddress,
32-
uint value,
38+
uint amount,
3339
bytes32 secretHash,
3440
uint blockNumber
3541
) internal view returns (bytes32) {
3642
return keccak256(
3743
abi.encode(
44+
tokenAddress,
3845
fromAddress,
3946
toAddress,
40-
value,
47+
amount,
4148
secretHash,
4249
blockNumber,
4350
block.chainid
@@ -46,53 +53,60 @@ contract YakuSwap is Ownable {
4653
}
4754

4855
function getSwapHash(
56+
address tokenAddress,
4957
address fromAddress,
5058
address toAddress,
51-
uint value,
59+
uint amount,
5260
bytes32 secretHash,
5361
uint blockNumber
5462
) external view returns (bytes32) {
5563
return _getSwapHash(
56-
fromAddress, toAddress, value, secretHash, blockNumber
64+
tokenAddress, fromAddress, toAddress, amount, secretHash, blockNumber
5765
);
5866
}
5967

60-
function createSwap(address toAddress, bytes32 secretHash) payable external {
68+
function createSwap(address tokenAddress, address toAddress, uint amount, bytes32 secretHash) external {
6169
require(toAddress != address(0), "Destination address cannot be zero");
70+
require(tokenAddress != address(0), "Token contract address cannot be zero");
6271

6372
bytes32 swapHash = _getSwapHash(
73+
tokenAddress,
6474
msg.sender,
6575
toAddress,
66-
msg.value,
76+
amount,
6777
secretHash,
6878
block.number
6979
);
7080

71-
require(swaps[swapHash] == SwapStatus.Uninitialized);
72-
swaps[swapHash] = SwapStatus.Created;
81+
require(swaps[swapHash] == SwapStatus.Uninitialized, "Invalid swap status");
82+
require(IERC20(tokenAddress).transferFrom(msg.sender, address(this), amount), "Could not transfer tokens");
7383

84+
swaps[swapHash] = SwapStatus.Created;
7485
emit SwapCreated(
7586
swapHash,
87+
tokenAddress,
7688
msg.sender,
7789
toAddress,
78-
msg.value,
90+
amount,
7991
secretHash,
8092
block.number
8193
);
8294
}
8395

8496
function completeSwap(
97+
address tokenAddress,
8598
address fromAddress,
8699
address toAddress,
87-
uint value,
100+
uint amount,
88101
uint blockNumber,
89102
string memory secret
90103
) external {
91104
bytes32 secretHash = sha256(abi.encodePacked(secret));
92105
bytes32 swapHash = _getSwapHash(
106+
tokenAddress,
93107
fromAddress,
94108
toAddress,
95-
value,
109+
amount,
96110
secretHash,
97111
blockNumber
98112
);
@@ -101,23 +115,24 @@ contract YakuSwap is Ownable {
101115
require(block.number < blockNumber + MAX_BLOCK_HEIGHT, "Deadline exceeded");
102116
swaps[swapHash] = SwapStatus.Completed;
103117

104-
uint swapAmount = value * 993 / 1000;
105-
totalFees += value - swapAmount;
118+
uint swapAmount = amount * 993 / 1000;
119+
totalFees[tokenAddress] += amount - swapAmount;
106120

107-
(bool success,) = toAddress.call{value : swapAmount}("");
108-
require(success, "Transfer failed");
121+
require(IERC20(tokenAddress).transfer(toAddress, swapAmount), "Transfer failed");
109122
}
110123

111124
function cancelSwap(
125+
address tokenAddress,
112126
address toAddress,
113-
uint value,
127+
uint amount,
114128
bytes32 secretHash,
115129
uint blockNumber
116130
) public {
117131
bytes32 swapHash = _getSwapHash(
132+
tokenAddress,
118133
msg.sender,
119134
toAddress,
120-
value,
135+
amount,
121136
secretHash,
122137
blockNumber
123138
);
@@ -126,15 +141,13 @@ contract YakuSwap is Ownable {
126141
require(block.number >= blockNumber + MAX_BLOCK_HEIGHT, "MAX_BLOCK_HEIGHT not exceeded");
127142

128143
swaps[swapHash] = SwapStatus.Cancelled;
129-
(bool success,) = msg.sender.call{value : value}("");
130-
require(success, "Transfer failed");
144+
require(IERC20(tokenAddress).transfer(msg.sender, amount), "Transfer failed");
131145
}
132146

133-
function withdrawFees() external onlyOwner {
134-
uint feesToWithdraw = totalFees;
135-
totalFees = 0;
147+
function withdrawFees(address tokenAddress) external onlyOwner {
148+
uint feesToWithdraw = totalFees[tokenAddress];
149+
totalFees[tokenAddress] = 0;
136150

137-
(bool success,) = owner().call{value : feesToWithdraw}("");
138-
require(success, "Transfer failed");
151+
require(IERC20(tokenAddress).transfer(msg.sender, feesToWithdraw), "Transfer failed");
139152
}
140153
}

0 commit comments

Comments
 (0)