Skip to content

Commit 6e063fa

Browse files
author
yakuhito
committed
potential fix for front-runnable functions
1 parent b717af4 commit 6e063fa

2 files changed

Lines changed: 61 additions & 28 deletions

File tree

‎contracts/yakuSwap.sol‎

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,57 +15,75 @@ contract yakuSwap is Ownable {
1515
SwapStatus status;
1616
uint startBlock;
1717
uint amount;
18+
bytes32 secretHash;
1819
address fromAddress;
1920
address toAddress;
2021
uint16 maxBlockHeight;
2122
}
2223

23-
mapping (bytes32 => Swap) public swaps; // key = secretHash
24+
mapping (bytes32 => Swap) public swaps;
2425
uint public totalFees = 0;
2526

27+
function getSwapId(bytes32 secretHash, address fromAddress) public pure returns (bytes32) {
28+
return keccak256(abi.encodePacked(
29+
secretHash,
30+
fromAddress
31+
));
32+
}
33+
2634
function createSwap(bytes32 _secretHash, address _toAddress, uint16 _maxBlockHeight) payable public {
2735
require(msg.value >= 1000);
2836
require(_maxBlockHeight > 10);
29-
require(swaps[_secretHash].status == SwapStatus.Uninitialized);
3037
require(_toAddress != address(0) && msg.sender != address(0));
3138

39+
bytes32 swapId = getSwapId(_secretHash, msg.sender);
40+
require(swaps[swapId].status == SwapStatus.Uninitialized);
41+
3242
uint swapAmount = msg.value / 1000 * 993;
3343
Swap memory newSwap = Swap(
3444
SwapStatus.Created,
3545
block.number,
3646
swapAmount,
47+
_secretHash,
3748
msg.sender,
3849
_toAddress,
3950
_maxBlockHeight
4051
);
4152

42-
swaps[_secretHash] = newSwap;
53+
swaps[swapId] = newSwap;
4354
totalFees += msg.value - newSwap.amount;
4455
}
4556

