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

gem-ether

v0.0.2

Published

**gem-ether** is a module that provide an easier way to interact with a smart contract deployed on one of the ethereum related network (mainnet, polygon, mumbai, rinkeby and so on...).

Downloads

4

Readme

GEM Ether

gem-ether is a module that provide an easier way to interact with a smart contract deployed on one of the ethereum related network (mainnet, polygon, mumbai, rinkeby and so on...).

The main class of this module is Ether, it need a smart contract address and the relative smart contract json ABI on construction, from there, you can interact with the provided smart contract: send transaction, read property, listen for events and listen for receipt!

  1. Initializing
  2. Fetch Data from a contract
  3. Send Transaction to a contract
    1. Connect the user
    2. Transaction signer
      1. SELF-SIGNED Transaction
      2. SERVER-SIGNED Transaction
    3. Wait confirmation
  4. Parse the contract response
  5. Ether utils

Initializing

To create an Ether instance connected with a deployed contract, you just need to provide the contract address and the contract json ABI.

import Ether from "gem-ether/dist"

const abi : JSON = // contract abi
const address : string = // contract address

const EtherContractInstance = new Ether(address, abi)

Fetch Data

To fetch data from a view or a public attribute, you don't need the user interaction.

const response = await EtherContractInstance.read('contractViewMethod', [...params])

if(response.error) 
  console.log( response.error )
else 
  console.log( response.value() )

If you want, you can use readValue that tries to read the value directly:

try {
  const value = await EtherContractInstance.readValue('contractViewMethod', [...params])
  console.log(value)
}
catch(e) {...}

Send Data

Connect the User

When initializing an Ether instance, you need to call the connect method. Here you can specify which provider you want to use. You can choose between our predefined:

  • metamask
  • walletConnect

or even specify a custom RPC url.

const provider = 'metamask' | 'walletConnect'

await EtherContractInstance.connect(provider)

Transaction Signer

When interacting with a smart contract, the user need to 'authorize' the transaction by signing it with its private key.

Normally the dapp won't store the user private key, instead the user makes requests using a provider that know its private key (like metamask or other wallet).

We named this type of transaction "SELF SIGNED TRANSACTION" and it's the default type of any transaction made with this library.

Another scenario could be, you need to trigger a smart contract functionality without any interaction, from a your wallet.

We named this type of transaction "SERVER SIGNED TRANSACTION" and you obviously need to provide the private key from which the transactions will be signed.

SELF SIGNED TRANSACTION

The normal transaction you're thinking of. User use metamask prompt, or some other wallet to sign the transaction with its private key, then send the transaction from that account.

const tx = await EtherContractInstance.send('contractMethod', [...params], {value, maxGas, ...})

if(tx.error) 
  console.log( tx.error )
else 
  console.log( tx.value() )

SERVER SIGNED TRANSACTION

Permit also to specify a private key if you want to send signed transaction from a your wallet while using a non browser environment with an rpc provider (like Infura). Generally client-side application don't want to do this! Never pass here your private key unless you are working server-side or you need to send a transaction from a your wallet, without metamask and without your physical confirmation.

The wallet must be your, you need the private key to do that and you must not ask for your users private key.

For example can be useful if you're developing an api that need to fully interact with ethereum, you can create a route that automatically trigger some smart contract method taking care of the transaction fee, without you physically confirm on metamask prompt.

Be cautious, the gas fee can go crazy some time! If you're using this method you probably want to add a check in your code, cause this don't need your confirmation and can lead to some unexpected cost.

Notice that even if you specify a private key as a signer, you need to call the send method passing "GEM_SIGNER" instead of "PROVIDER_SIGNER" to make a SERVER SIGNED TRANSACTION!

const abi : JSON = // contract abi
const address : string = // contract address
const privKey : string = // signer private key (hide this from your code)

const EtherContractInstance = new Ether(address, abi, privKey)
const tx = await EtherContractInstance.send('contractMethod', [...params], {value, maxGas, ...}, 'GEM_SIGNER')

if(tx.error) 
  console.log( tx.error )
else 
  console.log( tx.value() )

Wait Confirmation

You probably want to wait at least 5-6 confirmation or even more before updating your dapp, to avoid the risk of a block reversal.

...

if(tx.error) {
  // handle error
}
else {

  console.log('0 confirmation')

  await tx.wait(1)
  console.log('1 confirmation')

  await tx.wait(2)
  console.log('2 confirmation')
  
  // ...

}

Ether Utils

import Ether, { Utils } from "gem-ether/dist"

// Convert from ether to wei
Utils.toWei(1) = BigNumber(1000000000000000000)

// Convert from wei to ether
Utils.toWei("1000000000000000000") = "1"