2018-03-09 01:15:56 -08:00
|
|
|
package tx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2018-04-02 05:05:23 -07:00
|
|
|
"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 {
|
2018-06-27 17:32:06 -07:00
|
|
|
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 {
|
2018-04-25 07:49:31 -07:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var m BroadcastTxBody
|
2018-03-09 01:15:56 -08:00
|
|
|
|
2018-04-25 07:49:31 -07: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))
|
2018-04-25 07:49:31 -07:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(500)
|
|
|
|
w.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
2018-03-09 01:15:56 -08:00
|
|
|
|
2018-04-25 07:49:31 -07:00
|
|
|
w.Write([]byte(string(res.Height)))
|
|
|
|
}
|
2018-03-09 01:15:56 -08:00
|
|
|
}
|