[feature] contracts namespace

* `open-sign-transaction-flow` cleaner version of
`open-modal-wallet-for-transaction`
* The contract namespace provides the `call` effect:
- takes contract identifier from contracts, method, params and callback
- select the correct contract address based on current network
- encode params, decodes results
- start wallet transaction flow for write calls

Signed-off-by: yenda <eric@status.im>
This commit is contained in:
yenda 2019-03-29 17:37:52 +01:00
parent 4ccb1ea52d
commit e690dbdf89
No known key found for this signature in database
GPG Key ID: 0095623C0069DCE6
2 changed files with 93 additions and 6 deletions

View File

@ -2,16 +2,15 @@
(:require [clojure.set :as set]
[status-im.constants :as constants]
[status-im.i18n :as i18n]
[status-im.transport.utils :as transport.utils]
[status-im.ui.screens.navigation :as navigation]
[status-im.ui.screens.wallet.utils :as wallet.utils]
[status-im.utils.config :as config]
[status-im.utils.core :as utils.core]
[status-im.utils.ethereum.core :as ethereum]
[status-im.utils.ethereum.tokens :as tokens]
[status-im.utils.hex :as utils.hex]
[status-im.utils.core :as utils.core]
[status-im.utils.money :as money]
[status-im.utils.fx :as fx]
[status-im.ui.screens.wallet.utils :as wallet.utils]
[status-im.utils.config :as config]))
[status-im.utils.hex :as utils.hex]
[status-im.utils.money :as money]))
(def min-gas-price-wei (money/bignumber 1))
@ -249,3 +248,28 @@
(if wallet-set-up-passed?
:wallet-send-modal-stack
:wallet-send-modal-stack-with-onboarding)]]}))
(fx/defn open-sign-transaction-flow
[{:keys [db] :as cofx} {:keys [gas gas-price] :as transaction}]
(let [go-to-view-id (if (get-in db [:account/account :wallet-set-up-passed?])
:wallet-send-modal-stack
:wallet-send-modal-stack-with-onboarding)]
(fx/merge cofx
(cond-> {:db (-> db
(assoc-in [:wallet :send-transaction]
transaction)
(assoc-in [:wallet :send-transaction :original-gas]
gas))}
(not gas)
(assoc :update-estimated-gas
{:web3 (:web3 db)
:obj (select-keys transaction [:to :data])
:success-event :wallet/update-estimated-gas-success})
(not gas-price)
(assoc :update-gas-price
{:web3 (:web3 db)
:success-event :wallet/update-gas-price-success
:edit? false}))
(update-wallet)
(navigation/navigate-to-cofx go-to-view-id {}))))

View File

@ -0,0 +1,63 @@
(ns status-im.utils.ethereum.contracts
(:require [re-frame.core :as re-frame]
[status-im.models.wallet :as wallet]
[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]))
(def contracts
{:status/tribute-to-talk
{:address
{:mainnet nil
:testnet "0x3da3fc53e24707f36c5b4433b442e896c4955f0e"
:rinkeby nil}
:methods
{:get-manifest
{:signature "getManifest(address)"
:return-params ["bytes"]}
:set-manifest
{:signature "setManifest(bytes)"
:write? true}}}
:status/sticker-market
{:address
{:mainnet nil
:testnet "0x39d16CdB56b5a6a89e1A397A13Fe48034694316E"
:rinkeby nil}
:methods
{:pack-count
{:signature "packCount()"
:return-params ["uint256"]}
:pack-data
{:signature "getPackData(uint256)"
:return-params ["bytes4[]" "address" "bool" "uint256" "uint256" "bytes"]}}}})
(re-frame/reg-fx
::call
(fn [[address data callback]]
(ethereum/call {:to address
:data data}
callback)))
(fx/defn call
[{:keys [db] :as cofx} {:keys [contract method params callback on-result]}]
(let [chain-keyword (-> (get-in db [:account/account :networks (:network db)])
ethereum/network->chain-keyword)
contract-address (get-in contracts [contract :address chain-keyword])]
(when contract-address
(let [{:keys [signature return-params write?]}
(get-in contracts [contract :methods method])
data (abi-spec/encode signature params)]
(if write?
(wallet/open-sign-transaction-flow
cofx
{:to contract-address
:data data
:id "approve"
:symbol :ETH
:method "eth_sendTransaction"
:amount (money/bignumber 0)
:on-result on-result})
{::call {:address contract-address
:data data
:callback #(callback (abi-spec/decode % return-params))}})))))