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

@rigoblock/api

v0.2.15

Published

A JS wrapper around RigoBlock Smart Contracts to simplify their consumption.

Downloads

2

Readme

API

Building the package

Start Ganache

lerna run --scope @rigoblock/dapp ganache --stream

Then run the following command:

yarn build

Available Scripts

From the project root you can run:

yarn lint

Lints all files.

yarn abi-extract

Extracts all abis of the deployed contracts, then copies them over to the .tmp folder in json format. Requires Ganache to be running.

yarn abi-gen

Generates contract wrappers from the abi JSON files.

yarn tsc

Compiles all .ts files to Javascript, including map and declaration files into the dist folder.

yarn tsc:watch

Compiles on watch mode

yarn build

Extracts abis saving them as JSON files, generates wrappers and compiles .ts files into dist folder. Requires Ganache to be running unless abi files have been saved previously.

Initialising the API

import Api from '@rigoblock/api'

const api = new Api()
await api.init(web3, rpcUrl)

rpcUrl is optional and defaults to ws://localhost:8545 to work with Ganache.

Instantiating contracts

To instantiate a contract we use the createAndValidate() function, which will check if the contract is deployed on the blockChain, throwing an error if it isn't. The function accepts two parameters: a web3 instance and the contract's address.

Some contracts are already deployed when the API is initialised, their address is saved under a address property. To check if a contract is deployed we can use the custom isDeployed() function.

api.contract.Authority.isDeployed() // true

const authority = await api.contract.Authority.createAndValidate(
  api.web3,
  api.contract.Authority.address
)

Calling methods

Example

Creating a Vault from the VaultFactory contract

import Api from '@rigoblock/api'

const api = new Api()
await api.init()
const vaultFactory = api.contract.VaultFactory.createAndValidate(
  api.web3,
  api.contract.VaultFactory.address
)
const txOptions = { from: '0x1234...' }
const txObject = await vaultFactory.createVault('rocksolidvault', 'VLT')
const gasEstimate = await txObject.estimateGas(txOptions)
const gasPrice = await api.web3.eth.getGasPrice()
const receipt = await txObject.send({
  ...txOptions,
  gas: new BigNumber(gasEstimate).times(1.2).toFixed(0),
  gasPrice
})

const vaultAddress = receipt.events.VaultCreated.returnValues.vault

We add some more gas to the estimate as it can be incorrect in case new blocks are added between the estimate and the actual transaction taking place.

Adding custom methods

Custom methods can be added to our contracts using the Handlebars template.

Events

To get past events of a contract, use the getPastEvents function. To create a subscription to a single event, use the relative method and pass it a callback to retrieve the data.

Example

Listening for the VaultCreated event

await vaultFactory.VaultCreatedEvent(
  {
    fromBlock: 0,
    toBlock: 'latest'
  },
  (err, event) => (err ? console.error(err) : console.log(event))
)

If we wish to listen for all events, use the allEvents method.

Example

await vaultFactory.allEvents(
  {
    fromBlock,
    toBlock
  },
  (err, events) => (err ? console.error(err) : console.log(events))
)