cosmos-sdk/client/tx/broadcast.go

38 lines
704 B
Go
Raw Normal View History

2018-03-09 01:15:56 -08:00
package tx
import (
"encoding/json"
"net/http"
"github.com/cosmos/cosmos-sdk/client/context"
2018-03-09 01:15:56 -08:00
)
2018-04-18 21:49:24 -07:00
// Tx Broadcast Body
2018-03-09 01:15:56 -08:00
type BroadcastTxBody struct {
TxBytes string `json:"tx"`
2018-03-09 01:15:56 -08:00
}
2018-04-18 21:49:24 -07:00
// BroadcastTx REST Handler
2018-08-06 11:11:30 -07:00
func BroadcastTxRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var m BroadcastTxBody
2018-03-09 01:15:56 -08:00
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&m)
if err != nil {
w.WriteHeader(400)
w.Write([]byte(err.Error()))
return
}
2018-03-09 01:15:56 -08:00
2018-08-06 11:11:30 -07:00
res, err := cliCtx.BroadcastTx([]byte(m.TxBytes))
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
2018-03-09 01:15:56 -08:00
w.Write([]byte(string(res.Height)))
}
2018-03-09 01:15:56 -08:00
}