hive-stream 2.0.5 → 3.0.0

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 (89) hide show
  1. package/.claude/settings.local.json +12 -0
  2. package/.env.example +2 -2
  3. package/.travis.yml +11 -11
  4. package/CHANGELOG.md +166 -0
  5. package/CLAUDE.md +75 -0
  6. package/LICENSE +21 -21
  7. package/README.md +338 -238
  8. package/dist/actions.d.ts +41 -10
  9. package/dist/actions.js +126 -23
  10. package/dist/actions.js.map +1 -1
  11. package/dist/adapters/base.adapter.d.ts +25 -25
  12. package/dist/adapters/base.adapter.js +63 -49
  13. package/dist/adapters/base.adapter.js.map +1 -1
  14. package/dist/adapters/mongodb.adapter.d.ts +50 -37
  15. package/dist/adapters/mongodb.adapter.js +363 -158
  16. package/dist/adapters/mongodb.adapter.js.map +1 -1
  17. package/dist/adapters/postgresql.adapter.d.ts +49 -0
  18. package/dist/adapters/postgresql.adapter.js +507 -0
  19. package/dist/adapters/postgresql.adapter.js.map +1 -0
  20. package/dist/adapters/sqlite.adapter.d.ts +40 -41
  21. package/dist/adapters/sqlite.adapter.js +470 -397
  22. package/dist/adapters/sqlite.adapter.js.map +1 -1
  23. package/dist/api.d.ts +6 -6
  24. package/dist/api.js +95 -55
  25. package/dist/api.js.map +1 -1
  26. package/dist/config.d.ts +16 -16
  27. package/dist/config.js +18 -18
  28. package/dist/config.js.map +1 -1
  29. package/dist/contracts/coinflip.contract.d.ts +27 -14
  30. package/dist/contracts/coinflip.contract.js +253 -94
  31. package/dist/contracts/coinflip.contract.js.map +1 -1
  32. package/dist/contracts/dice.contract.d.ts +37 -29
  33. package/dist/contracts/dice.contract.js +282 -155
  34. package/dist/contracts/dice.contract.js.map +1 -1
  35. package/dist/contracts/lotto.contract.d.ts +20 -20
  36. package/dist/contracts/lotto.contract.js +246 -246
  37. package/dist/contracts/nft.contract.d.ts +24 -0
  38. package/dist/contracts/nft.contract.js +533 -0
  39. package/dist/contracts/nft.contract.js.map +1 -0
  40. package/dist/contracts/token.contract.d.ts +18 -0
  41. package/dist/contracts/token.contract.js +263 -0
  42. package/dist/contracts/token.contract.js.map +1 -0
  43. package/dist/exchanges/bittrex.d.ts +6 -6
  44. package/dist/exchanges/bittrex.js +34 -34
  45. package/dist/exchanges/coingecko.d.ts +5 -0
  46. package/dist/exchanges/coingecko.js +40 -0
  47. package/dist/exchanges/coingecko.js.map +1 -0
  48. package/dist/exchanges/exchange.d.ts +9 -9
  49. package/dist/exchanges/exchange.js +26 -26
  50. package/dist/hive-rates.d.ts +9 -9
  51. package/dist/hive-rates.js +121 -75
  52. package/dist/hive-rates.js.map +1 -1
  53. package/dist/index.d.ts +12 -11
  54. package/dist/index.js +33 -32
  55. package/dist/index.js.map +1 -1
  56. package/dist/streamer.d.ts +140 -93
  57. package/dist/streamer.js +793 -545
  58. package/dist/streamer.js.map +1 -1
  59. package/dist/test.d.ts +1 -1
  60. package/dist/test.js +25 -25
  61. package/dist/test.js.map +1 -1
  62. package/dist/types/hive-stream.d.ts +35 -6
  63. package/dist/types/hive-stream.js +2 -2
  64. package/dist/utils.d.ts +27 -27
  65. package/dist/utils.js +271 -261
  66. package/dist/utils.js.map +1 -1
  67. package/ecosystem.config.js +17 -17
  68. package/jest.config.js +8 -8
  69. package/package.json +53 -48
  70. package/test-contract-block.md +18 -18
  71. package/tests/actions.spec.ts +252 -0
  72. package/tests/adapters/actions-persistence.spec.ts +144 -0
  73. package/tests/adapters/postgresql.adapter.spec.ts +127 -0
  74. package/tests/adapters/sqlite.adapter.spec.ts +180 -42
  75. package/tests/contracts/coinflip.contract.spec.ts +221 -131
  76. package/tests/contracts/dice.contract.spec.ts +202 -159
  77. package/tests/contracts/entrants.json +728 -728
  78. package/tests/contracts/lotto.contract.spec.ts +323 -323
  79. package/tests/contracts/nft.contract.spec.ts +948 -0
  80. package/tests/contracts/token.contract.spec.ts +334 -0
  81. package/tests/helpers/mock-adapter.ts +214 -0
  82. package/tests/setup.ts +29 -18
  83. package/tests/streamer-actions.spec.ts +263 -0
  84. package/tests/streamer.spec.ts +248 -151
  85. package/tests/utils.spec.ts +91 -94
  86. package/tsconfig.build.json +3 -22
  87. package/tslint.json +20 -20
  88. package/wallaby.js +26 -26
  89. package/.env +0 -3
