-
Notifications
You must be signed in to change notification settings - Fork 5
/
tx.go
56 lines (45 loc) · 1.29 KB
/
tx.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
//
// Copyright 2019 Wireline, Inc.
//
package gql
import (
"encoding/base64"
"errors"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/rpc/core"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
)
func decodeStdTx(codec *amino.Codec, tx string) (*auth.StdTx, error) {
bytes, err := base64.StdEncoding.DecodeString(tx)
if err != nil {
return nil, errors.New("{ \"log\": \"Tx bytes not base64 encoded.\" }")
}
var stdTx auth.StdTx
err = codec.UnmarshalJSON(bytes, &stdTx)
if err != nil {
return nil, errors.New("{ \"log\": \"Invalid Tx bytes, check request JSON.\" }")
}
return &stdTx, nil
}
func broadcastTx(r *mutationResolver, stdTx *auth.StdTx) (*ctypes.ResultBroadcastTxCommit, error) {
txBytes, err := r.Resolver.codec.MarshalBinaryLengthPrefixed(stdTx)
if err != nil {
return nil, err
}
ctx := &rpctypes.Context{}
res, err := core.BroadcastTxCommit(ctx, txBytes)
if err != nil {
return nil, err
}
if res.CheckTx.IsErr() {
errBytes, _ := res.CheckTx.MarshalJSON()
return nil, errors.New(string(errBytes))
}
if res.DeliverTx.IsErr() {
errBytes, _ := res.DeliverTx.MarshalJSON()
return nil, errors.New(string(errBytes))
}
return res, nil
}