Set of extra functions helpful for working with web3.js.
Add [district0x/district-web3-utils "1.0.3"]
into your project.clj
Include [district.web3-utils]
in your CLJS file
wei->eth [x]
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
wei->eth-number [x]
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
eth->wei [x]
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"
eth->wei-number [x]
Safely Converts ether into wei and coerces into number.
(web3-utils/eth->wei-number "1.1")
;; => 1100000000000000000
zero-address? [address]
True if address is zero address
(web3-utils/zero-address? "0x")
;; => true
(web3-utils/zero-address? "0x0000000000000000000000000000000000000000")
;; => true
empty-address? [address]
True if address is zero address, empty string or nil
(web3-utils/empty-address? "0x")
;; => true
(web3-utils/empty-address? nil)
;; => true
remove-0x [address]
Removes initial "0x" from an address
(web3-utils/remove-0x "0x0000000000000000000000000000000000000000")
;; => "0000000000000000000000000000000000000000"
web3-time->date-time [x]
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
web3-time->local-date-time [x]
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]
prepend-address-zeros [address]
If given address is shorter than it should be, it prepends missing places with zeros.
(web3-utils/prepend-address-zeros "0x123")
;; => "0x0000000000000000000000000000000000000123"
bytes32->str [x]
Converts web3 bytes32 encoded string into normal string.
(web3-utils/bytes32->str "0x636f6e7374727563746564000000000000000000000000000000000000000000")
;; => "constructed"
uint->address [x]
Converts Solidity's uint into web3 address
(web3-utils/uint->address (web3/to-big-number 1234))
;; => "0x00000000000000000000000000000000000004d2"
lein deps
# To run tests and rerun on changes
lein doo chrome tests
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close