Clojurescript re-mount module, that handles the web3 chain the user wallet is connected to.
Add
into your project.clj
Include [district.ui.web3-chain]
in your CLJS file, where you use mount/start
Warning: district0x modules are still in early stages, therefore API can change in a future.
This namespace contains web3-chain mount module. Once you start mount it'll take care of loading web3 chain.
You can pass following args to initiate this module:
:disable-loading-at-start?
Pass true if you don't want load chain at start:disable-polling?
Pass true if you want to disable polling for chain changes (needed for MetaMask account switching):polling-interval-ms
How often should poll for chain changes. Default: 4000:load-injected-chain-only?
Pass true if you want to load chain only when web3 is injected into a browser (ns my-district.core
(:require [mount.core :as mount]
[district.ui.web3-chain]))
(-> (mount/with-args
{:web3 {:url "https://mainnet.infura.io/"}
:web3-chain {:polling-interval-ms 5000}})
(mount/start))
re-frame subscriptions provided by this module:
::chain
Returns chain.
::has-chain?
Returns true if user is connected to a chain.
(ns my-district.home-page
(:require [district.ui.web3-chain.subs :as chain-subs]
[re-frame.core :refer [subscribe]]))
(defn home-page []
(let [chain (subscribe [::chain-subs/chain])]
(fn []
(if @chain
[:div "Your active chain is " @chain]
[:div "You don't have any active chain"]))))
re-frame events provided by this module:
::load-chain [opts]
Loads web3 chain
::set-chain [chain-id]
Sets chain into db
::poll-chain [opts]
Event fired when polling for chain changes in an interval. Note, polling is used only as fallback option, since MetaMask provides callback registration for chain changed event).
::chain-changed
Fired when chain have been changed. Use this event to hook into event flow from your modules. One example using re-frame-forward-events-fx may look like this:
(ns my-district.events
(:require [district.ui.web3-chain.events :as chain-events]
[re-frame.core :refer [reg-event-fx]]
[day8.re-frame.forward-events-fx]))
(reg-event-fx
::my-event
(fn []
{:register :my-forwarder
:events #{::chain-events/chain-changed}
:dispatch-to [::do-something]}))
::request-switch-chain [chain-id & chain-info]
Makes a RPC request to trigger a chain switch in the connected wallet. If chain id is unknown and chain-info is provided, it will trigger ::request-add-chain event
::request-add-chain [chain-info]
Makes a RPC request to trigger adding a new chain to the connected wallet.
DB queries provided by this module:
You should use them in your events, instead of trying to get this module's
data directly with get-in
into re-frame db.
chain [db]
Returns chain
(ns my-district.events
(:require [district.ui.web3-chain.queries :as chain-queries]
[re-frame.core :refer [reg-event-fx]]))
(reg-event-fx
::my-event
(fn [{:keys [:db]}]
(if (empty? (chain-queries/chain db))
{:dispatch [::do-something]}
{:dispatch [::do-other-thing]})))
has-chain? [db]
Returns true if user is connected to chain.
assoc-chain [db chain]
Associates chain and returns new re-frame db.
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