From 6e3784d67168eeda44842222e1b7faa24f657632 Mon Sep 17 00:00:00 2001 From: Andrey Shovkoplyas Date: Tue, 4 Feb 2020 13:48:52 +0100 Subject: [PATCH] [#9886] App shows 'buy' for stickerpack user owns when pressing on sticker in chat Signed-off-by: Andrey Shovkoplyas --- src/status_im/ethereum/json_rpc.cljs | 22 +++++++++++------ src/status_im/events.cljs | 5 ---- src/status_im/stickers/core.cljs | 19 +++++++------- src/status_im/wallet/core.cljs | 37 +++++++++------------------- 4 files changed, 35 insertions(+), 48 deletions(-) diff --git a/src/status_im/ethereum/json_rpc.cljs b/src/status_im/ethereum/json_rpc.cljs index 137a150310..0eabb58562 100644 --- a/src/status_im/ethereum/json_rpc.cljs +++ b/src/status_im/ethereum/json_rpc.cljs @@ -168,14 +168,21 @@ "mailservers_getChatRequestRanges" {} "mailservers_deleteChatRequestRange" {}}) +(defn on-error-retry [call-method {:keys [method number-of-retries on-error] :as arg}] + (if (pos? number-of-retries) + (fn [] + (log/debug "[on-error-retry]" method "number-of-retries" number-of-retries) + (call-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) @@ -191,7 +198,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? @@ -200,12 +207,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)} @@ -217,7 +225,7 @@ #(on-success (abi-spec/decode % outputs)) on-success) :on-error - on-error})) + (on-error-retry eth-call arg)})) ;; effects (re-frame/reg-fx diff --git a/src/status_im/events.cljs b/src/status_im/events.cljs index e47cf7c6a0..c13b3283c4 100644 --- a/src/status_im/events.cljs +++ b/src/status_im/events.cljs @@ -1009,11 +1009,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]] diff --git a/src/status_im/stickers/core.cljs b/src/status_im/stickers/core.cljs index c66e3c085e..5c9fa8fa8c 100644 --- a/src/status_im/stickers/core.cljs +++ b/src/status_im/stickers/core.cljs @@ -64,6 +64,7 @@ :method "balanceOf(address)" :params [address] :outputs ["uint256"] + :number-of-retries 3 :on-success (fn [[count]] (dotimes [id count] @@ -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 @@ -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]))}))})))}))) @@ -127,11 +130,14 @@ (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) + pack-contract (contracts/get-address db :status/sticker-pack) + 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 [pack-contract address]}))))) (fx/defn load-pack [cofx url id price] @@ -196,11 +202,4 @@ {}))))) (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)}) \ No newline at end of file diff --git a/src/status_im/wallet/core.cljs b/src/status_im/wallet/core.cljs index 181e52ff6b..85082ebb0f 100644 --- a/src/status_im/wallet/core.cljs +++ b/src/status_im/wallet/core.cljs @@ -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 @@ -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))] @@ -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