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.
- package/.claude/settings.local.json +12 -0
- package/.env.example +2 -2
- package/.travis.yml +11 -11
- package/AGENTS.md +35 -0
- package/CHANGELOG.md +166 -0
- package/CLAUDE.md +75 -0
- package/DOCUMENTATION.md +380 -0
- package/LICENSE +21 -21
- package/README.md +429 -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 +43 -25
- package/dist/adapters/base.adapter.js +79 -49
- package/dist/adapters/base.adapter.js.map +1 -1
- package/dist/adapters/mongodb.adapter.d.ts +44 -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 +66 -0
- package/dist/adapters/postgresql.adapter.js +598 -0
- package/dist/adapters/postgresql.adapter.js.map +1 -0
- package/dist/adapters/sqlite.adapter.d.ts +57 -41
- package/dist/adapters/sqlite.adapter.js +561 -397
- package/dist/adapters/sqlite.adapter.js.map +1 -1
- package/dist/api.d.ts +6 -6
- package/dist/api.js +181 -55
- package/dist/api.js.map +1 -1
- package/dist/config.d.ts +19 -16
- package/dist/config.js +21 -18
- package/dist/config.js.map +1 -1
- package/dist/contracts/coinflip.contract.d.ts +9 -14
- package/dist/contracts/coinflip.contract.js +232 -94
- package/dist/contracts/coinflip.contract.js.map +1 -1
- package/dist/contracts/contract.d.ts +3 -0
- package/dist/contracts/contract.js +26 -0
- package/dist/contracts/contract.js.map +1 -0
- package/dist/contracts/dice.contract.d.ts +10 -29
- package/dist/contracts/dice.contract.js +217 -155
- package/dist/contracts/dice.contract.js.map +1 -1
- package/dist/contracts/exchange.contract.d.ts +11 -0
- package/dist/contracts/exchange.contract.js +492 -0
- package/dist/contracts/exchange.contract.js.map +1 -0
- package/dist/contracts/lotto.contract.d.ts +16 -20
- package/dist/contracts/lotto.contract.js +238 -246
- package/dist/contracts/lotto.contract.js.map +1 -1
- package/dist/contracts/nft.contract.d.ts +28 -0
- package/dist/contracts/nft.contract.js +598 -0
- package/dist/contracts/nft.contract.js.map +1 -0
- package/dist/contracts/poll.contract.d.ts +4 -0
- package/dist/contracts/poll.contract.js +105 -0
- package/dist/contracts/poll.contract.js.map +1 -0
- package/dist/contracts/rps.contract.d.ts +9 -0
- package/dist/contracts/rps.contract.js +217 -0
- package/dist/contracts/rps.contract.js.map +1 -0
- package/dist/contracts/tipjar.contract.d.ts +4 -0
- package/dist/contracts/tipjar.contract.js +60 -0
- package/dist/contracts/tipjar.contract.js.map +1 -0
- package/dist/contracts/token.contract.d.ts +4 -0
- package/dist/contracts/token.contract.js +311 -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 +11 -0
- package/dist/exchanges/coingecko.js +57 -0
- package/dist/exchanges/coingecko.js.map +1 -0
- package/dist/exchanges/exchange.d.ts +16 -9
- package/dist/exchanges/exchange.js +80 -26
- package/dist/exchanges/exchange.js.map +1 -1
- package/dist/hive-rates.d.ts +34 -9
- package/dist/hive-rates.js +208 -75
- package/dist/hive-rates.js.map +1 -1
- package/dist/index.d.ts +19 -11
- package/dist/index.js +47 -32
- package/dist/index.js.map +1 -1
- package/dist/streamer.d.ts +233 -93
- package/dist/streamer.js +1063 -545
- package/dist/streamer.js.map +1 -1
- package/dist/test.d.ts +1 -1
- package/dist/test.js +24 -25
- package/dist/test.js.map +1 -1
- package/dist/types/hive-stream.d.ts +106 -6
- package/dist/types/hive-stream.js +2 -2
- package/dist/types/rates.d.ts +47 -0
- package/dist/types/rates.js +29 -0
- package/dist/types/rates.js.map +1 -0
- package/dist/utils.d.ts +334 -27
- package/dist/utils.js +960 -261
- package/dist/utils.js.map +1 -1
- package/ecosystem.config.js +17 -17
- package/examples/contracts/README.md +8 -0
- package/examples/contracts/exchange.ts +38 -0
- package/examples/contracts/poll.ts +21 -0
- package/examples/contracts/rps.ts +19 -0
- package/examples/contracts/tipjar.ts +19 -0
- package/jest.config.js +8 -8
- package/package.json +54 -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 +94 -132
- package/tests/contracts/dice.contract.spec.ts +87 -160
- package/tests/contracts/entrants.json +728 -728
- package/tests/contracts/exchange.contract.spec.ts +84 -0
- package/tests/contracts/lotto.contract.spec.ts +59 -324
- package/tests/contracts/nft.contract.spec.ts +948 -0
- package/tests/contracts/token.contract.spec.ts +90 -0
- package/tests/exchanges/coingecko.exchange.spec.ts +169 -0
- package/tests/exchanges/exchange.base.spec.ts +246 -0
- package/tests/helpers/mock-adapter.ts +214 -0
- package/tests/helpers/mock-fetch.ts +165 -0
- package/tests/hive-chain-features.spec.ts +238 -0
- package/tests/hive-rates.spec.ts +443 -0
- package/tests/integration/hive-rates.integration.spec.ts +35 -0
- package/tests/setup.ts +29 -18
- package/tests/streamer-actions.spec.ts +274 -0
- package/tests/streamer.spec.ts +342 -152
- package/tests/types/rates.spec.ts +216 -0
- package/tests/utils.spec.ts +113 -95
- package/tsconfig.build.json +3 -22
- package/tslint.json +20 -20
- package/wallaby.js +26 -26
|
@@ -1,132 +1,94 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
jest.spyOn(sut, '
|
|
42
|
-
jest.spyOn(sut, '
|
|
43
|
-
jest.spyOn(sut, '
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
expect(
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
await sleep(100);
|
|
96
|
-
|
|
97
|
-
expect(contract['flip']).toBeCalled();
|
|
98
|
-
expect(contract['_instance'].getTransaction).toBeCalledWith(778782, 'fhkjsdhfkjsdf');
|
|
99
|
-
expect(contract['_instance'].transferHiveTokens).toBeCalledWith('beggars', 'testuser', '0.001', 'HIVE', '[Lost] | Guess: heads | Server Roll: tails | Previous block id: fkjs7878dkfj | BlockID: dfjfsdfsdfs4hfkj88787 | Trx ID: fhkjsdhfkjsdf | Server Seed: j93jgsjghjdhgjfhgkfdhgkj34872394723');
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
test('User sent an unsupported currency, refund them', async () => {
|
|
103
|
-
sut.registerContract('coinflip', contract);
|
|
104
|
-
|
|
105
|
-
contract['_instance'] = sut;
|
|
106
|
-
|
|
107
|
-
jest.spyOn(contract as any, 'flip');
|
|
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: 'coinflip',
|
|
117
|
-
action: 'flip',
|
|
118
|
-
payload: {
|
|
119
|
-
guess: 'heads'
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
sut.processOperation(['transfer', { from: 'testuser', amount: '10.000 HBD', memo }], 778782, 'dfjfsdfsdfs4hfkj88787', 'fkjsdkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
|
|
125
|
-
|
|
126
|
-
await sleep(100);
|
|
127
|
-
|
|
128
|
-
expect(contract['flip']).toBeCalled();
|
|
129
|
-
expect(sut.getTransaction).toBeCalledWith(778782, 'fhkjsdhfkjsdf');
|
|
130
|
-
expect(sut.transferHiveTokens).toBeCalledWith('beggars', 'testuser', '10.000', 'HBD', '[Refund] You sent an invalid currency.');
|
|
131
|
-
});
|
|
132
|
-
});
|
|
1
|
+
import { createCoinflipContract } from '../../src/contracts/coinflip.contract';
|
|
2
|
+
import { sleep } from '@hiveio/dhive/lib/utils';
|
|
3
|
+
import { Streamer } from '../../src/streamer';
|
|
4
|
+
import { createMockAdapter } from '../helpers/mock-adapter';
|
|
5
|
+
|
|
6
|
+
jest.mock('uuid', () => ({
|
|
7
|
+
v4: jest.fn()
|
|
8
|
+
}));
|
|
9
|
+
|
|
10
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
11
|
+
|
|
12
|
+
describe('Coinflip Contract', () => {
|
|
13
|
+
let sut: Streamer;
|
|
14
|
+
let contract: ReturnType<typeof createCoinflipContract>;
|
|
15
|
+
|
|
16
|
+
beforeEach(async () => {
|
|
17
|
+
sut = new Streamer();
|
|
18
|
+
await sut.registerAdapter(createMockAdapter());
|
|
19
|
+
contract = createCoinflipContract();
|
|
20
|
+
|
|
21
|
+
// @ts-ignore
|
|
22
|
+
sut.api = jest.fn();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
afterEach(async () => {
|
|
26
|
+
await sut.stop();
|
|
27
|
+
jest.restoreAllMocks();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('Registers the contract', async () => {
|
|
31
|
+
await sut.registerContract(contract);
|
|
32
|
+
|
|
33
|
+
const findContract = sut['contracts'].find(c => c.name === 'coinflip');
|
|
34
|
+
expect(findContract).not.toBeUndefined();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('User wins a flip', async () => {
|
|
38
|
+
await sut.registerContract(contract);
|
|
39
|
+
|
|
40
|
+
jest.spyOn(sut, 'getTransaction').mockResolvedValue({ test: 123 } as any);
|
|
41
|
+
jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
|
|
42
|
+
jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
|
|
43
|
+
jest.spyOn(sut['client'].database, 'getAccounts').mockResolvedValue([{ balance: '2000.000 HIVE' }] as any);
|
|
44
|
+
|
|
45
|
+
(uuidv4 as jest.Mock).mockReturnValue('j93jgsjghjdhgjfhgkfdhgkj34872394723');
|
|
46
|
+
|
|
47
|
+
const memo = JSON.stringify({
|
|
48
|
+
hive_stream: {
|
|
49
|
+
contract: 'coinflip',
|
|
50
|
+
action: 'flip',
|
|
51
|
+
payload: {
|
|
52
|
+
guess: 'heads',
|
|
53
|
+
seed: 'hj879879g7686876'
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
await sut.processOperation(['transfer', { from: 'testuser', amount: '9.000 HIVE', memo }], 778782, 'dfjfsdfsdfs4hfkj88787', 'fkjs7878dkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
|
|
59
|
+
|
|
60
|
+
await sleep(100);
|
|
61
|
+
|
|
62
|
+
expect(sut.getTransaction).toHaveBeenCalledWith(778782, 'fhkjsdhfkjsdf');
|
|
63
|
+
expect(sut.transferHiveTokens).toHaveBeenCalledWith('beggars', 'testuser', '18.000', 'HIVE', '[Winner] | Guess: heads | Server Roll: heads | Previous block id: fkjs7878dkfj | BlockID: dfjfsdfsdfs4hfkj88787 | Trx ID: fhkjsdhfkjsdf | Server Seed: j93jgsjghjdhgjfhgkfdhgkj34872394723');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('User loses a flip', async () => {
|
|
67
|
+
await sut.registerContract(contract);
|
|
68
|
+
|
|
69
|
+
jest.spyOn(sut, 'getTransaction').mockResolvedValue({ test: 123 } as any);
|
|
70
|
+
jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
|
|
71
|
+
jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
|
|
72
|
+
jest.spyOn(sut['client'].database, 'getAccounts').mockResolvedValue([{ balance: '2000.000 HIVE' }] as any);
|
|
73
|
+
|
|
74
|
+
(uuidv4 as jest.Mock).mockReturnValue('j93jgsjghjdhgjfhgkfdhgkj34872394723');
|
|
75
|
+
|
|
76
|
+
const memo = JSON.stringify({
|
|
77
|
+
hive_stream: {
|
|
78
|
+
contract: 'coinflip',
|
|
79
|
+
action: 'flip',
|
|
80
|
+
payload: {
|
|
81
|
+
guess: 'heads',
|
|
82
|
+
seed: 'tulips'
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
await sut.processOperation(['transfer', { from: 'testuser', amount: '9.000 HIVE', memo }], 778782, 'dfjfsdfsdfs4hfkj88787', 'fkjs7878dkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
|
|
88
|
+
|
|
89
|
+
await sleep(100);
|
|
90
|
+
|
|
91
|
+
expect(sut.getTransaction).toHaveBeenCalledWith(778782, 'fhkjsdhfkjsdf');
|
|
92
|
+
expect(sut.transferHiveTokens).toHaveBeenCalledWith('beggars', 'testuser', '0.001', 'HIVE', '[Lost] | Guess: heads | Server Roll: tails | Previous block id: fkjs7878dkfj | BlockID: dfjfsdfsdfs4hfkj88787 | Trx ID: fhkjsdhfkjsdf | Server Seed: j93jgsjghjdhgjfhgkfdhgkj34872394723');
|
|
93
|
+
});
|
|
94
|
+
});
|
|
@@ -1,160 +1,87 @@
|
|
|
1
|
-
import { sleep } from '@hiveio/dhive/lib/utils';
|
|
2
|
-
import {
|
|
3
|
-
import { Streamer } from '../../src/streamer';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
let
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
jest.spyOn(
|
|
40
|
-
|
|
41
|
-
jest.spyOn(sut, '
|
|
42
|
-
jest.spyOn(sut, '
|
|
43
|
-
|
|
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
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
});
|
|
160
|
-
});
|
|
1
|
+
import { sleep } from '@hiveio/dhive/lib/utils';
|
|
2
|
+
import { createDiceContract } from './../../src/contracts/dice.contract';
|
|
3
|
+
import { Streamer } from '../../src/streamer';
|
|
4
|
+
import { createMockAdapter } from '../helpers/mock-adapter';
|
|
5
|
+
|
|
6
|
+
describe('Dice Contract', () => {
|
|
7
|
+
let sut: Streamer;
|
|
8
|
+
let contract: ReturnType<typeof createDiceContract>;
|
|
9
|
+
|
|
10
|
+
beforeEach(async () => {
|
|
11
|
+
sut = new Streamer();
|
|
12
|
+
await sut.registerAdapter(createMockAdapter());
|
|
13
|
+
|
|
14
|
+
contract = createDiceContract({ name: 'testdice' });
|
|
15
|
+
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
sut.api = jest.fn();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterEach(async () => {
|
|
21
|
+
await sut.stop();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
afterAll(() => {
|
|
25
|
+
jest.restoreAllMocks();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('Registers the dice contract', async () => {
|
|
29
|
+
await sut.registerContract(contract);
|
|
30
|
+
|
|
31
|
+
const findContract = sut['contracts'].find(c => c.name === 'testdice');
|
|
32
|
+
|
|
33
|
+
expect(findContract).not.toBeUndefined();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('User wins a roll', async () => {
|
|
37
|
+
await sut.registerContract(contract);
|
|
38
|
+
|
|
39
|
+
jest.spyOn(sut, 'getTransaction').mockResolvedValue({ test: 123 } as any);
|
|
40
|
+
jest.spyOn(sut, 'verifyTransfer').mockResolvedValue(true as any);
|
|
41
|
+
jest.spyOn(sut, 'transferHiveTokens').mockResolvedValue(true as any);
|
|
42
|
+
jest.spyOn(sut['client'].database, 'getAccounts').mockResolvedValue([{ balance: '2000.000 HIVE' }] as any);
|
|
43
|
+
|
|
44
|
+
const memo = JSON.stringify({
|
|
45
|
+
hive_stream: {
|
|
46
|
+
contract: 'testdice',
|
|
47
|
+
action: 'roll',
|
|
48
|
+
payload: {
|
|
49
|
+
roll: 69
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
await sut.processOperation(['transfer', { from: 'testuser', amount: '9.000 HIVE', memo }], 778782, 'dfjfsdfsdfsd34hfkj88787', 'fkjsdkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
|
|
55
|
+
|
|
56
|
+
await sleep(100);
|
|
57
|
+
|
|
58
|
+
expect(sut.getTransaction).toHaveBeenCalledWith(778782, 'fhkjsdhfkjsdf');
|
|
59
|
+
expect(sut.transferHiveTokens).toHaveBeenCalledWith('beggars', 'testuser', '12.391', 'HIVE', 'You won 12.391 HIVE. Roll: 54, Your guess: 69');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('User loses a roll', async () => {
|
|
63
|
+
await sut.registerContract(contract);
|
|
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['client'].database, 'getAccounts').mockResolvedValue([{ balance: '2000.000 HIVE' }] as any);
|
|
69
|
+
|
|
70
|
+
const memo = JSON.stringify({
|
|
71
|
+
hive_stream: {
|
|
72
|
+
contract: 'testdice',
|
|
73
|
+
action: 'roll',
|
|
74
|
+
payload: {
|
|
75
|
+
roll: 10
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
await sut.processOperation(['transfer', { from: 'testuser', amount: '9.000 HIVE', memo }], 778782, 'dfjfsdfsdfsd34hfkj88787', 'fkjsdkfj', 'fhkjsdhfkjsdf', '2019-06-23' as any);
|
|
81
|
+
|
|
82
|
+
await sleep(100);
|
|
83
|
+
|
|
84
|
+
expect(sut.getTransaction).toHaveBeenCalledWith(778782, 'fhkjsdhfkjsdf');
|
|
85
|
+
expect(sut.transferHiveTokens).toHaveBeenCalledWith('beggars', 'testuser', '0.001', 'HIVE', 'You lost 9.000 HIVE. Roll: 54, Your guess: 10');
|
|
86
|
+
});
|
|
87
|
+
});
|