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