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

wallet-provider

v1.1.1

Published

SDK for using crypto wallets such as Metamask

Downloads

5

Readme

Integrating a crypto wallet into your dApp

SDK for using wallets such as

  • Metamask desktop
  • Metamask mobile
  • Opera crypto wallet

Introduction

Sometimes during integrating an Ethereum wallet (like Opera or MetaMask) into decentralized web applications you can encounter unexpected complexity.

After some more research and tinkering we managed to find an integration methods that seems to avoid the quirks of individual wallet providers.

In short, here’s what you should expect from wallet providers:

– They should automatically inject the window.ethereum global variable, which must provide these methods:

  • .send(rpcMethodName: string, params?: Array<any>): Promise<any> – this is the preferred and recommended way (by them) to call an RPC method as it returns the promise.
  • .sendAsync(payload, callback: (error, result) => void) – this is an optional alternative, only needed when provider internally uses a Web3 object prior to version 1.0.0-beta38.
  • .on(eventName, listener)– a method of the EventEmitter interface which allows listening for various provider events like “networkChanged”, “accountsChanged”, etc.
  • .removeListener(eventName, listener) – the same, but for removing the event listener.
  • They should inject window.web3.currentProvider – which is essentially the same object as window.ethereum.

The slow creation of the EIP standard and the volatile implementation of Web3 have led to inconsistencies in Ethereum wallet provider implementations. Hopefully, the situation will get better in the future, but for now we have what we have.

Installation

npm install --save git+https://github.com/monetha/wallet-provider.git

Exported methods

  • enableWallet - enables wallet provider usage so it can be used or throws error otherwise
  • getCurrentAccountAddress - gets current account address selected in metamask
  • sendAndWaitTx - sends given transaction using current wallet provider and waits until it is mined
  • sendTransaction - submits transaction using metamask and returns its hash
  • waitReceipt - waits for transaction to finish for the given hash
  • getProviderInstance - gets current wallet provider instance
  • toHex - converts any given value to hex string, e.g. "0x123"
  • toBN - converts any given value to bignumber.js one

In case of exception package throws IWalletProviderError with codes specified in IWalletProviderErrorCode.

Basic usage

import { 
    enableWallet, 
    getCurrentAccountAddress, 
    sendAndWaitTx, 
    getProviderInstance, 
    sendTransaction, 
    waitReceipt,
 } from 'wallet-provider';
import { TransactionConfig } from 'web3-core';
import Web3 from 'web3';
import { AbiItem } from 'web3-utils';
import { someContractAbi } from './contracts';

const run = async () => {

  try {
    // Enable provider
    await enableWallet();

    // Get owner address
    const requesterAddress = await getCurrentAccountAddress();
    if (!requesterAddress) {
      throw new Error('No address in wallet');
    }

    // Create transaction
    let txConfig: TransactionConfig = {
      from: requesterAddress,
      to: `0xA2703F979bc19c6bB1cE4aF61b661526B3677799`,
      value: '1000000000000000000', // 1 ETH in wei
    };

    // Simplest way to send and wait for receipt
    let receipt = await sendAndWaitTx(txConfig);

    // Or you can do manual steps by yourself:
    // First get the provider instance
    const provider = getProviderInstance();
    const web3 = new Web3(provider);

    // Send transaction, get hash
    const hash = await sendTransaction(txConfig);

    // Wait for receipt
    receipt = await waitReceipt(web3, hash);

    // Common usage of web3 library
    const balance = await web3.eth.getBalance(requesterAddress);

    const gasPrice = await web3.eth.getGasPrice();

    // Calling any contract methods is also easy
    const someContract = new web3.eth.Contract(someContractAbi as AbiItem[], '0x3A3ebe78B24f33cb05DDa241f817Db0adaD95ae5');
    const gas = await someContract.methods.exampleMethod().estimateGas();
    const data = someContract.methods.exampleMethod().encodeABI();

    txConfig = {
      from: requesterAddress,
      to: `0x3A3ebe78B24f33cb05DDa241f817Db0adaD95ae5`,
      data,
      gas,
      gasPrice,
    };
    receipt = await sendAndWaitTx(txConfig);
  } catch (e) {
    console.error(e);
  }
}