/
send.go
104 lines (89 loc) · 2.45 KB
/
send.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package api
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
)
// BankSendBody contains the necessary data to make a send transaction
type BankSendBody struct {
Sender sdk.AccAddress `json:"sender"`
Reciever sdk.AccAddress `json:"reciever"`
Amount string `json:"amount"`
ChainID string `json:"chain-id"`
Memo string `json:"memo,omitempty"`
Fees string `json:"fees,omitempty"`
GasAdjustment string `json:"gas_adjustment,omitempty"`
}
func (sb BankSendBody) Marshal() []byte {
out, err := json.Marshal(sb)
if err != nil {
panic(err)
}
return out
}
// BankSend handles the /tx/bank/send route
func (s *Server) BankSend(w http.ResponseWriter, r *http.Request) {
var sb BankSendBody
body, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write(newError(err).marshal())
return
}
err = cdc.UnmarshalJSON(body, &sb)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write(newError(err).marshal())
return
}
coins, err := sdk.ParseCoins(sb.Amount)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write(newError(fmt.Errorf("failed to parse amount %s into sdk.Coins", sb.Amount)).marshal())
return
}
var fees sdk.Coins
if sb.Fees != "" {
fees, err = sdk.ParseCoins(sb.Fees)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write(newError(fmt.Errorf("failed to parse fees %s into sdk.Coins", sb.Fees)).marshal())
return
}
}
stdTx := auth.NewStdTx(
[]sdk.Msg{bank.MsgSend{sb.Sender, sb.Reciever, coins}},
auth.NewStdFee(20000, fees),
[]auth.StdSignature{{}},
sb.Memo,
)
gas, err := s.SimulateGas(cdc.MustMarshalBinaryLengthPrefixed(stdTx))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write(newError(err).marshal())
return
}
if gas != 0 && sb.GasAdjustment != "" {
adj, err := strconv.ParseFloat(sb.GasAdjustment, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write(newError(fmt.Errorf("failed to parse gasAdjustment %s into float64", sb.GasAdjustment)).marshal())
return
}
gas = uint64(adj * float64(gas))
}
stdTx = auth.NewStdTx(
stdTx.Msgs,
auth.NewStdFee(gas, stdTx.Fee.Amount),
[]auth.StdSignature{},
stdTx.Memo,
)
w.WriteHeader(http.StatusOK)
w.Write(cdc.MustMarshalJSON(stdTx))
return
}