/
chain.go
198 lines (169 loc) · 6.55 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package testutil
import (
"context"
"fmt"
"math/big"
"testing"
"github.com/stretchr/testify/require"
"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"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/incubus-network/fury/app"
furyparams "github.com/incubus-network/fury/app/params"
"github.com/incubus-network/fury/tests/e2e/runner"
"github.com/incubus-network/fury/tests/util"
committeetypes "github.com/incubus-network/fury/x/committee/types"
communitytypes "github.com/incubus-network/fury/x/community/types"
earntypes "github.com/incubus-network/fury/x/earn/types"
evmutiltypes "github.com/incubus-network/fury/x/evmutil/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
erc20s map[common.Address]struct{}
EncodingConfig furyparams.EncodingConfig
Auth authtypes.QueryClient
Bank banktypes.QueryClient
Committee committeetypes.QueryClient
Community communitytypes.QueryClient
Earn earntypes.QueryClient
Evm evmtypes.QueryClient
Evmutil evmutiltypes.QueryClient
Tm tmservice.ServiceClient
Tx txtypes.ServiceClient
Upgrade upgradetypes.QueryClient
}
// 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),
erc20s: make(map[common.Address]struct{}),
}
chain.EncodingConfig = app.MakeEncodingConfig()
grpcConn, err := details.GrpcConn()
if err != nil {
return chain, err
}
chain.EvmClient, err = details.EvmClient()
if err != nil {
return chain, err
}
chain.Auth = authtypes.NewQueryClient(grpcConn)
chain.Bank = banktypes.NewQueryClient(grpcConn)
chain.Committee = committeetypes.NewQueryClient(grpcConn)
chain.Community = communitytypes.NewQueryClient(grpcConn)
chain.Earn = earntypes.NewQueryClient(grpcConn)
chain.Evm = evmtypes.NewQueryClient(grpcConn)
chain.Evmutil = evmutiltypes.NewQueryClient(grpcConn)
chain.Tm = tmservice.NewServiceClient(grpcConn)
chain.Tx = txtypes.NewServiceClient(grpcConn)
chain.Upgrade = upgradetypes.NewQueryClient(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("[%s] account used for funding (%s) address: %s\n", chain.ChainId, 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)
}
}
// ReturnAllFunds loops through all SigningAccounts and sends all their funds back to the
// initially funded account.
func (chain *Chain) ReturnAllFunds() {
whale := chain.GetAccount(FundedAccountName)
fmt.Println(chain.erc20s)
for _, a := range chain.accounts {
if a.SdkAddress.String() != whale.SdkAddress.String() {
// NOTE: assumes all cosmos coin conversion funds have been converted back to sdk.
// return all erc20 balance
for erc20Addr := range chain.erc20s {
erc20Bal := chain.GetErc20Balance(erc20Addr, a.EvmAddress)
// if account has no balance, do nothing
if erc20Bal.Cmp(big.NewInt(0)) == 0 {
continue
}
_, err := a.TransferErc20(erc20Addr, whale.EvmAddress, erc20Bal)
if err != nil {
a.l.Printf("FAILED TO RETURN ERC20 FUNDS (contract: %s, balance: %d): %s\n",
erc20Addr, erc20Bal, err,
)
}
}
// get sdk balance of account
balance := chain.QuerySdkForBalances(a.SdkAddress)
// assumes 200,000 gas w/ min fee of .001
gas := sdk.NewInt64Coin(chain.StakingDenom, 200)
// ensure they have enough gas to return funds
if balance.AmountOf(chain.StakingDenom).LT(gas.Amount) {
a.l.Printf("ACCOUNT LACKS GAS MONEY TO RETURN FUNDS: %s\n", balance)
continue
}
// send it all back (minus gas) to the whale!
res := a.BankSend(whale.SdkAddress, balance.Sub(gas))
if res.Err != nil {
a.l.Printf("failed to return funds: %s\n", res.Err)
}
}
}
}
// RegisterErc20 is a method to record the address of erc20s on this chain.
// The full balances of each registered erc20 will be returned to the funded
// account when ReturnAllFunds is called.
func (chain *Chain) RegisterErc20(address common.Address) {
chain.erc20s[address] = struct{}{}
}
// 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
}
// GetModuleBalances returns the balance of a requested module account
func (chain *Chain) GetModuleBalances(moduleName string) sdk.Coins {
addr := authtypes.NewModuleAddress(moduleName)
return chain.QuerySdkForBalances(addr)
}
// GetErc20Balance fetches the ERC20 balance of `contract` for `address`.
func (chain *Chain) GetErc20Balance(contract, address common.Address) *big.Int {
resData, err := chain.EvmClient.CallContract(context.Background(), ethereum.CallMsg{
To: &contract,
Data: util.BuildErc20BalanceOfCallData(address),
}, nil)
require.NoError(chain.t, err)
return new(big.Int).SetBytes(resData)
}