-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlcd.go
264 lines (241 loc) · 6.76 KB
/
lcd.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package cosmos
import (
"bytes"
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"sync"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys"
sdk "github.com/cosmos/cosmos-sdk/types"
sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/auth"
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
authutils "github.com/cosmos/cosmos-sdk/x/auth/client/utils"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
abci "github.com/tendermint/tendermint/abci/types"
)
// LCD is a simple cosmos LCD client.
type LCD struct {
endpoint string
cdc *codec.Codec
kb keys.Keybase
chainID string
accName string
accPassword string
gasPrices sdktypes.DecCoins
// local state
acc *auth.BaseAccount
accountMutex sync.Mutex
broadcastMutex sync.Mutex
}
// NewLCD initializes a cosmos LCD client.
func NewLCD(endpoint string, cdc *codec.Codec, kb keys.Keybase, chainID, accName, accPassword, gasPrices string) (*LCD, error) {
gasPricesDecoded, err := sdktypes.ParseDecCoins(gasPrices)
if err != nil {
return nil, err
}
return &LCD{
endpoint: endpoint,
cdc: cdc,
kb: kb,
chainID: chainID,
accName: accName,
accPassword: accPassword,
gasPrices: gasPricesDecoded,
}, nil
}
// Get executes a get request on the LCD.
func (lcd *LCD) Get(path string, ptr interface{}) error {
resp, err := http.Get(lcd.endpoint + path)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("request status code is not 2XX, got %d with body: %s", resp.StatusCode, body)
}
cosResp := rest.ResponseWithHeight{}
if err := lcd.cdc.UnmarshalJSON(body, &cosResp); err != nil {
return err
}
if len(cosResp.Result) > 0 {
if err := lcd.cdc.UnmarshalJSON(cosResp.Result, ptr); err != nil {
return err
}
}
return nil
}
// Post executes a post request on the LCD.
// The response is expected to be a ResponseWithHeight that contains the expected result.
func (lcd *LCD) Post(path string, req interface{}, ptr interface{}) error {
cosResp := rest.ResponseWithHeight{}
if err := lcd.PostBare(path, req, &cosResp); err != nil {
return err
}
if len(cosResp.Result) > 0 {
if err := lcd.cdc.UnmarshalJSON(cosResp.Result, ptr); err != nil {
return err
}
}
return nil
}
// PostBare executes a post request on the LCD.
// There is no expectation on the type of response.
func (lcd *LCD) PostBare(path string, req interface{}, ptr interface{}) error {
reqBody, err := lcd.cdc.MarshalJSON(req)
if err != nil {
return err
}
resp, err := http.Post(lcd.endpoint+path, "application/json", bytes.NewReader(reqBody))
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("request status code is not 2XX, got %d with body: %s", resp.StatusCode, body)
}
if err := lcd.cdc.UnmarshalJSON(body, ptr); err != nil {
return err
}
return nil
}
//BroadcastMsg sign and broadcast a transaction from a message.
func (lcd *LCD) BroadcastMsg(msg sdk.Msg) ([]byte, error) {
return lcd.BroadcastMsgs([]sdk.Msg{msg})
}
//BroadcastMsgs sign and broadcast a transaction from multiple messages.
func (lcd *LCD) BroadcastMsgs(msgs []sdk.Msg) ([]byte, error) {
// Lock the getAccount + create and sign tx + broadcast
lcd.broadcastMutex.Lock()
defer lcd.broadcastMutex.Unlock()
acc, err := lcd.getAccount()
if err != nil {
return nil, err
}
// create and sign the tx
tx, err := lcd.createAndSignTx(msgs, acc)
if err != nil {
return nil, err
}
// broadcast the tx
req := authrest.BroadcastReq{
Tx: tx,
Mode: "block", // TODO: should be changed to "sync" and wait for the tx event
}
var res sdk.TxResponse
if err := lcd.PostBare("txs", req, &res); err != nil {
return nil, err
}
if abci.CodeTypeOK != res.Code {
return nil, fmt.Errorf("transaction returned with invalid code %d: %s", res.Code, res.RawLog)
}
// only increase sequence if no error during broadcast of tx
if err := lcd.setAccountSequence(acc.GetSequence() + 1); err != nil {
return nil, err
}
// decode result
result, err := hex.DecodeString(res.Data)
if err != nil {
return nil, err
}
return result, nil
}
// getAccount returns the local account.
func (lcd *LCD) getAccount() (*auth.BaseAccount, error) {
lcd.accountMutex.Lock()
defer lcd.accountMutex.Unlock()
if lcd.acc == nil {
accKb, err := lcd.kb.Get(lcd.accName)
if err != nil {
return nil, err
}
lcd.acc = auth.NewBaseAccount(accKb.GetAddress(), nil, accKb.GetPubKey(), 0, 0)
}
var accR *auth.BaseAccount
if err := lcd.Get("auth/accounts/"+lcd.acc.GetAddress().String(), &accR); err != nil {
return nil, err
}
if accR.Address.Empty() {
return nil, fmt.Errorf("account %q doesn't exist", lcd.acc.GetAddress().String())
}
// replace seq if sup
if lcd.acc.GetSequence() > accR.GetSequence() {
accR.SetSequence(lcd.acc.GetSequence())
}
lcd.acc = accR
return lcd.acc, nil
}
// setAccountSequence sets the sequence on the local account.
func (lcd *LCD) setAccountSequence(seq uint64) error {
lcd.accountMutex.Lock()
defer lcd.accountMutex.Unlock()
if lcd.acc == nil {
return fmt.Errorf("lcd.acc should not be nil. use getAccount first")
}
return lcd.acc.SetSequence(seq)
}
func (lcd *LCD) createAndSignTx(msgs []sdk.Msg, acc *auth.BaseAccount) (authtypes.StdTx, error) {
// Create TxBuilder
txBuilder := authtypes.NewTxBuilder(
authutils.GetTxEncoder(lcd.cdc),
acc.GetAccountNumber(),
acc.GetSequence(),
flags.DefaultGasLimit,
GasAdjustment,
true,
lcd.chainID,
"",
nil,
lcd.gasPrices,
).WithKeybase(lcd.kb)
// calculate gas
if txBuilder.SimulateAndExecute() {
gasAdjustment := strconv.FormatFloat(txBuilder.GasAdjustment(), 'f', -1, 64)
req := SimulateReq{
BaseReq: rest.NewBaseReq(
acc.Address.String(),
"",
lcd.chainID,
"",
gasAdjustment,
acc.GetAccountNumber(),
acc.GetSequence(),
nil,
nil,
true,
),
Msgs: msgs,
}
var res rest.GasEstimateResponse
if err := lcd.PostBare("txs/simulate", req, &res); err != nil {
return authtypes.StdTx{}, err
}
txBuilder = txBuilder.WithGas(res.GasEstimate)
}
// create StdSignMsg
stdSignMsg, err := txBuilder.BuildSignMsg(msgs)
if err != nil {
return authtypes.StdTx{}, err
}
// create StdTx
stdTx := authtypes.NewStdTx(stdSignMsg.Msgs, stdSignMsg.Fee, nil, stdSignMsg.Memo)
// sign StdTx
signedTx, err := txBuilder.SignStdTx(lcd.accName, lcd.accPassword, stdTx, false)
if err != nil {
return authtypes.StdTx{}, err
}
return signedTx, nil
}