Skip to content

Commit

Permalink
update structs and logic to comply with cosmos-sdk 0.38
Browse files Browse the repository at this point in the history
  • Loading branch information
gsora committed Mar 2, 2020
1 parent 4b2ce21 commit 6ac7da8
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 66 deletions.
58 changes: 8 additions & 50 deletions broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"strconv"
)

// TxMode identifies when an LCD should replies to a client
Expand Down Expand Up @@ -68,60 +68,18 @@ func broadcastTx(tx SignedTransactionPayload, lcdEndpoint string, txMode TxMode)
// deserialize LCD response into a cosmos TxResponse
var txr TxResponse

txrJdec := json.NewDecoder(resp.Body)
err = txrJdec.Decode(&txr)
jdec := json.NewDecoder(resp.Body)

err = jdec.Decode(&txr)
if err != nil {
return "", fmt.Errorf("could not deserialize cosmos txresponse from lcd: %w", err)
}

// something went wrong in tx validation on the LCD side,
// deserialize raw_log and return the error inside
allOk := true
for _, l := range txr.Logs {
allOk = allOk && l.Success
}

if !allOk || len(txr.Logs) <= 0 {
// txr.RawLog can either be a RawLog
// or it might be a lot of good stuff,
// we better cycle txr.Logs and build a nice error message
var jerr RawLog
jd := json.NewDecoder(strings.NewReader(txr.RawLog))
jsonErr := jd.Decode(&jerr)

if jsonErr == nil {
return "", fmt.Errorf(
"codespace %s: %s, code %d",
jerr.Codespace,
jerr.Message,
jerr.Code,
)
}

// parse logs!
message := ""
for i, log := range txr.Logs {
var jerr RawLog
jd := json.NewDecoder(strings.NewReader(log.Log))
jsonErr := jd.Decode(&jerr)

if jsonErr != nil {
// bail out
message = txr.RawLog
break
}

if i == 0 {
message = message + jerr.Message
} else {
message = message + ", " + jerr.Message
}
}

if txr.Code != 0 || len(txr.Logs) <= 0 {
return "", fmt.Errorf(
"codespace %s: %s, code %d",
txr.Codespace,
message,
txr.RawLog,
txr.Code,
)
}
Expand All @@ -147,8 +105,8 @@ func (w *Wallet) SignAndBroadcast(tx TransactionPayload, lcdEndpoint string, txM
signedTx, err := w.Sign(
tx,
nodeInfo.Info.Network,
accountData.Result.Value.AccountNumber,
accountData.Result.Value.Sequence,
strconv.FormatInt(accountData.Result.Value.AccountNumber, 10),
strconv.FormatInt(accountData.Result.Value.Sequence, 10),
)
if err != nil {
return "", fmt.Errorf("could not sign transaction: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/awnumar/memguard v0.21.0
github.com/btcsuite/btcd v0.20.1-beta
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d
github.com/cosmos/cosmos-sdk v0.37.4
github.com/cosmos/cosmos-sdk v0.38.1
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d
github.com/jarcoal/httpmock v1.0.4
github.com/stretchr/testify v1.4.0
Expand Down
Loading

0 comments on commit 6ac7da8

Please sign in to comment.