@signumjs/monitor
Version:
Monitor transactions on Signum Network blockchain
102 lines (75 loc) • 2.57 kB
Markdown
A monitor to watch for specific changes on the Signum blockchain platform
Due to average blocktime of 240 seconds, transactions stay pending for a certain time. It is a repeating pattern
to watch for such changes and waiting for confirmation. This package simplifies this task.
`@signumjs/monitor` can be used with NodeJS or Web. Two formats are available
Install using [npm](https://www.npmjs.org/):
```
npm install @signumjs/monitor
```
or using [yarn](https://yarnpkg.com/):
``` yarn
yarn add @signumjs/monitor
```
// TODO:
```js
import {Monitor} from '@signumjs/monitor'
import {composeApi} from "@signumjs/core";
// A method that checks if an account exists
// > IMPORTANT: Do not use closures, when you need to serialize the monitor
async function tryFetchAccount() {
const api = composeApi({ nodeHost: 'https://testnet.signum.network:6876/'})
try{
const {account} = await api.account.getAccount('1234')
return account;
}catch (e){
// ignore error
return null;
}
}
// A comparing function to check if a certain condition for the returned data from fetch function
// is true. If it's true the monitor stops
function checkIfAccountExists(account) {
return account !== null;
}
// Create your monitor
const monitor = new Monitor({
asyncFetcherFn: tryFetchAccount,
compareFn: checkIfAccountExists,
intervalSecs: 10, // polling interval in seconds
key: 'monitor-account',
timeoutSecs: 2 * 240 // when reached timeout the monitor stops
});
// starts monitor
monitor.start();
// called when `checkIfAccountExists` returns true
monitor.onFulfilled(() => {
console.log('Yay, account active');
});
// called when `timeoutSecs` is reached
monitor.onTimeout(() => {
console.log('Hmm, something went wrong');
});
```
Each package is available as bundled standalone library using UMD.
This way _signumJS_ can be used also within `<script>`-Tags.
This might be useful for Wordpress and/or other PHP applications.
Just import the package using the HTML `<script>` tag.
`<script src='https://cdn.jsdelivr.net/npm/@signumjs/monitor/dist/signumjs.monitor.min.js'></script>`
```js
const {Monitor} = sig$Monitor;
const monitor = new Monitor({
//...
});
monitor.start()
monitor.onFulFilled(() => {
//...
})
```
See more here:
[@signumjs/monitor Online Documentation](https://signum-network.github.io/signumjs/modules/monitor.html)