Skip to content

Commit 16d13f3

Browse files
authored
fetch ERC20 token decimals when changing token address (#474)
1 parent a5029cf commit 16d13f3

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

ui/src/ethlance/ui/page/new_job/events.cljs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(ns ethlance.ui.page.new-job.events
22
(:require
3-
[cljs.core.async :as async :refer [<!] :refer-macros [go]]
3+
[cljs-web3-next.core :as web3]
44
[cljs-web3-next.eth :as w3n-eth]
55
[district.ui.notification.events :as notification.events]
66
[district.ui.router.effects :as router.effects]
@@ -9,12 +9,12 @@
99
[district.ui.web3-accounts.queries :as accounts-queries]
1010
[district.ui.web3-tx.events :as web3-events]
1111
[district.ui.web3.queries :as web3-queries]
12-
[ethlance.shared.constants :as constants]
1312
[ethlance.shared.contract-constants :as contract-constants]
1413
[ethlance.shared.utils :refer [base58->hex js-obj->clj-map]]
1514
[ethlance.ui.event.utils :as event.utils]
1615
[ethlance.ui.util.tokens :as util.tokens]
17-
[re-frame.core :as re]))
16+
[re-frame.core :as re]
17+
[taoensso.timbre :as log]))
1818

1919

2020
(def state-key :page.new-job)
@@ -100,32 +100,50 @@
100100
(fn [db [_ decimals]]
101101
(assoc-in db [state-key :job/token-decimals] decimals)))
102102

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+
103127

104128
(re/reg-event-fx
105129
:page.new-job/set-token-type
106130
(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]))]
121132
{:fx [(decimals-fx-fn)]
122133
:db (-> db
123134
(assoc-in ,,, [state-key :job/token-amount] default-token-amount)
124135
(assoc-in ,,, [state-key :job/token-type] token-type))})))
125136

126137

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+
127146
(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))
129147
(re/reg-event-fx :page.new-job/set-token-id (create-assoc-handler :job/token-id))
130148

131149

0 commit comments

Comments
 (0)