hive-stream 2.0.3 → 2.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/.env.example +3 -2
  2. package/.travis.yml +11 -11
  3. package/LICENSE +21 -21
  4. package/README.md +238 -236
  5. package/dist/actions.d.ts +10 -9
  6. package/dist/actions.js +23 -15
  7. package/dist/actions.js.map +1 -1
  8. package/dist/adapters/base.adapter.d.ts +25 -21
  9. package/dist/adapters/base.adapter.js +49 -63
  10. package/dist/adapters/base.adapter.js.map +1 -1
  11. package/dist/adapters/mongodb.adapter.d.ts +37 -29
  12. package/dist/adapters/mongodb.adapter.js +158 -113
  13. package/dist/adapters/mongodb.adapter.js.map +1 -1
  14. package/dist/adapters/sqlite.adapter.d.ts +41 -23
  15. package/dist/adapters/sqlite.adapter.js +397 -121
  16. package/dist/adapters/sqlite.adapter.js.map +1 -1
  17. package/dist/api.d.ts +6 -0
  18. package/dist/api.js +56 -0
  19. package/dist/api.js.map +1 -0
  20. package/dist/config.d.ts +16 -14
  21. package/dist/config.js +18 -15
  22. package/dist/config.js.map +1 -1
  23. package/dist/contracts/coinflip.contract.d.ts +14 -0
  24. package/dist/contracts/coinflip.contract.js +95 -0
  25. package/dist/contracts/coinflip.contract.js.map +1 -0
  26. package/dist/contracts/dice.contract.d.ts +29 -29
  27. package/dist/contracts/dice.contract.js +155 -157
  28. package/dist/contracts/dice.contract.js.map +1 -1
  29. package/dist/contracts/lotto.contract.d.ts +20 -16
  30. package/dist/contracts/lotto.contract.js +246 -107
  31. package/dist/contracts/lotto.contract.js.map +1 -1
  32. package/dist/exchanges/bittrex.d.ts +6 -0
  33. package/dist/exchanges/bittrex.js +35 -0
  34. package/dist/exchanges/bittrex.js.map +1 -0
  35. package/dist/exchanges/exchange.d.ts +9 -0
  36. package/dist/exchanges/exchange.js +27 -0
  37. package/dist/exchanges/exchange.js.map +1 -0
  38. package/dist/hive-rates.d.ts +9 -0
  39. package/dist/hive-rates.js +76 -0
  40. package/dist/hive-rates.js.map +1 -0
  41. package/dist/index.d.ts +11 -10
  42. package/dist/index.js +32 -15
  43. package/dist/index.js.map +1 -1
  44. package/dist/streamer.d.ts +93 -70
  45. package/dist/streamer.js +545 -439
  46. package/dist/streamer.js.map +1 -1
  47. package/dist/test.d.ts +1 -1
  48. package/dist/test.js +25 -27
  49. package/dist/test.js.map +1 -1
  50. package/dist/types/hive-stream.d.ts +6 -6
  51. package/dist/types/hive-stream.js +2 -2
  52. package/dist/utils.d.ts +27 -14
  53. package/dist/utils.js +261 -85
  54. package/dist/utils.js.map +1 -1
  55. package/ecosystem.config.js +17 -17
  56. package/jest.config.js +8 -13
  57. package/package.json +48 -44
  58. package/test-contract-block.md +18 -18
  59. package/tests/adapters/sqlite.adapter.spec.ts +43 -0
  60. package/tests/contracts/coinflip.contract.spec.ts +132 -0
  61. package/tests/contracts/dice.contract.spec.ts +159 -156
  62. package/tests/contracts/entrants.json +728 -728
  63. package/tests/contracts/lotto.contract.spec.ts +323 -372
  64. package/tests/setup.ts +18 -20
  65. package/tests/streamer.spec.ts +151 -151
  66. package/tests/utils.spec.ts +94 -99
  67. package/tsconfig.build.json +22 -20
  68. package/tslint.json +20 -20
  69. package/wallaby.js +26 -0
  70. package/.env +0 -1
  71. package/dist/adapters/file.adapter.d.ts +0 -8
  72. package/dist/adapters/file.adapter.js +0 -70
  73. package/dist/adapters/file.adapter.js.map +0 -1
  74. package/dist/test/setup.d.ts +0 -0
  75. package/dist/test/setup.js +0 -9
  76. package/dist/test/setup.js.map +0 -1
  77. package/dist/test/streamer.spec.d.ts +0 -1
  78. package/dist/test/streamer.spec.js +0 -145
  79. package/dist/test/streamer.spec.js.map +0 -1
  80. package/dist/test/utils.spec.d.ts +0 -1
  81. package/dist/test/utils.spec.js +0 -11
  82. package/dist/test/utils.spec.js.map +0 -1
  83. package/dist/tests/contracts/dice.contract.spec.d.ts +0 -1
  84. package/dist/tests/contracts/dice.contract.spec.js +0 -130
  85. package/dist/tests/contracts/dice.contract.spec.js.map +0 -1
  86. package/dist/tests/contracts/entrants.json +0 -729
  87. package/dist/tests/contracts/lotto.contract.spec.d.ts +0 -1
  88. package/dist/tests/contracts/lotto.contract.spec.js +0 -300
  89. package/dist/tests/contracts/lotto.contract.spec.js.map +0 -1
  90. package/dist/tests/setup.d.ts +0 -1
  91. package/dist/tests/setup.js +0 -19
  92. package/dist/tests/setup.js.map +0 -1
  93. package/dist/tests/streamer.spec.d.ts +0 -1
  94. package/dist/tests/streamer.spec.js +0 -123
  95. package/dist/tests/streamer.spec.js.map +0 -1
  96. package/dist/tests/utils.spec.d.ts +0 -1
  97. package/dist/tests/utils.spec.js +0 -87
  98. package/dist/tests/utils.spec.js.map +0 -1
  99. package/hive-stream.json +0 -1
