Skip to content

Commit

Permalink
[#9886] App shows 'buy' for stickerpack user owns when pressing on st…
Browse files Browse the repository at this point in the history
…icker in chat (Infura - related?)
  • Loading branch information
flexsurfer committed Feb 4, 2020
1 parent 6ad84f4 commit f78d0a4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 48 deletions.
21 changes: 14 additions & 7 deletions src/status_im/ethereum/json_rpc.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,20 @@
"mailservers_getChatRequestRanges" {}
"mailservers_deleteChatRequestRange" {}})

(defn on-error-retry [method {:keys [number-of-retries on-error] :as arg}]
(if (pos? number-of-retries)
(fn []
(method (update arg :number-of-retries dec)))
on-error))

(defn call
[{:keys [method params on-success on-error] :as p}]
[{:keys [method params on-success] :as arg}]
(if-let [method-options (json-rpc-api method)]
(let [{:keys [id on-result subscription?]
:or {on-result identity
id 1
params []}} method-options
on-error (or on-error
on-error (or (on-error-retry call arg)
#(log/warn :json-rpc/error method :error % :params params))]
(if (nil? method)
(log/error :json-rpc/method-not-found method)
Expand All @@ -137,7 +143,7 @@
(fn [response]
(if (string/blank? response)
(on-error {:message "Blank response"})
(let [{:keys [error result] :as response2} (types/json->clj response)]
(let [{:keys [error result]} (types/json->clj response)]
(if error
(on-error error)
(if subscription?
Expand All @@ -146,12 +152,13 @@
result on-success])
(on-success (on-result result))))))))))

(log/warn "method" method "not found" p)))
(log/warn "method" method "not found" arg)))

(defn eth-call
[{:keys [contract method params outputs on-success on-error block]
[{:keys [contract method params outputs on-success block]
:or {block "latest"
params []}}]
params []}
:as arg}]
(call {:method "eth_call"
:params [{:to contract
:data (abi-spec/encode method params)}
Expand All @@ -163,7 +170,7 @@
#(on-success (abi-spec/decode % outputs))
on-success)
:on-error
on-error}))
(on-error-retry eth-call arg)}))

;; effects
(re-frame/reg-fx
Expand Down
5 changes: 0 additions & 5 deletions src/status_im/events.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -1353,11 +1353,6 @@
(fn [cofx [_ id price]]
(stickers/approve-pack cofx id price)))

(handlers/register-handler-fx
:stickers/get-owned-packs
(fn [cofx _]
(stickers/get-owned-pack cofx)))

(handlers/register-handler-fx
:stickers/pack-owned
(fn [cofx [_ id]]
Expand Down
18 changes: 8 additions & 10 deletions src/status_im/stickers/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
:method "balanceOf(address)"
:params [address]
:outputs ["uint256"]
:number-of-retries 3
:on-success
(fn [[count]]
(dotimes [id count]
Expand All @@ -73,6 +74,7 @@
:method "tokenOfOwnerByIndex(address,uint256)"
:params [address id]
:outputs ["uint256"]
:number-of-retries 3
:on-success
(fn [[token-id]]
(json-rpc/eth-call
Expand All @@ -81,6 +83,7 @@
:method "tokenPackId(uint256)"
:params [token-id]
:outputs ["uint256"]
:number-of-retries 3
:on-success
(fn [[pack-id]]
(re-frame/dispatch [:stickers/pack-owned pack-id]))}))})))})))
Expand Down Expand Up @@ -123,11 +126,13 @@
(when (and id (string/starts-with? current-network "mainnet"))
(let [pack (or (get packs-installed id)
(get packs id))
contract-address (contracts/get-address db :status/stickers)]
contract-address (contracts/get-address db :status/stickers)
address (ethereum/default-address db)]
(fx/merge cofx
(navigation/navigate-to-cofx :stickers-pack-modal {:id id})
#(when (and contract-address (not pack))
{:stickers/pack-data-fx [contract-address id]})))))
{:stickers/pack-data-fx [contract-address id]
:stickers/owned-packs-fx [contract-address address]})))))

(fx/defn load-pack
[cofx url id price]
Expand Down Expand Up @@ -182,11 +187,4 @@
:stickers/set-pending-timout-fx nil})))))

(fx/defn pack-owned [{db :db} id]
{:db (update db :stickers/packs-owned conj id)})

(fx/defn get-owned-pack
[{:keys [db]}]
(let [contract (contracts/get-address db :status/sticker-pack)
address (ethereum/default-address db)]
(when contract
{:stickers/owned-packs-fx [contract address]})))
{:db (update db :stickers/packs-owned conj id)})
37 changes: 11 additions & 26 deletions src/status_im/wallet/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,13 @@
[status-im.ui.components.bottom-sheet.core :as bottom-sheet]))

(defn get-balance
[{:keys [address on-success on-error number-of-retries]
:as params
:or {number-of-retries 4}}]
(log/debug "[wallet] get-balance"
"address" address
"number-of-retries" number-of-retries)
[{:keys [address on-success on-error]}]
(json-rpc/call
{:method "eth_getBalance"
:params [address "latest"]
:on-success on-success
:on-error (fn [error]
(if (pos? number-of-retries)
(get-balance
(update params :number-of-retries dec))
(on-error error)))}))
{:method "eth_getBalance"
:params [address "latest"]
:on-success on-success
:number-of-retries 4
:on-error on-error}))

(re-frame/reg-fx
:wallet/get-balances
Expand Down Expand Up @@ -183,15 +175,11 @@
balances)))

(defn get-token-balances
[{:keys [addresses tokens init? assets number-of-retries]
:as params
:or {number-of-retries 4}}]
(log/debug "[wallet] get-token-balances"
"addresses" addresses
"number-of-retries" number-of-retries)
[{:keys [addresses tokens init? assets]}]
(json-rpc/call
{:method "wallet_getTokensBalances"
:params [addresses (keys tokens)]
{:method "wallet_getTokensBalances"
:params [addresses (keys tokens)]
:number-of-retries 4
:on-success
(fn [results]
(when-let [balances (clean-up-results results tokens (if init? nil assets))]
Expand All @@ -201,10 +189,7 @@
[::tokens-found balances]
[::update-tokens-balances-success balances]))))
:on-error
(fn [error]
(if (pos? number-of-retries)
(get-token-balances (update params :number-of-retries dec))
(re-frame/dispatch [::update-token-balance-fail error])))}))
#(re-frame/dispatch [::update-token-balance-fail %])}))

(re-frame/reg-fx
:wallet/get-tokens-balances
Expand Down

0 comments on commit f78d0a4

Please sign in to comment.