hive-stream 2.0.6 → 3.0.1

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 (123) hide show
  1. package/.claude/settings.local.json +12 -0
  2. package/.env.example +2 -2
  3. package/.travis.yml +11 -11
  4. package/AGENTS.md +35 -0
  5. package/CHANGELOG.md +166 -0
  6. package/CLAUDE.md +75 -0
  7. package/DOCUMENTATION.md +380 -0
  8. package/LICENSE +21 -21
  9. package/README.md +429 -238
  10. package/dist/actions.d.ts +41 -10
  11. package/dist/actions.js +126 -23
  12. package/dist/actions.js.map +1 -1
  13. package/dist/adapters/base.adapter.d.ts +43 -25
  14. package/dist/adapters/base.adapter.js +79 -49
  15. package/dist/adapters/base.adapter.js.map +1 -1
  16. package/dist/adapters/mongodb.adapter.d.ts +44 -37
  17. package/dist/adapters/mongodb.adapter.js +363 -158
  18. package/dist/adapters/mongodb.adapter.js.map +1 -1
  19. package/dist/adapters/postgresql.adapter.d.ts +66 -0
  20. package/dist/adapters/postgresql.adapter.js +598 -0
  21. package/dist/adapters/postgresql.adapter.js.map +1 -0
  22. package/dist/adapters/sqlite.adapter.d.ts +57 -41
  23. package/dist/adapters/sqlite.adapter.js +561 -397
  24. package/dist/adapters/sqlite.adapter.js.map +1 -1
  25. package/dist/api.d.ts +6 -6
  26. package/dist/api.js +181 -55
  27. package/dist/api.js.map +1 -1
  28. package/dist/config.d.ts +19 -16
  29. package/dist/config.js +21 -18
  30. package/dist/config.js.map +1 -1
  31. package/dist/contracts/coinflip.contract.d.ts +9 -14
  32. package/dist/contracts/coinflip.contract.js +232 -94
  33. package/dist/contracts/coinflip.contract.js.map +1 -1
  34. package/dist/contracts/contract.d.ts +3 -0
  35. package/dist/contracts/contract.js +26 -0
  36. package/dist/contracts/contract.js.map +1 -0
  37. package/dist/contracts/dice.contract.d.ts +10 -29
  38. package/dist/contracts/dice.contract.js +217 -155
  39. package/dist/contracts/dice.contract.js.map +1 -1
  40. package/dist/contracts/exchange.contract.d.ts +11 -0
  41. package/dist/contracts/exchange.contract.js +492 -0
  42. package/dist/contracts/exchange.contract.js.map +1 -0
  43. package/dist/contracts/lotto.contract.d.ts +16 -20
  44. package/dist/contracts/lotto.contract.js +238 -246
  45. package/dist/contracts/lotto.contract.js.map +1 -1
  46. package/dist/contracts/nft.contract.d.ts +28 -0
  47. package/dist/contracts/nft.contract.js +598 -0
  48. package/dist/contracts/nft.contract.js.map +1 -0
  49. package/dist/contracts/poll.contract.d.ts +4 -0
  50. package/dist/contracts/poll.contract.js +105 -0
  51. package/dist/contracts/poll.contract.js.map +1 -0
  52. package/dist/contracts/rps.contract.d.ts +9 -0
  53. package/dist/contracts/rps.contract.js +217 -0
  54. package/dist/contracts/rps.contract.js.map +1 -0
  55. package/dist/contracts/tipjar.contract.d.ts +4 -0
  56. package/dist/contracts/tipjar.contract.js +60 -0
  57. package/dist/contracts/tipjar.contract.js.map +1 -0
  58. package/dist/contracts/token.contract.d.ts +4 -0
  59. package/dist/contracts/token.contract.js +311 -0
  60. package/dist/contracts/token.contract.js.map +1 -0
  61. package/dist/exchanges/bittrex.d.ts +6 -6
  62. package/dist/exchanges/bittrex.js +34 -34
  63. package/dist/exchanges/coingecko.d.ts +11 -0
  64. package/dist/exchanges/coingecko.js +57 -0
  65. package/dist/exchanges/coingecko.js.map +1 -0
  66. package/dist/exchanges/exchange.d.ts +16 -9
  67. package/dist/exchanges/exchange.js +80 -26
  68. package/dist/exchanges/exchange.js.map +1 -1
  69. package/dist/hive-rates.d.ts +34 -9
  70. package/dist/hive-rates.js +208 -75
  71. package/dist/hive-rates.js.map +1 -1
  72. package/dist/index.d.ts +19 -11
  73. package/dist/index.js +47 -32
  74. package/dist/index.js.map +1 -1
  75. package/dist/streamer.d.ts +233 -93
  76. package/dist/streamer.js +1063 -545
  77. package/dist/streamer.js.map +1 -1
  78. package/dist/test.d.ts +1 -1
  79. package/dist/test.js +24 -25
  80. package/dist/test.js.map +1 -1
  81. package/dist/types/hive-stream.d.ts +106 -6
  82. package/dist/types/hive-stream.js +2 -2
  83. package/dist/types/rates.d.ts +47 -0
  84. package/dist/types/rates.js +29 -0
  85. package/dist/types/rates.js.map +1 -0
  86. package/dist/utils.d.ts +334 -27
  87. package/dist/utils.js +960 -261
  88. package/dist/utils.js.map +1 -1
  89. package/ecosystem.config.js +17 -17
  90. package/examples/contracts/README.md +8 -0
  91. package/examples/contracts/exchange.ts +38 -0
  92. package/examples/contracts/poll.ts +21 -0
  93. package/examples/contracts/rps.ts +19 -0
  94. package/examples/contracts/tipjar.ts +19 -0
  95. package/jest.config.js +8 -8
  96. package/package.json +54 -48
  97. package/test-contract-block.md +18 -18
  98. package/tests/actions.spec.ts +252 -0
  99. package/tests/adapters/actions-persistence.spec.ts +144 -0
  100. package/tests/adapters/postgresql.adapter.spec.ts +127 -0
  101. package/tests/adapters/sqlite.adapter.spec.ts +180 -42
  102. package/tests/contracts/coinflip.contract.spec.ts +94 -132
  103. package/tests/contracts/dice.contract.spec.ts +87 -160
  104. package/tests/contracts/entrants.json +728 -728
  105. package/tests/contracts/exchange.contract.spec.ts +84 -0
  106. package/tests/contracts/lotto.contract.spec.ts +59 -324
  107. package/tests/contracts/nft.contract.spec.ts +948 -0
  108. package/tests/contracts/token.contract.spec.ts +90 -0
  109. package/tests/exchanges/coingecko.exchange.spec.ts +169 -0
  110. package/tests/exchanges/exchange.base.spec.ts +246 -0
  111. package/tests/helpers/mock-adapter.ts +214 -0
  112. package/tests/helpers/mock-fetch.ts +165 -0
  113. package/tests/hive-chain-features.spec.ts +238 -0
  114. package/tests/hive-rates.spec.ts +443 -0
  115. package/tests/integration/hive-rates.integration.spec.ts +35 -0
  116. package/tests/setup.ts +29 -18
  117. package/tests/streamer-actions.spec.ts +274 -0
  118. package/tests/streamer.spec.ts +342 -152
  119. package/tests/types/rates.spec.ts +216 -0
  120. package/tests/utils.spec.ts +113 -95
  121. package/tsconfig.build.json +3 -22
  122. package/tslint.json +20 -20
  123. package/wallaby.js +26 -26
