[feature] implement erc20 get-transaction for SNT and STT

- change `ethereum/get-transaction` to use call-private-rpc instead
of web3 object
- implement a version for erc20 tokens SNT and STT that provides enriches
result of get-transaction with `:recipient` and `:snt-amount`

Signed-off-by: yenda <eric@status.im>
This commit is contained in:
yenda 2019-04-03 23:04:14 +02:00
parent 2c028a474b
commit 9d84ef3ad1
No known key found for this signature in database
GPG Key ID: 0095623C0069DCE6
2 changed files with 48 additions and 8 deletions

View File

@ -3,6 +3,7 @@
[status-im.js-dependencies :as dependencies] [status-im.js-dependencies :as dependencies]
[status-im.native-module.core :as status] [status-im.native-module.core :as status]
[status-im.utils.ethereum.tokens :as tokens] [status-im.utils.ethereum.tokens :as tokens]
[status-im.utils.ethereum.abi-spec :as abi-spec]
[status-im.utils.money :as money] [status-im.utils.money :as money]
[taoensso.timbre :as log])) [taoensso.timbre :as log]))
@ -132,7 +133,7 @@
:method "eth_call" :method "eth_call"
:params [params "latest"]})) :params [params "latest"]}))
(fn [response] (fn [response]
(if (= "" response) (if (string/blank? response)
(log/warn :web3-response-error) (log/warn :web3-response-error)
(callback (get (js->clj (.parse js/JSON response)) "result")))))) (callback (get (js->clj (.parse js/JSON response)) "result"))))))
@ -175,11 +176,25 @@
(cb (js->clj result :keywordize-keys true)) (cb (js->clj result :keywordize-keys true))
(handle-error error))))) (handle-error error)))))
(defn get-transaction [web3 number cb] (defn- decode-uint
(.getTransaction (.-eth web3) number (fn [error result] [hex]
(if-not error (first (abi-spec/decode hex ["uint"])))
(cb (js->clj result :keywordize-keys true))
(handle-error error))))) (defn get-transaction [transaction-hash callback]
(status/call-private-rpc
(.stringify js/JSON (clj->js {:jsonprc "2.0"
:id 1
:method "eth_getTransactionByHash"
:params [transaction-hash]}))
(fn [response]
(if (string/blank? response)
(log/warn :web3-response-error)
(callback (-> (.parse js/JSON response)
(js->clj :keywordize-keys true)
:result
(update :gasPrice decode-uint)
(update :value decode-uint)
(update :gas decode-uint)))))))
(defn get-transaction-receipt [web3 number cb] (defn get-transaction-receipt [web3 number cb]
(.getTransactionReceipt (.-eth web3) number (fn [error result] (.getTransactionReceipt (.-eth web3) number (fn [error result]

View File

@ -15,7 +15,9 @@
=> 29166666 => 29166666
" "
(:require [status-im.utils.ethereum.core :as ethereum] (:require [clojure.string :as string]
[status-im.utils.ethereum.core :as ethereum]
[status-im.utils.ethereum.abi-spec :as abi-spec]
[status-im.native-module.core :as status] [status-im.native-module.core :as status]
[status-im.utils.security :as security] [status-im.utils.security :as security]
[status-im.js-dependencies :as dependencies] [status-im.js-dependencies :as dependencies]
@ -26,7 +28,7 @@
(def snt-contracts (def snt-contracts
{:mainnet "0x744d70fdbe2ba4cf95131626614a1763df805b9e" {:mainnet "0x744d70fdbe2ba4cf95131626614a1763df805b9e"
:testnet "0xc55cF4B03948D7EBc8b9E8BAD92643703811d162" :testnet "0xc55cf4b03948d7ebc8b9e8bad92643703811d162"
:rinkeby nil}) :rinkeby nil})
(def abi (def abi
@ -147,3 +149,26 @@
(ethereum/normalized-address owner-address) (ethereum/normalized-address owner-address)
(ethereum/normalized-address spender-address)) (ethereum/normalized-address spender-address))
#(cb (ethereum/hex->bignumber %)))) #(cb (ethereum/hex->bignumber %))))
(defn is-transfer?
[input-data]
(string/starts-with? input-data "0xa9059cbb"))
(defn is-snt-contract?
[contract]
((into #{} (vals snt-contracts)) contract))
(defn get-transaction
"only supports SNT for now"
[transaction-hash callback]
(ethereum/get-transaction
transaction-hash
(fn [{:keys [to input] :as result}]
(when (and result
(is-snt-contract? to)
(is-transfer? input))
(let [[recipient snt-value]
(abi-spec/decode (subs input 10) ["address" "uint"])]
(callback (assoc result
:recipient recipient
:snt-value snt-value)))))))