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.
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.
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,
});
Refer to these docs for reference.
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));
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));
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));
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));
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.
Make sure you have Yarn installed globally.
- Clone the repo.
- Run
lerna bootstrap
. - (Optional) Run
npm install
inside any of thetest-app
folders. Note thatyarn
may not work properly.