-
Notifications
You must be signed in to change notification settings - Fork 16
/
client.go
101 lines (88 loc) · 2.86 KB
/
client.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
package types
import (
"errors"
"math/big"
apptypes "github.com/okex/exchain/app/types"
"github.com/okex/exchain/libs/cosmos-sdk/codec"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
authtypes "github.com/okex/exchain/libs/cosmos-sdk/x/auth/types"
tmbytes "github.com/okex/exchain/libs/tendermint/libs/bytes"
rpcclient "github.com/okex/exchain/libs/tendermint/rpc/client"
)
// BaseClient shows the expected behavior for a base client
type BaseClient interface {
ClientQuery
ClientTx
TxHandler
SimulationHandler
GetCodec() *codec.Codec
GetConfig() ClientConfig
}
// TxHandler shows the expected behavior to handle tx
type TxHandler interface {
BuildAndBroadcast(fromName, passphrase, memo string, msgs []sdk.Msg, accNumber, seqNumber uint64) (sdk.TxResponse, error)
BuildStdTx(fromName, passphrase, memo string, msgs []sdk.Msg, accNumber, seqNumber uint64) (*authtypes.StdTx, error)
BuildUnsignedStdTxOffline(msgs []sdk.Msg, memo string) *authtypes.StdTx
}
// SimulationHandler shows the expected behavior to handle simulation
type SimulationHandler interface {
CalculateGas(txBytes []byte) (authtypes.StdFee, error)
BuildTxForSim(msgs []sdk.Msg, memo string, accNumber, seqNumber uint64) ([]byte, error)
}
// ClientQuery shows the expected query behavior
type ClientQuery interface {
rpcclient.SignClient
rpcclient.HistoryClient
rpcclient.StatusClient
Query(path string, key tmbytes.HexBytes) ([]byte, int64, error)
QueryStore(key tmbytes.HexBytes, storeName, endPath string) ([]byte, int64, error)
}
// ClientTx shows the expected tx behavior
type ClientTx interface {
Broadcast(txBytes []byte, broadcastMode string) (res sdk.TxResponse, err error)
}
// ClientConfig records the base config of gosdk client
type ClientConfig struct {
NodeURI string
ChainID string
ChainIDBigInt *big.Int
BroadcastMode string
Gas uint64
GasAdjustment float64
Fees sdk.DecCoins
GasPrices sdk.DecCoins
}
// NewClientConfig creates a new instance of ClientConfig
func NewClientConfig(nodeURI, chainID string, broadcastMode string, feesStr string, gas uint64, gasAdjustment float64,
gasPricesStr string) (cliConfig ClientConfig, err error) {
var fees, gasPrices sdk.DecCoins
if len(feesStr) != 0 {
fees, err = sdk.ParseDecCoins(feesStr)
if err != nil {
return
}
}
if len(gasPricesStr) != 0 {
if gasAdjustment <= 1 {
return cliConfig, errors.New("failed. gasAdjustment must be greater than 1 with the auto gas calculating")
}
gasPrices, err = sdk.ParseDecCoins(gasPricesStr)
if err != nil {
return
}
}
chainIDBigInt, err := apptypes.ParseChainID(chainID)
if err != nil {
return
}
return ClientConfig{
NodeURI: nodeURI,
ChainID: chainID,
ChainIDBigInt: chainIDBigInt,
BroadcastMode: broadcastMode,
Gas: gas,
GasAdjustment: gasAdjustment,
Fees: fees,
GasPrices: gasPrices,
}, err
}