/district-web3-utils

⚠️ This code now resides at d0x monorepo

Primary LanguageClojureEclipse Public License 1.0EPL-1.0

district-web3-utils

CircleCI

Set of extra functions helpful for working with web3.js.

Installation

Add Clojars Project into your project.clj
Include [district.web3-utils] in your CLJS file

API Overview

district.web3-utils

Safely Converts wei into ether.

(web3-utils/wei->eth "1000000000000000000")
;; => "1"

(web3-utils/wei->eth (web3/to-big-number 1000000000000000000))
;; => #object[BigNumber 1]

(web3-utils/wei->eth "abc")
;; => nil

Safely Converts wei into ether and coerces into number.

(web3-utils/wei->eth-number "1000000000000000000")
;; => 1

(web3-utils/wei->eth-number (web3/to-big-number 1000000000000000000))
;; => 1

Safely Converts ether into wei.

(web3-utils/eth->wei "1.1")
;; => "1100000000000000000"

;; handles comma as fraction decimals separator as well
(web3-utils/eth->wei "1,1")
;; => "1100000000000000000"

Safely Converts ether into wei and coerces into number.

(web3-utils/eth->wei-number "1.1")
;; => 1100000000000000000

True if address is zero address

(web3-utils/zero-address? "0x")
;; => true

(web3-utils/zero-address? "0x0000000000000000000000000000000000000000")
;; => true

True if address is zero address, empty string or nil

(web3-utils/empty-address? "0x")
;; => true

(web3-utils/empty-address? nil)
;; => true

Removes initial "0x" from an address

(web3-utils/remove-0x "0x0000000000000000000000000000000000000000")
;; => "0000000000000000000000000000000000000000"

Converts time returned by smart-contracts (usually as BigNumber UNIX epoch) into cljs-time.

(web3-utils/web3-time->date-time (web3/to-big-number 1516223428))
;; => #object[Object 20180117T211028]

(web3-utils/web3-time->date-time 1516223428)
;; => #object[Object 20180117T211028]

(web3-utils/web3-time->date-time (web3/to-big-number 0))
;; => nil

Converts time returned by smart-contracts (usually as BigNumber UNIX epoch) into local cljs-time.

(web3-utils/web3-time->local-date-time (web3/to-big-number 1516223428))
;; => #object[Object 20180117T211028]

(web3-utils/web3-time->local-date-time 1516223428)
;; => #object[Object 20180117T211028]

If given address is shorter than it should be, it prepends missing places with zeros.

(web3-utils/prepend-address-zeros "0x123")
;; => "0x0000000000000000000000000000000000000123"

Converts web3 bytes32 encoded string into normal string.

(web3-utils/bytes32->str "0x636f6e7374727563746564000000000000000000000000000000000000000000")
;; => "constructed"

Converts Solidity's uint into web3 address

(web3-utils/uint->address (web3/to-big-number 1234))
;; => "0x00000000000000000000000000000000000004d2"

Development

  1. Run test suite:
  • Browser
    • npx shadow-cljs watch test-browser
    • open https://d0x-vm:6502
    • tests refresh automatically on code change
  • CI (Headless Chrome, Karma)
    • npx shadow-cljs compile test-ci
    • CHROME_BIN=`which chromium-browser` npx karma start karma.conf.js --single-run
  1. Build
  • on merging pull request to master on GitHub, CI builds & publishes new version automatically
  • update version in build.clj
  • to build: clj -T:build jar
  • to release: clj -T:build deploy (needs CLOJARS_USERNAME and CLOJARS_PASSWORD env vars to be set)