Git Product home page Git Product logo

drizzle-utils's Introduction

Drizzle Utils

A library for interacting with Ethereum smart contracts based on RxJS streams.

You can easily subscribe to events, contract state, and read-only methods. This library supports browser, node (i.e. server-side), and React Native environments.

Usage

Note that the $ at the end of a variable name denotes an RxJS stream. You can call .subscribe on it and use any RxJS stream operators of your choosing.

Getting started

Install via NPM:

npm install @drizzle-utils/core @drizzle-utils/get-web3

Usage example:

// import/require the packages you need
const getWeb3 = require("@drizzle-utils/get-web3");
const createDrizzleUtils = require("@drizzle-utils/core");

// initialize the tooling
const web3 = await getWeb3();
const drizzleUtils = await createDrizzleUtils({ web3 });
const accounts = await drizzleUtils.getAccounts();

// `instance` is a web3 Contract instance of the deployed contract
const instance = await drizzleUtils.getContractInstance({
  artifact: contractArtifact,
});

Getting the Web3 instance

Refer to these docs for reference.

Streaming contract events

Returns a Promise that resolves to an RxJS stream of events.

const event$ = await drizzleUtils.createEvent$({
  instance: contractInstance, // web3 contract instance
});

event$.subscribe(event => console.log(event));

Alternatively, you can pass in the artifact:

const event$ = await drizzleUtils.createEvent$({
  artifact: contractArtifact,
});

event$.subscribe(event => console.log(event));

Streaming the contract state

Returns a Promise that resolves to an RxJS stream of contract states.

const state$ = await drizzleUtils.createState$({
  artifact: contractArtifact,
});

state$.subscribe(state => console.log(state));

Streaming a constant read-only method from a contract

In this example, get is a read-only method in our contract.

const call$ = await drizzleUtils.createCall$({
  methodCall: instance.methods.get(),
});

call$.subscribe(result => console.log(result));

Current account stream

When the user changes accounts via MetaMask, you can watch for changes using the drizzleUtils.currentAccount$ stream.

// The current account stream is created upon instantiation
drizzleUtils.currentAccount$.subscribe(account => console.log(account));

Using specific packages

Find the package under the packages directory and refer to the README there. For example, under packages/get-web3, the README file located there illustrates multiple ways to use a custom provider.

For development

Make sure you have Yarn installed globally.

  1. Clone the repo.
  2. Run lerna bootstrap.
  3. (Optional) Run npm install inside any of the test-app folders. Note that yarn may not work properly.

drizzle-utils's People

Contributors

adrianmcli avatar honestbonsai avatar dependabot[bot] avatar fainashalts avatar crazyrabbitltc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.