hive-stream 2.0.2 → 2.0.5

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 (88) hide show
  1. package/.env +3 -1
  2. package/.env.example +3 -2
  3. package/.travis.yml +11 -11
  4. package/LICENSE +21 -21
  5. package/README.md +238 -236
  6. package/dist/actions.d.ts +10 -9
  7. package/dist/actions.js +23 -15
  8. package/dist/actions.js.map +1 -1
  9. package/dist/adapters/base.adapter.d.ts +25 -21
  10. package/dist/adapters/base.adapter.js +49 -63
  11. package/dist/adapters/base.adapter.js.map +1 -1
  12. package/dist/adapters/mongodb.adapter.d.ts +37 -29
  13. package/dist/adapters/mongodb.adapter.js +158 -113
  14. package/dist/adapters/mongodb.adapter.js.map +1 -1
  15. package/dist/adapters/sqlite.adapter.d.ts +41 -23
  16. package/dist/adapters/sqlite.adapter.js +397 -121
  17. package/dist/adapters/sqlite.adapter.js.map +1 -1
  18. package/dist/api.d.ts +6 -0
  19. package/dist/api.js +56 -0
  20. package/dist/api.js.map +1 -0
  21. package/dist/config.d.ts +16 -14
  22. package/dist/config.js +18 -15
  23. package/dist/config.js.map +1 -1
  24. package/dist/contracts/coinflip.contract.d.ts +14 -0
  25. package/dist/contracts/coinflip.contract.js +95 -0
  26. package/dist/contracts/coinflip.contract.js.map +1 -0
  27. package/dist/contracts/dice.contract.d.ts +29 -29
  28. package/dist/contracts/dice.contract.js +155 -157
  29. package/dist/contracts/dice.contract.js.map +1 -1
  30. package/dist/contracts/lotto.contract.d.ts +20 -16
  31. package/dist/contracts/lotto.contract.js +246 -107
  32. package/dist/contracts/lotto.contract.js.map +1 -1
  33. package/dist/exchanges/bittrex.d.ts +6 -0
  34. package/dist/exchanges/bittrex.js +35 -0
  35. package/dist/exchanges/bittrex.js.map +1 -0
  36. package/dist/exchanges/exchange.d.ts +9 -0
  37. package/dist/exchanges/exchange.js +27 -0
  38. package/dist/exchanges/exchange.js.map +1 -0
  39. package/dist/hive-rates.d.ts +9 -0
  40. package/dist/hive-rates.js +76 -0
  41. package/dist/hive-rates.js.map +1 -0
  42. package/dist/index.d.ts +11 -10
  43. package/dist/index.js +32 -15
  44. package/dist/index.js.map +1 -1
  45. package/dist/streamer.d.ts +93 -70
  46. package/dist/streamer.js +545 -439
  47. package/dist/streamer.js.map +1 -1
  48. package/dist/test.d.ts +1 -1
  49. package/dist/test.js +25 -27
  50. package/dist/test.js.map +1 -1
  51. package/dist/types/hive-stream.d.ts +6 -6
  52. package/dist/types/hive-stream.js +2 -2
  53. package/dist/utils.d.ts +27 -14
  54. package/dist/utils.js +261 -85
  55. package/dist/utils.js.map +1 -1
  56. package/ecosystem.config.js +17 -17
  57. package/jest.config.js +9 -14
  58. package/package.json +48 -40
  59. package/test-contract-block.md +18 -18
  60. package/tests/adapters/sqlite.adapter.spec.ts +43 -0
  61. package/tests/contracts/coinflip.contract.spec.ts +132 -0
  62. package/tests/contracts/dice.contract.spec.ts +160 -0
  63. package/tests/contracts/entrants.json +729 -0
  64. package/tests/contracts/lotto.contract.spec.ts +324 -0
  65. package/tests/setup.ts +19 -0
  66. package/tests/streamer.spec.ts +152 -0
  67. package/tests/utils.spec.ts +95 -0
  68. package/tsconfig.build.json +22 -19
  69. package/tslint.json +20 -20
  70. package/wallaby.js +26 -0
  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/hive-stream.json +0 -1
  84. package/test/contracts/dice.contract.spec.ts +0 -253
  85. package/test/contracts/lotto.contract.spec.ts +0 -61
  86. package/test/setup.ts +0 -9
  87. package/test/streamer.spec.ts +0 -348
  88. package/test/utils.spec.ts +0 -12
