Fix bugs in ethereum module

* Fix geth json-rpc errors caused by non-hex encoded integer
  parameters and nil :to parameter
* use personal_getTransaction instead of
  personal_signAndSendTransaction used by geth < 1.5
This commit is contained in:
Teemu Patja 2017-01-19 11:29:28 +02:00
parent afce955ff3
commit 480d85f6b5
No known key found for this signature in database
GPG Key ID: F5B7035E6580FD4C
1 changed files with 19 additions and 11 deletions

View File

@ -4,7 +4,8 @@
[clojure.java.io :as io]
[commiteth.config :refer [env]]
[clojure.string :refer [join]]
[clojure.tools.logging :as log]))
[clojure.tools.logging :as log]
[clojure.string :as str]))
(def eth-rpc-url "http://localhost:8545")
(defn eth-account [] (:eth-account env))
@ -57,12 +58,16 @@
(defn send-transaction
[from to value & [params]]
;; todo: estimate gas instead of hardcoding
(let [gas 2100000]
(eth-rpc "personal_signAndSendTransaction" [(merge params {:from from
:to to
:value value
:gas gas})
(eth-password)])))
(let [gas (format "0x%x" 2600000)
args (merge params {:from from
:value value
:gas gas})]
(eth-rpc
"personal_sendTransaction"
[(if (not (nil? to))
(merge args {:to to})
args)
(eth-password)])))
(defn get-transaction-receipt
[hash]
@ -74,14 +79,16 @@
(format "%064x" param)
(clojure.string/replace (format "%64s" (subs param 2)) " " "0")))
(defn deploy-contract
[owner]
(let [contract-code (-> "contracts/wallet.data" io/resource slurp)
owner1 (format-param (eth-account))
owner2 (format-param owner)
data (str contract-code owner1 owner2)]
data (str contract-code owner1 owner2)
value (format "0x%x" 1)]
(println data)
(send-transaction (eth-account) nil 1 {:data data})))
(send-transaction (eth-account) nil value {:data data})))
(defn- format-call-params
[method-id & params]
@ -95,5 +102,6 @@
(defn execute
[from contract method-id & params]
(let [data (apply format-call-params method-id params)]
(send-transaction from contract 1 {:data data})))
(let [data (apply format-call-params method-id params)
value (format "0x%x" 1)]
(send-transaction from contract value {:data data})))