46-
function completeSwap(bytes32 _secretHash, string memory _secret) public {
47-
Swap storage swap = swaps[_secretHash];
57+
function completeSwap(bytes32 _swapId, string memory _secret) public {
58+
Swap storage swap = swaps[_swapId];
4859

4960
require(swap.status == SwapStatus.Created);
5061
require(block.number < swap.startBlock + swap.maxBlockHeight);
51-
require(_secretHash == sha256(abi.encodePacked(_secret)));
62+
require(swap.secretHash == sha256(abi.encodePacked(_secret)));
5263

5364
swap.status = SwapStatus.Completed;
54-
payable(swap.toAddress).transfer(swap.amount);
65+
if(!payable(swap.toAddress).send(swap.amount)) {
66+
swap.status = SwapStatus.Created;
67+
}
5568
}
5669

57-
function cancelSwap(bytes32 _secretHash) public {
58-
Swap storage swap = swaps[_secretHash];
70+
function cancelSwap(bytes32 _swapId) public {
71+
Swap storage swap = swaps[_swapId];
5972

6073
require(swap.status == SwapStatus.Created);
6174
require(block.number >= swap.startBlock + swap.maxBlockHeight);
6275

6376
swap.status = SwapStatus.Cancelled;
64-
payable(swap.fromAddress).transfer(swap.amount);
77+
if(!payable(swap.fromAddress).send(swap.amount)) {
78+
swap.status = SwapStatus.Created;
79+
}
6580
}
6681

6782
function getFees() public onlyOwner {
83+
uint oldTotalFees = totalFees;
6884
totalFees = 0;
69-
payable(owner()).transfer(totalFees);
85+
if(!payable(owner()).send(totalFees)) {
86+
totalFees = oldTotalFees;
87+
}
7088
}
7189
}

‎test/test.js‎

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ describe("yakuSwap Contract", function () {
3737

3838
await transaction.wait();
3939

40-
const swap = await yakuSwap.connect(addr3).swaps(SECRET_HASH);
40+
const swapId = await yakuSwap.connect(addr3).getSwapId(SECRET_HASH, addr1.address);
41+
const swap = await yakuSwap.connect(addr3).swaps(swapId);
4142
const totalFees = await yakuSwap.connect(addr3).totalFees();
4243
expect(swap.status).to.equal(1);
4344
expect(swap.startBlock).to.equal(await ethers.provider.getBlockNumber());
4445
expect(swap.amount).to.equal(SWAP_AMOUNT_AFTER_FEE);
46+
expect(swap.secretHash).to.equal(SECRET_HASH);
4547
expect(swap.fromAddress).to.equal(addr1.address);
4648
expect(swap.toAddress).to.equal(addr2.address);
4749
expect(swap.maxBlockHeight).to.equal(MAX_BLOCK_HEIGHT);
@@ -64,15 +66,15 @@ describe("yakuSwap Contract", function () {
6466
).to.be.revertedWith("");
6567
});
6668

67-
it("Should fail if the same secretHash is used twice", async function () {
69+
it("Should fail if the same secretHash is used twice by the same address", async function () {
6870
const transaction = await yakuSwap.connect(addr1).createSwap(SECRET_HASH, addr2.address, MAX_BLOCK_HEIGHT, {
6971
value: SWAP_AMOUNT,
7072
});
7173

7274
await transaction.wait();
7375

7476
await expect(
75-
yakuSwap.connect(addr3).createSwap(SECRET_HASH, addrs[0].address, MAX_BLOCK_HEIGHT * 2, {
77+
yakuSwap.connect(addr1).createSwap(SECRET_HASH, addrs[0].address, MAX_BLOCK_HEIGHT * 2, {
7678
value: SWAP_AMOUNT.mul(2),
7779
})
7880
).to.be.revertedWith("");
@@ -88,33 +90,39 @@ describe("yakuSwap Contract", function () {
8890
});
8991

9092
describe("completeSwap", function () {
93+
let swapId;
94+
9195
beforeEach(async function () {
9296
const transaction = await yakuSwap.connect(addr1).createSwap(SECRET_HASH, addr2.address, MAX_BLOCK_HEIGHT, {
9397
value: SWAP_AMOUNT,
9498
});
9599

96100
await transaction.wait();
101+
102+
swapId = await yakuSwap.connect(addr3).getSwapId(SECRET_HASH, addr1.address);
97103
});
98104

99105

100106
it("Should work if everyhting's ok", async function () {
101-
const transaction = await yakuSwap.connect(addr3).completeSwap(SECRET_HASH, SECRET);
107+
const transaction = await yakuSwap.connect(addr3).completeSwap(swapId, SECRET);
102108

103109
await transaction.wait();
104110

105-
const swap = await yakuSwap.connect(addr3).swaps(SECRET_HASH);
111+
const swap = await yakuSwap.connect(addr3).swaps(swapId);
106112
expect(swap.status).to.equal(2);
107113
});
108114

109115
it("Should fail if secret is invalid", async function () {
110116
await expect(
111-
yakuSwap.connect(addr2).completeSwap(SECRET_HASH, WRONG_SECRET)
117+
yakuSwap.connect(addr2).completeSwap(swapId, WRONG_SECRET)
112118
).to.be.revertedWith("");
113119
});
114120

115121
it("Should fail if secretHash is invalid", async function () {
122+
const wrongSwapId = await yakuSwap.connect(addr3).getSwapId(WRONG_SECRET_HASH, addr1.address);
123+
116124
await expect(
117-
yakuSwap.connect(addr2).completeSwap(WRONG_SECRET_HASH, WRONG_SECRET)
125+
yakuSwap.connect(addr2).completeSwap(wrongSwapId, WRONG_SECRET)
118126
).to.be.revertedWith("");
119127
});
120128

@@ -123,40 +131,44 @@ describe("yakuSwap Contract", function () {
123131
await ethers.provider.send('evm_mine');
124132
}
125133
await expect(
126-
yakuSwap.connect(addr2).completeSwap(SECRET_HASH, SECRET)
134+
yakuSwap.connect(addr2).completeSwap(swapId, SECRET)
127135
).to.be.revertedWith("");
128136
});
129137

130138
it("Should fail if the swap was completed", async function () {
131-
const transaction = await yakuSwap.connect(addr3).completeSwap(SECRET_HASH, SECRET);
139+
const transaction = await yakuSwap.connect(addr3).completeSwap(swapId, SECRET);
132140

133141
await transaction.wait();
134142

135143
await expect(
136-
yakuSwap.connect(addr2).completeSwap(SECRET_HASH, SECRET)
144+
yakuSwap.connect(addr2).completeSwap(swapId, SECRET)
137145
).to.be.revertedWith("");
138146
});
139147
});
140148

141149
describe("cancelSwap", function () {
150+
let swapId;
151+
142152
beforeEach(async function () {
143153
const transaction = await yakuSwap.connect(addr1).createSwap(SECRET_HASH, addr2.address, MAX_BLOCK_HEIGHT, {
144154
value: SWAP_AMOUNT,
145155
});
146156

147157
await transaction.wait();
158+
159+
swapId = await yakuSwap.connect(addr3).getSwapId(SECRET_HASH, addr1.address);
148160
});
149161

150162
it("Should work if everyhting's ok", async function () {
151163
for(var i = 0; i < MAX_BLOCK_HEIGHT; i++) {
152164
await ethers.provider.send('evm_mine');
153165
}
154166

155-
const transaction = await yakuSwap.connect(addr3).cancelSwap(SECRET_HASH);
167+
const transaction = await yakuSwap.connect(addr3).cancelSwap(swapId);
156168

157169
await transaction.wait();
158170

159-
const swap = await yakuSwap.connect(addr3).swaps(SECRET_HASH);
171+
const swap = await yakuSwap.connect(addr3).swaps(swapId);
160172
expect(swap.status).to.equal(3);
161173
});
162174

@@ -165,19 +177,21 @@ describe("yakuSwap Contract", function () {
165177
await ethers.provider.send('evm_mine');
166178
}
167179

180+
const wrongSwapId = await yakuSwap.connect(addr3).getSwapId(WRONG_SECRET_HASH, addr1.address);
181+
168182
await expect(
169-
yakuSwap.connect(addr1).cancelSwap(WRONG_SECRET_HASH)
183+
yakuSwap.connect(addr1).cancelSwap(wrongSwapId)
170184
).to.be.revertedWith("");
171185
});
172186

173187
it("Should fail if maxBlockHeight hasn't been reached yet", async function () {
174188
await expect(
175-
yakuSwap.connect(addr1).cancelSwap(SECRET_HASH)
189+
yakuSwap.connect(addr1).cancelSwap(swapId)
176190
).to.be.revertedWith("");
177191
});
178192

179193
it("Should fail if the swap has been completed before", async function () {
180-
const transaction = await yakuSwap.connect(addr3).completeSwap(SECRET_HASH, SECRET);
194+
const transaction = await yakuSwap.connect(addr3).completeSwap(swapId, SECRET);
181195

182196
await transaction.wait();
183197

@@ -186,7 +200,7 @@ describe("yakuSwap Contract", function () {
186200
}
187201

188202
await expect(
189-
yakuSwap.connect(addr1).cancelSwap(SECRET_HASH)
203+
yakuSwap.connect(addr1).cancelSwap(swapId)
190204
).to.be.revertedWith("");
191205
});
192206
});
@@ -199,7 +213,8 @@ describe("yakuSwap Contract", function () {
199213

200214
await transaction.wait();
201215

202-
const transaction2 = await yakuSwap.connect(addr3).completeSwap(SECRET_HASH, SECRET);
216+
const swapId = await yakuSwap.connect(addr3).getSwapId(SECRET_HASH, addr1.address);
217+
const transaction2 = await yakuSwap.connect(addr3).completeSwap(swapId, SECRET);
203218

204219
await transaction2.wait();
205220
});

0 commit comments

Comments
 (0)