-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgosdk_test.go
160 lines (133 loc) · 4.14 KB
/
gosdk_test.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
package gosdk_test
import (
"fmt"
"strconv"
"testing"
"github.com/NibiruChain/nibiru/gosdk"
"github.com/NibiruChain/nibiru/x/common/denoms"
"github.com/NibiruChain/nibiru/x/common/testutil"
"github.com/NibiruChain/nibiru/x/common/testutil/cli"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
// --------------------------------------------------
// NibiruClientSuite
// --------------------------------------------------
var _ suite.SetupAllSuite = (*NibiruClientSuite)(nil)
type NibiruClientSuite struct {
suite.Suite
gosdk *gosdk.NibiruClient
grpcConn *grpc.ClientConn
cfg *cli.Config
network *cli.Network
val *cli.Validator
}
func TestNibiruClientTestSuite_RunAll(t *testing.T) {
suite.Run(t, new(NibiruClientSuite))
}
func (s *NibiruClientSuite) RPCEndpoint() string {
return s.val.RPCAddress
}
// SetupSuite implements the suite.SetupAllSuite interface. This function runs
// prior to all of the other tests in the suite.
func (s *NibiruClientSuite) SetupSuite() {
nibiru, err := gosdk.CreateBlockchain(s.T())
s.NoError(err)
s.network = nibiru.Network
s.cfg = nibiru.Cfg
s.val = nibiru.Val
s.grpcConn = nibiru.GrpcConn
}
func ConnectGrpcToVal(val *cli.Validator) (*grpc.ClientConn, error) {
grpcUrl := val.AppConfig.GRPC.Address
return gosdk.GetGRPCConnection(
grpcUrl, true, 5,
)
}
func (s *NibiruClientSuite) ConnectGrpc() {
grpcConn, err := ConnectGrpcToVal(s.val)
s.NoError(err)
s.NotNil(grpcConn)
s.grpcConn = grpcConn
}
func (s *NibiruClientSuite) TestNewQueryClient() {
_, err := gosdk.NewQueryClient(s.grpcConn)
s.NoError(err)
}
func (s *NibiruClientSuite) TestNewNibiruClient() {
rpcEndpt := s.val.RPCAddress
gosdk, err := gosdk.NewNibiruClient(s.cfg.ChainID, s.grpcConn, rpcEndpt)
s.NoError(err)
s.gosdk = &gosdk
s.gosdk.Keyring = s.val.ClientCtx.Keyring
s.T().Run("DoTestBroadcastMsgs", func(t *testing.T) {
s.DoTestBroadcastMsgs()
})
s.T().Run("DoTestBroadcastMsgsGrpc", func(t *testing.T) {
s.NoError(s.network.WaitForNextBlock())
s.DoTestBroadcastMsgsGrpc()
})
}
func (s *NibiruClientSuite) UsefulPrints() {
tmCfgRootDir := s.val.Ctx.Config.RootDir
fmt.Printf("tmCfgRootDir: %v\n", tmCfgRootDir)
fmt.Printf("s.val.Dir: %v\n", s.val.Dir)
fmt.Printf("s.val.ClientCtx.KeyringDir: %v\n", s.val.ClientCtx.KeyringDir)
}
func (s *NibiruClientSuite) AssertTxResponseSuccess(txResp *sdk.TxResponse) (txHashHex string) {
s.NotNil(txResp)
s.EqualValues(txResp.Code, 0)
return txResp.TxHash
}
func (s *NibiruClientSuite) msgSendVars() (from, to sdk.AccAddress, amt sdk.Coins, msgSend sdk.Msg) {
from = s.val.Address
to = testutil.AccAddress()
amt = sdk.NewCoins(sdk.NewInt64Coin(denoms.NIBI, 420))
msgSend = banktypes.NewMsgSend(from, to, amt)
return from, to, amt, msgSend
}
func (s *NibiruClientSuite) DoTestBroadcastMsgs() (txHashHex string) {
from, _, _, msgSend := s.msgSendVars()
txResp, err := s.gosdk.BroadcastMsgs(
from, msgSend,
)
s.NoError(err)
return s.AssertTxResponseSuccess(txResp)
}
func (s *NibiruClientSuite) DoTestBroadcastMsgsGrpc() (txHashHex string) {
from, _, _, msgSend := s.msgSendVars()
txResp, err := s.gosdk.BroadcastMsgsGrpc(
from, msgSend,
)
s.NoError(err)
txHashHex = s.AssertTxResponseSuccess(txResp)
base := 10
var txRespCode string = strconv.FormatUint(uint64(txResp.Code), base)
s.EqualValuesf(txResp.Code, 0,
"code: %v\nraw log: %s", txRespCode, txResp.RawLog)
return txHashHex
}
func (s *NibiruClientSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite")
s.network.Cleanup()
}
// --------------------------------------------------
// NibiruClientSuite_NoNetwork
// --------------------------------------------------
type NibiruClientSuite_NoNetwork struct {
suite.Suite
}
func TestNibiruClientSuite_NoNetwork_RunAll(t *testing.T) {
suite.Run(t, new(NibiruClientSuite_NoNetwork))
}
func (s *NibiruClientSuite_NoNetwork) TestGetGrpcConnection_NoNetwork() {
grpcConn, err := gosdk.GetGRPCConnection(
gosdk.DefaultNetworkInfo.GrpcEndpoint, true, 2,
)
s.Error(err)
s.Nil(grpcConn)
_, err = gosdk.NewQueryClient(grpcConn)
s.Error(err)
}