-
Notifications
You must be signed in to change notification settings - Fork 982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[refactoring] remove web3, clean up wallet effects #8232
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be good to have something like
(status/call-private-rpc "name" params)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is what I made initially but it turns out you always have different options so I figured it is better to have this generic function taking a map.