@@ -0,0 +1,84 @@
1
+ import { Streamer } from '../../src/streamer';
2
+ import { SqliteAdapter } from '../../src/adapters/sqlite.adapter';
3
+ import { createExchangeContract } from '../../src/contracts/exchange.contract';
4
+
5
+ describe('Exchange Contract', () => {
6
+ let streamer: Streamer;
7
+ let adapter: SqliteAdapter;
8
+ let contract: ReturnType<typeof createExchangeContract>;
9
+
10
+ beforeEach(async () => {
11
+ streamer = new Streamer({
12
+ JSON_ID: 'hivestream',
13
+ PAYLOAD_IDENTIFIER: 'hive_stream'
14
+ });
15
+
16
+ adapter = new SqliteAdapter(':memory:');
17
+ await streamer.registerAdapter(adapter);
18
+
19
+ contract = createExchangeContract({ name: 'exchange' });
20
+ await streamer.registerContract(contract);
21
+ });
22
+
23
+ afterEach(async () => {
24
+ await streamer.stop();
25
+ });
26
+
27
+ const baseContext = (trigger: 'custom_json' | 'transfer' | 'time', sender: string) => ({
28
+ trigger,
29
+ streamer,
30
+ adapter,
31
+ config: streamer['config'],
32
+ block: { number: 100, id: 'block', previousId: 'prev', time: new Date() },
33
+ transaction: { id: `trx-${trigger}-${sender}` },
34
+ sender,
35
+ customJson: trigger === 'custom_json' ? { id: 'hivestream', json: {}, isSignedWithActiveKey: true } : undefined,
36
+ transfer: trigger === 'transfer' ? { from: sender, to: 'exchange', rawAmount: '', amount: '', asset: '', memo: '' } : undefined
37
+ });
38
+
39
+ test('Deposits, places orders, and matches', async () => {
40
+ await contract.actions.createPair.handler({ base: 'HIVE', quote: 'HBD' }, baseContext('custom_json', 'alice'));
41
+
42
+ const aliceDepositContext = baseContext('transfer', 'alice');
43
+ aliceDepositContext.transfer.rawAmount = '100.000 HBD';
44
+ aliceDepositContext.transfer.amount = '100.000';
45
+ aliceDepositContext.transfer.asset = 'HBD';
46
+ await contract.actions.deposit.handler({}, aliceDepositContext);
47
+
48
+ const bobDepositContext = baseContext('transfer', 'bob');
49
+ bobDepositContext.transfer.rawAmount = '10.000 HIVE';
50
+ bobDepositContext.transfer.amount = '10.000';
51
+ bobDepositContext.transfer.asset = 'HIVE';
52
+ await contract.actions.deposit.handler({}, bobDepositContext);
53
+
54
+ await contract.actions.placeOrder.handler({
55
+ side: 'buy',
56
+ base: 'HIVE',
57
+ quote: 'HBD',
58
+ price: '2',
59
+ amount: '5'
60
+ }, baseContext('custom_json', 'alice'));
61
+
62
+ await contract.actions.placeOrder.handler({
63
+ side: 'sell',
64
+ base: 'HIVE',
65
+ quote: 'HBD',
66
+ price: '2',
67
+ amount: '5'
68
+ }, baseContext('custom_json', 'bob'));
69
+
70
+ await contract.actions.matchOrders.handler({ base: 'HIVE', quote: 'HBD', limit: 10, snapshot: true, depth: 20 }, baseContext('time', 'system'));
71
+
72
+ const aliceHive = await adapter.query('SELECT available, locked FROM exchange_balances WHERE account = ? AND asset = ?', ['alice', 'HIVE']);
73
+ const bobHbd = await adapter.query('SELECT available, locked FROM exchange_balances WHERE account = ? AND asset = ?', ['bob', 'HBD']);
74
+
75
+ expect(aliceHive[0].available).toBe('4.995');
76
+ expect(bobHbd[0].available).toBe('9.980');
77
+
78
+ const snapshots = await adapter.query(
79
+ 'SELECT bids, asks FROM exchange_orderbook_snapshots WHERE base_asset = ? AND quote_asset = ?',
80
+ ['HIVE', 'HBD']
81
+ );
82
+ expect(snapshots.length).toBeGreaterThan(0);
83
+ });
84
+ });
@@ -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';
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
-
324
- });
1
+ import { createLottoContract } from './../../src/contracts/lotto.contract';
2
+ import { Streamer } from '../../src/streamer';
3
+ import { createMockAdapter } from '../helpers/mock-adapter';
4
+
5
+ const wait = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
6
+
7
+ describe('Lotto Contract', () => {
8
+ let sut: Streamer;
9
+ let contract: ReturnType<typeof createLottoContract>;
10
+ let adapter: any;
11
+
12
+ beforeEach(async () => {
13
+ sut = new Streamer();
14
+ adapter = createMockAdapter();
15
+ await sut.registerAdapter(adapter);
16
+
17
+ contract = createLottoContract({ name: 'testlotto' });
18
+
19
+ // @ts-ignore
20
+ sut.api = jest.fn();
21
+ });
22
+
23
+ afterEach(async () => {
24
+ await sut.stop();
25
+ });
26
+
27
+ test('Registers the lotto contract', async () => {
28
+ await sut.registerContract(contract);
29
+
30
+ const findContract = sut['contracts'].find(c => c.name === 'testlotto');
31
+ expect(findContract).not.toBeUndefined();
32
+ });
33
+
34
+ test('Buys a lotto ticket and inserts a draw entry', async () => {
35
+ await sut.registerContract(contract);
36
+
37
+ jest.spyOn(sut, 'getTransaction').mockResolvedValue({ test: 123 } as any);
38
+ jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
39
+
40
+ const insertSpy = jest.spyOn(adapter, 'insert').mockResolvedValue(true as any);
41
+ jest.spyOn(adapter, 'find').mockResolvedValue([] as any);
42
+
43
+ const memo = JSON.stringify({
44
+ hive_stream: {
45
+ contract: 'testlotto',
46
+ action: 'buy',
47
+ payload: {
48
+ type: 'hourly'
49
+ }
50
+ }
51
+ });
52
+
53
+ await sut.processOperation(['transfer', { from: 'testuser', amount: '10.000 HIVE', memo }], 778782, 'blockid', 'prev', 'trxid', '2019-06-23' as any);
54
+
55
+ await wait(50);
56
+
57
+ expect(insertSpy).toHaveBeenCalled();
58
+ });
59
+ });