From fff720ad71b2a328eb50f9dfbec831a143c567e6 Mon Sep 17 00:00:00 2001 From: Teemu Patja Date: Mon, 28 Aug 2017 23:09:57 +0300 Subject: [PATCH] Use web3 via cljs-web3 * take cljs-web3 to use and no longer rely on window/web3 injected by wallet --- project.clj | 3 ++- src/cljs/commiteth/handlers.cljs | 20 +++++++++++++++----- src/cljs/commiteth/subscriptions.cljs | 4 ++++ src/cljs/commiteth/update_address.cljs | 13 ++++++------- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/project.clj b/project.clj index cd48fa7..bafd64e 100644 --- a/project.clj +++ b/project.clj @@ -49,7 +49,8 @@ [com.andrewmcveigh/cljs-time "0.5.1"] [akiroz.re-frame/storage "0.1.2"] [cljsjs/chartjs "2.6.0-0"] - [org.web3j/core "2.3.0"]] + [org.web3j/core "2.3.0"] + [cljs-web3 "0.19.0-0-2"]] :min-lein-version "2.0.0" :source-paths ["src/clj" "src/cljc"] diff --git a/src/cljs/commiteth/handlers.cljs b/src/cljs/commiteth/handlers.cljs index db314d4..b955121 100644 --- a/src/cljs/commiteth/handlers.cljs +++ b/src/cljs/commiteth/handlers.cljs @@ -7,6 +7,8 @@ inject-cofx]] [ajax.core :refer [GET POST]] [cuerdas.core :as str] + [cljs-web3.core :as web3] + [cljs-web3.eth :as web3-eth] [akiroz.re-frame.storage :as rf-storage :refer [reg-co-fx!]])) @@ -41,7 +43,15 @@ :initialize-db [(inject-cofx :store)] (fn [{:keys [db store]} [_]] - {:db (merge db/default-db store)})) + (println (boolean (-> js/window .-web3))) + (let [injected-web3 (-> js/window .-web3) + web3 (if (boolean injected-web3) + (new (-> js/window .-Web3) (web3/current-provider injected-web3)) + ;; TODO: put RPC URL in db + (web3/create-web3 "http://localhost:8545"))] + {:db (-> db/default-db + (merge {:web3 web3}) + (merge store))}))) (reg-event-db :assoc-in @@ -351,8 +361,8 @@ owner-address :owner_address contract-address :contract_address confirm-hash :confirm_hash} issue]] - (let [web3-eth (-> js/web3 .-eth) - bignum (-> js/web3 .-BigNumber) + (let [web3 (:web3 db) + bignum (-> web3 .-BigNumber) confirm-method-id (sig->method-id "confirmTransaction(uint256)") confirm-id (strip-0x confirm-hash) data (str confirm-method-id @@ -365,8 +375,8 @@ :data data}] (println "data:" data) (try - (.sendTransaction web3-eth (clj->js payload) - (send-transaction-callback issue-id)) + (web3-eth/send-transaction! web3 (clj->js payload) + (send-transaction-callback issue-id)) {:db (assoc-in db [:owner-bounties issue-id :confirming?] true)} (catch js/Error e {:db (assoc-in db [:owner-bounties issue-id :confirm-failed?] true) diff --git a/src/cljs/commiteth/subscriptions.cljs b/src/cljs/commiteth/subscriptions.cljs index 330a861..1ce5110 100644 --- a/src/cljs/commiteth/subscriptions.cljs +++ b/src/cljs/commiteth/subscriptions.cljs @@ -1,6 +1,10 @@ (ns commiteth.subscriptions (:require [re-frame.core :refer [reg-sub]])) +(reg-sub + :db + (fn [db _] db)) + (reg-sub :page (fn [db _] diff --git a/src/cljs/commiteth/update_address.cljs b/src/cljs/commiteth/update_address.cljs index 8d8c9f0..9d9fb80 100644 --- a/src/cljs/commiteth/update_address.cljs +++ b/src/cljs/commiteth/update_address.cljs @@ -2,19 +2,18 @@ (:require [re-frame.core :as rf] [commiteth.common :refer [input dropdown]] [reagent.core :as r] - [reagent.crypt :as crypt])) + [reagent.crypt :as crypt] + [cljs-web3.eth :as web3-eth])) (defn update-address-page [] - (let [user (rf/subscribe [:user]) + (let [db (rf/subscribe [:db]) + user (rf/subscribe [:user]) updating-address (rf/subscribe [:get-in [:updating-address]]) address (r/atom @(rf/subscribe [:get-in [:user :address]]))] (fn [] - (let [web3 (.-web3 js/window) - web3-accounts (vec (when-not (nil? web3) (-> web3 - .-eth - .-accounts - js->clj)))] + (let [web3 (:web3 @db) + web3-accounts (web3-eth/accounts web3)] (println "web3-accounts" web3-accounts) [:div.ui.container.grid [:div.ui.form.sixteen.wide.column