@@ -0,0 +1,324 @@
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
+ });
package/tests/setup.ts ADDED
@@ -0,0 +1,19 @@
1
+ import { GlobalWithFetchMock } from 'jest-fetch-mock';
2
+
3
+ // (global as any).console = {
4
+ // log: jest.fn(), // console.log are ignored in tests
5
+
6
+ // // Keep native behaviour for other methods, use those to print out things in your own tests, not `console.log`
7
+ // error: console.error,
8
+ // warn: console.warn,
9
+ // info: console.info,
10
+ // debug: console.debug,
11
+ // };
12
+
13
+ const customGlobal: GlobalWithFetchMock = global as unknown as GlobalWithFetchMock;
14
+ customGlobal.fetch = require('jest-fetch-mock');
15
+ customGlobal.fetchMock = customGlobal.fetch;
16
+
17
+ process.on('unhandledRejection', (error) => {
18
+ throw error; // Or whatever you like...
19
+ });
@@ -0,0 +1,152 @@
1
+ import { TimeAction } from '../src/actions';
2
+ import { Streamer } from '../src/streamer';
3
+
4
+ describe('Streamer', () => {
5
+ let sut: Streamer;
6
+
7
+ beforeEach(() => {
8
+ sut = new Streamer({
9
+ JSON_ID: 'testing'
10
+ });
11
+ });
12
+
13
+ afterEach(() => {
14
+ sut.stop();
15
+ });
16
+
17
+ describe('Adapters', () => {
18
+ test('Registers adapter and calls the create lifecycle method', () => {
19
+ const adapter = {
20
+ create: jest.fn().mockResolvedValue(true),
21
+ destroy: jest.fn()
22
+ };
23
+
24
+ sut.registerAdapter(adapter);
25
+
26
+ expect(adapter.create).toBeCalled();
27
+ });
28
+ });
29
+
30
+ describe('Actions', () => {
31
+ test('Registers a new action', async () => {
32
+ const adapter = {
33
+ create: jest.fn().mockResolvedValue(true),
34
+ destroy: jest.fn(),
35
+ loadActions: jest.fn().mockResolvedValue([])
36
+ };
37
+
38
+ sut.registerAdapter(adapter);
39
+
40
+ const action = new TimeAction('1m', 'testoneminute', 'testcontract', 'testmethod');
41
+
42
+ await sut.registerAction(action);
43
+
44
+ const foundAction = sut['actions'].find(a => a.id === 'testoneminute');
45
+
46
+ expect(foundAction).not.toBeUndefined();
47
+ });
48
+
49
+ test('Does not allow duplicate actions of the same id', async () => {
50
+ const adapter = {
51
+ create: jest.fn().mockResolvedValue(true),
52
+ destroy: jest.fn(),
53
+ loadActions: jest.fn().mockResolvedValue([])
54
+ };
55
+
56
+ sut.registerAdapter(adapter);
57
+
58
+ const action = new TimeAction('1m', 'testoneminute', 'testcontract', 'testmethod');
59
+ const action2 = new TimeAction('1m', 'testoneminute', 'testcontract', 'testmethod');
60
+
61
+ await sut.registerAction(action);
62
+ await sut.registerAction(action2);
63
+
64
+ expect(sut['actions'].length).toStrictEqual(1);
65
+ });
66
+
67
+ test('Registers actions loaded from adapter loadActions call', async () => {
68
+ const adapter = {
69
+ create: jest.fn().mockResolvedValue(true),
70
+ destroy: jest.fn(),
71
+ loadActions: jest.fn().mockResolvedValue([new TimeAction('1m', 'testoneminute', 'testcontract', 'testmethod')])
72
+ };
73
+
74
+ sut.registerAdapter(adapter);
75
+
76
+ const action = new TimeAction('1h', 'testonehour', 'testcontract', 'testmethod');
77
+
78
+ await sut.registerAction(action);
79
+
80
+ const foundAction = sut['actions'].find(a => a.id === 'testoneminute');
81
+
82
+ expect(foundAction).not.toBeUndefined();
83
+ });
84
+ });
85
+
86
+ describe('Contracts', () => {
87
+ test('Should register a new contract', () => {
88
+ const contract = {
89
+ myMethod: jest.fn()
90
+ };
91
+
92
+ sut.registerContract('testcontract', contract);
93
+
94
+ expect(contract['_instance']).toBeInstanceOf(Streamer);
95
+ expect(sut['contracts'].length).toStrictEqual(1);
96
+ });
97
+
98
+ test('Should register a new contract and call its create method', () => {
99
+ const contract = {
100
+ create: jest.fn(),
101
+ myMethod: jest.fn()
102
+ };
103
+
104
+ sut.registerContract('testcontract', contract);
105
+
106
+ expect(contract.create).toBeCalled();
107
+ expect(contract['_instance']).toBeInstanceOf(Streamer);
108
+ expect(sut['contracts'].length).toStrictEqual(1);
109
+ });
110
+
111
+ test('Should unregister a registered contract', () => {
112
+ const contract = {
113
+ myMethod: jest.fn()
114
+ };
115
+
116
+ sut.registerContract('testcontract', contract);
117
+ sut.unregisterContract('testcontract');
118
+
119
+ expect(sut['contracts'].length).toStrictEqual(0);
120
+ });
121
+
122
+ test('Should unregister a registered contract and call its destroy method', () => {
123
+ const contract = {
124
+ destroy: jest.fn(),
125
+ myMethod: jest.fn()
126
+ };
127
+
128
+ sut.registerContract('testcontract', contract);
129
+ sut.unregisterContract('testcontract');
130
+
131
+ expect(contract.destroy).toBeCalled();
132
+ expect(sut['contracts'].length).toStrictEqual(0);
133
+ });
134
+ });
135
+
136
+ test('Start method should resume from previous block number', async () => {
137
+ const adapter = {
138
+ loadState: jest.fn().mockResolvedValue({ lastBlockNumber: 509992 })
139
+ };
140
+
141
+ sut.registerAdapter(adapter);
142
+
143
+ jest.spyOn(sut as any, 'getBlock').mockImplementation(() => true);
144
+ jest.spyOn(sut as any, 'getLatestBlock').mockImplementation(() => true);
145
+
146
+ await sut.start();
147
+
148
+ expect(sut['lastBlockNumber']).toStrictEqual(509992);
149
+
150
+ sut.stop();
151
+ });
152
+ });
@@ -0,0 +1,95 @@
1
+ import { Utils } from './../src/utils';
2
+
3
+ describe('Utils', () => {
4
+
5
+ describe('Round Precision', () => {
6
+ test('Properly rounds precision of number to 3 places', () => {
7
+ const value = 99.299223;
8
+
9
+ expect(Utils.roundPrecision(value, 3)).toStrictEqual(99.299);
10
+ });
11
+
12
+ test('Properly rounds precision of number up and to 3 places', () => {
13
+ const value = 99.2966;
14
+
15
+ expect(Utils.roundPrecision(value, 3)).toStrictEqual(99.297);
16
+ });
17
+
18
+ test('Invalid numeric values passed', () => {
19
+ expect(Utils.roundPrecision('dasd', 3)).toBeNaN();
20
+ });
21
+ });
22
+
23
+ test('Should generate two deterministic numbers', () => {
24
+ // Should generate a deterministic random number
25
+ expect(Utils.randomNumber('dasdasdas', '2312fsdfsdfsdf', 'kfjlksdjflksdjf999')).toStrictEqual(26);
26
+
27
+ expect(Utils.randomNumber('fdfsdfsdfsdfsf', '2312fsdfsdfsdf', 'kfjlksdjflksdjf999')).toStrictEqual(43);
28
+ });
29
+
30
+ test('Should shuffle array in a non-deterministic way', () => {
31
+ const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
32
+ const arrayCloned = [...array];
33
+
34
+ Utils.shuffle(array);
35
+
36
+ expect(array).not.toMatchObject(arrayCloned);
37
+ });
38
+
39
+ describe('Generate String', () => {
40
+ test('Generates a memo 6 characters in length', () => {
41
+ expect(Utils.randomString(6)).toHaveLength(6);
42
+ });
43
+
44
+ test('Generates a memo using default 12 character length', () => {
45
+ expect(Utils.randomString()).toHaveLength(12);
46
+ });
47
+ });
48
+
49
+ describe('Random Range', () => {
50
+ test('Should generate a random number between 0 and 10', () => {
51
+ expect(Utils.randomRange(0, 10)).toBeLessThanOrEqual(10);
52
+ });
53
+
54
+ test('Should generate the number 10', () => {
55
+ expect(Utils.randomRange(10, 10)).toStrictEqual(10);
56
+ });
57
+
58
+ test('Only pass min and not max', () => {
59
+ expect(Utils.randomRange(0)).toBeLessThanOrEqual(2000);
60
+ });
61
+
62
+ test('Pass non numeric values to random range', () => {
63
+ expect(Utils.randomRange('dd' as any, 'asjj' as any)).toBeNaN();
64
+ });
65
+ });
66
+
67
+ describe('Convert Hive Amount', () => {
68
+ test('Converts amount', async () => {
69
+ const amount = 25;
70
+ const fiatSymbol = 'USD';
71
+ const hiveSymbol = 'HIVE';
72
+
73
+ (fetch as any)
74
+ .once(JSON.stringify({'success':true,'message':'','result':{'Bid':6905.98900000,'Ask':6925.65900000,'Last':6925.66500000}}))
75
+ .once(JSON.stringify({'success':true,'message':'','result':{'Bid':0.00003083,'Ask':0.00003169,'Last':0.00003192}}))
76
+ .once(JSON.stringify({'success':true,'message':'','result':{'Bid':0.00010800,'Ask':0.00010900,'Last':0.00010800}}))
77
+ .once(JSON.stringify({'rates':{'CAD':1.4383752203,'HKD':7.7558193453,'ISK':140.4989335064,'PHP':51.4003524066,'DKK':6.9281276083,'HUF':326.7458035797,'CZK':25.6283038116,'GBP':0.862190485,'RON':4.4960586108,'SEK':10.2675507744,'IDR':16574.9976815358,'INR':76.1620142817,'BRL':5.0683483261,'RUB':79.8293610313,'HRK':7.053695632,'JPY':110.4609106928,'THB':32.8646944264,'CHF':0.982101456,'EUR':0.9273856997,'MYR':4.4449596587,'BGN':1.8137809515,'TRY':6.5859222851,'CNY':7.0838356673,'NOK':11.3667810442,'NZD':1.7563757767,'ZAR':17.6333116943,'USD':1.0,'MXN':24.6805156264,'SGD':1.4601687842,'AUD':1.7236390615,'ILS':3.6698506909,'KRW':1256.7003616804,'PLN':4.2711675786},'base':'USD','date':'2020-03-23'}));
78
+
79
+ const value = await Utils.convertHiveAmount(amount, fiatSymbol, hiveSymbol);
80
+
81
+ expect(fetch).toBeCalledWith(`https://api.bittrex.com/v3/markets/USD-BTC/ticker`);
82
+ expect(fetch).toBeCalledWith(`https://api.bittrex.com/v3/markets/HIVE-BTC/ticker`);
83
+ expect(fetch).toBeCalledWith(`https://api.bittrex.com/v3/markets/HBD-BTC/ticker`);
84
+
85
+ expect(value).toStrictEqual(113.088);
86
+ });
87
+ });
88
+
89
+ describe('Get transfer URL', () => {
90
+ test('Gets a transfer URL string', () => {
91
+ expect(Utils.getTransferUrl('beggars', 'TEST123', '10.000 HIVE', 'http://localhost:5001')).toStrictEqual(`https://hivesigner.com/sign/transfer?to=beggars&memo=TEST123&amount=10.000 HIVE&redirect_uri=http://localhost:5001`);
92
+ });
93
+ });
94
+
95
+ });
@@ -1,20 +1,23 @@
1
- {
2
- "compilerOptions": {
3
- "module": "commonjs",
4
- "esModuleInterop": true,
5
- "target": "es6",
6
- "allowSyntheticDefaultImports": true,
7
- "moduleResolution": "node",
8
- "declaration": true,
9
- "sourceMap": true,
10
- "outDir": "dist",
11
- "baseUrl": ".",
12
- "paths": {
13
- "*": [
14
- "node_modules/*",
15
- "src/types/*"
16
- ]
17
- }
18
- },
19
- "exclude": ["node_modules", ".vscode", "dist", "test"]
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "esModuleInterop": true,
5
+ "target": "esnext",
6
+ "allowSyntheticDefaultImports": true,
7
+ "emitDecoratorMetadata": true,
8
+ "experimentalDecorators": true,
9
+ "resolveJsonModule": true,
10
+ "moduleResolution": "node",
11
+ "declaration": true,
12
+ "sourceMap": true,
13
+ "outDir": "dist",
14
+ "baseUrl": ".",
15
+ "paths": {
16
+ "*": [
17
+ "node_modules/*",
18
+ "src/types/*"
19
+ ]
20
+ }
21
+ },
22
+ "exclude": ["node_modules", ".vscode", "dist", "tests"]
20
23
  }
package/tslint.json CHANGED
@@ -1,21 +1,21 @@
1
- {
2
- "extends": [
3
- "tslint:recommended"
4
- ],
5
- "jsRules": {},
6
- "rules": {
7
- "quotemark": [true, "single"],
8
- "ordered-imports": false,
9
- "interface-name": false,
10
- "object-literal-sort-keys": false,
11
- "eofline": false,
12
- "no-console": false,
13
- "member-ordering": false,
14
- "max-line-length": false,
15
- "trailing-comma": false,
16
- "no-string-literal": false,
17
- "arrow-parens": false,
18
- "object-literal-key-quotes": false
19
- },
20
- "rulesDirectory": []
1
+ {
2
+ "extends": [
3
+ "tslint:recommended"
4
+ ],
5
+ "jsRules": {},
6
+ "rules": {
7
+ "quotemark": [true, "single"],
8
+ "ordered-imports": false,
9
+ "interface-name": false,
10
+ "object-literal-sort-keys": false,
11
+ "eofline": false,
12
+ "no-console": false,
13
+ "member-ordering": false,
14
+ "max-line-length": false,
15
+ "trailing-comma": false,
16
+ "no-string-literal": false,
17
+ "arrow-parens": false,
18
+ "object-literal-key-quotes": false
19
+ },
20
+ "rulesDirectory": []
21
21
  }