@@ -1,160 +1,203 @@
1
- import { sleep } from '@hiveio/dhive/lib/utils';
2
- import { DiceContract } from './../../src/contracts/dice.contract';
3
- import { Streamer } from '../../src/streamer';
4
-
5
- describe('Dice Contract', () => {
6
- let sut: Streamer;
7
- let contract: DiceContract;
8
-
9
- beforeEach(() => {
10
- sut = new Streamer();
11
- contract = new DiceContract();
12
-
13
- // @ts-ignore
14
- sut.api = jest.fn();
15
- });
16
-
17
- afterEach(() => {
18
- sut.stop();
19
- });
20
-
21
- afterAll(() => {
22
- jest.restoreAllMocks();
23
- });
24
-
25
- test('Registers the dice contract', () => {
26
- sut.registerContract('testdice', contract);
27
-
28
- const findContract = sut['contracts'].find(c => c.name === 'testdice');
29
-
30
- expect(findContract).not.toBeUndefined();
31
- });
32
-
33
- test('User wins a roll', async () => {
34
- sut.registerContract('testdice', contract);
35
-
36
- contract['_instance'] = sut;
37
-
38
- jest.spyOn(contract as any, 'roll');
39
- jest.spyOn(contract as any, 'getBalance').mockResolvedValue(2000);
40
-
41
- jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
42
- jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
43
- jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
44
-
45
- const memo = JSON.stringify({
46
- hivePayload: {
47
- id: 'hivestream',
48
- name: 'testdice',
49
- action: 'roll',
50
- payload: {
51
- roll: 69
52
- }
53
- }
54
- });
55
-
56
- sut.processOperation(['transfer', { from: 'testuser', amount: '9.000 HIVE', memo }], 778782, 'dfjfsdfsdfsd34hfkj88787', 'fkjsdkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
57
-
58
- await sleep(100);
59
-
60
- expect(contract['roll']).toBeCalled();
61
- expect(contract['_instance'].getTransaction).toBeCalledWith(778782, 'fhkjsdhfkjsdf');
62
- expect(contract['_instance'].transferHiveTokens).toBeCalledWith('beggars', 'testuser', '12.391', 'HIVE', 'You won 12.391 HIVE. Roll: 54, Your guess: 69');
63
- });
64
-
65
- test('User loses a roll', async () => {
66
- sut.registerContract('testdice', contract);
67
-
68
- contract['_instance'] = sut;
69
-
70
- jest.spyOn(contract as any, 'roll');
71
- jest.spyOn(contract as any, 'getBalance').mockResolvedValue(2000);
72
-
73
- jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
74
- jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
75
- jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
76
-
77
- const memo = JSON.stringify({
78
- hivePayload: {
79
- id: 'hivestream',
80
- name: 'testdice',
81
- action: 'roll',
82
- payload: {
83
- roll: 69
84
- }
85
- }
86
- });
87
-
88
- sut.processOperation(['transfer', { from: 'testuser', amount: '9.000 HIVE', memo }], 778782, 'dfjfsdfsdfs4hfkj88787', 'fkjsdkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
89
-
90
- await sleep(100);
91
-
92
- expect(contract['roll']).toBeCalled();
93
- expect(sut.getTransaction).toBeCalledWith(778782, 'fhkjsdhfkjsdf');
94
- expect(sut.transferHiveTokens).toBeCalledWith('beggars', 'testuser', '0.001', 'HIVE', 'You lost 9 HIVE. Roll: 81, Your guess: 69');
95
- });
96
-
97
- test('User sent an invalid amount, refund them', async () => {
98
- sut.registerContract('testdice', contract);
99
-
100
- contract['_instance'] = sut;
101
-
102
- jest.spyOn(contract as any, 'roll');
103
- jest.spyOn(contract as any, 'getBalance').mockResolvedValue(2000);
104
-
105
- jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
106
- jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
107
- jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
108
-
109
- const memo = JSON.stringify({
110
- hivePayload: {
111
- id: 'hivestream',
112
- name: 'testdice',
113
- action: 'roll',
114
- payload: {
115
- roll: 69
116
- }
117
- }
118
- });
119
-
120
- sut.processOperation(['transfer', { from: 'testuser', amount: '100.000 HIVE', memo }], 778782, 'dfjfsdfsdfs4hfkj88787', 'fkjsdkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
121
-
122
- await sleep(100);
123
-
124
- expect(contract['roll']).toBeCalled();
125
- expect(sut.getTransaction).toBeCalledWith(778782, 'fhkjsdhfkjsdf');
126
- expect(sut.transferHiveTokens).toBeCalledWith('beggars', 'testuser', '100.000', 'HIVE', '[Refund] You sent an invalid bet amount.');
127
- });
128
-
129
- test('User sent an unsupported currency, refund them', async () => {
130
- sut.registerContract('testdice', contract);
131
-
132
- contract['_instance'] = sut;
133
-
134
- jest.spyOn(contract as any, 'roll');
135
- jest.spyOn(contract as any, 'getBalance').mockResolvedValue(2000);
136
-
137
- jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
138
- jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
139
- jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
140
-
141
- const memo = JSON.stringify({
142
- hivePayload: {
143
- id: 'hivestream',
144
- name: 'testdice',
145
- action: 'roll',
146
- payload: {
147
- roll: 69
148
- }
149
- }
150
- });
151
-
152
- sut.processOperation(['transfer', { from: 'testuser', amount: '10.000 HBD', memo }], 778782, 'dfjfsdfsdfs4hfkj88787', 'fkjsdkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
153
-
154
- await sleep(100);
155
-
156
- expect(contract['roll']).toBeCalled();
157
- expect(sut.getTransaction).toBeCalledWith(778782, 'fhkjsdhfkjsdf');
158
- expect(sut.transferHiveTokens).toBeCalledWith('beggars', 'testuser', '10.000', 'HBD', '[Refund] Invalid bet params.');
159
- });
1
+ import { sleep } from '@hiveio/dhive/lib/utils';
2
+ import { DiceContract } from './../../src/contracts/dice.contract';
3
+ import { Streamer } from '../../src/streamer';
4
+ import { createMockAdapter } from '../helpers/mock-adapter';
5
+ import BigNumber from 'bignumber.js';
6
+
7
+ describe('Dice Contract', () => {
8
+ let sut: Streamer;
9
+ let contract: DiceContract;
10
+
11
+ beforeEach(async () => {
12
+ sut = new Streamer();
13
+ await sut.registerAdapter(createMockAdapter());
14
+
15
+ contract = new DiceContract();
16
+
17
+ // @ts-ignore
18
+ sut.api = jest.fn();
19
+ });
20
+
21
+ afterEach(async () => {
22
+ await sut.stop();
23
+ });
24
+
25
+ afterAll(() => {
26
+ jest.restoreAllMocks();
27
+ });
28
+
29
+ test('Registers the dice contract', () => {
30
+ sut.registerContract('testdice', contract);
31
+
32
+ const findContract = sut['contracts'].find(c => c.name === 'testdice');
33
+
34
+ expect(findContract).not.toBeUndefined();
35
+ });
36
+
37
+ test('User wins a roll', async () => {
38
+ sut.registerContract('testdice', contract);
39
+
40
+ contract['_instance'] = sut;
41
+
42
+ jest.spyOn(contract as any, 'roll');
43
+ jest.spyOn(contract as any, 'getBalance').mockResolvedValue(new BigNumber(2000));
44
+
45
+ jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
46
+ jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
47
+ jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
48
+
49
+ const memo = JSON.stringify({
50
+ hivePayload: {
51
+ id: 'hivestream',
52
+ name: 'testdice',
53
+ action: 'roll',
54
+ payload: {
55
+ roll: 69
56
+ }
57
+ }
58
+ });
59
+
60
+ sut.processOperation(['transfer', { from: 'testuser', amount: '9.000 HIVE', memo }], 778782, 'dfjfsdfsdfsd34hfkj88787', 'fkjsdkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
61
+
62
+ await sleep(100);
63
+
64
+ expect(contract['roll']).toBeCalled();
65
+ expect(contract['_instance'].getTransaction).toBeCalledWith(778782, 'fhkjsdhfkjsdf');
66
+ expect(contract['_instance'].transferHiveTokens).toBeCalledWith('beggars', 'testuser', '12.391', 'HIVE', 'You won 12.391 HIVE. Roll: 54, Your guess: 69');
67
+ });
68
+
69
+ test('User loses a roll', async () => {
70
+ sut.registerContract('testdice', contract);
71
+
72
+ contract['_instance'] = sut;
73
+
74
+ jest.spyOn(contract as any, 'roll');
75
+ jest.spyOn(contract as any, 'getBalance').mockResolvedValue(new BigNumber(2000));
76
+
77
+ jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
78
+ jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
79
+ jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
80
+
81
+ const memo = JSON.stringify({
82
+ hivePayload: {
83
+ id: 'hivestream',
84
+ name: 'testdice',
85
+ action: 'roll',
86
+ payload: {
87
+ roll: 69
88
+ }
89
+ }
90
+ });
91
+
92
+ sut.processOperation(['transfer', { from: 'testuser', amount: '9.000 HIVE', memo }], 778782, 'dfjfsdfsdfs4hfkj88787', 'fkjsdkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
93
+
94
+ await sleep(100);
95
+
96
+ expect(contract['roll']).toBeCalled();
97
+ expect(sut.getTransaction).toBeCalledWith(778782, 'fhkjsdhfkjsdf');
98
+ expect(sut.transferHiveTokens).toBeCalledWith('beggars', 'testuser', '0.001', 'HIVE', 'You lost 9.000 HIVE. Roll: 81, Your guess: 69');
99
+ });
100
+
101
+ test('User sent an invalid amount, refund them', async () => {
102
+ sut.registerContract('testdice', contract);
103
+
104
+ contract['_instance'] = sut;
105
+
106
+ jest.spyOn(contract as any, 'roll');
107
+ jest.spyOn(contract as any, 'getBalance').mockResolvedValue(new BigNumber(2000));
108
+
109
+ jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
110
+ jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
111
+ jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
112
+
113
+ const memo = JSON.stringify({
114
+ hivePayload: {
115
+ id: 'hivestream',
116
+ name: 'testdice',
117
+ action: 'roll',
118
+ payload: {
119
+ roll: 69
120
+ }
121
+ }
122
+ });
123
+
124
+ sut.processOperation(['transfer', { from: 'testuser', amount: '100.000 HIVE', memo }], 778782, 'dfjfsdfsdfs4hfkj88787', 'fkjsdkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
125
+
126
+ await sleep(100);
127
+
128
+ expect(contract['roll']).toBeCalled();
129
+ expect(sut.getTransaction).toBeCalledWith(778782, 'fhkjsdhfkjsdf');
130
+ expect(sut.transferHiveTokens).toBeCalledWith('beggars', 'testuser', '100.000', 'HIVE', '[Refund] You sent an invalid bet amount.');
131
+ });
132
+
133
+ test('User sent an unsupported currency, refund them', async () => {
134
+ sut.registerContract('testdice', contract);
135
+
136
+ contract['_instance'] = sut;
137
+
138
+ jest.spyOn(contract as any, 'roll');
139
+ jest.spyOn(contract as any, 'getBalance').mockResolvedValue(new BigNumber(2000));
140
+
141
+ jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
142
+ jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
143
+ jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
144
+
145
+ const memo = JSON.stringify({
146
+ hivePayload: {
147
+ id: 'hivestream',
148
+ name: 'testdice',
149
+ action: 'roll',
150
+ payload: {
151
+ roll: 69
152
+ }
153
+ }
154
+ });
155
+
156
+ sut.processOperation(['transfer', { from: 'testuser', amount: '10.000 HBD', memo }], 778782, 'dfjfsdfsdfs4hfkj88787', 'fkjsdkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
157
+
158
+ await sleep(100);
159
+
160
+ expect(contract['roll']).toBeCalled();
161
+ expect(sut.getTransaction).toBeCalledWith(778782, 'fhkjsdhfkjsdf');
162
+ expect(sut.transferHiveTokens).toBeCalledWith('beggars', 'testuser', '10.000', 'HBD', '[Refund] Invalid bet params.');
163
+ });
164
+
165
+ test('Queue processes multiple concurrent rolls', async () => {
166
+ sut.registerContract('testdice', contract);
167
+
168
+ contract['_instance'] = sut;
169
+
170
+ jest.spyOn(contract as any, 'getBalance').mockResolvedValue(new BigNumber(2000));
171
+ jest.spyOn(sut, 'getTransaction').mockResolvedValue({test: 123} as any);
172
+ jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
173
+ jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
174
+
175
+ const memo1 = JSON.stringify({
176
+ hivePayload: {
177
+ id: 'hivestream',
178
+ name: 'testdice',
179
+ action: 'roll',
180
+ payload: { roll: 50 }
181
+ }
182
+ });
183
+
184
+ const memo2 = JSON.stringify({
185
+ hivePayload: {
186
+ id: 'hivestream',
187
+ name: 'testdice',
188
+ action: 'roll',
189
+ payload: { roll: 75 }
190
+ }
191
+ });
192
+
193
+ // Process multiple bets concurrently
194
+ const bet1Promise = sut.processOperation(['transfer', { from: 'user1', amount: '5.000 HIVE', memo: memo1 }], 778782, 'block1', 'prevblock1', 'trx1', '2019-06-23' as any);
195
+ const bet2Promise = sut.processOperation(['transfer', { from: 'user2', amount: '5.000 HIVE', memo: memo2 }], 778783, 'block2', 'prevblock2', 'trx2', '2019-06-23' as any);
196
+
197
+ await Promise.all([bet1Promise, bet2Promise]);
198
+ await sleep(200); // Allow queue processing to complete
199
+
200
+ // Both transfers should have been processed
201
+ expect(sut.transferHiveTokens).toHaveBeenCalledTimes(2);
202
+ });
160
203
  });