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

@defi.org/web3-candies

v4.20.19

Published

Common utilities for web3

Downloads

3,091

Readme

Web3 Candies 🍬🍭🍦

Sweet Web3 + TypeScript + HardHat (optional) development stack

Installation

npm install --save @defi.org/web3-candies

If not using hardhat, or running in a browser, skip the optional dependencies: --omit optional

Usage example

import { bn, bn18, bnm, ether, erc20s, erc20, account, maxUint256 } from "@defi.org/web3-candies";
import { resetNetworkFork } from "@defi.org/web3-candies/dist/hardhat"; // to allow hardhat dependencies to be optional

const x = bn18(1000.1234); // x = "1000123400000000000000" [BigNumber.js object representing wei, parsed with 18 decimals]
console.log(x.gt(ether)); // true
console.log(bnm(x).toFormat()); // prints "1,000.1234"

const owner = await account(); // web3 account [0]

console.log(await erc20s.eth.WETH().amount(123.456)); // prints 123456000000000000000
console.log(await erc20s.eth.USDC().mantissa("123123456789")); // prints 123123.456789
console.log(await erc20s.eth.USDC().to18("123456000")); // prints 123456000000000000000 (18 decimals)

const myToken = erc20("foo", myTokenAddress); // web3 instantiated ERC20 Contract
await myToken.methods.approve(other, maxUint256).send({ from: owner }); // approve max uint value for other to spend

run tests with env variable DEBUG=web3-candies to see logs

use hardhatDefaultConfig in hardhat.config.ts for sugary hardhat defaults

Sweets included

See the tests for working examples

  • estimateGasPrices(): simple implementation of gas estimator for slow/avg/fast gas price

BigNumber.js utils

  • bn: convert string|number|BN|BigNumber to BigNumber.js object
  • bne: exponentiate n to decimals (bne(123.456789, 3) ==> 123456)
  • bnm: mantissa of n in decimals (bnm(123456.789, 3) ==> 123.456789)
  • bn6, bn9, bn18: convenience functions for bn with 6,9,18 decimals
  • zero, one, ten, ether, maxUint256, zeroAddress: hardcoded useful values
  • parsebn: parse formatted human-readable string to BigNumber.js object
  • convertDecimals: convert from source decimals to target decimals

ERC20s, NFTs

  • erc20s.eth/bsc/poly/arb/avax/oeth/ftm...: well known ERC20 base tokens per network
  • erc20<T>(...): web3 ERC20 contract, with optional extending abi to merge
  • await erc20s.eth.WETH().decimals(): memoized version of decimals method
  • await erc20s.eth.WETH().amount(1.234): returns amount in wei, converted to token decimals (in this case 1234000000000000000) (memoized)
  • await erc20s.eth.USDC().mantissa(123123456789): returns token amount to mantissa with decimals (in this case 123123.456789) (memoized)
  • await erc20s.eth.USDC().to18(100): returns amount in 18 decimals, given amount in token decimals (in this case 100e18) (memoized)

contract utils

  • contract<T>(...): create web3 Contract instance, supporting types
  • deployArtifact(...): quickly deploy a compiled contract, for ex from tests
  • parseEvents: parse tx receipt events
  • etherscanVerify(...): verify sources for previously deployed contracts
  • waitForTxConfirmations: pass tx object to wait for tx confirmations

network utils

  • web3(): the globally accesible singleton. call setWeb3Instance(web3) if needed
  • networks.eth/bsc/poly/arb/avax/oeth/ftm...: constants
  • account: alias for web3.accounts
  • block: alias for web3.getBlock, with parsed timestamp (seconds)
  • findBlock: find a block closest to timestamp (millis)

hardhat utils

to allow hh to be optional, import from '@defi.org/web3-candies/dist/hardhat'

  • dist/hardhat/deploy: deployment script with prompts and confirmations, saves deployment artifacts locally, waits for confirmations, optionally verifies sources on etherscan
  • hardhatDefaultConfig: sweet hardhat config
  • gasReportedConfig: hardhat-gas-reporter preconfigured config
  • hre(): the globally accessible singleton
  • tag: tag address for use with hre.tracer in logs
  • artifact: read compiled artifact
  • impersonate: impersonate accounts
  • setBalance: sets account native token balance
  • resetNetworkFork: resets the fork, with optional blockNumber
  • getNetworkForkingBlockNumber, getNetworkForkingUrl: read hardhat config
  • mineBlocks: mine blocks in a loop, simulating chain progression with seconds
  • mineBlock: mine a single block with the given added seconds

test utils

  • useChaiBigNumber(): hoist and use @defi.org/chai-bignumber mocha+chai assertions
  • expectRevert: expects given fn to revert, containing reason string or regex

timing utils

  • throttle(this, seconds, fn): sugar for lodash throttle
  • sleep: async sleep seconds
  • keepTrying: keep trying to invoke fn catching and logging exceptions, with 1 sec sleep between invocations
  • preventMacSleep: runs a shell subprocess that prevents macbooks from sleeping

peerDependencies

  • Default (recommended) Web3 + Hardhat development dependencies are installed
    • Use the bundled tsconfig, solhint, and other project files as a starting point

How to manually verify sources in Etherscan in case of an error during deploy?

  • First, try running etherscanVerify
  • After running deploy script, the deployments artifact backup should hold build-info json with all the metadata required
  • Alternatively, after compiling with hardhat, ./artifacts/build-info should have this json
  • Extract the object under input: this is the Solidity standard-json-input required by Etherscan
  • Constructor arguments should be sent abi-encoded to Etherscan, this is printed during the deploy script, also accessible via abiEncodedConstructorArgs function