mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 02:12:23 +00:00
add broadcast tx route
This commit is contained in:
parent
c5c0086b13
commit
26de99f4d5
2 changed files with 47 additions and 0 deletions
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
@ -87,4 +87,10 @@ var routes = Routes{
|
|||
"/api/get_internal_transactions_hash/{transactions_hash}",
|
||||
GetInternalTransactionsHash,
|
||||
},
|
||||
Route{
|
||||
"BroadcastTx",
|
||||
"GET",
|
||||
"/api/broadcast_tx/{transaction_hash}",
|
||||
BroadcastTx,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue