hive-stream 3.0.0 → 3.0.2

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 (107) hide show
  1. package/AGENTS.md +35 -0
  2. package/DOCUMENTATION.md +396 -0
  3. package/README.md +160 -39
  4. package/dist/actions.d.ts +3 -3
  5. package/dist/actions.js +7 -7
  6. package/dist/actions.js.map +1 -1
  7. package/dist/adapters/base.adapter.d.ts +19 -1
  8. package/dist/adapters/base.adapter.js +16 -0
  9. package/dist/adapters/base.adapter.js.map +1 -1
  10. package/dist/adapters/mongodb.adapter.d.ts +5 -11
  11. package/dist/adapters/mongodb.adapter.js +10 -10
  12. package/dist/adapters/mongodb.adapter.js.map +1 -1
  13. package/dist/adapters/postgresql.adapter.d.ts +17 -0
  14. package/dist/adapters/postgresql.adapter.js +99 -8
  15. package/dist/adapters/postgresql.adapter.js.map +1 -1
  16. package/dist/adapters/sqlite.adapter.d.ts +17 -0
  17. package/dist/adapters/sqlite.adapter.js +99 -8
  18. package/dist/adapters/sqlite.adapter.js.map +1 -1
  19. package/dist/api.js +86 -0
  20. package/dist/api.js.map +1 -1
  21. package/dist/config.d.ts +26 -0
  22. package/dist/config.js +76 -4
  23. package/dist/config.js.map +1 -1
  24. package/dist/contracts/coinflip.contract.d.ts +8 -26
  25. package/dist/contracts/coinflip.contract.js +123 -144
  26. package/dist/contracts/coinflip.contract.js.map +1 -1
  27. package/dist/contracts/contract.d.ts +3 -0
  28. package/dist/contracts/contract.js +26 -0
  29. package/dist/contracts/contract.js.map +1 -0
  30. package/dist/contracts/dice.contract.d.ts +9 -36
  31. package/dist/contracts/dice.contract.js +135 -200
  32. package/dist/contracts/dice.contract.js.map +1 -1
  33. package/dist/contracts/exchange.contract.d.ts +11 -0
  34. package/dist/contracts/exchange.contract.js +492 -0
  35. package/dist/contracts/exchange.contract.js.map +1 -0
  36. package/dist/contracts/lotto.contract.d.ts +15 -19
  37. package/dist/contracts/lotto.contract.js +154 -162
  38. package/dist/contracts/lotto.contract.js.map +1 -1
  39. package/dist/contracts/nft.contract.d.ts +4 -0
  40. package/dist/contracts/nft.contract.js +65 -0
  41. package/dist/contracts/nft.contract.js.map +1 -1
  42. package/dist/contracts/poll.contract.d.ts +4 -0
  43. package/dist/contracts/poll.contract.js +105 -0
  44. package/dist/contracts/poll.contract.js.map +1 -0
  45. package/dist/contracts/rps.contract.d.ts +9 -0
  46. package/dist/contracts/rps.contract.js +217 -0
  47. package/dist/contracts/rps.contract.js.map +1 -0
  48. package/dist/contracts/tipjar.contract.d.ts +4 -0
  49. package/dist/contracts/tipjar.contract.js +60 -0
  50. package/dist/contracts/tipjar.contract.js.map +1 -0
  51. package/dist/contracts/token.contract.d.ts +3 -17
  52. package/dist/contracts/token.contract.js +128 -80
  53. package/dist/contracts/token.contract.js.map +1 -1
  54. package/dist/exchanges/coingecko.d.ts +7 -1
  55. package/dist/exchanges/coingecko.js +38 -21
  56. package/dist/exchanges/coingecko.js.map +1 -1
  57. package/dist/exchanges/exchange.d.ts +15 -8
  58. package/dist/exchanges/exchange.js +65 -11
  59. package/dist/exchanges/exchange.js.map +1 -1
  60. package/dist/hive-rates.d.ts +29 -4
  61. package/dist/hive-rates.js +179 -92
  62. package/dist/hive-rates.js.map +1 -1
  63. package/dist/index.d.ts +11 -3
  64. package/dist/index.js +19 -4
  65. package/dist/index.js.map +1 -1
  66. package/dist/metadata.d.ts +63 -0
  67. package/dist/metadata.js +407 -0
  68. package/dist/metadata.js.map +1 -0
  69. package/dist/streamer.d.ts +104 -11
  70. package/dist/streamer.js +415 -143
  71. package/dist/streamer.js.map +1 -1
  72. package/dist/test.js +11 -12
  73. package/dist/test.js.map +1 -1
  74. package/dist/types/hive-stream.d.ts +85 -14
  75. package/dist/types/rates.d.ts +47 -0
  76. package/dist/types/rates.js +29 -0
  77. package/dist/types/rates.js.map +1 -0
  78. package/dist/utils.d.ts +318 -11
  79. package/dist/utils.js +804 -115
  80. package/dist/utils.js.map +1 -1
  81. package/examples/contracts/README.md +8 -0
  82. package/examples/contracts/exchange.ts +38 -0
  83. package/examples/contracts/poll.ts +21 -0
  84. package/examples/contracts/rps.ts +19 -0
  85. package/examples/contracts/tipjar.ts +19 -0
  86. package/package.json +20 -19
  87. package/test-contract-block.md +3 -3
  88. package/tests/actions.spec.ts +7 -7
  89. package/tests/adapters/actions-persistence.spec.ts +4 -4
  90. package/tests/adapters/sqlite.adapter.spec.ts +2 -2
  91. package/tests/config-input.spec.ts +90 -0
  92. package/tests/contracts/coinflip.contract.spec.ts +26 -154
  93. package/tests/contracts/dice.contract.spec.ts +24 -140
  94. package/tests/contracts/exchange.contract.spec.ts +84 -0
  95. package/tests/contracts/lotto.contract.spec.ts +30 -295
  96. package/tests/contracts/token.contract.spec.ts +72 -316
  97. package/tests/exchanges/coingecko.exchange.spec.ts +169 -0
  98. package/tests/exchanges/exchange.base.spec.ts +246 -0
  99. package/tests/helpers/mock-fetch.ts +165 -0
  100. package/tests/hive-chain-features.spec.ts +319 -0
  101. package/tests/hive-rates.spec.ts +443 -0
  102. package/tests/integration/hive-rates.integration.spec.ts +35 -0
  103. package/tests/metadata.spec.ts +63 -0
  104. package/tests/streamer-actions.spec.ts +29 -18
  105. package/tests/streamer.spec.ts +142 -49
  106. package/tests/types/rates.spec.ts +216 -0
  107. package/tests/utils.spec.ts +27 -6
