From 26de99f4d5caa84e74a7323af8d60a7662bce44c Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Tue, 12 Jun 2018 13:55:13 -0400 Subject: [PATCH 1/2] add broadcast tx route --- blockExplorerApi/handler.go | 41 +++++++++++++++++++++++++++++++++++++ blockExplorerApi/routes.go | 6 ++++++ 2 files changed, 47 insertions(+) diff --git a/blockExplorerApi/handler.go b/blockExplorerApi/handler.go index b84d5d3537..58256351c2 100644 --- a/blockExplorerApi/handler.go +++ b/blockExplorerApi/handler.go @@ -10,6 +10,9 @@ import ( "github.com/ethereum/go-ethereum/shyftdb" "github.com/gorilla/mux" + "bytes" + "io/ioutil" + "encoding/json" ) // GetTransaction gets txs @@ -244,3 +247,41 @@ func GetInternalTransactionsHash(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Get Internal Transaction Hash", transactionHash) } + +func BroadcastTx(w http.ResponseWriter, r *http.Request) { + // Example return result (returns tx hash): + // {"jsonrpc":"2.0","id":1,"result":"0xafa4c62f29dbf16bbfac4eea7cbd001a9aa95c59974043a17f863172f8208029"} + + // http params + vars := mux.Vars(r) + transactionHash := vars["transaction_hash"] + + // format the transactionHash into a proper sendRawTransaction jsonrpc request + formatted_json := []byte(fmt.Sprintf(`{"jsonrpc":"2.0","method":"eth_sendRawTransaction","params":["%s"],"id":0}`, transactionHash)) + + // send json rpc request + resp, _ := http.Post("http://localhost:8545", "application/json", bytes.NewBuffer(formatted_json)) + body, _ := ioutil.ReadAll(resp.Body) + byt := []byte(string(body)) + + // read json and return result as http response, be it an error or tx hash + var dat map[string]interface{} + if err := json.Unmarshal(byt, &dat); err != nil { + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) + fmt.Fprintln(w, "ERROR parsing json") + } + tx_hash := dat["result"] + if(tx_hash == nil) { + errObj := dat["error"] + errObj2 := errObj.(map[string]interface{}) + fmt.Println() + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) + fmt.Fprintln(w, "ERROR:", errObj2["message"]) + } else { + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusOK) + fmt.Fprintln(w, "Transaction Hash:", tx_hash) + } +} \ No newline at end of file diff --git a/blockExplorerApi/routes.go b/blockExplorerApi/routes.go index 4ce0dc80c8..fd6517eb5e 100644 --- a/blockExplorerApi/routes.go +++ b/blockExplorerApi/routes.go @@ -87,4 +87,10 @@ var routes = Routes{ "/api/get_internal_transactions_hash/{transactions_hash}", GetInternalTransactionsHash, }, + Route{ + "BroadcastTx", + "GET", + "/api/broadcast_tx/{transaction_hash}", + BroadcastTx, + }, } From 84fe57433164fb7a5a3ea46ab4ad7106b6090683 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Tue, 12 Jun 2018 14:11:40 -0400 Subject: [PATCH 2/2] refactor broadcasttx function --- blockExplorerApi/handler.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/blockExplorerApi/handler.go b/blockExplorerApi/handler.go index 58256351c2..496941db7b 100644 --- a/blockExplorerApi/handler.go +++ b/blockExplorerApi/handler.go @@ -273,12 +273,10 @@ func BroadcastTx(w http.ResponseWriter, r *http.Request) { } tx_hash := dat["result"] if(tx_hash == nil) { - errObj := dat["error"] - errObj2 := errObj.(map[string]interface{}) - fmt.Println() + errMap := dat["error"].(map[string]interface{}) w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusOK) - fmt.Fprintln(w, "ERROR:", errObj2["message"]) + fmt.Fprintln(w, "ERROR:", errMap["message"]) } else { w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusOK)