From 480d85f6b5565776e49eb8d2f5e149620729ec0c Mon Sep 17 00:00:00 2001 From: Teemu Patja Date: Thu, 19 Jan 2017 11:29:28 +0200 Subject: [PATCH] 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 --- src/clj/commiteth/eth/core.clj | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/clj/commiteth/eth/core.clj b/src/clj/commiteth/eth/core.clj index a1259f4..04d3881 100644 --- a/src/clj/commiteth/eth/core.clj +++ b/src/clj/commiteth/eth/core.clj @@ -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})))