mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 02:12:23 +00:00
Merge pull request #37 from ShyftNetwork/addBroadcastTxRoute
add broadcast tx route
This commit is contained in:
commit
d7afc81fb1
2 changed files with 45 additions and 0 deletions
|
|
@ -10,6 +10,9 @@ import (
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/shyftdb"
|
"github.com/ethereum/go-ethereum/shyftdb"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
|
"bytes"
|
||||||
|
"io/ioutil"
|
||||||
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetTransaction gets txs
|
// GetTransaction gets txs
|
||||||
|
|
@ -244,3 +247,39 @@ func GetInternalTransactionsHash(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
fmt.Fprintln(w, "Get Internal Transaction Hash", transactionHash)
|
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) {
|
||||||
|
errMap := dat["error"].(map[string]interface{})
|
||||||
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
fmt.Fprintln(w, "ERROR:", errMap["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}",
|
"/api/get_internal_transactions_hash/{transactions_hash}",
|
||||||
GetInternalTransactionsHash,
|
GetInternalTransactionsHash,
|
||||||
},
|
},
|
||||||
|
Route{
|
||||||
|
"BroadcastTx",
|
||||||
|
"GET",
|
||||||
|
"/api/broadcast_tx/{transaction_hash}",
|
||||||
|
BroadcastTx,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue