/
chain.go
116 lines (99 loc) · 3.75 KB
/
chain.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
package testutil
import (
"context"
"fmt"
"testing"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
"github.com/cosmos/cosmos-sdk/crypto/hd"
sdk "github.com/cosmos/cosmos-sdk/types"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/stretchr/testify/require"
"github.com/zeta-protocol/black/app"
blackparams "github.com/zeta-protocol/black/app/params"
"github.com/zeta-protocol/black/tests/e2e/runner"
"github.com/zeta-protocol/black/tests/util"
communitytypes "github.com/zeta-protocol/black/x/community/types"
earntypes "github.com/zeta-protocol/black/x/earn/types"
)
// Chain wraps query clients & accounts for a network
type Chain struct {
accounts map[string]*SigningAccount
t *testing.T
StakingDenom string
ChainId string
EvmClient *ethclient.Client
ContractAddrs map[string]common.Address
EncodingConfig blackparams.EncodingConfig
Auth authtypes.QueryClient
Bank banktypes.QueryClient
Community communitytypes.QueryClient
Earn earntypes.QueryClient
Evm evmtypes.QueryClient
Tm tmservice.ServiceClient
Tx txtypes.ServiceClient
}
// NewChain creates the query clients & signing account management for a chain run on a set of ports.
// A signing client for the fundedAccountMnemonic is initialized. This account is referred to in the
// code as "whale" and it is used to supply funds to all new accounts.
func NewChain(t *testing.T, details *runner.ChainDetails, fundedAccountMnemonic string) (*Chain, error) {
chain := &Chain{
t: t,
StakingDenom: details.StakingDenom,
ChainId: details.ChainId,
ContractAddrs: make(map[string]common.Address),
}
chain.EncodingConfig = app.MakeEncodingConfig()
grpcUrl := fmt.Sprintf("http://localhost:%s", details.GrpcPort)
grpcConn, err := util.NewGrpcConnection(grpcUrl)
if err != nil {
return chain, err
}
evmRpcUrl := fmt.Sprintf("http://localhost:%s", details.EvmPort)
chain.EvmClient, err = ethclient.Dial(evmRpcUrl)
if err != nil {
return chain, err
}
chain.Auth = authtypes.NewQueryClient(grpcConn)
chain.Bank = banktypes.NewQueryClient(grpcConn)
chain.Community = communitytypes.NewQueryClient(grpcConn)
chain.Earn = earntypes.NewQueryClient(grpcConn)
chain.Evm = evmtypes.NewQueryClient(grpcConn)
chain.Tm = tmservice.NewServiceClient(grpcConn)
chain.Tx = txtypes.NewServiceClient(grpcConn)
// initialize accounts map
chain.accounts = make(map[string]*SigningAccount)
// setup the signing account for the initially funded account (used to fund all other accounts)
whale := chain.AddNewSigningAccount(
FundedAccountName,
hd.CreateHDPath(Bip44CoinType, 0, 0),
chain.ChainId,
fundedAccountMnemonic,
)
// check that funded account is actually funded.
fmt.Printf("account used for funding (%s) address: %s\n", FundedAccountName, whale.SdkAddress)
whaleFunds := chain.QuerySdkForBalances(whale.SdkAddress)
if whaleFunds.IsZero() {
chain.t.Fatal("funded account mnemonic is for account with no funds")
}
return chain, nil
}
// Shutdown performs closes all the account request channels for this chain.
func (chain *Chain) Shutdown() {
// close all account request channels
for _, a := range chain.accounts {
close(a.sdkReqChan)
}
}
// QuerySdkForBalances gets the balance of a particular address on this Chain.
func (chain *Chain) QuerySdkForBalances(addr sdk.AccAddress) sdk.Coins {
res, err := chain.Bank.AllBalances(context.Background(), &banktypes.QueryAllBalancesRequest{
Address: addr.String(),
})
require.NoError(chain.t, err)
return res.Balances
}