forked from karagozemin/OverSync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTLCEscrow.test.ts
More file actions
383 lines (332 loc) · 12.3 KB
/
Copy pathHTLCEscrow.test.ts
File metadata and controls
383 lines (332 loc) · 12.3 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
import { expect } from "chai";
import { ethers } from "hardhat";
import { time } from "@nomicfoundation/hardhat-network-helpers";
import { HTLCEscrow, TestERC20 } from "../../typechain-types";
const ZERO_ADDR = "0x0000000000000000000000000000000000000000";
const TIMELOCK = 600; // 10 minutes
const SAFETY_DEPOSIT = ethers.parseEther("0.001");
const AMOUNT = ethers.parseEther("0.5");
async function deployEscrow() {
const HTLCEscrow = await ethers.getContractFactory("HTLCEscrow");
// resolverRegistry = address(0) → permissionless createOrder
return (await HTLCEscrow.deploy(ZERO_ADDR, 0)) as unknown as HTLCEscrow;
}
async function deployToken() {
const Token = await ethers.getContractFactory("TestERC20");
return (await Token.deploy("MockToken", "MOCK", ethers.parseEther("1000000"))) as unknown as TestERC20;
}
function randomBytes32() {
return ethers.hexlify(ethers.randomBytes(32));
}
describe("HTLCEscrow v2", () => {
describe("createOrder", () => {
it("locks native ETH with correct hashlock/timelock", async () => {
const [sender, beneficiary] = await ethers.getSigners();
const escrow = await deployEscrow();
const preimage = randomBytes32();
const hashlock = ethers.sha256(preimage);
const tx = await escrow.connect(sender).createOrder(
beneficiary.address,
sender.address,
ZERO_ADDR,
AMOUNT,
SAFETY_DEPOSIT,
hashlock,
TIMELOCK,
{ value: AMOUNT + SAFETY_DEPOSIT }
);
const receipt = await tx.wait();
const orderCreated = receipt!.logs.find(
(l: any) => l.fragment?.name === "OrderCreated"
) as any;
expect(orderCreated).to.not.be.undefined;
const orderId = orderCreated.args.orderId;
expect(orderId).to.equal(1n);
const order = await escrow.getOrder(orderId);
expect(order.amount).to.equal(AMOUNT);
expect(order.safetyDeposit).to.equal(SAFETY_DEPOSIT);
expect(order.beneficiary).to.equal(beneficiary.address);
expect(order.status).to.equal(0); // Funded
expect(await ethers.provider.getBalance(await escrow.getAddress())).to.equal(
AMOUNT + SAFETY_DEPOSIT
);
});
it("rejects zero amount", async () => {
const [sender, beneficiary] = await ethers.getSigners();
const escrow = await deployEscrow();
await expect(
escrow.connect(sender).createOrder(
beneficiary.address,
sender.address,
ZERO_ADDR,
0,
SAFETY_DEPOSIT,
randomBytes32(),
TIMELOCK,
{ value: SAFETY_DEPOSIT }
)
).to.be.revertedWithCustomError(escrow, "InvalidAmount");
});
it("rejects zero hashlock", async () => {
const [sender, beneficiary] = await ethers.getSigners();
const escrow = await deployEscrow();
await expect(
escrow.connect(sender).createOrder(
beneficiary.address,
sender.address,
ZERO_ADDR,
AMOUNT,
SAFETY_DEPOSIT,
ethers.ZeroHash,
TIMELOCK,
{ value: AMOUNT + SAFETY_DEPOSIT }
)
).to.be.revertedWithCustomError(escrow, "InvalidHashlock");
});
it("rejects timelock below MIN_TIMELOCK and above MAX_TIMELOCK", async () => {
const [sender, beneficiary] = await ethers.getSigners();
const escrow = await deployEscrow();
const hashlock = ethers.sha256(randomBytes32());
await expect(
escrow.connect(sender).createOrder(
beneficiary.address,
sender.address,
ZERO_ADDR,
AMOUNT,
SAFETY_DEPOSIT,
hashlock,
299,
{ value: AMOUNT + SAFETY_DEPOSIT }
)
).to.be.revertedWithCustomError(escrow, "InvalidTimelock");
await expect(
escrow.connect(sender).createOrder(
beneficiary.address,
sender.address,
ZERO_ADDR,
AMOUNT,
SAFETY_DEPOSIT,
hashlock,
24 * 60 * 60 + 1,
{ value: AMOUNT + SAFETY_DEPOSIT }
)
).to.be.revertedWithCustomError(escrow, "InvalidTimelock");
});
it("rejects msg.value mismatch for native orders", async () => {
const [sender, beneficiary] = await ethers.getSigners();
const escrow = await deployEscrow();
const hashlock = ethers.sha256(randomBytes32());
await expect(
escrow.connect(sender).createOrder(
beneficiary.address,
sender.address,
ZERO_ADDR,
AMOUNT,
SAFETY_DEPOSIT,
hashlock,
TIMELOCK,
{ value: AMOUNT } // missing safety deposit
)
).to.be.revertedWithCustomError(escrow, "InvalidValue");
});
it("locks ERC20 with correct allowance", async () => {
const [sender, beneficiary] = await ethers.getSigners();
const escrow = await deployEscrow();
const token = await deployToken();
await token.connect(sender).approve(await escrow.getAddress(), AMOUNT);
const preimage = randomBytes32();
const hashlock = ethers.sha256(preimage);
await escrow.connect(sender).createOrder(
beneficiary.address,
sender.address,
await token.getAddress(),
AMOUNT,
SAFETY_DEPOSIT,
hashlock,
TIMELOCK,
{ value: SAFETY_DEPOSIT }
);
expect(await token.balanceOf(await escrow.getAddress())).to.equal(AMOUNT);
});
});
describe("claimOrder", () => {
it("pays beneficiary on correct sha256 preimage and pays caller the safety deposit", async () => {
const [sender, beneficiary, relayer] = await ethers.getSigners();
const escrow = await deployEscrow();
const preimage = randomBytes32();
const hashlock = ethers.sha256(preimage);
await escrow.connect(sender).createOrder(
beneficiary.address,
sender.address,
ZERO_ADDR,
AMOUNT,
SAFETY_DEPOSIT,
hashlock,
TIMELOCK,
{ value: AMOUNT + SAFETY_DEPOSIT }
);
const beneficiaryBefore = await ethers.provider.getBalance(beneficiary.address);
const relayerBefore = await ethers.provider.getBalance(relayer.address);
const tx = await escrow.connect(relayer).claimOrder(1, preimage);
const receipt = await tx.wait();
const gas = receipt!.gasUsed * receipt!.gasPrice!;
const beneficiaryAfter = await ethers.provider.getBalance(beneficiary.address);
const relayerAfter = await ethers.provider.getBalance(relayer.address);
expect(beneficiaryAfter - beneficiaryBefore).to.equal(AMOUNT);
expect(relayerAfter - relayerBefore + gas).to.equal(SAFETY_DEPOSIT);
const order = await escrow.getOrder(1);
expect(order.status).to.equal(1); // Claimed
});
it("also accepts a keccak256 hashlock (EVM convention)", async () => {
const [sender, beneficiary] = await ethers.getSigners();
const escrow = await deployEscrow();
const preimage = randomBytes32();
const hashlock = ethers.keccak256(preimage);
await escrow.connect(sender).createOrder(
beneficiary.address,
sender.address,
ZERO_ADDR,
AMOUNT,
SAFETY_DEPOSIT,
hashlock,
TIMELOCK,
{ value: AMOUNT + SAFETY_DEPOSIT }
);
await expect(escrow.connect(beneficiary).claimOrder(1, preimage)).to.not.be.reverted;
});
it("rejects invalid preimage", async () => {
const [sender, beneficiary] = await ethers.getSigners();
const escrow = await deployEscrow();
const preimage = randomBytes32();
const hashlock = ethers.sha256(preimage);
await escrow.connect(sender).createOrder(
beneficiary.address,
sender.address,
ZERO_ADDR,
AMOUNT,
SAFETY_DEPOSIT,
hashlock,
TIMELOCK,
{ value: AMOUNT + SAFETY_DEPOSIT }
);
const wrong = randomBytes32();
await expect(
escrow.connect(beneficiary).claimOrder(1, wrong)
).to.be.revertedWithCustomError(escrow, "InvalidPreimage");
});
it("rejects claim after expiry", async () => {
const [sender, beneficiary] = await ethers.getSigners();
const escrow = await deployEscrow();
const preimage = randomBytes32();
const hashlock = ethers.sha256(preimage);
await escrow.connect(sender).createOrder(
beneficiary.address,
sender.address,
ZERO_ADDR,
AMOUNT,
SAFETY_DEPOSIT,
hashlock,
TIMELOCK,
{ value: AMOUNT + SAFETY_DEPOSIT }
);
await time.increase(TIMELOCK + 1);
await expect(
escrow.connect(beneficiary).claimOrder(1, preimage)
).to.be.revertedWithCustomError(escrow, "Expired");
});
it("rejects double claim", async () => {
const [sender, beneficiary] = await ethers.getSigners();
const escrow = await deployEscrow();
const preimage = randomBytes32();
const hashlock = ethers.sha256(preimage);
await escrow.connect(sender).createOrder(
beneficiary.address,
sender.address,
ZERO_ADDR,
AMOUNT,
SAFETY_DEPOSIT,
hashlock,
TIMELOCK,
{ value: AMOUNT + SAFETY_DEPOSIT }
);
await escrow.connect(beneficiary).claimOrder(1, preimage);
await expect(
escrow.connect(beneficiary).claimOrder(1, preimage)
).to.be.revertedWithCustomError(escrow, "OrderNotClaimable");
});
});
describe("refundOrder", () => {
it("returns the locked amount to the refund address after timeout, permissionlessly", async () => {
const [sender, beneficiary, cleaner] = await ethers.getSigners();
const escrow = await deployEscrow();
const preimage = randomBytes32();
const hashlock = ethers.sha256(preimage);
const refundAddr = ethers.Wallet.createRandom().address;
await escrow.connect(sender).createOrder(
beneficiary.address,
refundAddr,
ZERO_ADDR,
AMOUNT,
SAFETY_DEPOSIT,
hashlock,
TIMELOCK,
{ value: AMOUNT + SAFETY_DEPOSIT }
);
// Refund before expiry → revert
await expect(
escrow.connect(cleaner).refundOrder(1)
).to.be.revertedWithCustomError(escrow, "NotExpired");
await time.increase(TIMELOCK + 1);
const refundBefore = await ethers.provider.getBalance(refundAddr);
const cleanerBefore = await ethers.provider.getBalance(cleaner.address);
const tx = await escrow.connect(cleaner).refundOrder(1);
const receipt = await tx.wait();
const gas = receipt!.gasUsed * receipt!.gasPrice!;
expect(await ethers.provider.getBalance(refundAddr)).to.equal(refundBefore + AMOUNT);
expect(await ethers.provider.getBalance(cleaner.address) + gas).to.equal(
cleanerBefore + SAFETY_DEPOSIT
);
const order = await escrow.getOrder(1);
expect(order.status).to.equal(2); // Refunded
});
it("rejects refund after a successful claim", async () => {
const [sender, beneficiary, cleaner] = await ethers.getSigners();
const escrow = await deployEscrow();
const preimage = randomBytes32();
const hashlock = ethers.sha256(preimage);
await escrow.connect(sender).createOrder(
beneficiary.address,
sender.address,
ZERO_ADDR,
AMOUNT,
SAFETY_DEPOSIT,
hashlock,
TIMELOCK,
{ value: AMOUNT + SAFETY_DEPOSIT }
);
await escrow.connect(beneficiary).claimOrder(1, preimage);
await time.increase(TIMELOCK + 1);
await expect(
escrow.connect(cleaner).refundOrder(1)
).to.be.revertedWithCustomError(escrow, "OrderNotRefundable");
});
});
describe("non-custodial guarantees", () => {
it("contract has no admin escape hatch", async () => {
const escrow = await deployEscrow();
const escrowContract = escrow as any;
// None of the dangerous admin functions exist on the v2 contract.
expect(escrowContract.emergencyWithdraw).to.be.undefined;
expect(escrowContract.pause).to.be.undefined;
expect(escrowContract.withdraw).to.be.undefined;
expect(escrowContract.transferOwnership).to.be.undefined;
});
it("receive() rejects stray ETH", async () => {
const [sender] = await ethers.getSigners();
const escrow = await deployEscrow();
await expect(
sender.sendTransaction({ to: await escrow.getAddress(), value: 1n })
).to.be.reverted;
});
});
});