Giter Site home page Giter Site logo

william3johnson / openzeppelin-network.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from openzeppelin/openzeppelin-network.js

0.0 1.0 0.0 845 KB

An easy to use and reliable library that provides one line access to Web3 API.

License: MIT License

JavaScript 4.47% TypeScript 95.53%

openzeppelin-network.js's Introduction

โš ๏ธ This project is deprecated. We are no longer actively developing new features nor addressing issues. Read here for more info, and reach out if you are interested in taking over maintenance. We suggest looking into web3-react for a popular alternative to this project.

openzeppelin-network.js

CircleCI npm (scoped)

One line access to Web3 in your dApp.

  • Hides various Web3 providers behind common API.
  • One line access to the Web3 providers (Metamask, Infura, Geth, Portis and etc).
  • Supports multiple Web3 providers within the same app.
  • First class support of meta-txs.
  • React integration using hooks.
  • Network, accounts, and connection changed events for all web3 providers.
  • Will fire events even for HTTP and forcefully terminated providers.

Overview

Installation

npm install @openzeppelin/network

Usage

With React Hooks

Import the library:

import { useWeb3Injected, useWeb3Network, useEphemeralKey } from '@openzeppelin/network/react';

Get Web3Context with React Hooks inside functional component:

const injected = useWeb3Injected();
const local = useWeb3Network('http://127.0.0.1:8545');

Use Web3Context to get fresh data:

const { accounts, networkId, networkName, providerName, lib, connected } = web3Context;

Network.js will re-render component when network, accounts or connetion state change.

To use GSN with any hook specify GSN as an option, providing a signing key:

const local = useWeb3Network('http://127.0.0.1:8545', {
  gsn: { signKey: useEphemeralKey() }
});

With Vanilla Javascript

Import the library:

import { fromInjected, fromConnection } from '@openzeppelin/network';

Get Web3Context:

const injected = await fromInjected();
const local = await fromConnection('http://127.0.0.1:8545');

To use GSN include a gsn option, including a signing key:

const local = await fromConnection('http://127.0.0.1:8545', {
  gsn: { signKey: ephemeral() }
});

Use Web3Context to get fresh data immediately:

const { accounts, networkId, networkName, providerName, lib, connected } = web3Context;

Subscribe to events to get notified:

function updateNetwork(networkId, networkName) {}
function updateAccounts(accounts) {}
function updateConnection(connected) {}

web3Context.on(Web3Context.NetworkIdChangedEventName, updateNetwork);
web3Context.on(Web3Context.AccountsChangedEventName, updateAccounts);
web3Context.on(Web3Context.ConnectionChangedEventName, updateConnection);

Unsubscribe from events once you don't need them:

web3Context.off(Web3Context.NetworkIdChangedEventName, updateNetwork);
web3Context.off(Web3Context.AccountsChangedEventName, updateAccounts);
web3Context.off(Web3Context.ConnectionChangedEventName, updateConnection);

Learn More

  • Try it out on the GSN Starter Kit: run npx @openzeppelin/cli unpack gsn in a new directory and follow the instructions.
  • For detailed usage information, take a look at the API Reference.

License

Released under the MIT License.

openzeppelin-network.js's People

Contributors

ylv-io avatar frangio avatar nventuro avatar spalladino avatar abcoathup avatar dependabot[bot] avatar ravidsrk avatar

Watchers

 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.