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.
- package/.claude/settings.local.json +12 -0
- package/.env.example +2 -2
- package/.travis.yml +11 -11
- package/CHANGELOG.md +166 -0
- package/CLAUDE.md +75 -0
- package/LICENSE +21 -21
- package/README.md +338 -238
- package/dist/actions.d.ts +41 -10
- package/dist/actions.js +126 -23
- package/dist/actions.js.map +1 -1
- package/dist/adapters/base.adapter.d.ts +25 -25
- package/dist/adapters/base.adapter.js +63 -49
- package/dist/adapters/base.adapter.js.map +1 -1
- package/dist/adapters/mongodb.adapter.d.ts +50 -37
- package/dist/adapters/mongodb.adapter.js +363 -158
- package/dist/adapters/mongodb.adapter.js.map +1 -1
- package/dist/adapters/postgresql.adapter.d.ts +49 -0
- package/dist/adapters/postgresql.adapter.js +507 -0
- package/dist/adapters/postgresql.adapter.js.map +1 -0
- package/dist/adapters/sqlite.adapter.d.ts +40 -41
- package/dist/adapters/sqlite.adapter.js +470 -397
- package/dist/adapters/sqlite.adapter.js.map +1 -1
- package/dist/api.d.ts +6 -6
- package/dist/api.js +95 -55
- package/dist/api.js.map +1 -1
- package/dist/config.d.ts +16 -16
- package/dist/config.js +18 -18
- package/dist/config.js.map +1 -1
- package/dist/contracts/coinflip.contract.d.ts +27 -14
- package/dist/contracts/coinflip.contract.js +253 -94
- package/dist/contracts/coinflip.contract.js.map +1 -1
- package/dist/contracts/dice.contract.d.ts +37 -29
- package/dist/contracts/dice.contract.js +282 -155
- package/dist/contracts/dice.contract.js.map +1 -1
- package/dist/contracts/lotto.contract.d.ts +20 -20
- package/dist/contracts/lotto.contract.js +246 -246
- package/dist/contracts/nft.contract.d.ts +24 -0
- package/dist/contracts/nft.contract.js +533 -0
- package/dist/contracts/nft.contract.js.map +1 -0
- package/dist/contracts/token.contract.d.ts +18 -0
- package/dist/contracts/token.contract.js +263 -0
- package/dist/contracts/token.contract.js.map +1 -0
- package/dist/exchanges/bittrex.d.ts +6 -6
- package/dist/exchanges/bittrex.js +34 -34
- package/dist/exchanges/coingecko.d.ts +5 -0
- package/dist/exchanges/coingecko.js +40 -0
- package/dist/exchanges/coingecko.js.map +1 -0
- package/dist/exchanges/exchange.d.ts +9 -9
- package/dist/exchanges/exchange.js +26 -26
- package/dist/hive-rates.d.ts +9 -9
- package/dist/hive-rates.js +121 -75
- package/dist/hive-rates.js.map +1 -1
- package/dist/index.d.ts +12 -11
- package/dist/index.js +33 -32
- package/dist/index.js.map +1 -1
- package/dist/streamer.d.ts +140 -93
- package/dist/streamer.js +793 -545
- package/dist/streamer.js.map +1 -1
- package/dist/test.d.ts +1 -1
- package/dist/test.js +25 -25
- package/dist/test.js.map +1 -1
- package/dist/types/hive-stream.d.ts +35 -6
- package/dist/types/hive-stream.js +2 -2
- package/dist/utils.d.ts +27 -27
- package/dist/utils.js +271 -261
- package/dist/utils.js.map +1 -1
- package/ecosystem.config.js +17 -17
- package/jest.config.js +8 -8
- package/package.json +53 -48
- package/test-contract-block.md +18 -18
- package/tests/actions.spec.ts +252 -0
- package/tests/adapters/actions-persistence.spec.ts +144 -0
- package/tests/adapters/postgresql.adapter.spec.ts +127 -0
- package/tests/adapters/sqlite.adapter.spec.ts +180 -42
- package/tests/contracts/coinflip.contract.spec.ts +221 -131
- package/tests/contracts/dice.contract.spec.ts +202 -159
- package/tests/contracts/entrants.json +728 -728
- package/tests/contracts/lotto.contract.spec.ts +323 -323
- package/tests/contracts/nft.contract.spec.ts +948 -0
- package/tests/contracts/token.contract.spec.ts +334 -0
- package/tests/helpers/mock-adapter.ts +214 -0
- package/tests/setup.ts +29 -18
- package/tests/streamer-actions.spec.ts +263 -0
- package/tests/streamer.spec.ts +248 -151
- package/tests/utils.spec.ts +91 -94
- package/tsconfig.build.json +3 -22
- package/tslint.json +20 -20
- package/wallaby.js +26 -26
- 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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
sut.
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
jest.spyOn(
|
|
43
|
-
jest.spyOn(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
jest.spyOn(
|
|
75
|
-
jest.spyOn(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
sut.
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
jest.spyOn(
|
|
107
|
-
jest.spyOn(
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
sut.
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
jest.spyOn(
|
|
139
|
-
jest.spyOn(
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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
|
});
|