@@ -1,373 +1,324 @@
1
- import { MongoClient, Db } from 'mongodb';
2
- import { sleep } from '@hivechain/dhive/lib/utils';
3
-
4
- import { TimeAction } from './../../src/actions';
5
- import { LottoContract } from './../../src/contracts/lotto.contract';
6
- import { Streamer } from '../../src/streamer';
7
-
8
- import fiftyValidEntrants from './entrants.json';
9
-
10
- describe('Lotto Contract', () => {
11
- let sut: Streamer;
12
- let contract: LottoContract;
13
- let connection: MongoClient;
14
- let db: Db;
15
-
16
- beforeAll(async () => {
17
- try {
18
- const url = `mongodb://127.0.0.1/lotto-test`
19
- connection = await MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true });
20
- db = await connection.db();
21
- } catch (e) {
22
- throw e;
23
- }
24
- });
25
-
26
- beforeEach(async () => {
27
- sut = new Streamer({ ACTIVE_KEY: '' });
28
- contract = new LottoContract();
29
-
30
- sut['adapter']['db'] = db;
31
-
32
- jest.restoreAllMocks();
33
-
34
- await sut.start();
35
- });
36
-
37
- afterEach(async () => {
38
- await sut.stop();
39
-
40
- await db.collection('lottery').deleteMany({});
41
- });
42
-
43
- afterAll(() => {
44
- connection.close();
45
- });
46
-
47
- test('Registers the lotto contract', () => {
48
- sut.registerContract('testlotto', contract);
49
-
50
- const findContract = sut['contracts'].find(c => c.name === 'testlotto');
51
-
52
- expect(findContract).not.toBeUndefined();
53
- });
54
-
55
- test('User enters the lotto, existing draw found', async () => {
56
- try {
57
- sut.registerContract('testlotto', contract);
58
-
59
- contract['_instance'] = sut;
60
-
61
- contract['adapter']['db'] = db;
62
-
63
- const lottery = db.collection('lottery');
64
- await lottery.insertOne({ startDate: new Date(), type: 'hourly', status: 'active', entries: [] });
65
-
66
- jest.spyOn(contract, 'buy');
67
- jest.spyOn(contract as any, 'getBalance').mockResolvedValue(2000);
68
-
69
- jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
70
- jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
71
- jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
72
- jest.spyOn(sut, 'transferHiveTokensMultiple').mockResolvedValue(true as any);
73
-
74
- const memo = JSON.stringify({
75
- hivePayload: {
76
- id: 'hivestream',
77
- name: 'testlotto',
78
- action: 'buy',
79
- payload: {
80
- type: 'hourly'
81
- }
82
- }
83
- });
84
-
85
- sut.processOperation(['transfer', { from: 'testuser', amount: '10.000 HIVE', memo }], 778782, 'dfjfsdfsdfsd34hfkj88787', 'fkjsdkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
86
-
87
- await sleep(100);
88
-
89
- expect(contract.buy).toBeCalled();
90
- } catch (e) {
91
- throw e;
92
- }
93
- });
94
-
95
- test('User enters the lotto, but they have hit the entry limit', async () => {
96
- try {
97
- sut.registerContract('testlotto', contract);
98
-
99
- contract['_instance'] = sut;
100
-
101
- contract['adapter']['db'] = db;
102
-
103
- const lottery = db.collection('lottery');
104
- const entries = [];
105
-
106
- for (const entrant of fiftyValidEntrants) {
107
- entries.push({
108
- account: entrant.from,
109
- date: new Date()
110
- });
111
- }
112
-
113
- await lottery.insertOne({ startDate: new Date(), type: 'hourly', status: 'active', entries });
114
-
115
- jest.spyOn(contract, 'buy');
116
- jest.spyOn(contract as any, 'getBalance').mockResolvedValue(2000);
117
-
118
- jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
119
- jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
120
- jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
121
- jest.spyOn(sut, 'transferHiveTokensMultiple').mockResolvedValue(true as any);
122
-
123
- const memo = JSON.stringify({
124
- hivePayload: {
125
- id: 'hivestream',
126
- name: 'testlotto',
127
- action: 'buy',
128
- payload: {
129
- type: 'hourly'
130
- }
131
- }
132
- });
133
-
134
- sut.processOperation(['transfer', { from: 'beggars', amount: '10.000 HIVE', memo }], 778782, 'dfjfsdfsdfsd34hfkj88787', 'fkjsdkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
135
-
136
- await sleep(100);
137
-
138
- expect(sut.transferHiveTokens).toBeCalledWith('beggars', 'beggars', '10.000', 'HIVE', '[Refund] You have exceeded the allow number of entries');
139
- } catch (e) {
140
- throw e;
141
- }
142
- });
143
-
144
- test('Draw the hourly lottery', async () => {
145
- try {
146
- sut.registerContract('testlotto', contract);
147
-
148
- contract['_instance'] = sut;
149
-
150
- contract['adapter']['db'] = db;
151
-
152
- const lottery = db.collection('lottery');
153
- const entries = [];
154
-
155
- for (const entrant of fiftyValidEntrants) {
156
- entries.push({
157
- account: entrant.from,
158
- date: new Date()
159
- });
160
- }
161
-
162
- await lottery.insertOne({ startDate: new Date(), type: 'hourly', status: 'active', entries });
163
-
164
- jest.spyOn(contract, 'buy');
165
- jest.spyOn(contract as any, 'getBalance').mockResolvedValue(2000);
166
-
167
- jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
168
- jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
169
- jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
170
- jest.spyOn(sut, 'transferHiveTokensMultiple').mockResolvedValue(true as any);
171
-
172
- const drawn = await contract.drawHourlyLottery();
173
-
174
- expect(drawn).toHaveLength(3);
175
- expect(drawn.includes(undefined)).toBeFalsy();
176
- expect(sut.transferHiveTokensMultiple).toBeCalledTimes(1);
177
- expect(sut.transferHiveTokensMultiple).toBeCalledWith('beggars', expect.any(Array), '164.667', 'HIVE', 'Congratulations you won the hourly lottery. You won 164.667 HIVE');
178
- } catch (e) {
179
- throw e;
180
- }
181
- });
182
-
183
- test('Draw the hourly lottery, but not enough entrants, so we refund', async () => {
184
- try {
185
- sut.registerContract('testlotto', contract);
186
-
187
- contract['_instance'] = sut;
188
-
189
- contract['adapter']['db'] = db;
190
-
191
- const lottery = db.collection('lottery');
192
- const entries = [];
193
- const reducedEntries = fiftyValidEntrants.slice(0, 2);
194
-
195
- for (const entrant of reducedEntries) {
196
- entries.push({
197
- account: entrant.from,
198
- date: new Date()
199
- });
200
- }
201
-
202
- await lottery.insertOne({ startDate: new Date(), type: 'hourly', status: 'active', entries });
203
-
204
- jest.spyOn(contract, 'buy');
205
- jest.spyOn(contract as any, 'getBalance').mockResolvedValue(2000);
206
-
207
- jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
208
- jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
209
- jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
210
- jest.spyOn(sut, 'transferHiveTokensMultiple').mockResolvedValue(true as any);
211
-
212
- const drawn = await contract.drawHourlyLottery();
213
-
214
- expect(sut.transferHiveTokensMultiple).toBeCalledTimes(1);
215
- } catch (e) {
216
- throw e;
217
- }
218
- });
219
-
220
- test('Draw the hourly lottery, balance cannot afford to pay out winnings', async () => {
221
- try {
222
- sut.registerContract('testlotto', contract);
223
-
224
- contract['_instance'] = sut;
225
-
226
- contract['adapter']['db'] = db;
227
-
228
- const lottery = db.collection('lottery');
229
- const entries = [];
230
-
231
- for (const entrant of fiftyValidEntrants) {
232
- entries.push({
233
- account: entrant.from,
234
- date: new Date()
235
- });
236
- }
237
-
238
- await lottery.insertOne({ startDate: new Date(), type: 'hourly', status: 'active', entries });
239
-
240
- jest.spyOn(contract, 'buy');
241
- jest.spyOn(contract as any, 'getBalance').mockResolvedValue(10);
242
-
243
- jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
244
- jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
245
- jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
246
- jest.spyOn(sut, 'transferHiveTokensMultiple').mockResolvedValue(true as any);
247
-
248
- expect(contract.drawHourlyLottery()).rejects.toEqual(new Error('Balance is less than amount to pay out'));
249
- } catch (e) {
250
- throw e;
251
- }
252
- });
253
-
254
- test('Draw the daily lottery', async () => {
255
- try {
256
- sut.registerContract('testlotto', contract);
257
-
258
- contract['_instance'] = sut;
259
-
260
- contract['adapter']['db'] = db;
261
-
262
- const lottery = db.collection('lottery');
263
- const entries = [];
264
- const entrants = [...fiftyValidEntrants, ...fiftyValidEntrants];
265
-
266
- for (const entrant of entrants) {
267
- entries.push({
268
- account: entrant.from,
269
- date: new Date()
270
- });
271
- }
272
-
273
- await lottery.insertOne({ startDate: new Date(), type: 'daily', status: 'active', entries });
274
-
275
- jest.spyOn(contract, 'buy');
276
- jest.spyOn(contract as any, 'getBalance').mockResolvedValue(2000);
277
-
278
- jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
279
- jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
280
- jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
281
- jest.spyOn(sut, 'transferHiveTokensMultiple').mockResolvedValue(true as any);
282
-
283
- const drawn = await contract.drawDailyLottery();
284
-
285
- expect(drawn).toHaveLength(10);
286
- expect(drawn.includes(undefined)).toBeFalsy();
287
- expect(sut.transferHiveTokensMultiple).toBeCalledWith('beggars', expect.any(Array), '98.800', 'HIVE', 'Congratulations you won the daily lottery. You won 98.800 HIVE');
288
- } catch (e) {
289
- throw e;
290
- }
291
- });
292
-
293
- test('User attempts to enter lotto with invalid currency, refund them', async () => {
294
- try {
295
- sut.registerContract('testlotto', contract);
296
-
297
- contract['_instance'] = sut;
298
-
299
- contract['adapter']['db'] = db;
300
-
301
- const lottery = db.collection('lottery');
302
- await lottery.insertOne({ startDate: new Date(), type: 'hourly', status: 'active', entries: [] });
303
-
304
- jest.spyOn(contract, 'buy');
305
- jest.spyOn(contract as any, 'getBalance').mockResolvedValue(2000);
306
-
307
- jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
308
- jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
309
- jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
310
- jest.spyOn(sut, 'transferHiveTokensMultiple').mockResolvedValue(true as any);
311
-
312
- const memo = JSON.stringify({
313
- hivePayload: {
314
- id: 'hivestream',
315
- name: 'testlotto',
316
- action: 'buy',
317
- payload: {
318
- type: 'hourly'
319
- }
320
- }
321
- });
322
-
323
- sut.processOperation(['transfer', { from: 'testuser', amount: '10.000 HBD', memo }], 778782, 'dfjfsdfsdfsd34hfkj88787', 'fkjsdkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
324
-
325
- await sleep(100);
326
-
327
- expect(sut.transferHiveTokens).toBeCalledWith('beggars', 'testuser', '10.000', 'HBD', '[Refund] You sent an invalid currency.');
328
- } catch (e) {
329
- throw e;
330
- }
331
- });
332
-
333
- test('User sent too much, refund them the diference', async () => {
334
- try {
335
- sut.registerContract('testlotto', contract);
336
-
337
- contract['_instance'] = sut;
338
-
339
- contract['adapter']['db'] = db;
340
-
341
- const lottery = db.collection('lottery');
342
- await lottery.insertOne({ startDate: new Date(), type: 'hourly', status: 'active', entries: [] });
343
-
344
- jest.spyOn(contract, 'buy');
345
- jest.spyOn(contract as any, 'getBalance').mockResolvedValue(2000);
346
-
347
- jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
348
- jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
349
- jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
350
- jest.spyOn(sut, 'transferHiveTokensMultiple').mockResolvedValue(true as any);
351
-
352
- const memo = JSON.stringify({
353
- hivePayload: {
354
- id: 'hivestream',
355
- name: 'testlotto',
356
- action: 'buy',
357
- payload: {
358
- type: 'hourly'
359
- }
360
- }
361
- });
362
-
363
- sut.processOperation(['transfer', { from: 'testuser', amount: '20.000 HIVE', memo }], 778782, 'dfjfsdfsdfsd34hfkj88787', 'fkjsdkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
364
-
365
- await sleep(100);
366
-
367
- expect(sut.transferHiveTokens).toBeCalledWith('beggars', 'testuser', '10.000', 'HIVE', '[Refund] A ticket costs 10 HIVE. You sent 20.000 HIVE. You were refunded 10.000 HIVE.');
368
- } catch (e) {
369
- throw e;
370
- }
371
- });
372
-
1
+ import { sleep } from '@hiveio/dhive/lib/utils';
2
+
3
+ import { TimeAction } from './../../src/actions';
4
+ import { LottoContract } from './../../src/contracts/lotto.contract';
5
+ import { Streamer } from '../../src/streamer';
6
+
7
+ import fiftyValidEntrants from './entrants.json';
8
+
9
+ describe('Lotto Contract', () => {
10
+ let sut: Streamer;
11
+ let contract: LottoContract;
12
+
13
+ beforeEach(async () => {
14
+ sut = new Streamer({ ACTIVE_KEY: '' });
15
+ contract = new LottoContract();
16
+
17
+ // @ts-ignore
18
+ sut.adapter = {
19
+ db: jest.fn(),
20
+ create: jest.fn(),
21
+ destroy: jest.fn(),
22
+ loadActions: jest.fn(),
23
+ loadState: jest.fn(),
24
+ saveState: jest.fn(),
25
+ processBlock: jest.fn(),
26
+ processOperation: jest.fn(),
27
+ processTransfer: jest.fn(),
28
+ processCustomJson: jest.fn(),
29
+ find: jest.fn(),
30
+ findOne: jest.fn(),
31
+ insert: jest.fn(),
32
+ replace: jest.fn()
33
+ };
34
+
35
+ jest.restoreAllMocks();
36
+
37
+ await sut.start();
38
+ });
39
+
40
+ afterEach(async () => {
41
+ await sut.stop();
42
+ });
43
+
44
+ test('Registers the lotto contract', () => {
45
+ sut.registerContract('testlotto', contract);
46
+
47
+ const findContract = sut['contracts'].find(c => c.name === 'testlotto');
48
+
49
+ expect(findContract).not.toBeUndefined();
50
+ });
51
+
52
+ test('User enters the lotto, existing draw found', async () => {
53
+ try {
54
+ sut.registerContract('testlotto', contract);
55
+
56
+ contract['_instance'] = sut;
57
+
58
+ const mockEntry = { startDate: new Date(), type: 'hourly', status: 'active', entries: [] };
59
+
60
+ jest.spyOn(sut['adapter'], 'find').mockResolvedValue(mockEntry);
61
+
62
+ jest.spyOn(contract, 'buy');
63
+ jest.spyOn(contract as any, 'getBalance').mockResolvedValue(2000);
64
+
65
+ jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
66
+ jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
67
+ jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
68
+ jest.spyOn(sut, 'transferHiveTokensMultiple').mockResolvedValue(true as any);
69
+
70
+ const memo = JSON.stringify({
71
+ hivePayload: {
72
+ id: 'hivestream',
73
+ name: 'testlotto',
74
+ action: 'buy',
75
+ payload: {
76
+ type: 'hourly'
77
+ }
78
+ }
79
+ });
80
+
81
+ sut.processOperation(['transfer', { from: 'testuser', amount: '10.000 HIVE', memo }], 778782, 'dfjfsdfsdfsd34hfkj88787', 'fkjsdkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
82
+
83
+ await sleep(100);
84
+
85
+ expect(contract.buy).toBeCalled();
86
+ } catch (e) {
87
+ throw e;
88
+ }
89
+ });
90
+
91
+ test('User enters the lotto, but they have hit the entry limit', async () => {
92
+ try {
93
+ sut.registerContract('testlotto', contract);
94
+
95
+ contract['_instance'] = sut;
96
+
97
+ const entries = [];
98
+
99
+ for (const entrant of fiftyValidEntrants) {
100
+ // @ts-ignore
101
+ entries.push({
102
+ account: entrant.from,
103
+ date: new Date()
104
+ });
105
+ }
106
+
107
+ const mockData = { startDate: new Date(), type: 'hourly', status: 'active', entries };
108
+ jest.spyOn(sut['adapter'], 'find').mockResolvedValue(mockData);
109
+
110
+ jest.spyOn(contract, 'buy');
111
+ jest.spyOn(contract as any, 'getBalance').mockResolvedValue(2000);
112
+
113
+ jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
114
+ jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
115
+ jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
116
+ jest.spyOn(sut, 'transferHiveTokensMultiple').mockResolvedValue(true as any);
117
+ jest.spyOn(contract, 'getPreviousUserTicketsForCurrentDrawType').mockResolvedValue(3);
118
+
119
+ const memo = JSON.stringify({
120
+ hivePayload: {
121
+ id: 'hivestream',
122
+ name: 'testlotto',
123
+ action: 'buy',
124
+ payload: {
125
+ type: 'hourly'
126
+ }
127
+ }
128
+ });
129
+
130
+ sut.processOperation(['transfer', { from: 'beggars', amount: '10.000 HIVE', memo }], 778782, 'dfjfsdfsdfsd34hfkj88787', 'fkjsdkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
131
+
132
+ await sleep(100);
133
+
134
+ expect(sut.transferHiveTokens).toBeCalledWith('beggars', 'beggars', '10.000', 'HIVE', '[Refund] You have exceeded the allowed number of entries');
135
+ } catch (e) {
136
+ throw e;
137
+ }
138
+ });
139
+
140
+ test('Draw the hourly lottery', async () => {
141
+ try {
142
+ sut.registerContract('testlotto', contract);
143
+
144
+ contract['_instance'] = sut;
145
+
146
+ const entries = [];
147
+
148
+ for (const entrant of fiftyValidEntrants) {
149
+ // @ts-ignore
150
+ entries.push({
151
+ account: entrant.from,
152
+ date: new Date()
153
+ });
154
+ }
155
+
156
+ const mockInsertedData = { startDate: new Date(), type: 'hourly', status: 'active', entries };
157
+ jest.spyOn(sut['adapter'], 'find').mockResolvedValue(mockInsertedData);
158
+
159
+ jest.spyOn(contract, 'buy');
160
+ jest.spyOn(contract as any, 'getBalance').mockResolvedValue(2000);
161
+
162
+ jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
163
+ jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
164
+ jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
165
+ jest.spyOn(sut, 'transferHiveTokensMultiple').mockResolvedValue(true as any);
166
+
167
+ const drawn = await contract.drawHourlyLottery();
168
+
169
+ if (drawn) {
170
+ expect(drawn).toHaveLength(3);
171
+ expect(sut.transferHiveTokensMultiple).toBeCalledTimes(2);
172
+ expect(sut.transferHiveTokensMultiple).toBeCalledWith('beggars', expect.any(Array), '164.667', 'HIVE', expect.stringContaining('Congratulations you won the hourly lottery. You won 164.667 HIVE'));
173
+ expect(sut.transferHiveTokensMultiple).toBeCalledWith(expect.any(String), expect.any(Array), '0.001', 'HIVE', expect.stringContaining('Sorry, you didn\'t win the hourly draw. Winners:'));
174
+ }
175
+ } catch (e) {
176
+ throw e;
177
+ }
178
+ });
179
+
180
+ test('Draw the hourly lottery, but not enough entrants, so we refund', async () => {
181
+ try {
182
+ sut.registerContract('testlotto', contract);
183
+
184
+ contract['_instance'] = sut;
185
+
186
+ const entries = [];
187
+ const reducedEntries = fiftyValidEntrants.slice(0, 2);
188
+
189
+ for (const entrant of reducedEntries) {
190
+ // @ts-ignore
191
+ entries.push({
192
+ account: entrant.from,
193
+ date: new Date()
194
+ });
195
+ }
196
+
197
+ const mockResponse = [{ startDate: new Date(), type: 'hourly', status: 'active', entries }];
198
+ jest.spyOn(sut['adapter'], 'find').mockResolvedValue(mockResponse);
199
+
200
+ jest.spyOn(contract, 'buy');
201
+ jest.spyOn(contract as any, 'getBalance').mockResolvedValue(2000);
202
+
203
+ jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
204
+ jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
205
+ jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
206
+ jest.spyOn(sut, 'transferHiveTokensMultiple').mockResolvedValue(true as any);
207
+
208
+ const drawn = await contract.drawHourlyLottery();
209
+
210
+ expect(sut.transferHiveTokensMultiple).toBeCalledTimes(1);
211
+ } catch (e) {
212
+ throw e;
213
+ }
214
+ });
215
+
216
+ test('Draw the hourly lottery, balance cannot afford to pay out winnings', async () => {
217
+ try {
218
+ sut.registerContract('testlotto', contract);
219
+
220
+ contract['_instance'] = sut;
221
+
222
+ const entries = [];
223
+
224
+ for (const entrant of fiftyValidEntrants) {
225
+ // @ts-ignore
226
+ entries.push({
227
+ account: entrant.from,
228
+ date: new Date()
229
+ });
230
+ }
231
+
232
+ const mockData = [{ startDate: new Date(), type: 'hourly', status: 'active', entries }];
233
+ jest.spyOn(sut['adapter'], 'find').mockResolvedValue(mockData);
234
+
235
+ jest.spyOn(contract, 'buy');
236
+ jest.spyOn(contract as any, 'getBalance').mockResolvedValue(10);
237
+
238
+ jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
239
+ jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
240
+ jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
241
+ jest.spyOn(sut, 'transferHiveTokensMultiple').mockResolvedValue(true as any);
242
+
243
+ expect(contract.drawHourlyLottery()).rejects.toEqual(new Error('Balance is less than amount to pay out'));
244
+ } catch (e) {
245
+ throw e;
246
+ }
247
+ });
248
+
249
+ test('Draw the daily lottery', async () => {
250
+ try {
251
+ sut.registerContract('testlotto', contract);
252
+
253
+ contract['_instance'] = sut;
254
+
255
+ const entries = [];
256
+ const entrants = [...fiftyValidEntrants, ...fiftyValidEntrants];
257
+
258
+ for (const entrant of entrants) {
259
+ // @ts-ignore
260
+ entries.push({
261
+ account: entrant.from,
262
+ date: new Date()
263
+ });
264
+ }
265
+
266
+ const mockData = [{ startDate: new Date(), type: 'daily', status: 'active', entries }];
267
+ jest.spyOn(sut['adapter'], 'find').mockResolvedValue(mockData);
268
+
269
+ jest.spyOn(contract, 'buy');
270
+ jest.spyOn(contract as any, 'getBalance').mockResolvedValue(2000);
271
+
272
+ jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
273
+ jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
274
+ jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
275
+ jest.spyOn(sut, 'transferHiveTokensMultiple').mockResolvedValue(true as any);
276
+
277
+ const drawn = await contract.drawDailyLottery();
278
+
279
+ expect(drawn).toHaveLength(10);
280
+ expect(sut.transferHiveTokensMultiple).toBeCalledWith('beggars', expect.any(Array), '98.800', 'HIVE', 'Congratulations you won the daily lottery. You won 98.800 HIVE');
281
+ } catch (e) {
282
+ throw e;
283
+ }
284
+ });
285
+
286
+ test('User attempts to enter lotto with invalid currency, refund them', async () => {
287
+ try {
288
+ sut.registerContract('testlotto', contract);
289
+
290
+ contract['_instance'] = sut;
291
+
292
+ const mockData = { startDate: new Date(), type: 'hourly', status: 'active', entries: [] };
293
+ jest.spyOn(sut['adapter'], 'find').mockResolvedValue(mockData);
294
+
295
+ jest.spyOn(contract, 'buy');
296
+ jest.spyOn(contract as any, 'getBalance').mockResolvedValue(2000);
297
+
298
+ jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
299
+ jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
300
+ jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
301
+ jest.spyOn(sut, 'transferHiveTokensMultiple').mockResolvedValue(true as any);
302
+
303
+ const memo = JSON.stringify({
304
+ hivePayload: {
305
+ id: 'hivestream',
306
+ name: 'testlotto',
307
+ action: 'buy',
308
+ payload: {
309
+ type: 'hourly'
310
+ }
311
+ }
312
+ });
313
+
314
+ sut.processOperation(['transfer', { from: 'testuser', amount: '10.000 HBD', memo }], 778782, 'dfjfsdfsdfsd34hfkj88787', 'fkjsdkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
315
+
316
+ await sleep(100);
317
+
318
+ expect(sut.transferHiveTokens).toBeCalledWith('beggars', 'testuser', '10.000', 'HBD', '[Refund] You sent an invalid currency.');
319
+ } catch (e) {
320
+ throw e;
321
+ }
322
+ });
323
+
373
324
  });