Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Latest commit

 

History

History
160 lines (125 loc) · 5.74 KB

README.md

File metadata and controls

160 lines (125 loc) · 5.74 KB

district-ui-web3-chain

CircleCI

Clojurescript re-mount module, that handles the web3 chain the user wallet is connected to.

Installation

Add Clojars Project into your project.clj Include [district.ui.web3-chain] in your CLJS file, where you use mount/start

API Overview

Warning: district0x modules are still in early stages, therefore API can change in a future.

district.ui.web3-chain

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))

district.ui.web3-chain.subs

re-frame subscriptions provided by this module:

Returns 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"]))))

district.ui.web3-chain.events

re-frame events provided by this module:

Loads web3 chain

Sets chain into db

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).

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]}))

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

Makes a RPC request to trigger adding a new chain to the connected wallet.

district.ui.web3-chain.queries

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.

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]})))

Returns true if user is connected to chain.

Associates chain and returns new re-frame db.

Dependency on other district UI modules

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)