-
Notifications
You must be signed in to change notification settings - Fork 23
/
events.cljs
752 lines (678 loc) · 30.7 KB
/
events.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
(ns district0x.ui.events
(:require
[ajax.core :as ajax]
[akiroz.re-frame.storage :as re-frame-storage]
[cemerick.url :as url]
[cljs-time.coerce :as time-coerce]
[cljs-time.core :as t]
[cljs-web3.core :as web3]
[cljs-web3.eth :as web3-eth]
[cljs-web3.personal :as web3-personal]
[cljs-web3.utils :as web3-utils]
[cljs.spec.alpha :as s]
[clojure.data :as data]
[clojure.set :as set]
[clojure.string :as string]
[day8.re-frame.async-flow-fx]
[day8.re-frame.http-fx]
[district0x.shared.big-number :as bn]
[district0x.shared.utils :as d0x-shared-utils]
[district0x.ui.db]
[district0x.ui.dispatch-fx]
[district0x.ui.interval-fx]
[district0x.ui.location-fx]
[district0x.ui.spec-interceptors :refer [validate-args conform-args validate-db validate-first-arg]]
[district0x.ui.spec]
[district0x.ui.utils :as d0x-ui-utils]
[district0x.ui.window-fx]
[goog.string :as gstring]
[goog.string.format]
[madvas.re-frame.google-analytics-fx]
[madvas.re-frame.web3-fx]
[medley.core :as medley]
[print.foo :include-macros true]
[re-frame.core :as re-frame :refer [reg-event-db reg-event-fx inject-cofx path trim-v after debug reg-fx console dispatch reg-cofx]]))
(re-frame-storage/reg-co-fx! :contribution {:fx :localstorage :cofx :localstorage})
(defn reg-empty-event-fx [id]
(reg-event-fx
id
(constantly nil)))
(reg-cofx
:current-url
(fn [coeffects _]
(assoc coeffects :current-url (d0x-ui-utils/current-url))))
(def interceptors [trim-v (validate-db :district0x.ui/db)])
(defn get-contract [db key]
(get-in db [:smart-contracts key]))
(defn get-instance
([db key]
(get-in db [:smart-contracts key :instance]))
([db key address]
(let [abi (:abi (get-contract db key))]
(web3-eth/contract-at (:web3 db) abi address))))
(defn all-contracts-loaded? [db]
(every? #(and (:abi %) (if goog.DEBUG (:bin %) true)) (vals (:smart-contracts db))))
(defn contract-xhrio [contract-name code-type version on-success on-failure]
{:method :get
:uri (gstring/format "./contracts/build/%s.%s?v=%s" contract-name (name code-type) (if goog.DEBUG
(.getTime (js/Date.))
version))
:timeout 6000
:response-format (if (= code-type :abi) (ajax/json-response-format) (ajax/text-response-format))
:on-success on-success
:on-failure on-failure})
(defn initialize-db [default-db localstorage current-url]
(let [web3 (if (d0x-ui-utils/provides-web3?)
(new (aget js/window "Web3") (web3/current-provider (aget js/window "web3")))
(web3/create-web3 (:node-url default-db)))]
(as-> default-db db
(d0x-shared-utils/merge-in db localstorage)
(update db :active-page merge {:query-params (medley/map-keys keyword (:query current-url))
:path (:path current-url)})
(assoc db :web3 web3)
(update-in db [:transaction-log :settings] merge {:open? false :highlighted-transaction nil}))))
(defn- has-tx-status? [tx-status {:keys [:status]}]
(= tx-status status))
(reg-event-fx
:district0x/initialize
[interceptors (inject-cofx :localstorage) (inject-cofx :current-url)]
(fn [{:keys [:localstorage :current-url]} [{:keys [:default-db :conversion-rates :effects]}]]
(let [db (district0x.ui.events/initialize-db default-db localstorage current-url)
transactions (get-in db [:transaction-log :transactions])
not-loaded-txs (medley/filter-vals (partial has-tx-status? :tx.status/not-loaded) transactions)
pending-txs (medley/filter-vals (partial has-tx-status? :tx.status/pending) transactions)]
(merge
{:db db
:ga/page-view [(d0x-ui-utils/current-location-hash)]
:window/on-resize {:dispatch [:district0x.window/resized]
:resize-interval 166}
:district0x/dispatch-n (concat
(for [tx-hash (keys not-loaded-txs)]
[:district0x/load-transaction-and-receipt tx-hash])
(for [tx-hash (keys pending-txs)]
[:district0x/load-transaction-receipt tx-hash]))
;; In some cases web3 injection may not yet happened, so we'll give it some time, just in case
:dispatch-later [{:ms (if (d0x-ui-utils/provides-web3?) 0 2000) :dispatch [:district0x/load-my-addresses]}]}
(when conversion-rates
{:district0x/dispatch [:district0x/load-conversion-rates (:currencies conversion-rates)]
:dispatch-interval {:dispatch [:district0x/load-conversion-rates (:currencies conversion-rates)]
:ms (or (:ms conversion-rates) 60000)
:db-path [:district0x/load-conversion-rates-interval]}})
effects))))
(reg-event-fx
:district0x/set-active-page
[interceptors (inject-cofx :current-url)]
(fn [{:keys [:db :current-url]} [{:keys [:handler] :as match}]]
(let [{:keys [:query :path]} current-url]
(merge
{:db (-> db
(assoc :active-page (merge match {:query-params (medley/map-keys keyword (:query current-url))
:path path}))
(assoc-in [:menu-drawer :open?] false))
:ga/page-view [(d0x-ui-utils/current-location-hash)]}
(when-not (= handler (:handler (:active-page db)))
{:window/scroll-to-top true})))))
(reg-event-fx
:district0x/load-my-addresses
interceptors
(fn [{:keys [db]}]
(let [new-db (if (d0x-ui-utils/provides-web3?)
(assoc db :web3 (new (aget js/window "Web3") (web3/current-provider (aget js/window "web3"))))
db)]
(merge
{:db new-db}
(if (or (:load-node-addresses? db)
(d0x-ui-utils/provides-web3?))
{:web3-fx.blockchain/fns
{:web3 (:web3 new-db)
:fns [{:f web3-eth/accounts
:on-success [:district0x/my-addresses-loaded]
:on-error [:district0x/dispatch-n [[:district0x/my-addresses-loaded []]]]}]}}
{:dispatch [:district0x/my-addresses-loaded []]})))))
(reg-event-fx
:district0x/load-smart-contracts
interceptors
(fn [{:keys [db]} [{:keys [:version]}]]
{:http-xhrio
(flatten
(for [[key {:keys [name]}] (:smart-contracts db)]
(for [code-type (if goog.DEBUG [:abi :bin] [:abi])]
(contract-xhrio name
code-type
version
[:district0x/smart-contract-loaded key code-type]
[:district0x.log/error :district0x/load-smart-contracts]))))}))
(reg-event-fx
:district0x/clear-smart-contracts
interceptors
(fn [{:keys [db]}]
{:db (update db :smart-contracts (partial medley/map-kv
(fn [contract-key contract]
[contract-key (dissoc contract :abi :bin :address :instance)])))}))
(reg-event-fx
:district0x/smart-contract-loaded
interceptors
(fn [{:keys [db]} [contract-key code-type code]]
(let [code (if (= code-type :abi) (clj->js code) (str "0x" code))
contract (get-contract db contract-key)
contract-address (:address contract)]
(let [new-db (cond-> db
true
(assoc-in [:smart-contracts contract-key code-type] code)
(= code-type :abi)
(update-in [:smart-contracts contract-key] merge
(when contract-address
{:instance (web3-eth/contract-at (:web3 db) code contract-address)})))]
(merge
{:db new-db
:district0x/dispatch-n [(when (all-contracts-loaded? new-db)
[:district0x/smart-contracts-loaded])]})))))
(reg-empty-event-fx :district0x/smart-contracts-loaded)
(reg-event-fx
:district0x/my-addresses-loaded
[interceptors (inject-cofx :localstorage)]
(fn [{:keys [db localstorage]} [addresses]]
(let [active-address (if (contains? (set addresses) (:active-address localstorage))
(:active-address localstorage)
(first addresses))]
{:db (-> db
(assoc :my-addresses addresses)
(assoc :active-address active-address))})))
(reg-event-fx
:district0x/deploy-contract
interceptors
(fn [{:keys [db]} [{:keys [:address-index :contract-key :on-success :args :gas] :as params
:or {gas 4500000}}]]
(let [contract (get-contract db contract-key)
tx-opts {:gas gas
:data (:bin contract)
:from (if address-index
(nth (:my-addresses db) address-index)
(:active-address db))}]
{:web3-fx.blockchain/fns
{:web3 (:web3 db)
:fns [{:f web3-eth/contract-new
:args (concat [(:abi contract)] args [tx-opts])
:on-success [:district0x/contract-deployed (select-keys params [:contract-key :on-success])]
:on-error [:district0x.log/error :district0x/deploy-contract contract-key]}]}})))
(reg-event-fx
:district0x/contract-deployed
[interceptors (inject-cofx :localstorage)]
(fn [{:keys [db localstorage]} [{:keys [:on-success :contract-key]} instance]]
(when-let [contract-address (aget instance "address")]
(console :log contract-key " deployed at " contract-address)
(merge
{:db (update-in db [:smart-contracts contract-key] merge {:address contract-address :instance instance})
:localstorage (assoc-in localstorage [:smart-contracts contract-key] {:address contract-address})}
(when on-success
{:dispatch on-success})))))
(reg-event-fx
:district0x/watch-eth-balances
interceptors
(fn [{:keys [db]} [{:keys [:addresses :on-address-balance-loaded]
:or {on-address-balance-loaded [:district0x/address-balance-loaded :eth]}}]]
(when (seq addresses)
{:web3-fx.blockchain/balances
{:web3 (:web3 db)
:watch? true
:blockchain-filter-opts "latest"
:db-path [:district0x/watch-eth-balances]
:addresses addresses
:dispatches [on-address-balance-loaded
[:district0x/blockchain-connection-error :district0x/watch-eth-balances]]}})))
(reg-event-fx
:district0x/load-eth-balances
interceptors
(fn [{:keys [db]} [{:keys [:addresses :on-address-balance-loaded]
:or {on-address-balance-loaded [:district0x/address-balance-loaded :eth]}}]]
(when (seq addresses)
{:web3-fx.blockchain/balances
{:web3 (:web3 db)
:addresses addresses
:dispatches [on-address-balance-loaded
[:district0x/blockchain-connection-error :district0x/watch-token-balances]]}})))
(reg-event-fx
:district0x/watch-my-eth-balances
interceptors
(fn [{:keys [db]} [args]]
(let [addresses (:my-addresses db)]
{:dispatch [:district0x/watch-eth-balances (assoc args :addresses addresses)]})))
(reg-event-fx
:district0x/watch-token-balances
interceptors
(fn [{:keys [db]} [{:keys [:addresses :instance :token-code]}]]
(when (seq addresses)
{:web3-fx.blockchain/balances
{:web3 (:web3 db)
:watch? true
:blockchain-filter-opts "latest"
:db-path [:district0x/watch-token-balances]
:addresses addresses
:instance instance
:dispatches [[:district0x/address-balance-loaded token-code]
[:district0x/blockchain-connection-error :district0x/watch-token-balances]]}})))
(reg-event-fx
:district0x/load-token-balances
interceptors
(fn [{:keys [db]} [{:keys [:addresses :instance :token-code]}]]
(when (seq addresses)
{:web3-fx.blockchain/balances
{:web3 (:web3 db)
:addresses addresses
:instance instance
:dispatches [[:district0x/address-balance-loaded token-code]
[:district0x/blockchain-connection-error :district0x/watch-token-balances]]}})))
(reg-event-fx
:district0x/watch-my-token-balances
interceptors
(fn [{:keys [db]} [args]]
(let [addresses (:my-addresses db)]
{:dispatch [:district0x/watch-token-balances (assoc args :addresses addresses)]})))
(reg-event-fx
:district0x/address-balance-loaded
interceptors
(fn [{:keys [db]} [token balance address :as a]]
{:db (assoc-in db [:balances address token] (d0x-shared-utils/big-num->ether balance))}))
(reg-event-fx
:district0x/load-conversion-rates
interceptors
(fn [{:keys [db]} [currencies]]
{:http-xhrio {:method :get
:uri (str "https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms="
(string/join "," (map name currencies)))
:timeout 20000
:response-format (ajax/json-response-format {:keywords? true})
:on-success [:district0x/conversion-rates-loaded]
:on-failure [:district0x.log/error :district0x/conversion-rates-loaded]}}))
(reg-event-db
:district0x/conversion-rates-loaded
interceptors
(fn [db [response]]
(update db :conversion-rates merge response)))
(reg-event-fx
:district0x/set-active-address
[interceptors (inject-cofx :localstorage)]
(fn [{:keys [db localstorage]} [address]]
{:db (-> db
(assoc :active-address address))
:localstorage (assoc localstorage :active-address address)}))
(reg-event-fx
:district0x/make-transaction
[interceptors (validate-first-arg (s/keys :req-un [:transaction/form-data
:transaction/tx-opts
:transaction/contract-key
:transaction/name]
:opt-un [:transaction/args-order
:transaction/contract-address
:transaction/wei-keys
:transaction/form-id
:transaction/result-href]))]
(fn [{:keys [db]} [{:keys [:form-data :form-id :args-order :contract-key :wei-keys
:contract-key :contract-method :contract-address :on-success] :as props}]]
(let [{:keys [:web3 :active-address]} db
props (update props :tx-opts (partial merge {:from active-address}))]
{:web3-fx.contract/state-fns
{:web3 web3
:db-path [:contract/state-fns]
:fns [{:instance (if contract-address
(get-instance db contract-key contract-address)
(get-instance db contract-key))
:method contract-method
:args (-> (d0x-shared-utils/update-multi form-data wei-keys d0x-shared-utils/eth->wei)
(d0x-shared-utils/map->vec args-order))
:tx-opts (:tx-opts props)
:on-success [:district0x/dispatch-n [[:district0x/load-transaction]
[:district0x.transactions/add props]
(when on-success on-success)]]
:on-error [:district0x.log/error :district0x/make-transaction props]
:on-tx-receipt [:district0x/on-tx-receipt props]}]}})))
(reg-event-fx
:district0x/on-tx-receipt
interceptors
(fn [{:keys [:db]} [{:keys [:on-tx-receipt :on-tx-receipt-n :form-data :on-error]
:or {on-error [:district0x.snackbar/show-transaction-error]}}
{:keys [:transaction-hash :gas-used] :as tx-receipt}]]
(let [success? (not= (get-in db [:transaction-log :transactions transaction-hash :gas]) gas-used)]
(merge
{:district0x/dispatch-n [[:district0x/transaction-receipt-loaded tx-receipt]
;; MetaMask can load transaction only at receipt time, not earlier
[:district0x/load-transaction transaction-hash]]}
(when (and success? on-tx-receipt)
{:dispatch (vec (concat on-tx-receipt [form-data tx-receipt]))})
(when (and success? on-tx-receipt-n)
{:dispatch-n (map #(vec (concat % [form-data tx-receipt])) on-tx-receipt-n)})
(when (and (not success?) on-error)
{:dispatch (vec (concat on-error [form-data tx-receipt]))})))))
(reg-event-fx
:district0x/load-transaction
[interceptors]
(fn [{:keys [:db]} [transaction-hash & a]]
{:web3-fx.blockchain/fns
{:web3 (:web3 db)
:fns [{:f web3-eth/get-transaction
:args [transaction-hash]
:on-success [:district0x/transaction-loaded transaction-hash]
:on-error [:district0x.log/error :district0x/load-transaction transaction-hash]}]}}))
(reg-event-fx
:district0x/transaction-loaded
[interceptors]
(fn [{:keys [:db]} [transaction-hash {:keys [:gas] :as transaction}]]
(let [gas-used (get-in db [:transaction-log :transactions transaction-hash :gas-used])
transaction (-> transaction
(assoc :status (if (and gas gas-used)
(if (= gas-used gas)
:tx.status/failure
:tx.status/success)
:tx.status/pending))
(assoc :hash transaction-hash))]
{:dispatch [:district0x.transactions/update transaction-hash transaction]})))
(reg-event-fx
:district0x/load-transaction-receipt
[interceptors]
(fn [{:keys [:db]} [transaction-hash]]
{:web3-fx.blockchain/fns
{:web3 (:web3 db)
:fns [{:f web3-eth/get-transaction-receipt
:args [transaction-hash]
:on-success [:district0x/transaction-receipt-loaded]
:on-error [:district0x.log/error :district0x/load-transaction-receipt transaction-hash]}]}}))
(reg-event-fx
:district0x/transaction-receipt-loaded
[interceptors]
(fn [{:keys [:db]} [{:keys [:gas-used :transaction-hash] :as tx-receipt}]]
(let [gas-limit (get-in db [:transaction-log :transactions transaction-hash :gas])
tx-receipt (assoc tx-receipt :status (if gas-limit
(if (= gas-limit gas-used)
:tx.status/failure
:tx.status/success)
:tx.status/not-loaded))]
{:dispatch [:district0x.transactions/update transaction-hash tx-receipt]})))
(reg-event-fx
:district0x.transactions/add
[interceptors (inject-cofx :localstorage)]
(fn [{:keys [:db :localstorage]} [{:keys [:tx-opts :form-id :contract-key :contract-method] :as props}
tx-hash]]
(let [tx-data (merge (select-keys props [:contract-method
:contract-key
:tx-opts
:name
:result-href])
{:created-on (time-coerce/to-long (t/now))
:hash tx-hash
:status :tx.status/not-loaded})
new-db (-> db
(assoc-in [:transaction-log :transactions tx-hash] tx-data)
(update-in [:transaction-log :ids-chronological] conj tx-hash)
(update-in (remove nil? [:transaction-log :ids-by-form contract-key contract-method (:from tx-opts) form-id])
conj
tx-hash))]
{:db new-db
:localstorage (merge localstorage (select-keys new-db [:transaction-log]))
:dispatch [:district0x.transaction-log/set-open true tx-hash]})))
(reg-event-fx
:district0x.transactions/update
[interceptors (inject-cofx :localstorage)]
(fn [{:keys [:db :localstorage]} [transaction-hash transaction]]
(let [new-db (update-in db
[:transaction-log :transactions transaction-hash]
merge
(cond-> transaction
true (select-keys [:block-hash :gas-used :gas :value :status])
(:value transaction) (update :value bn/->number)))]
(let [{:keys [:gas :gas-used :form-data :contract-key :contract-method]}
(get-in new-db [:transaction-log :transactions transaction-hash])]
(when (and gas gas-used)
(console :log
(keyword contract-key contract-method)
"gas-limit:" gas
"gas-used:" gas-used
(str (int (* (/ gas-used gas) 100)) "%")
form-data)))
(merge
{:db new-db
:localstorage (merge localstorage (select-keys new-db [:transaction-log]))}))))
(reg-event-fx
:district0x.transactions/clear
[interceptors (inject-cofx :localstorage)]
(fn [{:keys [:db :localstorage]}]
(let [cleared-txs (-> (select-keys district0x.ui.db/default-db [:transaction-log])
(update :transaction-log dissoc :settings))]
{:db (merge db cleared-txs)
:localstorage (merge localstorage cleared-txs)})))
(reg-event-fx
:district0x.transaction-log.settings/set
[interceptors (inject-cofx :localstorage)]
(fn [{:keys [:db :localstorage]} [key value]]
(let [new-db (assoc-in db [:transaction-log :settings key] value)]
{:db new-db
:localstorage (merge localstorage (select-keys new-db [:transaction-log]))})))
(reg-event-fx
:district0x.transaction-log/set-open
[interceptors (inject-cofx :localstorage)]
(fn [{:keys [:db]} [open? highlighted-tx]]
{:db (update-in db [:transaction-log :settings] merge {:open? open?
:highlighted-transaction highlighted-tx})}))
(reg-event-fx
:district0x/load-transaction-and-receipt
[interceptors]
(fn [{:keys [:db]} [transaction-hash]]
{:dispatch-n [[:district0x/load-transaction transaction-hash]
[:district0x/load-transaction-receipt transaction-hash]]}))
(reg-event-fx
:district0x-emails/set-email
[interceptors (validate-first-arg (s/keys :req [:district0x-emails/email] :opt [:district0x-emails/address]))]
(fn [{:keys [:db]} [form-data submit-props]]
(let [form-data (if-not (:district0x-emails/address form-data)
(assoc form-data :district0x-emails/address (:active-address db))
form-data)]
{:dispatch [:district0x/make-transaction
(merge
{:name (gstring/format "Set email %s" (:district0x-emails/email form-data))
:contract-key :district0x-emails
:contract-method :set-email
:form-data form-data
:args-order [:district0x-emails/email]
:form-id (select-keys form-data [:district0x-emails/address])
:tx-opts {:gas 100000 :gas-price 4000000000 :from (:district0x-emails/address form-data)}}
submit-props)]})))
(reg-event-fx
:district0x-emails/load
interceptors
(fn [{:keys [:db]} [address]]
(when address
(let [instance (get-instance db :district0x-emails)]
{:web3-fx.contract/constant-fns
{:fns [{:instance instance
:method :emails
:args [address]
:on-success [:district0x-emails/loaded address]
:on-error [:district0x.log/error]}]}}))))
(reg-event-fx
:district0x-emails/loaded
interceptors
(fn [{:keys [:db]} [address email]]
{:db (assoc-in db [:district0x-emails address] email)}))
(reg-event-fx
:district0x.contract/event-watch-once
interceptors
(fn [{:keys [:db]} [{:keys [:contract-key :on-success :on-error] :as event-params
:or {on-error [:district0x.log/error]}}]]
(let [event-id (str "event-listen-once-" (rand-int 99999))
db-path [:web3-event-filters]]
{:web3-fx.contract/events
{:db-path db-path
:events [(merge event-params
{:instance (get-instance db contract-key)
:event-id event-id
:on-success [:district0x.contract/event-stop-watching-once db-path event-id on-success]
:on-error [:district0x.contract/event-stop-watching-once db-path event-id on-error]})]}})))
(reg-event-fx
:district0x.contract/event-stop-watching-once
interceptors
(fn [{:keys [:db]} [db-path event-id dispatch & args]]
{:web3-fx.contract/events-stop-watching {:db-path db-path
:event-ids [event-id]}
:dispatch (vec (concat (d0x-shared-utils/collify dispatch) args))}))
(reg-event-fx
:district0x.server/http-get
interceptors
(fn [{:keys [db]} [{:keys [:http-xhrio :params :on-success :on-failure :endpoint]
:or {on-failure [:district0x.log/error]}} :as opts]]
{:http-xhrio (merge
{:method :get
:timeout 20000
:uri (str (url/url (:server-url db) endpoint))
:response-format (ajax/json-response-format {:keywords? true})
:on-success on-success
:on-failure [:district0x.log/error]
:params params}
http-xhrio)}))
(reg-event-fx
:district0x.search-results/load
interceptors
(fn [{:keys [db]} [{:keys [:http-xhrio :params :search-results-path :append? :endpoint :on-success
:id-key]
:as opts}]]
{:db (update-in db search-results-path merge (merge {:loading? true}
(when-not append?
{:ids []})))
:dispatch [:district0x.server/http-get {:http-xhrio http-xhrio
:endpoint endpoint
:params params
:on-success [:district0x.search-results/loaded opts]}]}))
(reg-event-fx
:district0x.search-results/loaded
interceptors
(fn [{:keys [db]} [{:keys [:search-results-path :params :id-key :on-success :append?]}
{:keys [:items :total-count] :as response}]]
(let [ids (if id-key (map #(get % id-key)) items)
existing-ids (if append?
(get-in db (concat search-results-path [:ids]))
[])]
(merge
{:db (update-in db search-results-path merge {:loading? false
:ids (concat existing-ids ids)
:total-count total-count})}
(when on-success
{:dispatch (vec (concat on-success [ids items response]))})))))
(reg-event-fx
:district0x.contract/constant-fn-call
interceptors
(fn [{:keys [db]} [contract-key & args]]
{:web3-fx.contract/constant-fns
{:fns [(concat [(get-instance db contract-key)] args [[:district0x.log/info]
[:district0x.log/error]])]}}))
(reg-event-fx
:district0x/clear-localstorage
interceptors
(fn [_]
{:localstorage nil}))
(reg-event-fx
:district0x.log/print-db
interceptors
(fn [{:keys [:db]}]
(print.foo/look db)
nil))
(reg-event-fx
:district0x.log/error
interceptors
(fn [{:keys [:db]} errors]
(apply console :error errors)
{:db db
:ga/event ["error" (first errors) (str (rest errors))]}))
(reg-event-fx
:district0x.log/info
interceptors
(fn [db result]
(apply console :log (bn/->numbers (if (and (not (string? result)) (some sequential? result))
(map bn/->numbers result)
result)))))
(reg-event-db
:district0x.snackbar/close
interceptors
(fn [db _]
(assoc-in db [:snackbar :open?] false)))
(reg-event-fx
:district0x.snackbar/show-message
interceptors
(fn [{:keys [db]} [message]]
{:db (update db :snackbar merge
{:open? true
:message message
:action nil
:on-action-touch-tap nil})}))
(reg-event-fx
:district0x.snackbar/show-transaction-error
interceptors
(fn [{:keys [db]}]
{:dispatch [:district0x.snackbar/show-message "Sorry, your transaction hasn't been processed"]}))
(reg-event-fx
:district0x.snackbar/show-message-redirect-action
interceptors
(fn [{:keys [db]} [{:keys [:message :route :route-params :routes :action]}]]
{:db (update db :snackbar merge
{:open? true
:message message
:action (or action "SHOW ME")
:on-action-touch-tap #(dispatch [:district0x.location/nav-to route route-params routes])})}))
(reg-event-db
:district0x.menu-drawer/set
interceptors
(fn [db [open?]]
(assoc-in db [:menu-drawer :open?] open?)))
(reg-event-fx
:district0x/blockchain-connection-error
interceptors
(fn [{:keys [:db]} errors]
(apply console :error "Blockchain Connection Error:" errors)
{:db (assoc db :blockchain-connection-error? true)
:dispatch [:district0x.snackbar/show-message "Sorry, we have trouble connecting into the Ethereum blockchain"]}))
(reg-event-fx
:district0x/unlock-account
interceptors
(fn [{:keys [db]} [address password seconds]]
{:web3-fx.blockchain/fns
{:web3 (:web3 db)
:fns [[web3-personal/unlock-account address password (or seconds 999999)
[:district0x.log/info]
[:district0x.log/error :blockchain/unlock-account]]]}}))
(reg-event-fx
:district0x.location/set-query
interceptors
(fn [_ args]
{:location/set-query args}))
(reg-event-fx
:district0x.location/nav-to
interceptors
(fn [{:keys [:db]} [route route-params routes]]
{:location/nav-to [route route-params routes]}))
(reg-event-fx
:district0x.location/add-to-query
interceptors
(fn [_ [query-params]]
{:location/add-to-query [query-params]}))
(reg-event-fx
:district0x.window/resized
interceptors
(fn [{:keys [db]} [width]]
{:db (assoc db :window-width-size (d0x-ui-utils/get-window-width-size width))}))
(reg-event-fx
:district0x.window/scroll-to-top
interceptors
(fn []
{:window/scroll-to-top true}))
(reg-event-fx
:district0x/set-ui-disabled
interceptors
(fn [{:keys [:db]} [disabled?]]
{:db (assoc db :ui-disabled? disabled?)}))
(reg-event-fx
:district0x/async-flow
interceptors
(fn [_ [params]]
{:async-flow params}))
(reg-event-fx
:district0x/dispatch-n
interceptors
(fn [_ [events & args]]
{:dispatch-n (mapv #(vec (concat % args)) (remove nil? events))}))