4.20.19 • Published 26 days ago

@defi.org/web3-candies v4.20.19

Weekly downloads
-
License
MIT
Repository
github
Last release
26 days ago

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
4.20.18

27 days ago

4.20.19

26 days ago

4.20.17

1 month ago

4.20.15

2 months ago

4.20.14

2 months ago

4.20.13

3 months ago

4.20.12

4 months ago

4.20.10

5 months ago

4.20.11

5 months ago

4.20.9

5 months ago

4.20.8

5 months ago

4.16.3

10 months ago

4.16.4

10 months ago

4.16.6

10 months ago

4.16.1

10 months ago

4.16.2

10 months ago

4.16.7

10 months ago

4.16.8

10 months ago

4.16.9

9 months ago

4.20.6

6 months ago

4.20.7

6 months ago

4.18.0

9 months ago

4.17.2

9 months ago

4.17.3

9 months ago

4.17.4

9 months ago

4.17.0

9 months ago

4.17.1

9 months ago

4.20.3

6 months ago

4.20.4

6 months ago

4.20.5

6 months ago

4.20.0

8 months ago

4.20.1

6 months ago

4.16.10

9 months ago

4.19.0

8 months ago

4.19.1

8 months ago

4.19.2

8 months ago

4.19.3

8 months ago

4.19.4

8 months ago

4.19.5

8 months ago

4.16.0

10 months ago

4.15.4

11 months ago

4.15.0

11 months ago

4.15.1

11 months ago

4.15.2

11 months ago

4.15.3

11 months ago

4.14.1

11 months ago

4.14.2

11 months ago

4.9.2

12 months ago

4.14.0

11 months ago

4.10.0

12 months ago

4.13.2

11 months ago

4.13.3

11 months ago

4.13.4

11 months ago

4.13.5

11 months ago

4.13.0

11 months ago

4.13.1

11 months ago

4.12.3

11 months ago

4.12.0

11 months ago

4.12.1

11 months ago

4.12.2

11 months ago

4.11.4

11 months ago

4.11.5

11 months ago

4.11.6

11 months ago

4.11.0

12 months ago

4.11.1

12 months ago

4.11.2

12 months ago

4.11.3

12 months ago

4.9.0

1 year ago

4.9.1

1 year ago

4.8.12

1 year ago

4.8.11

1 year ago

4.8.9

1 year ago

4.8.8

1 year ago

4.8.5

1 year ago

4.8.4

1 year ago

4.8.7

1 year ago

4.8.6

1 year ago

4.8.1

1 year ago

4.8.0

1 year ago

4.8.3

1 year ago

4.8.2

1 year ago

4.7.0

1 year ago

4.8.10

1 year ago

4.6.1

1 year ago

4.6.0

1 year ago

4.6.3

1 year ago

4.6.2

1 year ago

4.4.1

2 years ago

4.4.0

2 years ago

4.3.0

2 years ago

4.5.0

2 years ago

4.5.1

2 years ago

3.7.2

2 years ago

3.6.2

2 years ago

3.6.1

2 years ago

3.6.0

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

4.0.3

2 years ago

4.0.2

2 years ago

3.6.4

2 years ago

3.6.3

2 years ago

3.9.1

2 years ago

3.9.0

2 years ago

3.8.0

2 years ago

4.2.0

2 years ago

3.8.4

2 years ago

3.8.3

2 years ago

3.8.2

2 years ago

3.8.1

2 years ago

3.8.6

2 years ago

3.8.5

2 years ago

3.7.1

2 years ago

3.7.0

2 years ago

4.1.0

2 years ago

3.4.0

2 years ago

3.4.1

2 years ago

3.3.1

2 years ago

3.5.0

2 years ago

3.2.0

2 years ago

3.3.0

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.1.0

2 years ago

2.6.1

2 years ago

2.6.0

2 years ago

2.8.1

2 years ago

2.8.0

2 years ago

2.7.4

2 years ago

2.7.3

2 years ago

2.5.0

2 years ago

2.7.0

2 years ago

2.5.2

2 years ago

2.5.1

2 years ago

2.9.0

2 years ago

2.7.2

2 years ago

2.7.1

2 years ago

2.8.3

2 years ago

2.8.2

2 years ago

2.8.4

2 years ago

2.4.1

2 years ago

2.4.0

2 years ago

2.4.3

2 years ago

2.4.2

2 years ago

2.4.5

2 years ago

2.4.4

2 years ago

2.3.0

2 years ago

2.4.6

2 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.2.3

3 years ago

2.2.2

3 years ago

2.2.5

3 years ago

2.2.4

3 years ago

2.2.7

3 years ago

2.2.6

3 years ago

2.2.10

2 years ago

2.2.9

2 years ago

2.2.8

3 years ago

2.1.4

3 years ago

2.1.5

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.3

3 years ago

2.1.0

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.6.4

3 years ago

1.6.9

3 years ago

1.6.11

3 years ago

1.6.8

3 years ago

1.6.10

3 years ago

1.6.7

3 years ago

1.6.13

3 years ago

1.6.6

3 years ago

1.6.12

3 years ago

1.6.5

3 years ago

1.6.14

3 years ago

1.6.3

3 years ago

1.6.2

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago