-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2eTests.js
867 lines (804 loc) · 49.8 KB
/
e2eTests.js
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
const {expect,assert} = require("chai");
const { ethers, upgrades } = require("hardhat");
const h = require("./helpers/helpers");
const web3 = require("web3");
const {keccak256} = require("@ethersproject/keccak256");
require("chai").use(require("chai-as-promised")).should();
describe("Autopay - e2e tests", function() {
let fetch;
let autopay;
let queryDataStorage;
let accounts;
const abiCoder = new ethers.utils.AbiCoder;
const FETCH_QUERY_DATA_ARGS = abiCoder.encode(["string", "string"], ["fetch", "usd"])
const FETCH_QUERY_DATA = abiCoder.encode(["string", "bytes"], ["SpotPrice", FETCH_QUERY_DATA_ARGS])
const FETCH_QUERY_ID = keccak256(FETCH_QUERY_DATA)
const ETH_QUERY_DATA_ARGS = abiCoder.encode(["string", "string"], ["eth", "usd"])
const ETH_QUERY_DATA = abiCoder.encode(["string", "bytes"], ["SpotPrice", ETH_QUERY_DATA_ARGS])
const ETH_QUERY_ID = keccak256(ETH_QUERY_DATA)
const BTC_QUERY_DATA_ARGS = abiCoder.encode(["string", "string"], ["btc", "usd"])
const BTC_QUERY_DATA = abiCoder.encode(["string", "bytes"], ["SpotPrice", BTC_QUERY_DATA_ARGS])
const BTC_QUERY_ID = keccak256(BTC_QUERY_DATA)
const DOGE_QUERY_DATA_ARGS = abiCoder.encode(["string", "string"], ["doge", "usd"])
const DOGE_QUERY_DATA = abiCoder.encode(["string", "bytes"], ["SpotPrice", DOGE_QUERY_DATA_ARGS])
const DOGE_QUERY_ID = keccak256(DOGE_QUERY_DATA)
const LTC_QUERY_DATA_ARGS = abiCoder.encode(["string", "string"], ["ltc", "usd"])
const LTC_QUERY_DATA = abiCoder.encode(["string", "bytes"], ["SpotPrice", LTC_QUERY_DATA_ARGS])
const LTC_QUERY_ID = keccak256(LTC_QUERY_DATA)
beforeEach(async function() {
accounts = await ethers.getSigners();
const FetchPlayground = await ethers.getContractFactory("FetchPlayground");
fetch = await FetchPlayground.deploy();
await fetch.deployed();
await fetch.faucet(accounts[0].address);
const QueryDataStorage = await ethers.getContractFactory("QueryDataStorage");
queryDataStorage = await QueryDataStorage.deploy();
await queryDataStorage.deployed();
const Autopay = await ethers.getContractFactory("AutopayMock");
autopay = await upgrades.deployProxy(Autopay, [fetch.address, queryDataStorage.address])
await autopay.deployed();
});
it("test no pay structure, but multiple tips", async function() {
let autopay1 = autopay.connect(accounts[1])
await fetch.faucet(accounts[1].address)
await fetch.connect(accounts[1]).approve(autopay.address, web3.utils.toWei("100"))
await h.expectThrow(autopay1.tip(ETH_QUERY_ID, web3.utils.toWei("101"),ETH_QUERY_DATA)) // "ERC20: transfer amount exceeds balance"
// add first tip
await autopay1.tip(ETH_QUERY_ID, web3.utils.toWei("10"),ETH_QUERY_DATA)
blocky = await h.getBlock()
assert(await fetch.balanceOf(accounts[1].address) - web3.utils.toWei("990") == 0, "User balance should reduce correctly after tipping")
assert(await fetch.balanceOf(autopay.address) - web3.utils.toWei("10") == 0, "Autopay contract balance should increase correctly after user adds tip")
pastTips = await autopay.getPastTips(ETH_QUERY_ID)
assert(pastTips.length == 1, "Tips array should be correct length")
assert(pastTips[0].amount == web3.utils.toWei("10"), "Recorded tip amount should be correct")
assert(pastTips[0].timestamp == blocky.timestamp + 1, "Tip timestamp should be recorded correctly")
// submit value
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32("200"), 0, ETH_QUERY_DATA)
blockySubmit1 = await h.getBlock()
// add second tip
await autopay1.tip(ETH_QUERY_ID, web3.utils.toWei("20"),ETH_QUERY_DATA)
blocky2 = await h.getBlock()
assert(await fetch.balanceOf(accounts[1].address) - web3.utils.toWei("970") == 0, "User balance should reduce correctly after tipping")
assert(await fetch.balanceOf(autopay.address) - web3.utils.toWei("30") == 0, "Autopay contract balance should increase correctly after user adds tip")
pastTips = await autopay.getPastTips(ETH_QUERY_ID)
assert(pastTips.length == 2, "Tips array should be correct length")
assert(pastTips[0].amount == web3.utils.toWei("10"), "First recorded tip amount should be correct")
assert(pastTips[0].timestamp == blocky.timestamp + 1, "First tip timestamp should be recorded correctly")
assert(pastTips[1].amount == web3.utils.toWei("20"), "Second recorded tip amount should be correct")
assert(pastTips[1].timestamp == blocky2.timestamp + 1, "Second tip timestamp should be recorded correctly")
// add third tip
await autopay1.tip(ETH_QUERY_ID, web3.utils.toWei("10"),ETH_QUERY_DATA)
blocky3 = await h.getBlock()
assert(await fetch.balanceOf(accounts[1].address) - web3.utils.toWei("960") == 0, "User balance should reduce correctly after tipping")
assert(await fetch.balanceOf(autopay.address) - web3.utils.toWei("40") == 0, "Autopay contract balance should increase correctly after user adds tip")
pastTips = await autopay.getPastTips(ETH_QUERY_ID)
assert(pastTips.length == 2, "Tips array should be correct length")
assert(pastTips[0].amount == web3.utils.toWei("10"), "First recorded tip amount should be correct")
assert(pastTips[0].timestamp == blocky.timestamp + 1, "First tip timestamp should be recorded correctly")
assert(pastTips[1].amount == web3.utils.toWei("30"), "Second cumulative recorded tip amount should be correct")
assert(pastTips[1].timestamp == blocky3.timestamp + 1, "Second tip timestamp should be updated correctly")
// claim first tip
await h.advanceTime(3600 * 12)
await autopay.connect(accounts[2]).claimOneTimeTip(ETH_QUERY_ID, [blockySubmit1.timestamp])
assert(await fetch.balanceOf(accounts[2].address) - web3.utils.toWei("10") == 0, "Reporter balance should increase correctly after claiming tip")
assert(await fetch.balanceOf(autopay.address) - web3.utils.toWei("30") == 0, "Autopay contract balance should decrease correctly after paying tip")
pastTips = await autopay.getPastTips(ETH_QUERY_ID)
assert(pastTips.length == 2, "Tips array should be correct length")
assert(pastTips[0].amount == 0, "First recorded tip amount should be set to zero after tip claimed")
assert(pastTips[0].timestamp == blocky.timestamp + 1, "First tip timestamp should be recorded correctly")
assert(pastTips[1].amount == web3.utils.toWei("30"), "Second cumulative recorded tip amount should be correct")
assert(pastTips[1].timestamp == blocky3.timestamp + 1, "Second tip timestamp should be correct")
// submit value
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32("200"), 0, ETH_QUERY_DATA)
blockySubmit2 = await h.getBlock()
// claim second tip
await h.advanceTime(3600 * 12)
await autopay.connect(accounts[2]).claimOneTimeTip(ETH_QUERY_ID, [blockySubmit2.timestamp])
assert(await fetch.balanceOf(accounts[2].address) - web3.utils.toWei("40.0") == 0, "Reporter balance should increase correctly after claiming tip")
assert(await fetch.balanceOf(autopay.address) == 0, "Autopay contract balance should decrease correctly after paying tip")
pastTips = await autopay.getPastTips(ETH_QUERY_ID)
assert(pastTips.length == 2, "Tips array should be correct length")
assert(pastTips[0].amount == 0, "First recorded tip amount should be set to zero after tip claimed")
assert(pastTips[0].timestamp == blocky.timestamp + 1, "First tip timestamp should be recorded correctly")
assert(pastTips[1].amount == 0, "Second cumulative recorded tip amount should be set to zero after tip claimed")
assert(pastTips[1].timestamp == blocky3.timestamp + 1, "Second tip timestamp should be correct")
})
it("single queryID, multiple refills, pulls", async function() {
interval1 = 36000
window1 = 6000
reward1 = h.toWei("1")
ownerBalance = await fetch.balanceOf(accounts[0].address)
reporterBalance = await fetch.balanceOf(accounts[2].address)
blockyArray1 = new Array()
blockyArray2 = new Array()
// setup data feed queryId 1
blocky = await h.getBlock()
await autopay.connect(accounts[1]).setupDataFeed(ETH_QUERY_ID, reward1, blocky.timestamp, interval1, window1, 0, 0, ETH_QUERY_DATA,0);
feedBytes = abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"], [ETH_QUERY_ID, reward1, blocky.timestamp, interval1, window1, 0, 0])
feedId = feedId = ethers.utils.keccak256(feedBytes)
await fetch.approve(autopay.address, h.toWei("1000"));
// fund feed
await autopay.fundFeed(feedId, ETH_QUERY_ID, h.toWei("1000"));
// submit 10 values for queryId 1
for (i = 0; i < 10; i++) {
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(3575 + i), 0, ETH_QUERY_DATA);
blockyArray1[i] = await h.getBlock()
}
// advance time to next interval
await h.advanceTime(interval1)
// submit 10 values for queryId 1
for (i = 0; i < 10; i++) {
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(3585 + i), 0, ETH_QUERY_DATA);
blockyArray2[i] = await h.getBlock()
}
// advance time beyond buffer
await h.advanceTime(3600 * 12)
// make sure can't claim invalid tips, interval1
for (i = 1; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId, ETH_QUERY_ID, [blockyArray1[i].timestamp])); // "timestamp not first report within window"
}
assert(await fetch.balanceOf(autopay.address) - h.toWei("1000") == 0, "Autopay contract balance should not change")
assert(await fetch.balanceOf(accounts[2].address) == 0, "Reporter balance should still be zero")
// valid claim tip, interval1
await autopay.connect(accounts[2]).claimTip(feedId, ETH_QUERY_ID, [blockyArray1[0].timestamp])
assert(await fetch.balanceOf(autopay.address) - h.toWei("999") == 0, "Autopay contract balance should not change")
assert(await fetch.balanceOf(accounts[2].address) - reward1 == 0, "Reporter balance should update correctly")
// faucet more tokens, add tip
await fetch.faucet(accounts[0].address)
await fetch.approve(autopay.address, h.toWei("10"))
await autopay.tip(ETH_QUERY_ID, h.toWei("10"),ETH_QUERY_DATA)
blocky = await h.getBlock()
pastTips = await autopay.getPastTips(ETH_QUERY_ID)
assert(pastTips.length == 1, "Tips array should be correct length")
assert(pastTips[0].amount == web3.utils.toWei("10"), "First recorded tip amount should be correct")
assert(pastTips[0].timestamp == blocky.timestamp + 1, "First tip timestamp should be recorded correctly")
let abal = await fetch.balanceOf(autopay.address)
assert(ethers.utils.formatEther(abal) - 999 - 10 == 0, "Autopay contract balance should update correctly")
// faucet and add more funds to autopay feed
await fetch.faucet(accounts[0].address)
await fetch.approve(autopay.address, h.toWei("1000"))
await autopay.fundFeed(feedId, ETH_QUERY_ID, h.toWei("1000"));
feedDetails = await autopay.getDataFeed(feedId)
assert(feedDetails.reward == reward1, "Recorded reward amount should be correct")
assert(feedDetails.balance == h.toWei("1999"), "Recorded feed balance should be correct")
// submit another value (eligible for tip)
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(4000), 0, ETH_QUERY_DATA);
blocky = await h.getBlock()
// ensure can't claim one-time-tip for ineligible submissions
for (i = 1; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimOneTimeTip(ETH_QUERY_ID, [blockyArray1[i].timestamp])); // "Timestamp not eligible for tip"
}
for (i = 1; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimOneTimeTip(ETH_QUERY_ID, [blockyArray2[i].timestamp])); // "Timestamp not eligible for tip"
}
// claim valid one time tip
ownerBalanceBefore = await fetch.balanceOf(fetch.address)
await h.advanceTime(3600 * 12)
await autopay.connect(accounts[2]).claimOneTimeTip(ETH_QUERY_ID, [blocky.timestamp])
await h.expectThrow(autopay.connect(accounts[2]).claimOneTimeTip(ETH_QUERY_ID, [blocky.timestamp])) // Tip already claimed
assert(await fetch.balanceOf(autopay.address) - h.toWei("1999") == 0, "Autopay contract balance should change correctly")
assert(await fetch.balanceOf(accounts[2].address) - h.toWei("11.0") == 0, "Reporter balance should update correctly")
});
it("multiple queryID's, several disputes and refills", async function() {
interval1 = 86400
window1 = 3600
reward1 = h.toWei("1")
rewardMinusFee1 = reward1 * 0.99
blockyArray1QID1 = new Array()
blockyArray2QID1 = new Array()
interval2 = interval1
window2 = window1
reward2 = h.toWei("2")
rewardMinusFee2 = reward2 * 0.99
blockyArray1QID2 = new Array()
blockyArray2QID2 = new Array()
interval3 = interval1
window3 = window1
reward3 = h.toWei("3")
rewardMinusFee3 = reward3 * 0.99
blockyArray1QID3 = new Array()
blockyArray2QID3 = new Array()
blockyArray1QID1 = new Array()
blockyArray2QID1 = new Array()
await fetch.approve(autopay.address, h.toWei("10000000000"));
blocky = await h.getBlock()
// setup data feed queryId 1
await autopay.connect(accounts[1]).setupDataFeed(ETH_QUERY_ID, reward1, blocky.timestamp, interval1, window1, 0, 0, ETH_QUERY_DATA,0);
feedBytes = abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"], [ETH_QUERY_ID, reward1, blocky.timestamp, interval1, window1, 0, 0])
feedId1 = ethers.utils.keccak256(feedBytes)
// fund feed 1
await autopay.fundFeed(feedId1, ETH_QUERY_ID, h.toWei("100"));
// setup data feed queryId 2
await autopay.connect(accounts[1]).setupDataFeed(BTC_QUERY_ID, reward2, blocky.timestamp, interval2, window2, 0, 0, BTC_QUERY_DATA,0);
feedBytes = abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"], [BTC_QUERY_ID, reward2, blocky.timestamp, interval2,window2, 0, 0])
feedId2 = ethers.utils.keccak256(feedBytes)
// fund feed 2
await autopay.fundFeed(feedId2, BTC_QUERY_ID, h.toWei("100"));
// setup data feed queryId 3
await autopay.connect(accounts[1]).setupDataFeed(DOGE_QUERY_ID, reward3, blocky.timestamp, interval3, window3, 0, 0, DOGE_QUERY_DATA,0);
feedBytes = abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"], [DOGE_QUERY_ID, reward3, blocky.timestamp, interval3, window3, 0, 0])
feedId3 = ethers.utils.keccak256(feedBytes)
// fund feed 3
await autopay.fundFeed(feedId3, DOGE_QUERY_ID, h.toWei("100"));
// submit 10 values for queryId 1
for (i = 0; i < 10; i++) {
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(1575 + i), 0, ETH_QUERY_DATA);
blockyArray1QID1[i] = await h.getBlock()
}
// submit 10 values for queryId 2
for (i = 0; i < 10; i++) {
await fetch.connect(accounts[2]).submitValue(BTC_QUERY_ID, h.uintTob32(2575 + i), 0, BTC_QUERY_DATA);
blockyArray1QID2[i] = await h.getBlock()
}
// submit 10 values for queryId 3
for (i = 0; i < 10; i++) {
await fetch.connect(accounts[2]).submitValue(DOGE_QUERY_ID, h.uintTob32(3575 + i), 0, DOGE_QUERY_DATA);
blockyArray1QID3[i] = await h.getBlock()
}
// make sure can't claim any tips before dispute buffer queryID 1
for (i = 0; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [blockyArray1QID1[i].timestamp])); // buffer time hasn't passed
}
// make sure can't claim any tips before dispute buffer queryID 2
for (i = 0; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId2, BTC_QUERY_ID, [blockyArray1QID2[i].timestamp])); // buffer time hasn't passed
}
// make sure can't claim any tips before dispute buffer queryID 3
for (i = 0; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId3, DOGE_QUERY_ID, [blockyArray1QID3[i].timestamp])); // buffer time hasn't passed
}
// advance time to next interval
await h.advanceTime(interval1)
// submit 10 values for queryId 1
for (i = 0; i < 10; i++) {
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(1675 + i), 0, ETH_QUERY_DATA);
blockyArray2QID1[i] = await h.getBlock()
}
// submit 10 values for queryId 2
for (i = 0; i < 10; i++) {
await fetch.connect(accounts[2]).submitValue(BTC_QUERY_ID, h.uintTob32(2675 + i), 0, BTC_QUERY_DATA);
blockyArray2QID2[i] = await h.getBlock()
}
// submit 10 values for queryId 3
for (i = 0; i < 10; i++) {
await fetch.connect(accounts[2]).submitValue(DOGE_QUERY_ID, h.uintTob32(3675 + i), 0, DOGE_QUERY_DATA);
blockyArray2QID3[i] = await h.getBlock()
}
// make sure can't claim any tips before dispute buffer queryID 1
for (i = 0; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [blockyArray2QID1[i].timestamp])); // buffer time hasn't passed
}
// make sure can't claim any tips before dispute buffer queryID 2
for (i = 0; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId2, BTC_QUERY_ID, [blockyArray2QID2[i].timestamp])); // buffer time hasn't passed
}
// make sure can't claim any tips before dispute buffer queryID 3
for (i = 0; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId3, DOGE_QUERY_ID, [blockyArray2QID3[i].timestamp])); // buffer time hasn't passed
}
// claim tip interval 1 queryID 1
ownerBalanceBefore = await fetch.balanceOf(fetch.address)
autopayBalanceBefore = await fetch.balanceOf(autopay.address)
await autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [blockyArray1QID1[0].timestamp])
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [blockyArray1QID1[0].timestamp])) // tip already claimed
assert(await fetch.balanceOf(accounts[2].address) == h.toWei("1.00"), "Reporter balance should update correctly")
ownerBalanceAfter = await fetch.balanceOf(fetch.address);
assert((ownerBalanceAfter.sub(ownerBalanceBefore) - h.toWei("0.0")) == 0, "Owner balance should update correctly")
autopayBalanceAfter = await fetch.balanceOf(autopay.address);
assert(autopayBalanceBefore.sub(autopayBalanceAfter) - h.toWei("1") == 0, "Autopay contract balance should update correctly")
// advance time to next interval
await h.advanceTime(interval1)
// dispute first three values from interval 1, queryId 2
for (i = 0; i < 3; i++) {
await fetch.beginDispute(BTC_QUERY_ID, blockyArray1QID2[i].timestamp)
}
// ensure can't claim tips for disputed value
for (i = 0; i < 3; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId2, BTC_QUERY_ID, [blockyArray1QID2[i].timestamp])) // value with given timestamp doesn't exist
}
// claim tip for undisputed valid value
await autopay.connect(accounts[2]).claimTip(feedId2, BTC_QUERY_ID, [blockyArray1QID2[3].timestamp])
});
it("multiple queryID's, refills, pulls", async function() {
interval1 = 86400
window1 = 3600
reward1 = h.toWei("1")
rewardMinusFee1 = reward1 * 0.99
blockyArray1QID1 = new Array()
blockyArray2QID1 = new Array()
interval2 = interval1
window2 = window1
reward2 = h.toWei("2")
rewardMinusFee2 = reward2 * 0.99
blockyArray1QID2 = new Array()
blockyArray2QID2 = new Array()
interval3 = interval1
window3 = window1
reward3 = h.toWei("3")
rewardMinusFee3 = reward3 * 0.99
blockyArray1QID3 = new Array()
blockyArray2QID3 = new Array()
blockyArray1QID1 = new Array()
blockyArray2QID1 = new Array()
await fetch.approve(autopay.address, h.toWei("10000000000"));
blocky = await h.getBlock()
// setup data feed queryId 1
await autopay.connect(accounts[1]).setupDataFeed(ETH_QUERY_ID, reward1, blocky.timestamp, interval1, window1, 0, 0,ETH_QUERY_DATA,0);
feedBytes = abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"], [ETH_QUERY_ID, reward1, blocky.timestamp, interval1,window1, 0, 0])
feedId1 = ethers.utils.keccak256(feedBytes)
// fund feed 1
await autopay.fundFeed(feedId1, ETH_QUERY_ID, h.toWei("2"));
// setup data feed queryId 2
await autopay.connect(accounts[1]).setupDataFeed(BTC_QUERY_ID, reward2, blocky.timestamp + interval1, interval2, window2, 0, 0, BTC_QUERY_DATA,0);
feedBytes = abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"], [BTC_QUERY_ID, reward2, blocky.timestamp + interval1, interval2, window2, 0, 0])
feedId2 = ethers.utils.keccak256(feedBytes)
// fund feed 2
await autopay.fundFeed(feedId2, BTC_QUERY_ID, h.toWei("100"));
// setup data feed queryId 3
await autopay.connect(accounts[1]).setupDataFeed(DOGE_QUERY_ID, reward3, blocky.timestamp + interval1 + interval1, interval3, window3, 0, 0, DOGE_QUERY_DATA,0);
feedBytes = abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"], [DOGE_QUERY_ID, reward3, blocky.timestamp + interval1 + interval1, interval3, window3, 0, 0])
feedId3 = ethers.utils.keccak256(feedBytes)
// fund feed 3
await autopay.fundFeed(feedId3, DOGE_QUERY_ID, h.toWei("100"));
// submit 10 values for queryId 1
for (i = 0; i < 10; i++) {
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(1575 + i), 0, ETH_QUERY_DATA);
blockyArray1QID1[i] = await h.getBlock()
}
// submit 10 values for queryId 2
for (i = 0; i < 10; i++) {
await fetch.connect(accounts[2]).submitValue(BTC_QUERY_ID, h.uintTob32(2575 + i), 0, BTC_QUERY_DATA);
blockyArray1QID2[i] = await h.getBlock()
}
// submit 10 values for queryId 3
for (i = 0; i < 10; i++) {
await fetch.connect(accounts[2]).submitValue(DOGE_QUERY_ID, h.uintTob32(3575 + i), 0, DOGE_QUERY_DATA);
blockyArray1QID3[i] = await h.getBlock()
}
// make sure can't claim any tips before dispute buffer queryID 1
for (i = 0; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [blockyArray1QID1[i].timestamp])); // buffer time hasn't passed
}
// make sure can't claim any tips queryID 2
for (i = 0; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId2, BTC_QUERY_ID, [blockyArray1QID2[i].timestamp])); // buffer time hasn't passed
}
// make sure can't claim any tips queryID 3
for (i = 0; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId3, DOGE_QUERY_ID, [blockyArray1QID3[i].timestamp])); // buffer time hasn't passed
}
// advance time 1 interval
await h.advanceTime(interval1)
// make sure can't claim any tips before dispute buffer queryID 1
for (i = 1; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId1,ETH_QUERY_ID, [blockyArray1QID1[i].timestamp])); // buffer time hasn't passed
}
await autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [blockyArray1QID1[0].timestamp])
// make sure can't claim any tips queryID 2
for (i = 0; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId2, BTC_QUERY_ID, [blockyArray1QID2[i].timestamp])); // buffer time hasn't passed
}
// make sure can't claim any tips queryID 3
for (i = 0; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId3, DOGE_QUERY_ID, [blockyArray1QID3[i].timestamp])); // buffer time hasn't passed
}
// add 2 tips
await autopay.tip(LTC_QUERY_ID, h.toWei("1"),LTC_QUERY_DATA)
await autopay.tip(LTC_QUERY_ID, h.toWei("1"),LTC_QUERY_DATA)
// submit value
await fetch.connect(accounts[2]).submitValue(LTC_QUERY_ID, h.uintTob32(4575), 0, LTC_QUERY_DATA);
blockyTip1 = await h.getBlock()
await h.expectThrow(autopay.connect(accounts[2]).claimOneTimeTip(LTC_QUERY_ID, [blockyTip1.timestamp])) //buffer time hasn't passed
// submit 10 values for queryId 1
for (i = 0; i < 10; i++) {
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(1575 + i), 0, ETH_QUERY_DATA);
blockyArray2QID1[i] = await h.getBlock()
}
// submit 10 values for queryId 2
for (i = 0; i < 10; i++) {
await fetch.connect(accounts[2]).submitValue(BTC_QUERY_ID, h.uintTob32(2575 + i), 0, BTC_QUERY_DATA);
blockyArray2QID2[i] = await h.getBlock()
}
// submit 10 values for queryId 3
for (i = 0; i < 10; i++) {
await fetch.connect(accounts[2]).submitValue(DOGE_QUERY_ID, h.uintTob32(3575 + i), 0, DOGE_QUERY_DATA);
blockyArray2QID3[i] = await h.getBlock()
}
// make sure can't claim any tips before dispute buffer queryID 1
for (i = 0; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [blockyArray2QID1[i].timestamp])); // buffer time hasn't passed
}
// make sure can't claim any tips queryID 2
for (i = 0; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId2, BTC_QUERY_ID, [blockyArray2QID2[i].timestamp])); // buffer time hasn't passed
}
// make sure can't claim any tips queryID 3
for (i = 0; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId3, DOGE_QUERY_ID, [blockyArray2QID3[i].timestamp])); // buffer time hasn't passed
}
// advance time 1 interval
await h.advanceTime(interval1)
// make sure can't claim invalid tips queryID 1
for (i = 1; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [blockyArray2QID1[i].timestamp])); // buffer time hasn't passed
}
// make sure can't claim any tips queryID 2
for (i = 1; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId2, BTC_QUERY_ID, [blockyArray2QID2[i].timestamp])); // buffer time hasn't passed
}
// make sure can't claim any tips queryID 3
for (i = 0; i < 10; i++) {
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId3, DOGE_QUERY_ID, [blockyArray2QID3[i].timestamp])); // buffer time hasn't passed
}
// claim 3 good tips
await autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [blockyArray2QID1[0].timestamp])
await autopay.connect(accounts[2]).claimTip(feedId2, BTC_QUERY_ID, [blockyArray2QID2[0].timestamp])
await autopay.connect(accounts[2]).claimOneTimeTip(LTC_QUERY_ID, [blockyTip1.timestamp])
// refill 3 datafeed accounts
await autopay.fundFeed(feedId1, ETH_QUERY_ID, h.toWei("10"));
await autopay.fundFeed(feedId2, BTC_QUERY_ID, h.toWei("100"));
await autopay.fundFeed(feedId3, DOGE_QUERY_ID, h.toWei("100"));
// submit 10 values for queryId 1
for (i = 0; i < 10; i++) {
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(1575 + i), 0, ETH_QUERY_DATA);
blockyArray1QID1[i] = await h.getBlock()
}
// submit 10 values for queryId 2
for (i = 0; i < 10; i++) {
await fetch.connect(accounts[2]).submitValue(BTC_QUERY_ID, h.uintTob32(2575 + i), 0, BTC_QUERY_DATA);
blockyArray1QID2[i] = await h.getBlock()
}
// submit 10 values for queryId 3
for (i = 0; i < 10; i++) {
await fetch.connect(accounts[2]).submitValue(DOGE_QUERY_ID, h.uintTob32(3575 + i), 0, DOGE_QUERY_DATA);
blockyArray1QID3[i] = await h.getBlock()
}
// add 2 tips
await autopay.tip(LTC_QUERY_ID, h.toWei("1"),LTC_QUERY_DATA)
await autopay.tip(LTC_QUERY_ID, h.toWei("1"),LTC_QUERY_DATA)
// submit value
await fetch.connect(accounts[2]).submitValue(LTC_QUERY_ID, h.uintTob32(4575), 0, LTC_QUERY_DATA);
blockyTip2 = await h.getBlock()
await h.expectThrow(autopay.connect(accounts[2]).claimOneTimeTip(LTC_QUERY_ID, [blockyTip2.timestamp])) //buffer time hasn't passed
// advance time one interval
await h.advanceTime(interval1)
// refill 3 datafeed accounts
await autopay.fundFeed(feedId1, ETH_QUERY_ID, h.toWei("10"));
await autopay.fundFeed(feedId2, BTC_QUERY_ID, h.toWei("100"));
await autopay.fundFeed(feedId3, DOGE_QUERY_ID, h.toWei("100"));
// claim 4 good tips
await autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [blockyArray1QID1[0].timestamp])
await autopay.connect(accounts[2]).claimTip(feedId2, BTC_QUERY_ID, [blockyArray1QID2[0].timestamp])
await autopay.connect(accounts[2]).claimTip(feedId3, DOGE_QUERY_ID, [blockyArray1QID3[0].timestamp])
await autopay.connect(accounts[2]).claimOneTimeTip(LTC_QUERY_ID, [blockyTip2.timestamp])
feedDetails1 = await autopay.getDataFeed(feedId1)
feedDetails2 = await autopay.getDataFeed(feedId2)
feedDetails3 = await autopay.getDataFeed(feedId3)
pastTips = await autopay.getPastTips(LTC_QUERY_ID)
expect(feedDetails1.balance).to.equal(h.toWei("19"))
expect(feedDetails2.balance).to.equal(h.toWei("296"))
expect(feedDetails3.balance).to.equal(h.toWei("297"))
expect(await fetch.balanceOf(accounts[2].address)).to.equal(h.toWei("14.00"))
expect(pastTips.length).to.equal(2)
expect(pastTips[0].amount).to.equal(0)
expect(pastTips[1].amount).to.equal(0)
});
it("priceChange tests", async function() {
let firstBlocky = await h.getBlock();
await autopay.setupDataFeed(ETH_QUERY_ID,h.toWei("1"),firstBlocky.timestamp,86400,600,500,0,ETH_QUERY_DATA,0);
feedId1= ethers.utils.keccak256(abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"],[ETH_QUERY_ID,h.toWei("1"),firstBlocky.timestamp,86400,600,500,0]));
await fetch.approve(autopay.address, h.toWei("1000"));
await autopay.fundFeed(feedId1, ETH_QUERY_ID, h.toWei("1000"));
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA);
firstBlocky = await h.getBlock();
//can grab right away (change from zero)
await h.advanceTime(86400/2)
await autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [firstBlocky.timestamp])
await h.advanceTime(86400/2)
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA);
firstBlocky = await h.getBlock();
//revert on not enough change
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [firstBlocky.timestamp]))
await h.advanceTime(86400)
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA);
firstBlocky = await h.getBlock();
//not enough change but goes through on time
await h.advanceTime(86400/2)
await autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [firstBlocky.timestamp])
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(200), 0, ETH_QUERY_DATA);
firstBlocky = await h.getBlock();
//enough change, gets paid out
//new price > old price
await h.advanceTime(86400/2)
await autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [firstBlocky.timestamp])
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(50), 0, ETH_QUERY_DATA);
firstBlocky = await h.getBlock();
await h.advanceTime(86400/2)
//old price > new price
await autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [firstBlocky.timestamp])
//values are not bytes values
bytesData = abiCoder.encode(["bytes32","uint256", "uint256", "uint256", "uint256", "uint256"],[ETH_QUERY_ID,h.toWei("1"),firstBlocky.timestamp,3600,600,0])
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID,bytesData, 0, ETH_QUERY_DATA);
firstBlocky = await h.getBlock();
await h.advanceTime(86400/2)
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [firstBlocky.timestamp]))
});
it("more priceChange tests", async function() {
let firstBlocky = await h.getBlock();
await autopay.setupDataFeed(ETH_QUERY_ID, h.toWei("1"), firstBlocky.timestamp, 3600, 600, 500, 0, ETH_QUERY_DATA,0);
feedId1 = ethers.utils.keccak256(abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"], [ETH_QUERY_ID, h.toWei("1"), firstBlocky.timestamp, 3600, 600, 500, 0]));
await fetch.approve(autopay.address, h.toWei("1000"));
await autopay.fundFeed(feedId1, ETH_QUERY_ID, h.toWei("1000"));
// up threshold
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA);
firstBlocky = await h.getBlock();
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [firstBlocky.timestamp])) // buffer time not passed
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(106), 0, ETH_QUERY_DATA)
secondBlocky = await h.getBlock();
await h.advanceTime(86400/2)
await autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [firstBlocky.timestamp, secondBlocky.timestamp])
// down threshold
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA)
firstBlocky = await h.getBlock();
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(94), 0, ETH_QUERY_DATA)
secondBlocky = await h.getBlock();
await h.advanceTime(86400/2)
await autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [firstBlocky.timestamp, secondBlocky.timestamp])
// up down down up up - up bad
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA)
firstBlocky = await h.getBlock();
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(106), 0, ETH_QUERY_DATA)
secondBlocky = await h.getBlock();
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA)
thirdBlocky = await h.getBlock();
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(94), 0, ETH_QUERY_DATA)
fourthBlocky = await h.getBlock();
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA)
fifthBlocky = await h.getBlock();
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(106), 0, ETH_QUERY_DATA)
sixthBlocky = await h.getBlock();
// up more without meeting threshold
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(108), 0, ETH_QUERY_DATA)
firstBlockyBad = await h.getBlock()
await h.advanceTime(86400/2)
await autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [firstBlocky.timestamp, secondBlocky.timestamp, thirdBlocky.timestamp, fourthBlocky.timestamp, fifthBlocky.timestamp, sixthBlocky.timestamp])
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [firstBlockyBad.timestamp])) // threshold not met, not first within window
});
it("test incrementing of user tips total with multiple tipping/funding", async function() {
for(i=1; i<6; i++) {
await fetch.faucet(accounts[i].address)
await fetch.connect(accounts[i]).approve(autopay.address,h.toWei("1000"))
assert(await autopay.getTipsByAddress(accounts[i].address) == 0)
}
for(i=1; i<6; i++) {
await autopay.connect(accounts[i]).tip(ETH_QUERY_ID,h.toWei(i.toString()),ETH_QUERY_DATA)
assert(await autopay.getTipsByAddress(accounts[i].address) == h.toWei(i.toString()))
}
blocky = await h.getBlock()
await fetch.approve(autopay.address,h.toWei("1000"))
await autopay.setupDataFeed(ETH_QUERY_ID,h.toWei("1"),blocky.timestamp,3600,600,0,0,ETH_QUERY_DATA,0)
bytesId = ethers.utils.keccak256(abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"],[ETH_QUERY_ID,h.toWei("1"),blocky.timestamp,3600,600,0,0]))
for(i=1; i<6; i++) {
await autopay.connect(accounts[i]).fundFeed(bytesId, ETH_QUERY_ID, h.toWei(i.toString()))
assert(await autopay.getTipsByAddress(accounts[i].address) == h.toWei((i*2).toString()))
}
})
it("test time based rewards", async function() {
blocky0 = await h.getBlock()
const INTERVAL = 3600
// setup data feed with time based rewards
await fetch.faucet(accounts[2].address)
await fetch.connect(accounts[2]).approve(autopay.address, h.toWei("1000"))
feedId = ethers.utils.keccak256(abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"], [ETH_QUERY_ID, h.toWei("1"), blocky0.timestamp, INTERVAL, 600, 0, h.toWei("1")]));
await autopay.setupDataFeed(ETH_QUERY_ID, h.toWei("1"), blocky0.timestamp, 3600, 600, 0, h.toWei("1"), ETH_QUERY_DATA,0);
await autopay.connect(accounts[2]).fundFeed(feedId, ETH_QUERY_ID, h.toWei("1000"));
// advance some time within window
await h.advanceTime(10)
// submit value within window
await fetch.connect(accounts[1]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA);
blocky1 = await h.getBlock();
// advance some time to next window
await h.advanceTime(INTERVAL + 10)
// submit value inside next window
await fetch.connect(accounts[1]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA);
blocky2 = await h.getBlock();
// advance some time to next window
await h.advanceTime(INTERVAL + 10)
// submit value inside next window
await fetch.connect(accounts[1]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA);
blocky3 = await h.getBlock();
// advance time 12 hours to claim rewards
await h.advanceTime(3600 * 12)
// claim rewards
await autopay.connect(accounts[1]).claimTip(feedId, ETH_QUERY_ID, [blocky1.timestamp]);
expectedReward = (BigInt(h.toWei("1")) + BigInt(h.toWei("1")) * (BigInt(blocky1.timestamp) - BigInt(blocky0.timestamp)))
expectedBalance = expectedReward
expect(await fetch.balanceOf(accounts[1].address)).to.equal(expectedBalance)
await autopay.connect(accounts[1]).claimTip(feedId, ETH_QUERY_ID, [blocky2.timestamp]);
expectedReward = (BigInt(h.toWei("1")) + BigInt(h.toWei("1")) * (BigInt(blocky2.timestamp) - BigInt(blocky0.timestamp + INTERVAL * 1)))
expectedBalance = expectedBalance + expectedReward
expect(await fetch.balanceOf(accounts[1].address)).to.equal(expectedBalance)
await autopay.connect(accounts[1]).claimTip(feedId, ETH_QUERY_ID, [blocky3.timestamp]);
expectedReward = (BigInt(h.toWei("1")) + BigInt(h.toWei("1")) * (BigInt(blocky3.timestamp) - BigInt(blocky0.timestamp + INTERVAL * 2)))
expectedBalance = expectedBalance + expectedReward
expect(await fetch.balanceOf(accounts[1].address)).to.equal(expectedBalance)
})
it("test dispute on value", async function() {
// test one time tips
await fetch.approve(autopay.address, h.toWei("200"))
await autopay.tip(ETH_QUERY_ID, h.toWei("25"),ETH_QUERY_DATA)
assert(await autopay.getCurrentTip(ETH_QUERY_ID) == h.toWei("25"))
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA);
blocky1 = await h.getBlock();
await fetch.approve(autopay.address, h.toWei("25"));
await autopay.tip(ETH_QUERY_ID, h.toWei("25"),ETH_QUERY_DATA)
assert(await autopay.getCurrentTip(ETH_QUERY_ID) == h.toWei("25"))
await fetch.beginDispute(ETH_QUERY_ID,blocky1.timestamp)
await fetch.approve(autopay.address, h.toWei("25"));
await autopay.tip(ETH_QUERY_ID, h.toWei("25"),ETH_QUERY_DATA)
//now it should add to previous
assert(await autopay.getCurrentTip(ETH_QUERY_ID) == h.toWei("50"), "current tip should be double")
await fetch.connect(accounts[3]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA);
let blocky2 = await h.getBlock();
await h.advanceTime(3600 * 12)
//acount 2 fails to get one time tip
await h.expectThrow(autopay.connect(accounts[2]).claimOneTimeTip(ETH_QUERY_ID, [blocky1.timestamp]))
await h.expectThrow(autopay.connect(accounts[2]).claimOneTimeTip(ETH_QUERY_ID, [blocky2.timestamp]))
//account 3 gets all the one time tips
let bal1 = await fetch.balanceOf(accounts[3].address)
await autopay.connect(accounts[3]).claimOneTimeTip(ETH_QUERY_ID, [blocky2.timestamp])
let bal2 = await fetch.balanceOf(accounts[3].address)
expectedReward = h.toWei((75 * (1000 - 0) / 1000).toString())
assert(bal2 - bal1 == expectedReward, "one time tip payout should be correct")
// test autopay tips
await fetch.approve(autopay.address, h.toWei("100"))
let blocky3 = await h.getBlock();
await autopay.setupDataFeed(ETH_QUERY_ID, h.toWei("1"), blocky3.timestamp, 3600, 600, 0, 0, ETH_QUERY_DATA,h.toWei("100"));
feedId1 = ethers.utils.keccak256(abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"], [ETH_QUERY_ID, h.toWei("1"), blocky3.timestamp, 3600, 600, 0, 0]));
await fetch.connect(accounts[4]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA);
blocky4 = await h.getBlock();
await fetch.beginDispute(ETH_QUERY_ID,blocky4.timestamp)
await fetch.connect(accounts[5]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA);
blocky5 = await h.getBlock();
await fetch.beginDispute(ETH_QUERY_ID,blocky5.timestamp)
await fetch.connect(accounts[6]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA);
blocky6 = await h.getBlock();
await h.advanceTime(3600 * 12)
await h.expectThrow(autopay.connect(accounts[4]).claimTip(feedId1, ETH_QUERY_ID, [blocky4.timestamp]))
await h.expectThrow(autopay.connect(accounts[5]).claimTip(feedId1, ETH_QUERY_ID, [blocky5.timestamp]))
await autopay.connect(accounts[6]).claimTip(feedId1, ETH_QUERY_ID, [blocky6.timestamp])
expectedReward = h.toWei((1 * (1000 - 0) / 1000).toString())
assert(await fetch.balanceOf(accounts[6].address) == expectedReward, "autopay payout should be correct")
})
it("one time tip same block as report", async function() {
// deploy TipAndReport contract, which tips and reports in same block
const TipAndReport = await ethers.getContractFactory("TipAndReport");
tipAndReport = await TipAndReport.deploy(fetch.address, autopay.address);
await tipAndReport.deployed();
await fetch.transfer(tipAndReport.address, h.toWei("1"));
expect(await fetch.balanceOf(tipAndReport.address)).to.equal(h.toWei("1"));
await tipAndReport.tipAndSubmitValue(ETH_QUERY_ID, h.toWei("1"), h.uintTob32(100), ETH_QUERY_DATA);
blocky = await h.getBlock();
expect(await fetch.balanceOf(tipAndReport.address)).to.equal(0);
expect(await fetch.balanceOf(autopay.address)).to.equal(h.toWei("1"));
tip = await autopay.getPastTipByIndex(ETH_QUERY_ID, 0)
expect(tip.timestamp).to.equal(blocky.timestamp + 1)
expect(tip.amount).to.equal(h.toWei("1"))
expect(await fetch.getTimestampbyQueryIdandIndex(ETH_QUERY_ID, 0)).to.equal(blocky.timestamp)
await h.advanceTime(3600 * 12)
await h.expectThrow(tipAndReport.claimOneTimeTip(ETH_QUERY_ID, [blocky.timestamp]))
})
it("test no claimTips to pay out", async function() {
let blocky0 = await h.getBlock();
await autopay.setupDataFeed(ETH_QUERY_ID, h.toWei("1"), blocky0.timestamp, 3600, 600, 0, 0, ETH_QUERY_DATA,0);
feedId1 = ethers.utils.keccak256(abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"], [ETH_QUERY_ID, h.toWei("1"), blocky0.timestamp, 3600, 600, 0, 0]));
// submit a value, eligible for autopay reward but no balance to pay out
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA);
let blocky1 = await h.getBlock();
await h.advanceTime(3600 * 12)
// claim reward with zero balance
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [blocky1.timestamp]))
// claim reward with balance but report not eligibe
await fetch.faucet(accounts[0].address)
await fetch.approve(autopay.address, h.toWei("100"));
await autopay.fundFeed(feedId1, ETH_QUERY_ID, h.toWei("100"));
await h.advanceTime(3600 / 2)
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA);
let blocky2 = await h.getBlock();
await h.advanceTime(3600 * 12)
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [blocky2.timestamp]))
})
it("test tip, submitValue, tip, submitValue, tip, submitValue, dispute first two", async function() {
await fetch.approve(autopay.address, h.toWei("1000"));
await autopay.tip(ETH_QUERY_ID, h.toWei("1"),ETH_QUERY_DATA)
await fetch.connect(accounts[1]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA);
blocky1 = await h.getBlock();
await autopay.tip(ETH_QUERY_ID, h.toWei("10"),ETH_QUERY_DATA)
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(101), 0, ETH_QUERY_DATA);
blocky2 = await h.getBlock();
await autopay.tip(ETH_QUERY_ID, h.toWei("20"),ETH_QUERY_DATA)
await fetch.connect(accounts[3]).submitValue(ETH_QUERY_ID, h.uintTob32(102), 0, ETH_QUERY_DATA);
blocky3 = await h.getBlock();
await fetch.beginDispute(ETH_QUERY_ID, blocky1.timestamp)
await fetch.beginDispute(ETH_QUERY_ID, blocky2.timestamp)
await h.advanceTime(3600 * 12)
await h.expectThrow(autopay.connect(accounts[1]).claimOneTimeTip(ETH_QUERY_ID, [blocky1.timestamp]))
await h.expectThrow(autopay.connect(accounts[2]).claimOneTimeTip(ETH_QUERY_ID, [blocky2.timestamp]))
await autopay.connect(accounts[3]).claimOneTimeTip(ETH_QUERY_ID, [blocky3.timestamp])
reporterBal = await fetch.balanceOf(accounts[3].address)
expectedBal = h.toWei((31 * (1000 - 0) / 1000).toString())
expect(reporterBal).to.equal(expectedBal)
})
it("ensure getCurrentTip doesn't fail if no tip", async function() {
tipsArray = await autopay.getPastTips(ETH_QUERY_ID)
assert(tipsArray.length == 0, "tipsArray should be empty")
currentTip = await autopay.getCurrentTip(ETH_QUERY_ID)
assert(currentTip == 0, "currentTip should be 0")
})
it("test query data storage", async function() {
await fetch.approve(autopay.address, h.toWei("1000"))
queryDataArgs = abiCoder.encode(["string", "string"], ["eth", "usd"]);
queryData = abiCoder.encode(["string", "bytes"], ["SpotPrice", queryDataArgs]);
queryId = keccak256(queryData);
await autopay.tip(queryId,web3.utils.toWei("10"),queryData)
storedQueryData = await queryDataStorage.getQueryData(queryId);
assert(storedQueryData == queryData, "query data not stored correctly");
await autopay.tip(queryId,web3.utils.toWei("10"),queryData)
storedQueryData = await queryDataStorage.getQueryData(queryId);
assert(storedQueryData == queryData, "query data not stored correctly");
await fetch.connect(accounts[2]).submitValue(queryId, h.uintTob32(3550), 0, queryData);
await autopay.tip(queryId,web3.utils.toWei("10"),queryData)
storedQueryData = await queryDataStorage.getQueryData(queryId);
assert(storedQueryData == queryData, "query data not stored correctly");
queryDataArgs = abiCoder.encode(["string", "string"], ["eth", "usd"]);
queryData = abiCoder.encode(["string", "bytes"], ["SpotPrice", queryDataArgs]);
queryId = keccak256(queryData);
blocky = await h.getBlock();
await autopay.setupDataFeed(queryId,h.toWei("1"),blocky.timestamp,3600,600,1,3,queryData,0);
storedQueryData = await queryDataStorage.getQueryData(queryId);
assert(storedQueryData == queryData, "query data not stored correctly");
// setup second feed for same query id
await autopay.setupDataFeed(queryId,h.toWei("1"),blocky.timestamp,3600,1200,1,3,queryData,0);
storedQueryData = await queryDataStorage.getQueryData(queryId);
assert(storedQueryData == queryData, "query data not stored correctly");
})
it("reward expires after 1 month", async function() {
await fetch.approve(autopay.address, h.toWei("100"))
let blocky0 = await h.getBlock();
await autopay.setupDataFeed(ETH_QUERY_ID, h.toWei("1"), blocky0.timestamp, 3600, 600, 0, 0, ETH_QUERY_DATA, h.toWei("100"));
feedId1 = ethers.utils.keccak256(abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"], [ETH_QUERY_ID, h.toWei("1"), blocky0.timestamp, 3600, 600, 0, 0]));
// submit 2 values, eligible for autopay reward
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA);
blocky1 = await h.getBlock();
await h.advanceTime(3600)
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(101), 0, ETH_QUERY_DATA);
blocky2 = await h.getBlock();
await h.advanceTime(3600 * 12)
// claim reward
await autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [blocky2.timestamp]);
expectedReward = await autopay.getRewardAmount(feedId1, ETH_QUERY_ID, [blocky1.timestamp]);
assert(expectedReward == h.toWei("1.0"), "reward not correct");
// advance past expiration time
await h.advanceTime(3600 * 24 * 31)
await h.expectThrow(autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [blocky1.timestamp]));
})
it("price threshold met and report within interval with tbr, should get sloped reward", async function() {
await fetch.approve(autopay.address, h.toWei("100"))
let blocky0 = await h.getBlock();
await autopay.setupDataFeed(ETH_QUERY_ID, h.toWei("1"), blocky0.timestamp, 3600, 600, 100, h.toWei("1"), ETH_QUERY_DATA, h.toWei("100"));
feedId1 = ethers.utils.keccak256(abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"], [ETH_QUERY_ID, h.toWei("1"), blocky0.timestamp, 3600, 600, 100, h.toWei("1")]));
// submit value to set baseline
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(100), 0, ETH_QUERY_DATA);
await h.advanceTime(3601)
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(200), 0, ETH_QUERY_DATA);
blocky1 = await h.getBlock();
await h.advanceTime(3600 * 12)
assert(await fetch.balanceOf(accounts[2].address) == 0, "balance should be 0");
await autopay.connect(accounts[2]).claimTip(feedId1, ETH_QUERY_ID, [blocky1.timestamp]);
windowStart = blocky0.timestamp + 3600;
expectedRewardPlusFee = 1 + (blocky1.timestamp - windowStart)
expectedBalance = BigInt(h.toWei(expectedRewardPlusFee.toString())) * BigInt(1000 - 0) / BigInt(1000);
assert(await fetch.balanceOf(accounts[2].address) > h.toWei("1"), "reward not correct");
assert(await fetch.balanceOf(accounts[2].address) == expectedBalance, "reward not correct");
})
});