|
1 | 1 | (ns ethlance.ui.page.new-job.events |
2 | 2 | (:require |
3 | | - [cljs.core.async :as async :refer [<!] :refer-macros [go]] |
| 3 | + [cljs-web3-next.core :as web3] |
4 | 4 | [cljs-web3-next.eth :as w3n-eth] |
5 | 5 | [district.ui.notification.events :as notification.events] |
6 | 6 | [district.ui.router.effects :as router.effects] |
|
9 | 9 | [district.ui.web3-accounts.queries :as accounts-queries] |
10 | 10 | [district.ui.web3-tx.events :as web3-events] |
11 | 11 | [district.ui.web3.queries :as web3-queries] |
12 | | - [ethlance.shared.constants :as constants] |
13 | 12 | [ethlance.shared.contract-constants :as contract-constants] |
14 | 13 | [ethlance.shared.utils :refer [base58->hex js-obj->clj-map]] |
15 | 14 | [ethlance.ui.event.utils :as event.utils] |
16 | 15 | [ethlance.ui.util.tokens :as util.tokens] |
17 | | - [re-frame.core :as re])) |
| 16 | + [re-frame.core :as re] |
| 17 | + [taoensso.timbre :as log])) |
18 | 18 |
|
19 | 19 |
|
20 | 20 | (def state-key :page.new-job) |
|
100 | 100 | (fn [db [_ decimals]] |
101 | 101 | (assoc-in db [state-key :job/token-decimals] decimals))) |
102 | 102 |
|
| 103 | +(re/reg-event-db |
| 104 | + :page.new-job/decimals-response-error |
| 105 | + (fn [db [_ error]] |
| 106 | + (log/error "Failed to retrieve token decimals" {:error error}) |
| 107 | + (assoc-in db [state-key :job/token-decimals] 0))) |
| 108 | + |
| 109 | + |
| 110 | +(defn get-decimals-fx-fn [db token-type token-address] |
| 111 | + (let [default-decimals (if (= token-type :eth) 18 0)] |
| 112 | + (if (and (= :erc20 token-type) |
| 113 | + (web3/address? token-address)) |
| 114 | + (fn [] |
| 115 | + [:web3/call |
| 116 | + {:fns |
| 117 | + [{:instance (w3n-eth/contract-at |
| 118 | + (district.ui.web3.queries/web3 db) |
| 119 | + (ethlance.shared.contract-constants/abi token-type) |
| 120 | + token-address) |
| 121 | + :fn :decimals |
| 122 | + :args [] |
| 123 | + :on-success [:page.new-job/decimals-response] |
| 124 | + :on-error [:page.new-job/decimals-response-error]}]}]) |
| 125 | + (fn [] [:dispatch [:page.new-job/decimals-response default-decimals]])))) |
| 126 | + |
103 | 127 |
|
104 | 128 | (re/reg-event-fx |
105 | 129 | :page.new-job/set-token-type |
106 | 130 | (fn [{:keys [db] :as _cofx} [_ token-type]] |
107 | | - (let [default-decimals (if (= token-type :eth) 18 0) |
108 | | - decimals-fx-fn (if (= :erc20 token-type) |
109 | | - (fn [] |
110 | | - [:web3/call |
111 | | - {:fns |
112 | | - [{:instance (w3n-eth/contract-at |
113 | | - (district.ui.web3.queries/web3 db) |
114 | | - (ethlance.shared.contract-constants/abi token-type) |
115 | | - (get-in db [state-key :job/token-address])) |
116 | | - :fn :decimals |
117 | | - :args [] |
118 | | - :on-success [:page.new-job/decimals-response] |
119 | | - :on-error [:page.new-job/decimals-response]}]}]) |
120 | | - (fn [] [:dispatch [:page.new-job/decimals-response default-decimals]]))] |
| 131 | + (let [decimals-fx-fn (get-decimals-fx-fn db token-type (get-in db [state-key :job/token-address]))] |
121 | 132 | {:fx [(decimals-fx-fn)] |
122 | 133 | :db (-> db |
123 | 134 | (assoc-in ,,, [state-key :job/token-amount] default-token-amount) |
124 | 135 | (assoc-in ,,, [state-key :job/token-type] token-type))}))) |
125 | 136 |
|
126 | 137 |
|
| 138 | +(re/reg-event-fx |
| 139 | + :page.new-job/set-token-address |
| 140 | + (fn [{:keys [db] :as _cofx} [_ token-address]] |
| 141 | + (let [decimals-fx-fn (get-decimals-fx-fn db (get-in db [state-key :job/token-type]) token-address)] |
| 142 | + {:fx [(decimals-fx-fn)] |
| 143 | + :db (-> db |
| 144 | + (assoc-in ,,, [state-key :job/token-address] token-address))}))) |
| 145 | + |
127 | 146 | (re/reg-event-fx :page.new-job/set-token-amount (create-assoc-handler :job/token-amount)) |
128 | | -(re/reg-event-fx :page.new-job/set-token-address (create-assoc-handler :job/token-address)) |
129 | 147 | (re/reg-event-fx :page.new-job/set-token-id (create-assoc-handler :job/token-id)) |
130 | 148 |
|
131 | 149 |
|
|
0 commit comments