[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.native-module.core :as status]
[status-im.utils.ethereum.tokens :as tokens]
[status-im.utils.ethereum.abi-spec :as abi-spec]
[status-im.utils.money :as money]
[taoensso.timbre :as log]))
@ -132,7 +133,7 @@
:method "eth_call"
:params [params "latest"]}))
(fn [response]
(if (= "" response)
(if (string/blank? response)
(log/warn :web3-response-error)
(callback (get (js->clj (.parse js/JSON response)) "result"))))))
@ -175,11 +176,25 @@
(cb (js->clj result :keywordize-keys true))
(handle-error error)))))
(defn get-transaction [web3 number cb]
(.getTransaction (.-eth web3) number (fn [error result]
(if-not error
(cb (js->clj result :keywordize-keys true))
(handle-error error)))))
(defn- decode-uint
[hex]
(first (abi-spec/decode hex ["uint"])))
(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]
(.getTransactionReceipt (.-eth web3) number (fn [error result]

View File

@ -15,7 +15,9 @@
=> 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.utils.security :as security]
[status-im.js-dependencies :as dependencies]
@ -26,7 +28,7 @@
(def snt-contracts
{:mainnet "0x744d70fdbe2ba4cf95131626614a1763df805b9e"
:testnet "0xc55cF4B03948D7EBc8b9E8BAD92643703811d162"
:testnet "0xc55cf4b03948d7ebc8b9e8bad92643703811d162"
:rinkeby nil})
(def abi
@ -147,3 +149,26 @@
(ethereum/normalized-address owner-address)
(ethereum/normalized-address spender-address))
#(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)))))))