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,324 +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
|
-
|
|
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 as any);
|
|
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 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);
|
|
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 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
|
+
}
|
|
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 as any);
|
|
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
324
|
});
|