-
Notifications
You must be signed in to change notification settings - Fork 985
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refactoring] remove web3, clean up wallet effects
- introduce json-rpc namespace, which provides `call` and `eth-call`, a generic way of calling a json-rpc method taking care of conversions and error handling - remove web3 usage from wallet - clean up effects, reducing the amount of computations when login in
- Loading branch information
yenda
committed
May 20, 2019
1 parent
1cd5d69
commit 1b6419b
Showing
25 changed files
with
501 additions
and
716 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
(ns status-im.ethereum.contracts | ||
(:require [re-frame.core :as re-frame] | ||
[status-im.utils.ethereum.abi-spec :as abi-spec] | ||
[status-im.utils.ethereum.core :as ethereum] | ||
[status-im.utils.fx :as fx] | ||
[status-im.utils.money :as money] | ||
[status-im.wallet.core :as wallet])) | ||
|
||
(def contracts | ||
{:status/tribute-to-talk | ||
{:address | ||
{:mainnet nil | ||
:testnet "0x3da3fc53e24707f36c5b4433b442e896c4955f0e" | ||
:rinkeby nil} | ||
:methods | ||
{:get-manifest | ||
{:signature "getManifest(address)" | ||
:outputs ["bytes"]} | ||
:set-manifest | ||
{:signature "setManifest(bytes)" | ||
:write? true}}}}) | ||
|
||
(re-frame/reg-fx | ||
::call | ||
(fn [{:keys [address data callback]}] | ||
(ethereum/call {:to address | ||
:data data} | ||
callback))) | ||
|
||
(defn get-contract-address | ||
[db contract] | ||
(let [chain-keyword (-> (get-in db [:account/account :networks (:network db)]) | ||
ethereum/network->chain-keyword)] | ||
(get-in contracts [contract :address chain-keyword]))) | ||
|
||
(fx/defn call | ||
[{:keys [db] :as cofx} | ||
{:keys [contract contract-address method params | ||
callback on-result on-error details]}] | ||
(when-let [contract-address (or contract-address | ||
(get-contract-address db contract))] | ||
(let [{:keys [signature outputs write?]} | ||
(get-in contracts [contract :methods method]) | ||
data (abi-spec/encode signature params)] | ||
(if write? | ||
(wallet/open-sign-transaction-flow | ||
cofx | ||
(merge {:to contract-address | ||
:data data | ||
:id "approve" | ||
:symbol :ETH | ||
:method "eth_sendTransaction" | ||
:amount (money/bignumber 0) | ||
:on-result on-result | ||
:on-error on-error} | ||
details)) | ||
{::call {:address contract-address | ||
:data data | ||
:callback #(callback (if (empty? outputs) | ||
% | ||
(abi-spec/decode % outputs)))}})))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
(ns status-im.ethereum.json-rpc | ||
(:require [clojure.string :as string] | ||
[re-frame.core :as re-frame] | ||
[status-im.ethereum.decode :as decode] | ||
[status-im.native-module.core :as status] | ||
[status-im.utils.ethereum.abi-spec :as abi-spec] | ||
[status-im.utils.money :as money] | ||
[status-im.utils.types :as types] | ||
[taoensso.timbre :as log])) | ||
|
||
(def json-rpc-api | ||
{"eth_call" {} | ||
"eth_getBalance" | ||
{:on-result money/bignumber} | ||
"eth_estimateGas" | ||
{:on-result money/bignumber} | ||
"eth_gasPrice" | ||
{:on-result money/bignumber} | ||
"eth_getBlockByHash" | ||
{:on-result #(-> (update % :number decode/uint) | ||
(update :timestamp decode/uint))} | ||
"eth_getTransactionByHash" {} | ||
"eth_getTransactionReceipt" {} | ||
"eth_newBlockFilter" {:subscription? true} | ||
"eth_newFilter" {:subscription? true}}) | ||
|
||
(defn call | ||
[{:keys [method params on-success on-error]}] | ||
(when-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 | ||
#(log/error :json-rpc/error method :params params :error %))] | ||
(if (nil? method) | ||
(log/error :json-rpc/method-not-found method) | ||
(status/call-private-rpc | ||
(types/clj->json {:jsonrpc "2.0" | ||
:id id | ||
:method (if subscription? | ||
"eth_subscribeSignal" | ||
method) | ||
:params (if subscription? | ||
[method params] | ||
params)}) | ||
(fn [response] | ||
(if (string/blank? response) | ||
(on-error {:message "Blank response"}) | ||
(let [{:keys [error result] :as response2} (types/json->clj response)] | ||
(if error | ||
(on-error error) | ||
(if subscription? | ||
(re-frame/dispatch | ||
[:ethereum.callback/subscription-success | ||
result on-success]) | ||
(on-success (on-result result)))))))))))) | ||
|
||
(defn eth-call | ||
[{:keys [contract method params outputs on-success on-error block] | ||
:or {block "latest" | ||
params []}}] | ||
(call {:method "eth_call" | ||
:params [{:to contract | ||
:data (abi-spec/encode method params)} | ||
(if (int? block) | ||
(abi-spec/number-to-hex block) | ||
block)] | ||
:on-success | ||
(if outputs | ||
#(on-success (abi-spec/decode % outputs)) | ||
on-success) | ||
:on-error | ||
on-error})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.