hive-stream 2.0.3 → 2.0.6
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/.env.example +3 -2
- package/.travis.yml +11 -11
- package/LICENSE +21 -21
- package/README.md +238 -236
- package/dist/actions.d.ts +10 -9
- package/dist/actions.js +23 -15
- package/dist/actions.js.map +1 -1
- package/dist/adapters/base.adapter.d.ts +25 -21
- package/dist/adapters/base.adapter.js +49 -63
- package/dist/adapters/base.adapter.js.map +1 -1
- package/dist/adapters/mongodb.adapter.d.ts +37 -29
- package/dist/adapters/mongodb.adapter.js +158 -113
- package/dist/adapters/mongodb.adapter.js.map +1 -1
- package/dist/adapters/sqlite.adapter.d.ts +41 -23
- package/dist/adapters/sqlite.adapter.js +397 -121
- package/dist/adapters/sqlite.adapter.js.map +1 -1
- package/dist/api.d.ts +6 -0
- package/dist/api.js +56 -0
- package/dist/api.js.map +1 -0
- package/dist/config.d.ts +16 -14
- package/dist/config.js +18 -15
- package/dist/config.js.map +1 -1
- package/dist/contracts/coinflip.contract.d.ts +14 -0
- package/dist/contracts/coinflip.contract.js +95 -0
- package/dist/contracts/coinflip.contract.js.map +1 -0
- package/dist/contracts/dice.contract.d.ts +29 -29
- package/dist/contracts/dice.contract.js +155 -157
- package/dist/contracts/dice.contract.js.map +1 -1
- package/dist/contracts/lotto.contract.d.ts +20 -16
- package/dist/contracts/lotto.contract.js +246 -107
- package/dist/contracts/lotto.contract.js.map +1 -1
- package/dist/exchanges/bittrex.d.ts +6 -0
- package/dist/exchanges/bittrex.js +35 -0
- package/dist/exchanges/bittrex.js.map +1 -0
- package/dist/exchanges/exchange.d.ts +9 -0
- package/dist/exchanges/exchange.js +27 -0
- package/dist/exchanges/exchange.js.map +1 -0
- package/dist/hive-rates.d.ts +9 -0
- package/dist/hive-rates.js +76 -0
- package/dist/hive-rates.js.map +1 -0
- package/dist/index.d.ts +11 -10
- package/dist/index.js +32 -15
- package/dist/index.js.map +1 -1
- package/dist/streamer.d.ts +93 -70
- package/dist/streamer.js +545 -439
- package/dist/streamer.js.map +1 -1
- package/dist/test.d.ts +1 -1
- package/dist/test.js +25 -27
- package/dist/test.js.map +1 -1
- package/dist/types/hive-stream.d.ts +6 -6
- package/dist/types/hive-stream.js +2 -2
- package/dist/utils.d.ts +27 -14
- package/dist/utils.js +261 -85
- package/dist/utils.js.map +1 -1
- package/ecosystem.config.js +17 -17
- package/jest.config.js +8 -13
- package/package.json +48 -44
- package/test-contract-block.md +18 -18
- package/tests/adapters/sqlite.adapter.spec.ts +43 -0
- package/tests/contracts/coinflip.contract.spec.ts +132 -0
- package/tests/contracts/dice.contract.spec.ts +159 -156
- package/tests/contracts/entrants.json +728 -728
- package/tests/contracts/lotto.contract.spec.ts +323 -372
- package/tests/setup.ts +18 -20
- package/tests/streamer.spec.ts +151 -151
- package/tests/utils.spec.ts +94 -99
- package/tsconfig.build.json +22 -20
- package/tslint.json +20 -20
- package/wallaby.js +26 -0
- package/.env +0 -1
- package/dist/adapters/file.adapter.d.ts +0 -8
- package/dist/adapters/file.adapter.js +0 -70
- package/dist/adapters/file.adapter.js.map +0 -1
- package/dist/test/setup.d.ts +0 -0
- package/dist/test/setup.js +0 -9
- package/dist/test/setup.js.map +0 -1
- package/dist/test/streamer.spec.d.ts +0 -1
- package/dist/test/streamer.spec.js +0 -145
- package/dist/test/streamer.spec.js.map +0 -1
- package/dist/test/utils.spec.d.ts +0 -1
- package/dist/test/utils.spec.js +0 -11
- package/dist/test/utils.spec.js.map +0 -1
- package/dist/tests/contracts/dice.contract.spec.d.ts +0 -1
- package/dist/tests/contracts/dice.contract.spec.js +0 -130
- package/dist/tests/contracts/dice.contract.spec.js.map +0 -1
- package/dist/tests/contracts/entrants.json +0 -729
- package/dist/tests/contracts/lotto.contract.spec.d.ts +0 -1
- package/dist/tests/contracts/lotto.contract.spec.js +0 -300
- package/dist/tests/contracts/lotto.contract.spec.js.map +0 -1
- package/dist/tests/setup.d.ts +0 -1
- package/dist/tests/setup.js +0 -19
- package/dist/tests/setup.js.map +0 -1
- package/dist/tests/streamer.spec.d.ts +0 -1
- package/dist/tests/streamer.spec.js +0 -123
- package/dist/tests/streamer.spec.js.map +0 -1
- package/dist/tests/utils.spec.d.ts +0 -1
- package/dist/tests/utils.spec.js +0 -87
- package/dist/tests/utils.spec.js.map +0 -1
- package/hive-stream.json +0 -1
package/.env.example
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
ACTIVE_KEY=
|
|
2
|
-
POSTING_KEY=
|
|
1
|
+
ACTIVE_KEY=
|
|
2
|
+
POSTING_KEY=
|
|
3
|
+
EXCHANGE_RATE_API_KEY=
|
package/.travis.yml
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
language: node_js
|
|
2
|
-
node_js:
|
|
3
|
-
- "10"
|
|
4
|
-
deploy:
|
|
5
|
-
provider: npm
|
|
6
|
-
email: dwaynecharrington@gmail.com
|
|
7
|
-
api_key:
|
|
8
|
-
secure: fOmYW0vSSkrJF0CFnv7P4yzdQYfsgnBJBM0qiV31a04AqjOrtf0J0YxMtLSSJMDnj6BiaJIuxrBqgT+1KBmavyXnoQfV7KrlmeACBwSn9bneiZZDz0OT7Lk+JN6cLZ7Qo4B+4FToFfx1CHkZ1AaAZJpXV8kPyVpQ1wK+Lk+m1wTZPecWdZpDyjmr+/DIlBebe6Nv6ldWl1q/3gNTmK+ha6syy5Lhugp1WaShd2EoskZxyd9sV0XADE8xVe2mVAP/nNzCM2P6wPRMB3uP36IHnWdi7FbkNVN1ZbLEMp6XkEn0lnFbrnT9P48PWREclTGstNwfsLF0AirKLVD0Qt8m9xshyx2wWl6Vjljc343SblwxgIytqnqob+pZtVrZ8Ir0DDDAk8VN8zpQX8w2L7wMywYsQcCzRU/eoo1eoZS3IMxo6zzLu5usO7FSxqUalYaJxn4gvoGKYr6/IlYJW5yWMDVdqbom41qTUnmD/dGWdBiicMn7CihE8a0BCMQzByXHFT5frtyMFKW4/YckuL1NYjd56e2YqY9f1HKpTZ1yYAtSI5VwzU744PmcKDu1+RHftM7uX13tYywhhKhBS7APDU2bN9iRoQkcxMeI38UhaJU8wRcv1C71NiinWe/JQbUkKEGmhCYfyhh9dWv9el7Eijeg2SMJdsJcogettjRNKlk=
|
|
9
|
-
on:
|
|
10
|
-
tags: true
|
|
11
|
-
repo: Vheissu/hive-stream
|
|
1
|
+
language: node_js
|
|
2
|
+
node_js:
|
|
3
|
+
- "10"
|
|
4
|
+
deploy:
|
|
5
|
+
provider: npm
|
|
6
|
+
email: dwaynecharrington@gmail.com
|
|
7
|
+
api_key:
|
|
8
|
+
secure: fOmYW0vSSkrJF0CFnv7P4yzdQYfsgnBJBM0qiV31a04AqjOrtf0J0YxMtLSSJMDnj6BiaJIuxrBqgT+1KBmavyXnoQfV7KrlmeACBwSn9bneiZZDz0OT7Lk+JN6cLZ7Qo4B+4FToFfx1CHkZ1AaAZJpXV8kPyVpQ1wK+Lk+m1wTZPecWdZpDyjmr+/DIlBebe6Nv6ldWl1q/3gNTmK+ha6syy5Lhugp1WaShd2EoskZxyd9sV0XADE8xVe2mVAP/nNzCM2P6wPRMB3uP36IHnWdi7FbkNVN1ZbLEMp6XkEn0lnFbrnT9P48PWREclTGstNwfsLF0AirKLVD0Qt8m9xshyx2wWl6Vjljc343SblwxgIytqnqob+pZtVrZ8Ir0DDDAk8VN8zpQX8w2L7wMywYsQcCzRU/eoo1eoZS3IMxo6zzLu5usO7FSxqUalYaJxn4gvoGKYr6/IlYJW5yWMDVdqbom41qTUnmD/dGWdBiicMn7CihE8a0BCMQzByXHFT5frtyMFKW4/YckuL1NYjd56e2YqY9f1HKpTZ1yYAtSI5VwzU744PmcKDu1+RHftM7uX13tYywhhKhBS7APDU2bN9iRoQkcxMeI38UhaJU8wRcv1C71NiinWe/JQbUkKEGmhCYfyhh9dWv9el7Eijeg2SMJdsJcogettjRNKlk=
|
|
9
|
+
on:
|
|
10
|
+
tags: true
|
|
11
|
+
repo: Vheissu/hive-stream
|
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2019 Dwayne Charrington
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Dwayne Charrington
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,236 +1,238 @@
|
|
|
1
|
-
# Hive Stream
|
|
2
|
-
|
|
3
|
-
A Node.js layer for Hive that allows you to watch for specific actions on the Hive blockchain.
|
|
4
|
-
|
|
5
|
-
## Install
|
|
6
|
-
|
|
7
|
-
```shell
|
|
8
|
-
npm install hive-stream
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Quick Usage
|
|
12
|
-
|
|
13
|
-
```javascript
|
|
14
|
-
const { Streamer } = require('hive-stream');
|
|
15
|
-
|
|
16
|
-
const ss = new Streamer();
|
|
17
|
-
|
|
18
|
-
// Watch for all custom JSON operations
|
|
19
|
-
ss.onCustomJson((op, { sender, isSignedWithActiveKey }, blockNumber, blockId, prevBlockId, trxId, blockTime) => {
|
|
20
|
-
// React to custom JSON operations
|
|
21
|
-
});
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
## Configuration
|
|
25
|
-
|
|
26
|
-
The `Streamer` object can accept an object of configuration values which are all optional. However, some operations like
|
|
27
|
-
|
|
28
|
-
The `BLOCK_CHECK_INTERVAL` value is how often to check for new blocks or in cases of error or falling behind, to poll for new blocks.
|
|
29
|
-
|
|
30
|
-
The `BLOCKS_BEHIND_WARNING` value is a numeric value of the number of blocks your API will fall behind from the master before warning to the console.
|
|
31
|
-
|
|
32
|
-
The `API_URL` is the Hive API. If you want to enable debug mode, set to `DEBUG_MODE` to `true`. The configuration values and their defaults can be found [here](https://github.com/Vheissu/hive-stream/blob/master/config.js).
|
|
33
|
-
|
|
34
|
-
```
|
|
35
|
-
const options = {
|
|
36
|
-
ACTIVE_KEY: '',
|
|
37
|
-
POSTING_KEY: '',
|
|
38
|
-
APP_NAME: 'hive-stream',
|
|
39
|
-
USERNAME: '',
|
|
40
|
-
LAST_BLOCK_NUMBER: 0,
|
|
41
|
-
BLOCK_CHECK_INTERVAL: 1000,
|
|
42
|
-
BLOCKS_BEHIND_WARNING: 25,
|
|
43
|
-
API_URL: 'https://api.hiveit.com',
|
|
44
|
-
DEBUG_MODE: false
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const ss = new Streamer(options);
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
The configuration itself can also be overloaded using the `setConfig` method which allows you to pass one or more of the above configuration options, useful in situations where multiple keys might be used for issuing.
|
|
51
|
-
|
|
52
|
-
```
|
|
53
|
-
ss.setConfig({
|
|
54
|
-
ACTIVE_KEY: 'newactivekey',
|
|
55
|
-
USERNAME: 'newusername'
|
|
56
|
-
});
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
## Streamer
|
|
60
|
-
|
|
61
|
-
The following subscription methods are read only methods, they allow you to react to certain Hive and Hive Engine events on the blockchain. You do not need to pass in any keys to use these methods as they're purely read only.
|
|
62
|
-
|
|
63
|
-
**The following actions DO require calling the `start` method first to watch the blockchain**
|
|
64
|
-
|
|
65
|
-
#### Watch for transfers
|
|
66
|
-
|
|
67
|
-
```javascript
|
|
68
|
-
ss.onTransfer((op, blockNumber, blockId, prevBlockId, trxId, blockTime) => {
|
|
69
|
-
|
|
70
|
-
})
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
#### Watch for custom JSON operations
|
|
74
|
-
```javascript
|
|
75
|
-
ss.onCustomJson((op, { sender, isSignedWithActiveKey }, blockNumber, blockId, prevBlockId, trxId, blockTime) => {
|
|
76
|
-
|
|
77
|
-
})
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
#### Watch for custom JSON operations (with a specific ID)
|
|
81
|
-
```javascript
|
|
82
|
-
ss.onCustomJsonId((op, { sender, isSignedWithActiveKey }, blockNumber, blockId, prevBlockId, trxId, blockTime) => {
|
|
83
|
-
|
|
84
|
-
})
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
#### Watch for post operations
|
|
88
|
-
```javascript
|
|
89
|
-
ss.onPost((op, blockNumber, blockId, prevBlockId, trxId, blockTime) => {
|
|
90
|
-
|
|
91
|
-
});
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
#### Watch for comment operations
|
|
95
|
-
```javascript
|
|
96
|
-
ss.onComment((op, blockNumber, blockId, prevBlockId, trxId, blockTime) => {
|
|
97
|
-
|
|
98
|
-
});
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
## Actions (active key)
|
|
102
|
-
|
|
103
|
-
All of the below methods require an active key has been supplied in the constructor above called `ACTIVE_KEY`. The methods below are all promised based, so you can `await` them or use `then` to confirm a successful result.
|
|
104
|
-
|
|
105
|
-
**The following actions do NOT require calling the `start` method first to watch the blockchain**
|
|
106
|
-
|
|
107
|
-
```javascript
|
|
108
|
-
const ss = new Streamer({
|
|
109
|
-
ACTIVE_KEY: 'youractivekey'
|
|
110
|
-
});
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
### Transfer Hive (HIVE or HBD)
|
|
114
|
-
```javascript
|
|
115
|
-
transferHiveTokens(from, to, amount, symbol, memo = '') {
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
## Contracts
|
|
121
|
-
|
|
122
|
-
Hive Stream allows you to write contracts which get executed when a custom JSON operation matches. The only requirement is sending a payload which contains `hiveContract` inside of it.
|
|
123
|
-
|
|
124
|
-
The payload consists of:
|
|
125
|
-
|
|
126
|
-
`name` the name of the smart contract you registered.
|
|
127
|
-
|
|
128
|
-
`action` matches the name of a function defined inside of your contract
|
|
129
|
-
|
|
130
|
-
`payload` an object of data which will be provided to the action
|
|
131
|
-
|
|
132
|
-
### Writing contracts
|
|
133
|
-
|
|
134
|
-
Really, a contract is nothing more than a bunch of functions which get matched to values inside of JSON payloads.
|
|
135
|
-
|
|
136
|
-
### Register a contract
|
|
137
|
-
|
|
138
|
-
Register a file containing contract code which will be executed.
|
|
139
|
-
|
|
140
|
-
```javascript
|
|
141
|
-
import contract from './my-contract';
|
|
142
|
-
|
|
143
|
-
registerContract('mycontract', Contract);
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
### Unregister a contract
|
|
147
|
-
|
|
148
|
-
Unregister a contract that has been registered.
|
|
149
|
-
|
|
150
|
-
```javascript
|
|
151
|
-
unregisterContract('mycontract');
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
### Example Payload
|
|
155
|
-
|
|
156
|
-
```javascript
|
|
157
|
-
JSON.stringify({ hiveContract: { name: 'hivedice', action: 'roll', payload: { roll: 22, amount: '1'} } })
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
This will match a registered contract called `hivedice` and inside of the contract code, a function called `roll` and finally, the payload is sent to the function as an argument, allowing you to access the values inside of it. See the example file `dice.contract.ts` in the `src/contracts` folder in the repository.
|
|
161
|
-
|
|
162
|
-
## Time-based Actions
|
|
163
|
-
|
|
164
|
-
It's like a cron job for your contracts. Time-based actions allow you to execute contract functions over a wide variety of different
|
|
165
|
-
|
|
166
|
-
The following example will run a contract action every 30 seconds. All you do is register a new `TimeAction` instance.
|
|
167
|
-
|
|
168
|
-
```
|
|
169
|
-
import { TimeAction, Streamer } from 'hive-stream';
|
|
170
|
-
|
|
171
|
-
const streamer = new Streamer({
|
|
172
|
-
ACTIVE_KEY: ''
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
const testAction = new TimeAction('30s', 'test30s', 'hivedice', 'testauto');
|
|
176
|
-
|
|
177
|
-
streamer.registerAction(testAction);
|
|
178
|
-
|
|
179
|
-
streamer.start();
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
The `TimeAction` instance accepts the following values:
|
|
183
|
-
|
|
184
|
-
- timeValue - When should this action be run?
|
|
185
|
-
- uniqueId - A unique ID to describe your action
|
|
186
|
-
- contractName - The name of the contract
|
|
187
|
-
- contractMethod - The method we are calling inside of the contract
|
|
188
|
-
- date - An optional final parameter that accepts a date of creation
|
|
189
|
-
|
|
190
|
-
```
|
|
191
|
-
new TimeAction(timeValue, uniqueId, contractName, contractMethod, date)
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
### Valid time values
|
|
195
|
-
|
|
196
|
-
At the moment, the `timeValue` passed in as the first argument to `TimeAction` cannot accept just any value. However, there are many available out-of-the-box with more flexibility to come in the future.
|
|
197
|
-
|
|
198
|
-
- `3s` or `block` will run a task every block (3 seconds, approximately)
|
|
199
|
-
- `30s` will run a task every 30 seconds
|
|
200
|
-
- `1m` or `minute` will run a task every 60 seconds (1 minute)
|
|
201
|
-
- `15m` or `quarter` will run a task every 15 minutes
|
|
202
|
-
- `30m` or `halfhour` will run a task every 30 minutes
|
|
203
|
-
- `1h` or `hourly` will run a task every 60 minutes (every hour)
|
|
204
|
-
- `12h` or `halfday` will run a task every 12 hours (half a day)
|
|
205
|
-
- `24h` or `day` will run a task every 24 hours (day)
|
|
206
|
-
|
|
207
|
-
Values will be persisted if using one of the three adapters that ship with the library.
|
|
208
|
-
|
|
209
|
-
## Adapters
|
|
210
|
-
|
|
211
|
-
The Hive Stream library supports custom adapters for various actions that take place in the library. When the library first loads, it makes a call to get the last block number or when a block is processed, storing the processed block number. This library ships with two adapters; File and SQLite, both of which are file based adapters. The SQLite database works more like a traditional database and shows how you might create an adapter for a database like MongoDB or MySQL.
|
|
212
|
-
|
|
213
|
-
When creating an adapter, at a minimum your adapter requires two methods: `loadState` and `saveState`. It must also extend `AdapterBase` which is exported from the package.
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
1
|
+
# Hive Stream
|
|
2
|
+
|
|
3
|
+
A Node.js layer for Hive that allows you to watch for specific actions on the Hive blockchain.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```shell
|
|
8
|
+
npm install hive-stream
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Usage
|
|
12
|
+
|
|
13
|
+
```javascript
|
|
14
|
+
const { Streamer } = require('hive-stream');
|
|
15
|
+
|
|
16
|
+
const ss = new Streamer();
|
|
17
|
+
|
|
18
|
+
// Watch for all custom JSON operations
|
|
19
|
+
ss.onCustomJson((op, { sender, isSignedWithActiveKey }, blockNumber, blockId, prevBlockId, trxId, blockTime) => {
|
|
20
|
+
// React to custom JSON operations
|
|
21
|
+
});
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Configuration
|
|
25
|
+
|
|
26
|
+
The `Streamer` object can accept an object of configuration values which are all optional. However, some operations like transferring Hive Engine tokens or other operations on the blockchain that are not READ ONLY, will require the active key and/or posting keys supplied as well as a username.
|
|
27
|
+
|
|
28
|
+
The `BLOCK_CHECK_INTERVAL` value is how often to check for new blocks or in cases of error or falling behind, to poll for new blocks. You should keep this as the default 1000ms value which is one second. This allows you to account for situations where blocks fall behind the main block.
|
|
29
|
+
|
|
30
|
+
The `BLOCKS_BEHIND_WARNING` value is a numeric value of the number of blocks your API will fall behind from the master before warning to the console.
|
|
31
|
+
|
|
32
|
+
The `API_URL` is the Hive API. If you want to enable debug mode, set to `DEBUG_MODE` to `true`. The configuration values and their defaults can be found [here](https://github.com/Vheissu/hive-stream/blob/master/config.js).
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
const options = {
|
|
36
|
+
ACTIVE_KEY: '',
|
|
37
|
+
POSTING_KEY: '',
|
|
38
|
+
APP_NAME: 'hive-stream',
|
|
39
|
+
USERNAME: '',
|
|
40
|
+
LAST_BLOCK_NUMBER: 0,
|
|
41
|
+
BLOCK_CHECK_INTERVAL: 1000,
|
|
42
|
+
BLOCKS_BEHIND_WARNING: 25,
|
|
43
|
+
API_URL: 'https://api.hiveit.com',
|
|
44
|
+
DEBUG_MODE: false
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const ss = new Streamer(options);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The configuration itself can also be overloaded using the `setConfig` method which allows you to pass one or more of the above configuration options, useful in situations where multiple keys might be used for issuing.
|
|
51
|
+
|
|
52
|
+
```
|
|
53
|
+
ss.setConfig({
|
|
54
|
+
ACTIVE_KEY: 'newactivekey',
|
|
55
|
+
USERNAME: 'newusername'
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Streamer
|
|
60
|
+
|
|
61
|
+
The following subscription methods are read only methods, they allow you to react to certain Hive and Hive Engine events on the blockchain. You do not need to pass in any keys to use these methods as they're purely read only.
|
|
62
|
+
|
|
63
|
+
**The following actions DO require calling the `start` method first to watch the blockchain**
|
|
64
|
+
|
|
65
|
+
#### Watch for transfers
|
|
66
|
+
|
|
67
|
+
```javascript
|
|
68
|
+
ss.onTransfer((op, blockNumber, blockId, prevBlockId, trxId, blockTime) => {
|
|
69
|
+
|
|
70
|
+
})
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
#### Watch for custom JSON operations
|
|
74
|
+
```javascript
|
|
75
|
+
ss.onCustomJson((op, { sender, isSignedWithActiveKey }, blockNumber, blockId, prevBlockId, trxId, blockTime) => {
|
|
76
|
+
|
|
77
|
+
})
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
#### Watch for custom JSON operations (with a specific ID)
|
|
81
|
+
```javascript
|
|
82
|
+
ss.onCustomJsonId((op, { sender, isSignedWithActiveKey }, blockNumber, blockId, prevBlockId, trxId, blockTime) => {
|
|
83
|
+
|
|
84
|
+
})
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
#### Watch for post operations
|
|
88
|
+
```javascript
|
|
89
|
+
ss.onPost((op, blockNumber, blockId, prevBlockId, trxId, blockTime) => {
|
|
90
|
+
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
#### Watch for comment operations
|
|
95
|
+
```javascript
|
|
96
|
+
ss.onComment((op, blockNumber, blockId, prevBlockId, trxId, blockTime) => {
|
|
97
|
+
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Actions (active key)
|
|
102
|
+
|
|
103
|
+
All of the below methods require an active key has been supplied in the constructor above called `ACTIVE_KEY`. The methods below are all promised based, so you can `await` them or use `then` to confirm a successful result.
|
|
104
|
+
|
|
105
|
+
**The following actions do NOT require calling the `start` method first to watch the blockchain**
|
|
106
|
+
|
|
107
|
+
```javascript
|
|
108
|
+
const ss = new Streamer({
|
|
109
|
+
ACTIVE_KEY: 'youractivekey'
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Transfer Hive (HIVE or HBD)
|
|
114
|
+
```javascript
|
|
115
|
+
transferHiveTokens(from, to, amount, symbol, memo = '') {
|
|
116
|
+
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Contracts
|
|
121
|
+
|
|
122
|
+
Hive Stream allows you to write contracts which get executed when a custom JSON operation matches. The only requirement is sending a payload which contains `hiveContract` inside of it.
|
|
123
|
+
|
|
124
|
+
The payload consists of:
|
|
125
|
+
|
|
126
|
+
`name` the name of the smart contract you registered.
|
|
127
|
+
|
|
128
|
+
`action` matches the name of a function defined inside of your contract
|
|
129
|
+
|
|
130
|
+
`payload` an object of data which will be provided to the action
|
|
131
|
+
|
|
132
|
+
### Writing contracts
|
|
133
|
+
|
|
134
|
+
Really, a contract is nothing more than a bunch of functions which get matched to values inside of JSON payloads.
|
|
135
|
+
|
|
136
|
+
### Register a contract
|
|
137
|
+
|
|
138
|
+
Register a file containing contract code which will be executed.
|
|
139
|
+
|
|
140
|
+
```javascript
|
|
141
|
+
import contract from './my-contract';
|
|
142
|
+
|
|
143
|
+
registerContract('mycontract', Contract);
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Unregister a contract
|
|
147
|
+
|
|
148
|
+
Unregister a contract that has been registered.
|
|
149
|
+
|
|
150
|
+
```javascript
|
|
151
|
+
unregisterContract('mycontract');
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Example Payload
|
|
155
|
+
|
|
156
|
+
```javascript
|
|
157
|
+
JSON.stringify({ hiveContract: { name: 'hivedice', action: 'roll', payload: { roll: 22, amount: '1'} } })
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
This will match a registered contract called `hivedice` and inside of the contract code, a function called `roll` and finally, the payload is sent to the function as an argument, allowing you to access the values inside of it. See the example file `dice.contract.ts` in the `src/contracts` folder in the repository. there is also a coinflip and lotto contract showing you how to build a coinflip or lottery based contract.
|
|
161
|
+
|
|
162
|
+
## Time-based Actions
|
|
163
|
+
|
|
164
|
+
It's like a cron job for your contracts. Time-based actions allow you to execute contract functions over a wide variety of different periods. Want to call a function every 3 seconds block time or want to call a function once per day? Time-based actions are an easy way to run time code.
|
|
165
|
+
|
|
166
|
+
The following example will run a contract action every 30 seconds. All you do is register a new `TimeAction` instance.
|
|
167
|
+
|
|
168
|
+
```
|
|
169
|
+
import { TimeAction, Streamer } from 'hive-stream';
|
|
170
|
+
|
|
171
|
+
const streamer = new Streamer({
|
|
172
|
+
ACTIVE_KEY: ''
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
const testAction = new TimeAction('30s', 'test30s', 'hivedice', 'testauto');
|
|
176
|
+
|
|
177
|
+
streamer.registerAction(testAction);
|
|
178
|
+
|
|
179
|
+
streamer.start();
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
The `TimeAction` instance accepts the following values:
|
|
183
|
+
|
|
184
|
+
- timeValue - When should this action be run?
|
|
185
|
+
- uniqueId - A unique ID to describe your action
|
|
186
|
+
- contractName - The name of the contract
|
|
187
|
+
- contractMethod - The method we are calling inside of the contract
|
|
188
|
+
- date - An optional final parameter that accepts a date of creation
|
|
189
|
+
|
|
190
|
+
```
|
|
191
|
+
new TimeAction(timeValue, uniqueId, contractName, contractMethod, date)
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Valid time values
|
|
195
|
+
|
|
196
|
+
At the moment, the `timeValue` passed in as the first argument to `TimeAction` cannot accept just any value. However, there are many available out-of-the-box with more flexibility to come in the future.
|
|
197
|
+
|
|
198
|
+
- `3s` or `block` will run a task every block (3 seconds, approximately)
|
|
199
|
+
- `30s` will run a task every 30 seconds
|
|
200
|
+
- `1m` or `minute` will run a task every 60 seconds (1 minute)
|
|
201
|
+
- `15m` or `quarter` will run a task every 15 minutes
|
|
202
|
+
- `30m` or `halfhour` will run a task every 30 minutes
|
|
203
|
+
- `1h` or `hourly` will run a task every 60 minutes (every hour)
|
|
204
|
+
- `12h` or `halfday` will run a task every 12 hours (half a day)
|
|
205
|
+
- `24h` or `day` will run a task every 24 hours (day)
|
|
206
|
+
|
|
207
|
+
Values will be persisted if using one of the three adapters that ship with the library.
|
|
208
|
+
|
|
209
|
+
## Adapters
|
|
210
|
+
|
|
211
|
+
The Hive Stream library supports custom adapters for various actions that take place in the library. When the library first loads, it makes a call to get the last block number or when a block is processed, storing the processed block number. This library ships with two adapters; File and SQLite, both of which are file based adapters. The SQLite database works more like a traditional database and shows how you might create an adapter for a database like MongoDB or MySQL.
|
|
212
|
+
|
|
213
|
+
When creating an adapter, at a minimum your adapter requires two methods: `loadState` and `saveState`. It must also extend `AdapterBase` which is exported from the package.
|
|
214
|
+
|
|
215
|
+
You can see a few adapters that ship with Hive Stream in the `src/adapters` directory.
|
|
216
|
+
|
|
217
|
+
## Permanently running with PM2
|
|
218
|
+
|
|
219
|
+
Simply copy the `ecosystem.config.js` file from this repository into your application, globally install `pm2` via `npm install pm2 -g` and change the `script` value below to reflect the main file of your application.
|
|
220
|
+
|
|
221
|
+
**ecosystem.config.js**
|
|
222
|
+
|
|
223
|
+
```
|
|
224
|
+
module.exports = {
|
|
225
|
+
apps: [
|
|
226
|
+
{
|
|
227
|
+
name: 'hive-stream',
|
|
228
|
+
script: 'index.js',
|
|
229
|
+
ignore_watch: ['node_modules'],
|
|
230
|
+
env: {
|
|
231
|
+
NODE_ENV: 'development'
|
|
232
|
+
},
|
|
233
|
+
env_production: {
|
|
234
|
+
NODE_ENV: 'production'
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
]
|
|
238
|
+
```
|
package/dist/actions.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
export declare class TimeAction {
|
|
2
|
-
timeValue: string;
|
|
3
|
-
id: string;
|
|
4
|
-
contractName: string;
|
|
5
|
-
contractMethod: string;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
export declare class TimeAction {
|
|
2
|
+
timeValue: string;
|
|
3
|
+
id: string;
|
|
4
|
+
contractName: string;
|
|
5
|
+
contractMethod: string;
|
|
6
|
+
payload: any;
|
|
7
|
+
date: Date;
|
|
8
|
+
constructor(timeValue: string, id: string, contractName: string, contractMethod: string, payload?: any, date?: Date);
|
|
9
|
+
reset(): void;
|
|
10
|
+
}
|
package/dist/actions.js
CHANGED
|
@@ -1,16 +1,24 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
this.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TimeAction = void 0;
|
|
4
|
+
class TimeAction {
|
|
5
|
+
timeValue;
|
|
6
|
+
id;
|
|
7
|
+
contractName;
|
|
8
|
+
contractMethod;
|
|
9
|
+
payload;
|
|
10
|
+
date;
|
|
11
|
+
constructor(timeValue, id, contractName, contractMethod, payload = {}, date = new Date()) {
|
|
12
|
+
this.timeValue = timeValue;
|
|
13
|
+
this.id = id;
|
|
14
|
+
this.contractName = contractName;
|
|
15
|
+
this.contractMethod = contractMethod;
|
|
16
|
+
this.payload = payload;
|
|
17
|
+
this.date = date;
|
|
18
|
+
}
|
|
19
|
+
reset() {
|
|
20
|
+
this.date = new Date();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.TimeAction = TimeAction;
|
|
16
24
|
//# sourceMappingURL=actions.js.map
|
package/dist/actions.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"actions.js","sourceRoot":"","sources":["../src/actions.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"actions.js","sourceRoot":"","sources":["../src/actions.ts"],"names":[],"mappings":";;;AAAA,MAAa,UAAU;IACA;IAA0B;IAAmB;IAA6B;IAA+B;IAA0B;IAAtJ,YAAmB,SAAiB,EAAS,EAAU,EAAS,YAAoB,EAAS,cAAsB,EAAS,UAAe,EAAE,EAAS,OAAO,IAAI,IAAI,EAAE;QAApJ,cAAS,GAAT,SAAS,CAAQ;QAAS,OAAE,GAAF,EAAE,CAAQ;QAAS,iBAAY,GAAZ,YAAY,CAAQ;QAAS,mBAAc,GAAd,cAAc,CAAQ;QAAS,YAAO,GAAP,OAAO,CAAU;QAAS,SAAI,GAAJ,IAAI,CAAa;IAEvK,CAAC;IAED,KAAK;QACD,IAAI,CAAC,IAAI,GAAG,IAAI,IAAI,EAAE,CAAC;IAC3B,CAAC;CACJ;AARD,gCAQC"}
|