@@ -1,324 +1,59 @@
1
- import { sleep } from '@hiveio/dhive/lib/utils';
2
-
3
- import { TimeAction } from './../../src/actions';
4
- import { LottoContract } from './../../src/contracts/lotto.contract';
1
+ import { createLottoContract } from './../../src/contracts/lotto.contract';
5
2
  import { Streamer } from '../../src/streamer';
3
+ import { createMockAdapter } from '../helpers/mock-adapter';
6
4
 
7
- import fiftyValidEntrants from './entrants.json';
5
+ const wait = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
8
6
 
9
7
  describe('Lotto Contract', () => {
10
8
  let sut: Streamer;
11
- let contract: LottoContract;
9
+ let contract: ReturnType<typeof createLottoContract>;
10
+ let adapter: any;
12
11
 
13
12
  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
- };
13
+ sut = new Streamer();
14
+ adapter = createMockAdapter();
15
+ await sut.registerAdapter(adapter);
34
16
 
35
- jest.restoreAllMocks();
17
+ contract = createLottoContract({ name: 'testlotto' });
36
18
 
37
- await sut.start();
19
+ // @ts-ignore
20
+ sut.api = jest.fn();
38
21
  });
39
22
 
40
23
  afterEach(async () => {
41
24
  await sut.stop();
42
25
  });
43
26
 
44
- test('Registers the lotto contract', () => {
45
- sut.registerContract('testlotto', contract as any);
27
+ test('Registers the lotto contract', async () => {
28
+ await sut.registerContract(contract);
46
29
 
47
30
  const findContract = sut['contracts'].find(c => c.name === 'testlotto');
48
-
49
31
  expect(findContract).not.toBeUndefined();
50
32
  });
51
33
 
52
- test('User enters the lotto, existing draw found', async () => {
53
- try {
54
- sut.registerContract('testlotto', contract as any);
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 as any);
34
+ test('Buys a lotto ticket and inserts a draw entry', async () => {
35
+ await sut.registerContract(contract);
94
36
 
95
- contract['_instance'] = sut;
37
+ jest.spyOn(sut, 'getTransaction').mockResolvedValue({ test: 123 } as any);
38
+ jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
96
39
 
97
- const entries = [];
40
+ const insertSpy = jest.spyOn(adapter, 'insert').mockResolvedValue(true as any);
41
+ jest.spyOn(adapter, 'find').mockResolvedValue([] as any);
98
42
 
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
- }
43
+ const memo = JSON.stringify({
44
+ hive_stream: {
45
+ contract: 'testlotto',
46
+ action: 'buy',
47
+ payload: {
48
+ type: 'hourly'
127
49
  }
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 as any);
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 as any);
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 as any);
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 as any);
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
50
  }
51
+ });
265
52
 
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();
53
+ await sut.processOperation(['transfer', { from: 'testuser', amount: '10.000 HIVE', memo }], 778782, 'blockid', 'prev', 'trxid', '2019-06-23' as any);
278
54
 
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 as any);
289
-
290
- contract['_instance'] = sut;
55
+ await wait(50);
291
56
 
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
- }
57
+ expect(insertSpy).toHaveBeenCalled();
322
58
  });
323
-
324
- });
59
+ });