-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctionTests-FetchAutopay.js
786 lines (742 loc) · 44.8 KB
/
functionTests-FetchAutopay.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
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 - function tests", () => {
let fetch,autopay,accounts,firstBlocky,blocky,dataFeedBefore,bytesId,queryDataStorage;
let array = [];
let badArray = [];
let abiCoder = new ethers.utils.AbiCoder();
const QUERYID2 = h.uintTob32(2);
const FEE = 0
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 () => {
accounts = await ethers.getSigners();
const FetchPlayground = await ethers.getContractFactory("FetchPlayground");
fetch = await FetchPlayground.deploy();
await fetch.deployed();
for(i=0; i<2; i++){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();
firstBlocky = await h.getBlock();
await autopay.setupDataFeed(ETH_QUERY_ID,h.toWei("1"),firstBlocky.timestamp,3600,600,0,0,ETH_QUERY_DATA,0);
bytesId = keccak256(abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"],[ETH_QUERY_ID,h.toWei("1"),firstBlocky.timestamp,3600,600,0,0]));
await fetch.approve(autopay.address, h.toWei("1000"));
await autopay.fundFeed(bytesId, ETH_QUERY_ID, h.toWei("1000"));
payerBefore = await autopay.getDataFeed(bytesId);
await fetch.connect(accounts[1]).submitValue(ETH_QUERY_ID, h.uintTob32(3500), 0, ETH_QUERY_DATA);
blocky = await h.getBlock();
array[0] = blocky.timestamp;
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(3525), 0, ETH_QUERY_DATA);
blocky = await h.getBlock();
badArray[0] = blocky.timestamp;
await fetch.connect(accounts[1]).submitValue(ETH_QUERY_ID, h.uintTob32(3550), 0, ETH_QUERY_DATA);
h.advanceTime(3600);
await fetch.connect(accounts[1]).submitValue(ETH_QUERY_ID, h.uintTob32(3550), 0, ETH_QUERY_DATA);
blocky = await h.getBlock();
array[1] = blocky.timestamp;
badArray[1] = blocky.timestamp;
h.advanceTime(3600);
await fetch.connect(accounts[1]).submitValue(ETH_QUERY_ID, h.uintTob32(3575), 0, ETH_QUERY_DATA);
blocky = await h.getBlock();
array[2] = blocky.timestamp;
});
it("constructor", async () => {
expect(await autopay.fetch()).to.equal(fetch.address);
expect(await autopay.token()).to.equal(fetch.address);
expect(await autopay.queryDataStorage()).to.equal(queryDataStorage.address);
});
it("claimTip - require statements", async function() {
// **ERROR MSG: no tips submitted for this queryId
fakeFeedId = h.hash("fakeFeedId");
fakeQueryId = h.hash("fakeQueryId");
result = await h.expectThrowMessage(autopay.claimTip(fakeFeedId, fakeQueryId, [1234]));
assert.include(result.message, "no funds available for this feed");
// **ERROR MSG: buffer time has not passed
result = await h.expectThrowMessage(autopay.connect(accounts[1]).claimTip(bytesId, ETH_QUERY_ID, array));
assert.include(result.message, "buffer time has not passed");
// **ERROR MSG: message sender not reporter for given queryId and timestamp
await h.advanceTime(43200);
result = await h.expectThrowMessage(autopay.claimTip(bytesId, ETH_QUERY_ID, array));
assert.include(result.message, "message sender not reporter for given queryId and timestamp");
// **ERROR MSG: reward already claimed
await autopay.connect(accounts[1]).claimTip(bytesId, ETH_QUERY_ID, [array[0]]);
result = await h.expectThrowMessage(autopay.connect(accounts[1]).claimTip(bytesId, ETH_QUERY_ID, [array[0]]));
assert.include(result.message, "reward already claimed");
// **ERROR MSG: no value exists at timestamp
await fetch.beginDispute(ETH_QUERY_ID, array[1])
result = await h.expectThrowMessage(autopay.connect(accounts[1]).claimTip(bytesId, ETH_QUERY_ID, [array[1]]));
assert.include(result.message, "no value exists at timestamp");
// **ERROR MSG: price threshold not met
await autopay.setupDataFeed(ETH_QUERY_ID,h.toWei("1"),firstBlocky.timestamp,3600000,2,10000,0,ETH_QUERY_DATA,0);
feedIdPriceThreshold = keccak256(abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"],[ETH_QUERY_ID,h.toWei("1"),firstBlocky.timestamp,3600000,2,10000,0]));
await fetch.approve(autopay.address, h.toWei("1000"));
await autopay.fundFeed(feedIdPriceThreshold, ETH_QUERY_ID, h.toWei("1"));
await fetch.connect(accounts[1]).submitValue(ETH_QUERY_ID, h.uintTob32(3500), 0, ETH_QUERY_DATA);
await fetch.connect(accounts[1]).submitValue(ETH_QUERY_ID, h.uintTob32(3501), 0, ETH_QUERY_DATA);
blockyPT = await h.getBlock();
await h.advanceTime(43200);
result = await h.expectThrowMessage(autopay.connect(accounts[1]).claimTip(feedIdPriceThreshold, ETH_QUERY_ID, [blockyPT.timestamp]));
assert.include(result.message, "price threshold not met");
// **ERROR MSG: insufficient balance for all submitted timestamps
await fetch.connect(accounts[1]).submitValue(ETH_QUERY_ID, h.uintTob32(35000), 0, ETH_QUERY_DATA);
blockyPT1 = await h.getBlock();
await fetch.connect(accounts[1]).submitValue(ETH_QUERY_ID, h.uintTob32(350000), 0, ETH_QUERY_DATA);
blockyPT2 = await h.getBlock();
await h.advanceTime(43200);
result = await h.expectThrowMessage(autopay.connect(accounts[1]).claimTip(feedIdPriceThreshold, ETH_QUERY_ID, [blockyPT1.timestamp, blockyPT2.timestamp]));
assert.include(result.message, "insufficient balance for all submitted timestamps");
// **ERROR MSG: timestamp too old to claim tip
await h.advanceTime(86400 * 7 * 4 * 6)
result = await h.expectThrowMessage(autopay.connect(accounts[1]).claimTip(bytesId, ETH_QUERY_ID, [array[2]]));
assert.include(result.message, "timestamp too old to claim tip");
})
it("claimTip", async () => {
// Advancing time 12 hours to satisfy hardcoded buffer time.
await h.advanceTime(43200);
await expect(autopay.connect(accounts[1]).claimTip(bytesId, ETH_QUERY_ID, array)).to.emit(autopay, "TipClaimed").withArgs(bytesId, ETH_QUERY_ID, (h.toWei("3")), accounts[1].address);
let payerAfter = await autopay.getDataFeed(bytesId);
expect(payerBefore.balance).to.not.equal(payerAfter.balance);
expect(payerAfter.balance).to.equal(h.toWei("997"));
// Updating Balance Checks
// 1% of each tip being shaved for Fetch ~= .01 token/tip claimed
// That's why fetch balance is .03 lower than originally expected.
expect(await fetch.balanceOf(accounts[1].address)).to.equal(h.toWei("3.00"));
// Checking if owner (Fetch) account was updated by fee amount (0.03)
expect(await fetch.balanceOf(autopay.address)).to.equal(h.toWei("997"));
});
it("_getRewardAmount", async () => {
h.advanceTime(43200);
blocky = await h.getBlock();
await autopay.connect(accounts[10]).setupDataFeed(ETH_QUERY_ID,h.toWei("1"),blocky.timestamp,3600,600,0,0,ETH_QUERY_DATA,0);
bytesId0 = keccak256(abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"],[ETH_QUERY_ID,h.toWei("1"),blocky.timestamp,3600,600,0,0]));
await fetch.approve(autopay.address, h.toWei("100"));
await autopay.fundFeed(bytesId0, ETH_QUERY_ID, h.toWei("100"));
await fetch.connect(accounts[1]).submitValue(ETH_QUERY_ID, h.uintTob32(3550), 0, ETH_QUERY_DATA);
let goodBlocky = await h.getBlock();
h.advanceTime(43201);
// Variable updates
dataFeedBefore = await autopay.getDataFeed(bytesId0);
await autopay.connect(accounts[1]).claimTip(bytesId0, ETH_QUERY_ID, [goodBlocky.timestamp]);
dataFeedAfter = await autopay.getDataFeed(bytesId0);
claimedStatus = await autopay.getRewardClaimedStatus(bytesId0,ETH_QUERY_ID,goodBlocky.timestamp);
expect(dataFeedAfter.balance).to.equal(h.toWei("99"));
expect(claimedStatus).to.be.true;
});
it("fundFeed", async () => {
let result;
let dataFeedDetails;
//REQUIRE CHECKS
// require(_feed.reward > 0,"feed not set up");
result = await h.expectThrowMessage(autopay.fundFeed(bytesId, QUERYID2, h.toWei("1000000")));
assert.include(result.message, "feed not set up");
// require(_amount > 0, "must be sending an amount");
result = await h.expectThrowMessage(autopay.fundFeed(bytesId, ETH_QUERY_ID, 0));
assert.include(result.message, "must be sending an amount");
// require(IERC20(_feed.fetch).transferFrom(msg.sender,address(this),_amount),"ERC20: transfer amount exceeds balance");
result = await h.expectThrowMessage(autopay.fundFeed(bytesId, ETH_QUERY_ID, h.toWei("1000300")));
const acceptableMessages = [
"Arithmetic operation underflowed or overflowed outside of an unchecked block",
"Arithmetic operation overflowed outside of an unchecked block",
"Arithmetic operation underflowed outside of an unchecked block"
];
const isAcceptableMessage = acceptableMessages.some(message => result.message.includes(message));
assert.isTrue(isAcceptableMessage, 'Expected one of the acceptable messages');
//VARIABLE UPDATES
// _feed.balance += _amount;
dataFeedDetails = await autopay.getDataFeed(bytesId);
expect(dataFeedDetails.balance).to.equal(h.toWei("1000"));
//EVENT DETAILS
// emit DataFeedFunded(_feedId,_queryId,_amount,_feedFunder);
await fetch.approve(autopay.address, h.toWei("100"));
let initBal = await fetch.balanceOf(autopay.address)
await expect(autopay.fundFeed(bytesId, ETH_QUERY_ID, h.toWei("10"))).to.emit(autopay, "DataFeedFunded").withArgs(bytesId, ETH_QUERY_ID, h.toWei("10"), accounts[0].address, [web3.utils.toWei("1"), h.toWei("1010"), firstBlocky.timestamp, 3600, 600, 0, 0, 1]);
expect(await fetch.balanceOf(autopay.address) - initBal == h.toWei("10"), "balance should change")
});
it("setupDataFeed", async () => {
//**ERROR MSG: id must be hash of bytes data
result = await h.expectThrowMessage(autopay.setupDataFeed(h.uintTob32(200),h.toWei("1"),blocky.timestamp,3600,600,0,0,ETH_QUERY_DATA,0));//must be hash
assert.include(result.message, "id must be hash of bytes data");
//**ERROR MSG: reward must be greater than zero
result = await h.expectThrowMessage(autopay.setupDataFeed(ETH_QUERY_ID,h.toWei("0"),blocky.timestamp,3600,600,0,0,ETH_QUERY_DATA,0));//reward is zero
assert.include(result.message, "reward must be greater than zero");
//**ERROR MSG: feed must not be set up already
await autopay.setupDataFeed(ETH_QUERY_ID,h.toWei("1"),blocky.timestamp,601,600,0,0,ETH_QUERY_DATA,0);
result = await h.expectThrowMessage(autopay.setupDataFeed(ETH_QUERY_ID,h.toWei("1"),blocky.timestamp,601,600,0,0,ETH_QUERY_DATA,0));//already set up
assert.include(result.message, "feed must not be set up already");
//**ERROR MSG: window must be less than interval length
result = await h.expectThrowMessage(autopay.setupDataFeed(ETH_QUERY_ID,h.toWei("1"),blocky.timestamp,600,3600,0,0,ETH_QUERY_DATA,0));//interval > window
assert.include(result.message, "window must be less than interval length");
//**ERROR MSG: interval must be greater than zero
result = await h.expectThrowMessage(autopay.setupDataFeed(ETH_QUERY_ID,h.toWei("1"),blocky.timestamp,0,600,0,0,ETH_QUERY_DATA,0));//interval is zero
assert.include(result.message, "interval must be greater than zero");
// first, simulate call with callStatic to retrieve feedId returned by setupDataFeed
feedIdRetrieved = await autopay.callStatic.setupDataFeed(ETH_QUERY_ID,h.toWei("1"),firstBlocky.timestamp,3600,600,1,3,ETH_QUERY_DATA,0);
await autopay.setupDataFeed(ETH_QUERY_ID,h.toWei("1"),firstBlocky.timestamp,3600,600,1,3,ETH_QUERY_DATA,0);
feedId = keccak256(abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"],[ETH_QUERY_ID,h.toWei("1"),firstBlocky.timestamp,3600,600,1,3]));
assert(feedId == feedIdRetrieved, "setupDataFeed should return the feedId");
result = await autopay.getDataFeed(feedId);
expect(result.reward).to.equal(h.toWei("1"))
expect(result.balance).to.equal(0);
expect(result.startTime).to.equal(firstBlocky.timestamp);
expect(result.interval).to.equal(3600);
expect(result.window).to.equal(600);
expect(result.priceThreshold).to.equal(1);
expect(result.rewardIncreasePerSecond).to.equal(3);
expect(result.feedsWithFundingIndex).to.equal(0);
await fetch.approve(autopay.address, h.toWei("100"));
await autopay.setupDataFeed(ETH_QUERY_ID,h.toWei("1"),firstBlocky.timestamp,7600,600,2,4,ETH_QUERY_DATA,h.toWei("10"));
queryDataArgs = abiCoder.encode(["string", "string"], ["btc", "usd"]);
queryData = abiCoder.encode(["string", "bytes"], ["SpotPrice", queryDataArgs]);
queryId = keccak256(queryData);
await autopay.setupDataFeed(queryId,h.toWei("1"),firstBlocky.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"),firstBlocky.timestamp,3600,1200,1,3,queryData,0);
storedQueryData = await queryDataStorage.getQueryData(queryId);
assert(storedQueryData == queryData, "query data not stored correctly");
});
it("getRewardClaimedStatus", async () => {
let v = array[0]
result = await autopay.getRewardClaimedStatus(bytesId,ETH_QUERY_ID,v);
expect(result).to.be.false;
h.advanceTime(86400)
await autopay.connect(accounts[1]).claimTip(bytesId, ETH_QUERY_ID, [v]);
result = await autopay.getRewardClaimedStatus(bytesId,ETH_QUERY_ID,v);
expect(result).to.be.true;
});
it("tip", async () => {
await fetch.faucet(accounts[0].address)
await h.expectThrow(autopay.tip(ETH_QUERY_ID,web3.utils.toWei("100"),ETH_QUERY_DATA));
await fetch.approve(autopay.address,web3.utils.toWei("1000"))
await h.expectThrow(autopay.tip(h.uintTob32(200),web3.utils.toWei("100"),'0x')) //must be hash
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("100"),ETH_QUERY_DATA)
let res = await autopay.getCurrentTip(ETH_QUERY_ID);
assert(res == web3.utils.toWei("100"), "tip 1nshould be correct")
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(3550), 0, ETH_QUERY_DATA);
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("200"),ETH_QUERY_DATA)
res = await autopay.getCurrentTip(ETH_QUERY_ID);
assert(res == web3.utils.toWei("200"), "tip 2 should be correct")
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("300"),ETH_QUERY_DATA)
res = await autopay.getCurrentTip(ETH_QUERY_ID);
assert(res == web3.utils.toWei("500"), "tip 3 should be correct")
// test query data storage
queryDataArgs = abiCoder.encode(["string", "string"], ["btc", "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");
});
it("claimOneTimeTip - require statements", async () => {
// **** 1) test require statements: ****
// **ERROR MSG: no tips submitted for this queryId
result = await h.expectThrowMessage(autopay.claimOneTimeTip(ETH_QUERY_ID,[12345]));
assert.include(result.message, "no tips submitted for this queryId")
// **ERROR MSG: buffer time has not passed
await fetch.approve(autopay.address,web3.utils.toWei("100"))
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("1"),ETH_QUERY_DATA)
await fetch.connect(accounts[4]).submitValue(ETH_QUERY_ID, h.uintTob32(3550), 0, ETH_QUERY_DATA);
blocky0 = await h.getBlock()
result = await h.expectThrowMessage(autopay.connect(accounts[4]).claimOneTimeTip(ETH_QUERY_ID,[blocky0.timestamp]));
assert.include(result.message, "buffer time has not passed")
await h.advanceTime(86400/2)
await autopay.connect(accounts[4]).claimOneTimeTip(ETH_QUERY_ID,[blocky0.timestamp])
// **ERROR MSG: value disputed
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("1"),ETH_QUERY_DATA)
await fetch.connect(accounts[4]).submitValue(ETH_QUERY_ID, h.uintTob32(3550), 0, ETH_QUERY_DATA);
blocky1 = await h.getBlock()
await h.advanceTime(86400/2)
await fetch.beginDispute(ETH_QUERY_ID, blocky1.timestamp)
// await autopay.connect(accounts[4]).claimOneTimeTip(ETH_QUERY_ID,[blocky1.timestamp])
result = await h.expectThrowMessage(autopay.connect(accounts[4]).claimOneTimeTip(ETH_QUERY_ID, [blocky1.timestamp]))
assert.include(result.message, "value disputed")
// **ERROR MSG: msg sender must be reporter address
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("1"),ETH_QUERY_DATA)
await fetch.connect(accounts[4]).submitValue(ETH_QUERY_ID, h.uintTob32(3550), 0, ETH_QUERY_DATA);
blocky2 = await h.getBlock()
await h.advanceTime(86400/2)
result = await h.expectThrowMessage(autopay.connect(accounts[3]).claimOneTimeTip(ETH_QUERY_ID, [blocky2.timestamp]))
assert.include(result.message, "msg sender must be reporter address")
await autopay.connect(accounts[4]).claimOneTimeTip(ETH_QUERY_ID, [blocky2.timestamp])
// **ERROR MSG: tip earned by previous submission
await autopay.tip(ETH_QUERY_ID, h.toWei("1"), ETH_QUERY_DATA)
await fetch.connect(accounts[4]).submitValue(ETH_QUERY_ID, h.uintTob32(3550), 0, ETH_QUERY_DATA);
blocky3 = await h.getBlock()
await fetch.connect(accounts[4]).submitValue(ETH_QUERY_ID, h.uintTob32(3550), 0, ETH_QUERY_DATA);
blocky4 = await h.getBlock()
await h.advanceTime(86400/2)
result = await h.expectThrowMessage(autopay.connect(accounts[4]).claimOneTimeTip(ETH_QUERY_ID, [blocky4.timestamp]))
assert.include(result.message, "tip earned by previous submission")
await autopay.connect(accounts[4]).claimOneTimeTip(ETH_QUERY_ID, [blocky3.timestamp])
// **ERROR MSG: timestamp not eligible for tip
await fetch.connect(accounts[4]).submitValue(h.hash(h.bytes("hello")), h.uintTob32(3550), 0, h.bytes("hello"))
blocky5 = await h.getBlock()
await autopay.tip(h.hash(h.bytes("hello")), h.toWei("1"), h.bytes("hello"))
await fetch.connect(accounts[4]).submitValue(h.hash(h.bytes("hello")), h.uintTob32(3550), 0, h.bytes("hello"))
blocky6 = await h.getBlock()
await h.advanceTime(86400/2)
result = await h.expectThrowMessage(autopay.connect(accounts[4]).claimOneTimeTip(h.hash(h.bytes("hello")), [blocky5.timestamp]))
assert.include(result.message, "timestamp not eligible for tip")
await autopay.connect(accounts[4]).claimOneTimeTip(h.hash(h.bytes("hello")), [blocky6.timestamp])
// **ERROR MSG: tip already claimed
result = await h.expectThrowMessage(autopay.connect(accounts[4]).claimOneTimeTip(h.hash(h.bytes("hello")), [blocky6.timestamp]))
assert.include(result.message, "tip already claimed")
});
it("claimOneTimeTip", async () => {
let startBal = await fetch.balanceOf(accounts[2].address);
await fetch.connect(accounts[4]).submitValue(ETH_QUERY_ID, h.uintTob32(3550), 0, ETH_QUERY_DATA);
blocky1 = await h.getBlock();
await h.expectThrow(autopay.claimOneTimeTip(ETH_QUERY_ID,[blocky1.timestamp]));
await fetch.approve(autopay.address,web3.utils.toWei("100"))
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("100"),ETH_QUERY_DATA)
await h.expectThrow(autopay.connect(accounts[4]).claimOneTimeTip(ETH_QUERY_ID,[blocky1.timestamp]));//timestamp not eligible
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(3550), 0, ETH_QUERY_DATA);
blocky = await h.getBlock();
await h.advanceTime(3600 * 12)
await autopay.connect(accounts[2]).claimOneTimeTip(ETH_QUERY_ID,[blocky.timestamp])
let res = await autopay.getCurrentTip(ETH_QUERY_ID);
assert(res == 0, "tip should be correct")
let finBal = await fetch.balanceOf(accounts[2].address);
assert(finBal - startBal == web3.utils.toWei("100"), "balance should change correctly")
});
it("getDataFeed", async () => {
result = await autopay.getDataFeed(bytesId);
expect(result[0]).to.equal(h.toWei("1"));
expect(result[1]).to.equal(h.toWei("1000"));
expect(result[2]).to.equal(firstBlocky.timestamp);
expect(result[3]).to.equal(3600);
expect(result[4]).to.equal(600);
expect(result[5]).to.equal(0);
expect(result[6]).to.equal(0);
});
it("getCurrentTip", async () => {
let res = await autopay.getCurrentTip(ETH_QUERY_ID);
assert(res == 0, "tip amount should be zero")
await h.expectThrow(autopay.tip(ETH_QUERY_ID,web3.utils.toWei("100"),ETH_QUERY_DATA));
await fetch.approve(autopay.address,web3.utils.toWei("100"))
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("100"),ETH_QUERY_DATA)
res = await autopay.getCurrentTip(ETH_QUERY_ID);
assert(res == web3.utils.toWei("100"), "tip should be correct")
});
it("getPastTips", async () => {
await fetch.faucet(accounts[0].address)
let res = await autopay.getPastTips(ETH_QUERY_ID)
assert(res.length == 0, "should be no tips",)
await fetch.approve(autopay.address,web3.utils.toWei("100"))
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("100"),ETH_QUERY_DATA)
let blocky1 = await h.getBlock();
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(3550), 0, ETH_QUERY_DATA);
await fetch.approve(autopay.address,web3.utils.toWei("200"))
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("200"),ETH_QUERY_DATA)
let blocky2 = await h.getBlock();
res = await autopay.getPastTips(ETH_QUERY_ID)
assert(res[0][0] == web3.utils.toWei("100"), "past tip amount should be correct")
assert(res[0][1] == blocky1.timestamp + 1, "past tip timestamp should be correct")
assert(res[1][0] == web3.utils.toWei("200"), "past tip amount should be correct")
assert(res[1][1] == blocky2.timestamp + 1, "past tip timestamp should be correct")
await fetch.approve(autopay.address,web3.utils.toWei("300"))
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("300"),ETH_QUERY_DATA)
let blocky3 = await h.getBlock();
res = await autopay.getPastTips(ETH_QUERY_ID)
assert(res[0][0] == web3.utils.toWei("100"), "past tip amount should be correct")
assert(res[0][1] == blocky1.timestamp + 1, "past tip 1 timestamp should be correct")
assert(res[1][0] == web3.utils.toWei("500"), "past tip amount 2 should be correct")
assert(res[1][1] == blocky3.timestamp + 1, "past tip 2 timestamp should be correct")
assert(res.length == 2, "length should be correct")
});
it("getPastTipByIndex", async () => {
await fetch.faucet(accounts[0].address)
await fetch.approve(autopay.address,web3.utils.toWei("100"))
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("100"),ETH_QUERY_DATA)
let blocky1 = await h.getBlock();
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(3550), 0, ETH_QUERY_DATA);
await fetch.approve(autopay.address,web3.utils.toWei("200"))
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("200"),ETH_QUERY_DATA)
let blocky2 = await h.getBlock();
res = await autopay.getPastTipByIndex(ETH_QUERY_ID,0)
assert(res[0] == web3.utils.toWei("100"), "past tip amount should be correct")
assert(res[1] == blocky1.timestamp + 1, "past tip amount should be correct")
res = await autopay.getPastTipByIndex(ETH_QUERY_ID,1)
assert(res[0] == web3.utils.toWei("200"), "past tip amount should be correct")
assert(res[1] == blocky2.timestamp + 1, "past tip amount should be correct")
await fetch.approve(autopay.address,web3.utils.toWei("300"))
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("300"),ETH_QUERY_DATA)
let blocky3 = await h.getBlock();
res = await autopay.getPastTipByIndex(ETH_QUERY_ID,0)
assert(res[0] == web3.utils.toWei("100"), "past tip amount should be correct")
assert(res[1] == blocky1.timestamp + 1, "past tip 1 timestamp should be correct")
res = await autopay.getPastTipByIndex(ETH_QUERY_ID,1)
assert(res[0] == web3.utils.toWei("500"), "past tip amount 2 should be correct")
assert(res[1] == blocky3.timestamp + 1, "past tip 2 timestamp should be correct")
});
it("getPastTipCount", async () => {
let res = await autopay.getPastTipCount(ETH_QUERY_ID)
assert(res == 0, "past tip count should be correct")
await fetch.faucet(accounts[0].address)
await fetch.approve(autopay.address,web3.utils.toWei("100"))
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("100"),ETH_QUERY_DATA)
await fetch.connect(accounts[2]).submitValue(ETH_QUERY_ID, h.uintTob32(3550), 0, ETH_QUERY_DATA);
await fetch.approve(autopay.address,web3.utils.toWei("100"))
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("100"),ETH_QUERY_DATA)
res = await autopay.getPastTipCount(ETH_QUERY_ID)
assert(res == 2, "past tip count2 should be correct")
await fetch.approve(autopay.address,web3.utils.toWei("100"))
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("100"),ETH_QUERY_DATA)
res = await autopay.getPastTipCount(ETH_QUERY_ID)
assert(res == 2, "past tip count 3 should be correct")
});
it("getFundedFeeds", async () => {
// Check one existing funded feed
let feedIds = await autopay.getFundedFeeds()
assert(feedIds.length == 1, "should be one funded feed from previous test")
let qId = await autopay.getQueryIdFromFeedId(feedIds[0])
assert(qId == ETH_QUERY_ID, "incorrect query ID")
// Check adding two funded feeds
let feedId3 = keccak256(abiCoder.encode(
["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"],
[DOGE_QUERY_ID,h.toWei("1"),blocky.timestamp,600,400,0,0]
));
let feedId4 = keccak256(abiCoder.encode(
["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"],
[LTC_QUERY_ID,h.toWei("1"),blocky.timestamp,600,400,0,0]
));
await autopay.setupDataFeed(DOGE_QUERY_ID,h.toWei("1"),blocky.timestamp,600,400,0,0,DOGE_QUERY_DATA,0);
await autopay.setupDataFeed(LTC_QUERY_ID,h.toWei("1"),blocky.timestamp,600,400,0,0,LTC_QUERY_DATA,0);
await fetch.approve(autopay.address, h.toWei("2"));
await autopay.fundFeed(feedId3,DOGE_QUERY_ID,h.toWei("1"))
await autopay.fundFeed(feedId4,LTC_QUERY_ID,h.toWei("1"))
feedIds = await autopay.getFundedFeeds()
assert(feedIds.length == 3, "should be two funded feeds")
assert(feedIds[1] == feedId3, "incorrect second funded feed")
assert(feedIds[2] == feedId4, "incorrect third funded feed")
// Check remove funded feed
await fetch.connect(accounts[2]).submitValue(DOGE_QUERY_ID, h.uintTob32(1234), 0, DOGE_QUERY_DATA);
_block = await h.getBlock();
await h.advanceTime(43200);
// Check feed details
let feed1Details = await autopay.getDataFeed(bytesId)
let feed3Details = await autopay.getDataFeed(feedId3)
let feed4Details = await autopay.getDataFeed(feedId4)
assert(feed1Details.feedsWithFundingIndex == 1, "queryId 1 feedsWithFundingIndex should be 1")
assert(feed3Details.feedsWithFundingIndex == 2, "queryId 3 feedsWithFundingIndex should be 2")
assert(feed4Details.feedsWithFundingIndex == 3, "queryId 4 feedsWithFundingIndex should be 3")
await autopay.connect(accounts[2]).claimTip(feedId3, DOGE_QUERY_ID, [_block.timestamp])
feedIds = await autopay.getFundedFeeds()
assert(feedIds.length == 2, "should be two funded feeds")
assert(feedIds[1] == feedId4, "incorrect second funded feed query ID")
feed1Details = await autopay.getDataFeed(bytesId)
feed3Details = await autopay.getDataFeed(feedId3)
feed4Details = await autopay.getDataFeed(feedId4)
assert(feed1Details.feedsWithFundingIndex == 1, "queryId 1 feedsWithFundingIndex should be 1")
assert(feed3Details.feedsWithFundingIndex == 0, "queryId 3 feedsWithFundingIndex should be 0")
assert(feed4Details.feedsWithFundingIndex == 2, "queryId 4 feedsWithFundingIndex should be 3")
});
it("getQueryIdFromFeedId", async () => {
//setting up dataFeed
//getting timestamp
let blocky = await h.getBlock()
//creating feedId to call function
let feedId3 = keccak256(abiCoder.encode(
["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"],
[DOGE_QUERY_ID,h.toWei("1"),blocky.timestamp,600,400,0,0]
));
//awaiting dataFeed setup
await autopay.setupDataFeed(DOGE_QUERY_ID,h.toWei("1"),blocky.timestamp,600,400,0,0,DOGE_QUERY_DATA,0);
//Function call
let response = await autopay.getQueryIdFromFeedId(feedId3)
//Expect call
expect(response).to.equal(DOGE_QUERY_ID)
});
it("getFundedQueryIds", async () => {
await fetch.faucet(accounts[0].address)
await fetch.approve(autopay.address,web3.utils.toWei("1000"))
fundedIds = await autopay.getFundedQueryIds()
assert(fundedIds.length == 0)
// Tip queryId 1
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("1"),ETH_QUERY_DATA)
fundedIds = await autopay.getFundedQueryIds()
assert(fundedIds.length == 1)
assert(fundedIds[0] == ETH_QUERY_ID)
assert(await autopay.queryIdsWithFundingIndex(ETH_QUERY_ID) == 1)
// Tip queryId 1
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("1"),ETH_QUERY_DATA)
fundedIds = await autopay.getFundedQueryIds()
assert(fundedIds.length == 1)
assert(fundedIds[0] == ETH_QUERY_ID)
assert(await autopay.queryIdsWithFundingIndex(ETH_QUERY_ID) == 1)
// Tip queryId 2
await autopay.tip(BTC_QUERY_ID,web3.utils.toWei("1"),BTC_QUERY_DATA)
fundedIds = await autopay.getFundedQueryIds()
assert(fundedIds.length == 2)
assert(fundedIds[0] == ETH_QUERY_ID)
assert(fundedIds[1] == BTC_QUERY_ID)
assert(await autopay.queryIdsWithFundingIndex(ETH_QUERY_ID) == 1)
assert(await autopay.queryIdsWithFundingIndex(BTC_QUERY_ID) == 2)
// Tip queryId 2
await autopay.tip(BTC_QUERY_ID,web3.utils.toWei("1"),BTC_QUERY_DATA)
fundedIds = await autopay.getFundedQueryIds()
assert(fundedIds.length == 2)
assert(fundedIds[0] == ETH_QUERY_ID)
assert(fundedIds[1] == BTC_QUERY_ID)
// Tip queryId 3
await autopay.tip(DOGE_QUERY_ID,web3.utils.toWei("1"),DOGE_QUERY_DATA)
fundedIds = await autopay.getFundedQueryIds()
assert(fundedIds.length == 3)
assert(fundedIds[0] == ETH_QUERY_ID)
assert(fundedIds[1] == BTC_QUERY_ID)
assert(fundedIds[2] == DOGE_QUERY_ID)
// Tip queryId 4
await autopay.tip(LTC_QUERY_ID,web3.utils.toWei("1"),LTC_QUERY_DATA)
fundedIds = await autopay.getFundedQueryIds()
assert(fundedIds.length == 4)
assert(fundedIds[0] == ETH_QUERY_ID)
assert(fundedIds[1] == BTC_QUERY_ID)
assert(fundedIds[2] == DOGE_QUERY_ID)
assert(fundedIds[3] == LTC_QUERY_ID)
assert(await autopay.queryIdsWithFundingIndex(ETH_QUERY_ID) == 1)
assert(await autopay.queryIdsWithFundingIndex(BTC_QUERY_ID) == 2)
assert(await autopay.queryIdsWithFundingIndex(DOGE_QUERY_ID) == 3)
assert(await autopay.queryIdsWithFundingIndex(LTC_QUERY_ID) == 4)
await fetch.submitValue(ETH_QUERY_ID, h.uintTob32(3550), 0, ETH_QUERY_DATA);
blocky1 = await h.getBlock();
await fetch.submitValue(BTC_QUERY_ID, h.uintTob32(3550), 0, BTC_QUERY_DATA);
blocky2 = await h.getBlock();
await fetch.submitValue(DOGE_QUERY_ID, h.uintTob32(3550), 0, DOGE_QUERY_DATA);
blocky3 = await h.getBlock();
await fetch.submitValue(LTC_QUERY_ID, h.uintTob32(3550), 0, LTC_QUERY_DATA);
blocky4 = await h.getBlock();
await h.advanceTime(3600 * 12)
await autopay.claimOneTimeTip(ETH_QUERY_ID, [blocky1.timestamp])
fundedIds = await autopay.getFundedQueryIds()
assert(fundedIds.length == 3)
assert(fundedIds[0] == LTC_QUERY_ID)
assert(fundedIds[1] == BTC_QUERY_ID)
assert(fundedIds[2] == DOGE_QUERY_ID)
assert(await autopay.queryIdsWithFundingIndex(ETH_QUERY_ID) == 0)
assert(await autopay.queryIdsWithFundingIndex(BTC_QUERY_ID) == 2)
assert(await autopay.queryIdsWithFundingIndex(DOGE_QUERY_ID) == 3)
assert(await autopay.queryIdsWithFundingIndex(LTC_QUERY_ID) == 1)
// Tip queryId 2
await autopay.tip(BTC_QUERY_ID,web3.utils.toWei("1"),BTC_QUERY_DATA)
await autopay.claimOneTimeTip(BTC_QUERY_ID, [blocky2.timestamp])
fundedIds = await autopay.getFundedQueryIds()
assert(fundedIds.length == 3)
assert(fundedIds[0] == LTC_QUERY_ID)
assert(fundedIds[1] == BTC_QUERY_ID)
assert(fundedIds[2] == DOGE_QUERY_ID)
assert(await autopay.queryIdsWithFundingIndex(ETH_QUERY_ID) == 0)
assert(await autopay.queryIdsWithFundingIndex(BTC_QUERY_ID) == 2)
assert(await autopay.queryIdsWithFundingIndex(DOGE_QUERY_ID) == 3)
assert(await autopay.queryIdsWithFundingIndex(LTC_QUERY_ID) == 1)
await autopay.claimOneTimeTip(DOGE_QUERY_ID, [blocky3.timestamp])
fundedIds = await autopay.getFundedQueryIds()
assert(fundedIds.length == 2)
assert(fundedIds[0] == LTC_QUERY_ID)
assert(fundedIds[1] == BTC_QUERY_ID)
assert(await autopay.queryIdsWithFundingIndex(ETH_QUERY_ID) == 0)
assert(await autopay.queryIdsWithFundingIndex(BTC_QUERY_ID) == 2)
assert(await autopay.queryIdsWithFundingIndex(DOGE_QUERY_ID) == 0)
assert(await autopay.queryIdsWithFundingIndex(LTC_QUERY_ID) == 1)
await autopay.claimOneTimeTip(LTC_QUERY_ID, [blocky4.timestamp])
fundedIds = await autopay.getFundedQueryIds()
assert(fundedIds.length == 1)
assert(fundedIds[0] == BTC_QUERY_ID)
assert(await autopay.queryIdsWithFundingIndex(ETH_QUERY_ID) == 0)
assert(await autopay.queryIdsWithFundingIndex(BTC_QUERY_ID) == 1)
assert(await autopay.queryIdsWithFundingIndex(DOGE_QUERY_ID) == 0)
assert(await autopay.queryIdsWithFundingIndex(LTC_QUERY_ID) == 0)
await fetch.submitValue(BTC_QUERY_ID, h.uintTob32(3550), 0, BTC_QUERY_DATA);
blocky2 = await h.getBlock();
await h.advanceTime(3600 * 12)
await autopay.claimOneTimeTip(BTC_QUERY_ID, [blocky2.timestamp])
fundedIds = await autopay.getFundedQueryIds()
assert(fundedIds.length == 0)
assert(await autopay.queryIdsWithFundingIndex(ETH_QUERY_ID) == 0)
assert(await autopay.queryIdsWithFundingIndex(BTC_QUERY_ID) == 0)
assert(await autopay.queryIdsWithFundingIndex(DOGE_QUERY_ID) == 0)
assert(await autopay.queryIdsWithFundingIndex(LTC_QUERY_ID) == 0)
// Tip queryId 2
await autopay.tip(LTC_QUERY_ID,web3.utils.toWei("1"),LTC_QUERY_DATA)
fundedIds = await autopay.getFundedQueryIds()
assert(fundedIds.length == 1)
assert(fundedIds[0] == LTC_QUERY_ID)
assert(await autopay.queryIdsWithFundingIndex(ETH_QUERY_ID) == 0)
assert(await autopay.queryIdsWithFundingIndex(BTC_QUERY_ID) == 0)
assert(await autopay.queryIdsWithFundingIndex(DOGE_QUERY_ID) == 0)
assert(await autopay.queryIdsWithFundingIndex(LTC_QUERY_ID) == 1)
});
it("getTipsByAddress", async () => {
let userAccount = accounts[5]
await fetch.faucet(userAccount.address)
await fetch.connect(userAccount).approve(autopay.address,h.toWei("1000"))
await autopay.connect(userAccount).tip(BTC_QUERY_ID,web3.utils.toWei("10"),BTC_QUERY_DATA)
assert(await autopay.getTipsByAddress(userAccount.address) == web3.utils.toWei("10"))
await autopay.connect(userAccount).setupDataFeed(ETH_QUERY_ID,h.toWei("1"),blocky.timestamp,3600,600,0,0,ETH_QUERY_DATA,0)
bytesId = keccak256(abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"],[ETH_QUERY_ID,h.toWei("1"),blocky.timestamp,3600,600,0,0]))
await autopay.connect(userAccount).fundFeed(bytesId, ETH_QUERY_ID, h.toWei("99"))
assert(await autopay.getTipsByAddress(userAccount.address) == web3.utils.toWei("109"))
})
it("getRewardAmount", async () => {
await h.advanceTime(3600)
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();
// query non-existent rewards
await h.expectThrow(autopay.getRewardAmount(feedId, h.uintTob32(1), [blocky.timestamp]))
// query rewards
expectedReward = (BigInt(h.toWei("1")) + BigInt(h.toWei("1")) * (BigInt(blocky1.timestamp) - BigInt(blocky0.timestamp)))
expectedReward = expectedReward - (expectedReward * BigInt(FEE) / BigInt(1000)) // fee
rewardSum = expectedReward
expect(await autopay.getRewardAmount(feedId, ETH_QUERY_ID, [blocky1.timestamp])).to.equal(expectedReward)
expectedReward = (BigInt(h.toWei("1")) + BigInt(h.toWei("1")) * (BigInt(blocky2.timestamp) - BigInt(blocky0.timestamp + INTERVAL * 1)))
expectedReward = expectedReward - (expectedReward * BigInt(FEE) / BigInt(1000)) // fee
rewardSum = rewardSum + expectedReward
expect(await autopay.getRewardAmount(feedId, ETH_QUERY_ID, [blocky2.timestamp])).to.equal(expectedReward)
expectedReward = (BigInt(h.toWei("1")) + BigInt(h.toWei("1")) * (BigInt(blocky3.timestamp) - BigInt(blocky0.timestamp + INTERVAL * 2)))
expectedReward = expectedReward - (expectedReward * BigInt(FEE) / BigInt(1000)) // fee
rewardSum = rewardSum + expectedReward
expect(await autopay.getRewardAmount(feedId, ETH_QUERY_ID, [blocky3.timestamp])).to.equal(expectedReward)
// query rewards for multiple queries
expect(await autopay.getRewardAmount(feedId, ETH_QUERY_ID, [blocky1.timestamp, blocky2.timestamp, blocky3.timestamp])).to.equal(rewardSum)
// query rewards 1 week later
await h.advanceTime(86400 * 7)
expect(await autopay.getRewardAmount(feedId, ETH_QUERY_ID, [blocky1.timestamp, blocky2.timestamp, blocky3.timestamp])).to.equal(rewardSum)
// query after 12 weeks
await h.advanceTime(86400 * 7 * 12)
await h.expectThrow(autopay.getRewardAmount(feedId, ETH_QUERY_ID, [blocky1.timestamp, blocky2.timestamp, blocky3.timestamp]))
})
it("bytesToUint", async() => {
let val1 = h.uintTob32(1)
let val2 = h.uintTob32(2)
let val3 = h.uintTob32(300000000000000)
let val4 = h.uintTob32(300000000000001)
let val5 = abiCoder.encode(["uint256"], [1])
let val6 = abiCoder.encode(["uint256"], ["21010191828172717718232237237237128"])
let val7 = '0x01'
let val8 = '0x10'
expect(await autopay.bytesToUint(val1)).to.equal(1)
expect(await autopay.bytesToUint(val2)).to.equal(2)
expect(await autopay.bytesToUint(val3)).to.equal(300000000000000)
expect(await autopay.bytesToUint(val4)).to.equal(300000000000001)
expect(await autopay.bytesToUint(val5)).to.equal(1)
expect(await autopay.bytesToUint(val6)).to.equal("21010191828172717718232237237237128")
expect(await autopay.bytesToUint(val7)).to.equal(1)
expect(await autopay.bytesToUint(val8)).to.equal(16)
})
it("getFundedSingleTipsInfo", async() => {
await fetch.faucet(accounts[0].address)
await fetch.approve(autopay.address,web3.utils.toWei("1000"))
await autopay.tip(ETH_QUERY_ID,web3.utils.toWei("100"),ETH_QUERY_DATA)
await autopay.tip(FETCH_QUERY_ID,web3.utils.toWei("100"),FETCH_QUERY_DATA)
let res = await autopay.getFundedSingleTipsInfo();
assert(res[0].queryData == ETH_QUERY_DATA)
assert(res[0].tip == web3.utils.toWei("100"), "first queryId tip should be correct")
assert(res[1].queryData == FETCH_QUERY_DATA, "second queryData should be correct")
assert(res[1].tip == web3.utils.toWei("100"), "second queryId tip should be correct")
})
it("getFundedFeedDetails", async() => {
await fetch.faucet(accounts[0].address)
await fetch.approve(autopay.address,web3.utils.toWei("1000"))
let res = await autopay.getFundedFeedDetails()
// feed that was funded in before each section
assert(res[0].details.reward == h.toWei("1"), "reward should correct")
assert(res[0].details.balance == h.toWei("1000"), "balance should correct")
assert(res[0].details.startTime == firstBlocky.timestamp, "startTime should correct")
assert(res[0].details.window == 600, "window should correct")
assert(res[0].details.interval == 3600, "interval should correct")
assert(res[0].details.priceThreshold == 0, "priceThreshold should correct")
assert(res[0].details.rewardIncreasePerSecond == 0, "rewardIncreasePerSecond should correct")
assert(res[0].details.feedsWithFundingIndex == 1, "feedsWithFundingIndex should correct")
})
it("getRewardClaimStatusList", async() => {
// setup feeds with funding
await fetch.faucet(accounts[0].address)
await fetch.approve(autopay.address,web3.utils.toWei("1000"))
await autopay.setupDataFeed(FETCH_QUERY_ID,h.toWei("10"),firstBlocky.timestamp,3600,600,0,0,FETCH_QUERY_DATA,h.toWei("1000"));
bytesId = keccak256(abiCoder.encode(["bytes32", "uint256", "uint256", "uint256", "uint256", "uint256", "uint256"],[FETCH_QUERY_ID,h.toWei("10"),firstBlocky.timestamp,3600,600,0,0]));
// submit to feeds
await fetch.submitValue(FETCH_QUERY_ID, h.uintTob32(3500), 0, FETCH_QUERY_DATA);
let blocky = await h.getBlock();
timestamp1 = blocky.timestamp;
h.advanceTime(3600);
await fetch.connect(accounts[0]).submitValue(FETCH_QUERY_ID, h.uintTob32(3525), 1, FETCH_QUERY_DATA);
blocky = await h.getBlock();
timestamp2 = blocky.timestamp;
h.advanceTime(3600);
await fetch.connect(accounts[0]).submitValue(FETCH_QUERY_ID, h.uintTob32(3550), 2, FETCH_QUERY_DATA);
blocky = await h.getBlock();
timestamp3 = blocky.timestamp;
h.advanceTime(3600);
// check timestamps
let res = await autopay.getRewardClaimStatusList(bytesId,ETH_QUERY_ID,[timestamp1,timestamp2,timestamp3])
assert(res[0] == false)
assert(res[1] == false)
assert(res[2] == false)
// claimTip and check status
h.advanceTime(84600);
await autopay.claimTip(bytesId, FETCH_QUERY_ID, [timestamp1])
res = await autopay.getRewardClaimStatusList(bytesId,FETCH_QUERY_ID,[timestamp1,timestamp2,timestamp3])
assert(res[0] == true)
assert(res[1] == false)
assert(res[2] == false)
await autopay.claimTip(bytesId, FETCH_QUERY_ID, [timestamp3])
res = await autopay.getRewardClaimStatusList(bytesId,FETCH_QUERY_ID,[timestamp1,timestamp2,timestamp3])
assert(res[0] == true)
assert(res[1] == false)
assert(res[2] == true)
await autopay.claimTip(bytesId, FETCH_QUERY_ID, [timestamp2])
res = await autopay.getRewardClaimStatusList(bytesId,FETCH_QUERY_ID,[timestamp1,timestamp2,timestamp3])
assert(res[0] == true)
assert(res[1] == true)
assert(res[2] == true)
})
});