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

@rgbk/api

v0.3.0

Published

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

Downloads

1

Readme

API Quick Start

RigoBlock TypeScript API to interact with the RigoBlock Protocol smart contracts.

Installation

npm install --save @rgbk/api

Or with Yarn:

yarn add @rgbk/api

Usage

Initialising the API

import Api from '@rgbk/api'

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

web3 parameter is optional and defaults to window.web3. The API class will then make a call to get the Network Id and set the correct RPC url.

Instantiating contracts

To instantiate a contract we use the createAndValidate() function, which first checks 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 isDeployed() method.

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

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

Calling methods

Creating a Vault from the VaultFactory contract

import Api from '@rgbk/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.

Contract Events

To get past events of a contract, use the getPastEvents function:

const pastEvents = await api.contract.vaultEventful.getPastEvents('allEvents', {
  fromBlock,
  toBlock
})

To create a subscription to a single event, use the relative method and pass it a callback to retrieve the data:

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.

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

Additional Resources

For a complete reference of the API, check our documentation website.

Additional examples of API usage can be found here.

Adding custom methods

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

Setup

Available Scripts

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.

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.

yarn doc-gen

Generates the documentation using Typedoc and further scripts to clean it up.

Contributing

Read our contribution guidelines.