atomicswap_test.gno
15.30 Kb ยท 431 lines
1package atomicswap
2
3import (
4 "crypto/sha256"
5 "encoding/hex"
6 "std"
7 "testing"
8 "time"
9
10 "gno.land/p/demo/avl"
11 "gno.land/p/demo/testutils"
12 "gno.land/p/demo/uassert"
13 "gno.land/r/demo/tests/test20"
14)
15
16var testRun bool
17
18func TestNewCustomCoinSwap_Claim(t *testing.T) {
19 t.Skip("skipping due to bad support for unit-test driven banker")
20 defer resetTestState()
21
22 // Setup
23 sender := testutils.TestAddress("sender1")
24 recipient := testutils.TestAddress("recipient1")
25 amount := std.Coins{{Denom: "ugnot", Amount: 1}}
26 hashlock := sha256.Sum256([]byte("secret"))
27 hashlockHex := hex.EncodeToString(hashlock[:])
28 timelock := time.Now().Add(1 * time.Hour)
29
30 // Create a new swap
31 testing.SetRealm(std.NewUserRealm(sender))
32 testing.SetOriginSend(amount)
33 id, swap := NewCustomCoinSwap(recipient, hashlockHex, timelock)
34 uassert.Equal(t, 1, id)
35
36 expected := `- status: active
37- sender: g1wdjkuer9wgc47h6lta047h6lta047h6l56jtjc
38- recipient: g1wfjkx6tsd9jkuap3ta047h6lta047h6lkk20gv
39- amount: 1ugnot
40- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b
41- timelock: 2009-02-14T00:31:30Z
42- remaining: 1h0m0s`
43 uassert.Equal(t, expected, swap.String())
44 uassert.Equal(t, expected, Render("1"))
45
46 // Test initial state
47 uassert.Equal(t, sender, swap.sender, "expected sender to match")
48 uassert.Equal(t, recipient, swap.recipient, "expected recipient to match")
49 uassert.Equal(t, swap.amountStr, amount.String(), "expected amount to match")
50 uassert.Equal(t, hashlockHex, swap.hashlock, "expected hashlock to match")
51 uassert.True(t, swap.timelock.Equal(timelock), "expected timelock to match")
52 uassert.False(t, swap.claimed, "expected claimed to be false")
53 uassert.False(t, swap.refunded, "expected refunded to be false")
54
55 // Test claim
56 testing.SetRealm(std.NewUserRealm(recipient))
57 uassert.PanicsWithMessage(t, "invalid preimage", func() { swap.Claim("invalid") })
58 swap.Claim("secret")
59 uassert.True(t, swap.claimed, "expected claimed to be true")
60
61 // Test refund (should fail because already claimed)
62 uassert.PanicsWithMessage(t, "already claimed", swap.Refund)
63 uassert.PanicsWithMessage(t, "already claimed", func() { swap.Claim("secret") })
64
65 expected = `- status: claimed
66- sender: g1wdjkuer9wgc47h6lta047h6lta047h6l56jtjc
67- recipient: g1wfjkx6tsd9jkuap3ta047h6lta047h6lkk20gv
68- amount: 1ugnot
69- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b
70- timelock: 2009-02-14T00:31:30Z
71- remaining: 1h0m0s`
72 uassert.Equal(t, expected, swap.String())
73 uassert.Equal(t, expected, Render("1"))
74}
75
76func TestNewCustomCoinSwap_Refund(t *testing.T) {
77 defer resetTestState()
78
79 // Setup
80 sender := testutils.TestAddress("sender2")
81 recipient := testutils.TestAddress("recipient2")
82 amount := std.Coins{{Denom: "ugnot", Amount: 1}}
83 hashlock := sha256.Sum256([]byte("secret"))
84 hashlockHex := hex.EncodeToString(hashlock[:])
85 timelock := time.Now().Add(1 * time.Hour)
86
87 // Create a new swap
88 testing.SetRealm(std.NewUserRealm(sender))
89 testing.SetOriginSend(amount)
90 id, swap := NewCustomCoinSwap(recipient, hashlockHex, timelock) // Create a new swap
91 uassert.Equal(t, 1, id)
92
93 expected := `- status: active
94- sender: g1wdjkuer9wge97h6lta047h6lta047h6ltfacad
95- recipient: g1wfjkx6tsd9jkuapjta047h6lta047h6lducc3v
96- amount: 1ugnot
97- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b
98- timelock: 2009-02-14T00:31:30Z
99- remaining: 1h0m0s`
100 uassert.Equal(t, expected, swap.String())
101 uassert.Equal(t, expected, Render("1"))
102
103 // Test Refund
104 pkgAddr := std.DerivePkgAddr("gno.land/r/demo/atomicswap")
105 testing.IssueCoins(pkgAddr, std.Coins{{"ugnot", 100000000}})
106 uassert.PanicsWithMessage(t, "timelock not expired", swap.Refund)
107 swap.timelock = time.Now().Add(-1 * time.Hour) // override timelock
108 swap.Refund()
109 uassert.True(t, swap.refunded, "expected refunded to be true")
110 expected = `- status: refunded
111- sender: g1wdjkuer9wge97h6lta047h6lta047h6ltfacad
112- recipient: g1wfjkx6tsd9jkuapjta047h6lta047h6lducc3v
113- amount: 1ugnot
114- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b
115- timelock: 2009-02-13T22:31:30Z
116- remaining: 0s`
117 uassert.Equal(t, expected, swap.String())
118 uassert.Equal(t, expected, Render("1"))
119}
120
121func TestNewCustomGRC20Swap_Claim(t *testing.T) {
122 defer resetTestState()
123
124 // Setup
125 sender := testutils.TestAddress("sender3")
126 recipient := testutils.TestAddress("recipient3")
127 rlm := std.DerivePkgAddr("gno.land/r/demo/atomicswap")
128 hashlock := sha256.Sum256([]byte("secret"))
129 hashlockHex := hex.EncodeToString(hashlock[:])
130 timelock := time.Now().Add(1 * time.Hour)
131
132 test20.PrivateLedger.Mint(sender, 100_000)
133 test20.PrivateLedger.Approve(sender, rlm, 70_000)
134
135 // Create a new swap
136 testing.SetRealm(std.NewUserRealm(sender))
137 id, swap := NewCustomGRC20Swap(recipient, hashlockHex, timelock, test20.Token)
138 uassert.Equal(t, 1, id)
139
140 expected := `- status: active
141- sender: g1wdjkuer9wge47h6lta047h6lta047h6l5rk38l
142- recipient: g1wfjkx6tsd9jkuapnta047h6lta047h6ly6k4pv
143- amount: 70000TST
144- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b
145- timelock: 2009-02-14T00:31:30Z
146- remaining: 1h0m0s`
147 uassert.Equal(t, expected, swap.String())
148 uassert.Equal(t, expected, Render("1"))
149
150 // Test initial state
151 uassert.Equal(t, sender, swap.sender, "expected sender to match")
152 uassert.Equal(t, recipient, swap.recipient, "expected recipient to match")
153 bal := test20.Token.BalanceOf(sender)
154 uassert.Equal(t, bal, uint64(30_000))
155 bal = test20.Token.BalanceOf(rlm)
156 uassert.Equal(t, bal, uint64(70_000))
157 bal = test20.Token.BalanceOf(recipient)
158 uassert.Equal(t, bal, uint64(0))
159
160 // uassert.Equal(t, swap.amountStr, amount.String(), "expected amount to match")
161 uassert.Equal(t, hashlockHex, swap.hashlock, "expected hashlock to match")
162 uassert.True(t, swap.timelock.Equal(timelock), "expected timelock to match")
163 uassert.False(t, swap.claimed, "expected claimed to be false")
164 uassert.False(t, swap.refunded, "expected refunded to be false")
165
166 // Test claim
167 testing.SetRealm(std.NewUserRealm(recipient))
168 uassert.PanicsWithMessage(t, "invalid preimage", func() { swap.Claim("invalid") })
169 swap.Claim("secret")
170 uassert.True(t, swap.claimed, "expected claimed to be true")
171
172 bal = test20.Token.BalanceOf(sender)
173 uassert.Equal(t, bal, uint64(30_000))
174 bal = test20.Token.BalanceOf(rlm)
175 uassert.Equal(t, bal, uint64(0))
176 bal = test20.Token.BalanceOf(recipient)
177 uassert.Equal(t, bal, uint64(70_000))
178
179 // Test refund (should fail because already claimed)
180 uassert.PanicsWithMessage(t, "already claimed", swap.Refund)
181 uassert.PanicsWithMessage(t, "already claimed", func() { swap.Claim("secret") })
182
183 expected = `- status: claimed
184- sender: g1wdjkuer9wge47h6lta047h6lta047h6l5rk38l
185- recipient: g1wfjkx6tsd9jkuapnta047h6lta047h6ly6k4pv
186- amount: 70000TST
187- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b
188- timelock: 2009-02-14T00:31:30Z
189- remaining: 1h0m0s`
190 uassert.Equal(t, expected, swap.String())
191 uassert.Equal(t, expected, Render("1"))
192}
193
194func TestNewCustomGRC20Swap_Refund(t *testing.T) {
195 defer resetTestState()
196
197 // Setup
198 sender := testutils.TestAddress("sender5")
199 recipient := testutils.TestAddress("recipient5")
200 rlm := std.DerivePkgAddr("gno.land/r/demo/atomicswap")
201 hashlock := sha256.Sum256([]byte("secret"))
202 hashlockHex := hex.EncodeToString(hashlock[:])
203 timelock := time.Now().Add(1 * time.Hour)
204
205 test20.PrivateLedger.Mint(sender, 100_000)
206 test20.PrivateLedger.Approve(sender, rlm, 70_000)
207
208 // Create a new swap
209 testing.SetRealm(std.NewUserRealm(sender))
210 id, swap := NewCustomGRC20Swap(recipient, hashlockHex, timelock, test20.Token)
211 uassert.Equal(t, 1, id)
212
213 expected := `- status: active
214- sender: g1wdjkuer9wg647h6lta047h6lta047h6l5p6k3k
215- recipient: g1wfjkx6tsd9jkuap4ta047h6lta047h6lmwmj6v
216- amount: 70000TST
217- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b
218- timelock: 2009-02-14T00:31:30Z
219- remaining: 1h0m0s`
220 uassert.Equal(t, expected, swap.String())
221 uassert.Equal(t, expected, Render("1"))
222
223 // Test initial state
224 uassert.Equal(t, sender, swap.sender, "expected sender to match")
225 uassert.Equal(t, recipient, swap.recipient, "expected recipient to match")
226 bal := test20.Token.BalanceOf(sender)
227 uassert.Equal(t, bal, uint64(30_000))
228 bal = test20.Token.BalanceOf(rlm)
229 uassert.Equal(t, bal, uint64(70_000))
230 bal = test20.Token.BalanceOf(recipient)
231 uassert.Equal(t, bal, uint64(0))
232
233 // Test Refund
234 pkgAddr := std.DerivePkgAddr("gno.land/r/demo/atomicswap")
235 testing.IssueCoins(pkgAddr, std.Coins{{"ugnot", 100000000}})
236 uassert.PanicsWithMessage(t, "timelock not expired", swap.Refund)
237 swap.timelock = time.Now().Add(-1 * time.Hour) // override timelock
238 swap.Refund()
239 uassert.True(t, swap.refunded, "expected refunded to be true")
240
241 bal = test20.Token.BalanceOf(sender)
242 uassert.Equal(t, bal, uint64(100_000))
243 bal = test20.Token.BalanceOf(rlm)
244 uassert.Equal(t, bal, uint64(0))
245 bal = test20.Token.BalanceOf(recipient)
246 uassert.Equal(t, bal, uint64(0))
247
248 expected = `- status: refunded
249- sender: g1wdjkuer9wg647h6lta047h6lta047h6l5p6k3k
250- recipient: g1wfjkx6tsd9jkuap4ta047h6lta047h6lmwmj6v
251- amount: 70000TST
252- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b
253- timelock: 2009-02-13T22:31:30Z
254- remaining: 0s`
255 uassert.Equal(t, expected, swap.String())
256 uassert.Equal(t, expected, Render("1"))
257}
258
259func TestNewGRC20Swap_Claim(t *testing.T) {
260 defer resetTestState()
261
262 // Setup
263 sender := testutils.TestAddress("sender4")
264 recipient := testutils.TestAddress("recipient4")
265 rlm := std.DerivePkgAddr("gno.land/r/demo/atomicswap")
266 hashlock := sha256.Sum256([]byte("secret"))
267 hashlockHex := hex.EncodeToString(hashlock[:])
268 timelock := time.Now().Add(defaultTimelockDuration)
269
270 test20.PrivateLedger.Mint(sender, 100_000)
271 test20.PrivateLedger.Approve(sender, rlm, 70_000)
272
273 // Create a new swap
274 testing.SetRealm(std.NewUserRealm(sender))
275 id, swap := NewGRC20Swap(recipient, hashlockHex, "gno.land/r/demo/tests/test20")
276 uassert.Equal(t, 1, id)
277
278 expected := `- status: active
279- sender: g1wdjkuer9wg697h6lta047h6lta047h6ltt3lty
280- recipient: g1wfjkx6tsd9jkuap5ta047h6lta047h6ljg4l2v
281- amount: 70000TST
282- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b
283- timelock: 2009-02-20T23:31:30Z
284- remaining: 168h0m0s`
285 uassert.Equal(t, expected, swap.String())
286 uassert.Equal(t, expected, Render("1"))
287
288 // Test initial state
289 uassert.Equal(t, sender, swap.sender, "expected sender to match")
290 uassert.Equal(t, recipient, swap.recipient, "expected recipient to match")
291 bal := test20.Token.BalanceOf(sender)
292 uassert.Equal(t, bal, uint64(30_000))
293 bal = test20.Token.BalanceOf(rlm)
294 uassert.Equal(t, bal, uint64(70_000))
295 bal = test20.Token.BalanceOf(recipient)
296 uassert.Equal(t, bal, uint64(0))
297
298 // uassert.Equal(t, swap.amountStr, amount.String(), "expected amount to match")
299 uassert.Equal(t, hashlockHex, swap.hashlock, "expected hashlock to match")
300 uassert.True(t, swap.timelock.Equal(timelock), "expected timelock to match")
301 uassert.False(t, swap.claimed, "expected claimed to be false")
302 uassert.False(t, swap.refunded, "expected refunded to be false")
303
304 // Test claim
305 testing.SetRealm(std.NewUserRealm(recipient))
306 uassert.PanicsWithMessage(t, "invalid preimage", func() { swap.Claim("invalid") })
307 swap.Claim("secret")
308 uassert.True(t, swap.claimed, "expected claimed to be true")
309
310 bal = test20.Token.BalanceOf(sender)
311 uassert.Equal(t, bal, uint64(30_000))
312 bal = test20.Token.BalanceOf(rlm)
313 uassert.Equal(t, bal, uint64(0))
314 bal = test20.Token.BalanceOf(recipient)
315 uassert.Equal(t, bal, uint64(70_000))
316
317 // Test refund (should fail because already claimed)
318 uassert.PanicsWithMessage(t, "already claimed", swap.Refund)
319 uassert.PanicsWithMessage(t, "already claimed", func() { swap.Claim("secret") })
320
321 expected = `- status: claimed
322- sender: g1wdjkuer9wg697h6lta047h6lta047h6ltt3lty
323- recipient: g1wfjkx6tsd9jkuap5ta047h6lta047h6ljg4l2v
324- amount: 70000TST
325- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b
326- timelock: 2009-02-20T23:31:30Z
327- remaining: 168h0m0s`
328 uassert.Equal(t, expected, swap.String())
329 uassert.Equal(t, expected, Render("1"))
330}
331
332func TestNewGRC20Swap_Refund(t *testing.T) {
333 defer resetTestState()
334
335 // Setup
336 sender := testutils.TestAddress("sender6")
337 recipient := testutils.TestAddress("recipient6")
338 rlm := std.DerivePkgAddr("gno.land/r/demo/atomicswap")
339 hashlock := sha256.Sum256([]byte("secret"))
340 hashlockHex := hex.EncodeToString(hashlock[:])
341 timelock := time.Now().Add(defaultTimelockDuration)
342
343 test20.PrivateLedger.Mint(sender, 100_000)
344 test20.PrivateLedger.Approve(sender, rlm, 70_000)
345
346 // Create a new swap
347 testing.SetRealm(std.NewUserRealm(sender))
348 id, swap := NewGRC20Swap(recipient, hashlockHex, "gno.land/r/demo/tests/test20")
349 uassert.Equal(t, 1, id)
350
351 expected := `- status: active
352- sender: g1wdjkuer9wgm97h6lta047h6lta047h6ltj497r
353- recipient: g1wfjkx6tsd9jkuapkta047h6lta047h6lqyf9rv
354- amount: 70000TST
355- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b
356- timelock: 2009-02-20T23:31:30Z
357- remaining: 168h0m0s`
358 uassert.Equal(t, expected, swap.String())
359 uassert.Equal(t, expected, Render("1"))
360
361 // Test initial state
362 uassert.Equal(t, sender, swap.sender, "expected sender to match")
363 uassert.Equal(t, recipient, swap.recipient, "expected recipient to match")
364 bal := test20.Token.BalanceOf(sender)
365 uassert.Equal(t, bal, uint64(30_000))
366 bal = test20.Token.BalanceOf(rlm)
367 uassert.Equal(t, bal, uint64(70_000))
368 bal = test20.Token.BalanceOf(recipient)
369 uassert.Equal(t, bal, uint64(0))
370
371 // Test Refund
372 pkgAddr := std.DerivePkgAddr("gno.land/r/demo/atomicswap")
373 testing.IssueCoins(pkgAddr, std.Coins{{"ugnot", 100000000}})
374 uassert.PanicsWithMessage(t, "timelock not expired", swap.Refund)
375 swap.timelock = time.Now().Add(-1 * time.Hour) // override timelock
376 swap.Refund()
377 uassert.True(t, swap.refunded, "expected refunded to be true")
378
379 bal = test20.Token.BalanceOf(sender)
380 uassert.Equal(t, bal, uint64(100_000))
381 bal = test20.Token.BalanceOf(rlm)
382 uassert.Equal(t, bal, uint64(0))
383 bal = test20.Token.BalanceOf(recipient)
384 uassert.Equal(t, bal, uint64(0))
385
386 expected = `- status: refunded
387- sender: g1wdjkuer9wgm97h6lta047h6lta047h6ltj497r
388- recipient: g1wfjkx6tsd9jkuapkta047h6lta047h6lqyf9rv
389- amount: 70000TST
390- hashlock: 2bb80d537b1da3e38bd30361aa855686bde0eacd7162fef6a25fe97bf527a25b
391- timelock: 2009-02-13T22:31:30Z
392- remaining: 0s`
393 uassert.Equal(t, expected, swap.String())
394 uassert.Equal(t, expected, Render("1"))
395}
396
397func TestRender(t *testing.T) {
398 defer resetTestState()
399
400 // Setup
401 alice := testutils.TestAddress("alice")
402 bob := testutils.TestAddress("bob")
403 charly := testutils.TestAddress("charly")
404 rlm := std.DerivePkgAddr("gno.land/r/demo/atomicswap")
405 hashlock := sha256.Sum256([]byte("secret"))
406 hashlockHex := hex.EncodeToString(hashlock[:])
407 timelock := time.Now().Add(1 * time.Hour)
408
409 test20.PrivateLedger.Mint(alice, 100_000)
410 testing.SetRealm(std.NewUserRealm(alice))
411
412 userTeller := test20.Token.CallerTeller()
413 userTeller.Approve(rlm, 10_000)
414 _, bobSwap := NewCustomGRC20Swap(bob, hashlockHex, timelock, test20.Token)
415
416 userTeller.Approve(rlm, 20_000)
417 _, _ = NewCustomGRC20Swap(charly, hashlockHex, timelock, test20.Token)
418
419 testing.SetRealm(std.NewUserRealm(bob))
420 bobSwap.Claim("secret")
421
422 expected := `- 2: g1v9kxjcm9ta047h6lta047h6lta047h6lzd40gh -(20000TST)> g1vd5xzunv09047h6lta047h6lta047h6lhsyveh - active
423- 1: g1v9kxjcm9ta047h6lta047h6lta047h6lzd40gh -(10000TST)> g1vfhkyh6lta047h6lta047h6lta047h6l03vdhu - claimed
424`
425 uassert.Equal(t, expected, Render(""))
426}
427
428func resetTestState() {
429 swaps = avl.Tree{}
430 counter = 0
431}