npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

monitorchain-interface-library

v2.0.0

Published

NodeJS libraries for interfacing and integrating MonitorChain by subscribers.

Downloads

6

Readme

MonitorChain Interface Library

monitorchain-interface-library

NodeJS libraries for interfacing and integrating MonitorChain by subscribers.

Install

$ npm install monitorchain-interface-library

USAGE

Following modules are being exported by the library:

  • AccessInterface - the interface is providing the public and restricted-to-subscribers methods
  • Web3 - returns the web3 provider. The type of the provider is defined by the protocol type (web socket, http, ipc). If a 12 words mnemonic is passed - return the truffle-hdwallet-provider's instance
  • ERC20Interface - can be used for accessing the ERC20 tokens' standard methods (name, symbol, totalSupply, etc.)

It supports both promises and the async-await calls with or without the callbacks.

Examples

const {AccessInterface} = require('monitorchain-interface-library');
const log = console.log;

const mc = new AccessInterface(
    'http://main.infura.io/<API KEY>',
    '0xF8CE9D2....71337Bd6201a', //The MonitorChain address
    '12 words mnemonic is here'
);

// Get the list of supported tokens (resolve a promise)
mc.getAllSupportedTokens().then(console.log);

// Get number of the supported tokens (using a callback)
mc.getNumberSupportedTokens((err, result) => {console.log(err, result)})

// Calculate a subscription price (async-await syntax)
const calc = async () => {
    // 45-days subscription for 50 tokens
    const price = await mc.calculatePrice(45, 50);
    console.log(price);
};

calc();

// Subscribe to given list of tokens (default period)
const tokens = [
    "0xB8c77482e45F1F44dE1745F52C74426C631bDD52",
    "0xf230b790E05390FC8295F4d3F60332c93BEd42e2"
];

mc.subscribe(tokens, (err, result) => {
    console.log(`${err ? 'Fail':'Success'}`)
});

// 45 days subscription
let token = "0xE41d2489571d322189246DaFA5ebDe1F4699F498";
mc.subscribe([token], 45, (err, result) => {
 console.log(`${err ? 'Fail':'Success'}`)
});

// Add a token to subscription (30 days), remit 1 eth for further purposes
const weiPerEth = "1000000000000000000";
let newToken = "0xA4e8C3Ec456107eA67d3075bF9e3DF3A75823DB0";
mc.init().then(() => {
    mc.wallet = 1;  // use the second address from the 'addresses' array generated by the truffle hdwallet provider
    mc.addTokenToSubscription(newToken, null, null, weiPerEth, (err, result) => {
        console.log(`${err ? err:'Success'}`)
    })
});

Using a built-in truffle hdwallet provider

const {AccessInterface} = require('monitorchain-interface-library');
const log = console.log;

const mc = new AccessInterface(
    'http://localhost:8545',
    '0xF8CE9D27Ff65E59cc5499a44f3fd71337Bd6201a',
    '12 words mnemonic is here'
);

const subscribe = async() => {
    mc.wallet = 2;

    log(await mc.getSubscriptionData());
    log(await mc.getTokensSubscribedTo());

    await mc.subscribe([
        "0xB8c77482e45F1F44dE1745F52C74426C631bDD52"
    ]);

    log(await mc.getSubscriptionData());
    log(await mc.remainingSubscriptionDays());
    log(await mc.getTokensSubscribedTo());
};

subscribe();

Using a custom web3 instance

const {AccessInterface} = require('monitorchain-interface-library');
const HDWalletProvider = require('truffle-hdwallet-provider');
const Web3 = require('web3');

const nodeAddress =  'http://localhost:8545';
const monitorChainAddress = '0xF8CE9D27Ff65E59cc5499a44f3fd71337Bd6201a';
const mnemonic = '12 words mnemonic is here';

const web3 = new Web3(new HDWalletProvider(mnemonic, nodeAddress, 0, 20));

// A static method 'web3' allows to pass a custom web3 instance
const mc = AccessInterface.web3(web3, monitorChainAddress);

mc.getAllSupportedTokens(console.log);

Listening for realtime events

const {AccessInterface, ERC20Interface} = require('monitorchain-interface-library');
const log = console.log;
const monitorChainAddress = '0xF8CE9D27Ff65E59cc5499a44f3fd71337Bd6201a';


const mc = new AccessInterface(
    'http://localhost:8545',
    monitorChainAddress,
    '12 words mnemonic is here'
);

const ws = new AccessInterface(
    'ws://localhost:8543',
    monitorChainAddress
);

const callback = async (err, result) => {
    if(err) throw err;
    const tokenAddress = await mc.getTokenForEventId(result);
    if (!tokenAddress) return; // return if a customer is not subscribed to token

    log(`${tokenAddress}: a status has been changed: ${result}`);
    log(await mc.getCurrentStatusDetails(tokenAddress));
    const token =ERC20Interface.web3(mc.w3, tokenAddress);
    const tokenInfo = await token.tokenInfo();
    log(JSON.stringify(tokenInfo, null, 4));
};

ws.onTokenStatusChanged(callback);

Troubleshooting

Transactions are too slow

Increase a gas price:

const {ERC20Interface} = require('monitorchain-interface-library');
const token = new ERC20Interface(...);
...
token.gasPrice = 3; //gWei
...
Error: Exceeds block gas limit

Decrease the gasLimit:

const {ERC20Interface} = require('monitorchain-interface-library');
const token = new ERC20Interface(...);
...
token.gasLimit = '3000000'
...

Error: Cannot find module 'ethereumjs-wallet/hdkey'

$ npm uninstall ethereumjs-wallet
$ npm install [email protected]

License

GPL-2.0