/
init_evm.go
70 lines (61 loc) · 2.42 KB
/
init_evm.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
package testutil
import (
"context"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/incubus-network/fury/tests/e2e/contracts/greeter"
"github.com/incubus-network/fury/x/earn/types"
evmutiltypes "github.com/incubus-network/fury/x/evmutil/types"
)
// InitFuryEvmData is run after the chain is running, but before the tests are run.
// It is used to initialize some EVM state, such as deploying contracts.
func (suite *E2eTestSuite) InitFuryEvmData() {
whale := suite.Fury.GetAccount(FundedAccountName)
// ensure funded account has nonzero erc20 balance
balance := suite.Fury.GetErc20Balance(suite.DeployedErc20.Address, whale.EvmAddress)
if balance.Cmp(big.NewInt(0)) != 1 {
panic(fmt.Sprintf("expected funded account (%s) to have erc20 balance", whale.EvmAddress.Hex()))
}
// expect the erc20 to be enabled for conversion to sdk.Coin
params, err := suite.Fury.Evmutil.Params(context.Background(), &evmutiltypes.QueryParamsRequest{})
if err != nil {
panic(fmt.Sprintf("failed to fetch evmutil params during init: %s", err))
}
found := false
erc20Addr := suite.DeployedErc20.Address.Hex()
for _, p := range params.Params.EnabledConversionPairs {
if common.BytesToAddress(p.FuryERC20Address).Hex() == erc20Addr {
found = true
suite.DeployedErc20.CosmosDenom = p.Denom
}
}
if !found {
panic(fmt.Sprintf("erc20 %s must be enabled for conversion to cosmos coin", erc20Addr))
}
suite.Fury.RegisterErc20(suite.DeployedErc20.Address)
// expect the erc20's cosmos denom to be a supported earn vault
_, err = suite.Fury.Earn.Vault(
context.Background(),
types.NewQueryVaultRequest(suite.DeployedErc20.CosmosDenom),
)
if err != nil {
panic(fmt.Sprintf("failed to find earn vault with denom %s: %s", suite.DeployedErc20.CosmosDenom, err))
}
// deploy an example contract
greeterAddr, _, _, err := greeter.DeployGreeter(
whale.evmSigner.Auth,
whale.evmSigner.EvmClient,
"what's up!",
)
suite.NoError(err, "failed to deploy a contract to the EVM")
suite.Fury.ContractAddrs["greeter"] = greeterAddr
}
// FundFuryErc20Balance sends the pre-deployed ERC20 token to the `toAddress`.
func (suite *E2eTestSuite) FundFuryErc20Balance(toAddress common.Address, amount *big.Int) EvmTxResponse {
// funded account should have erc20 balance
whale := suite.Fury.GetAccount(FundedAccountName)
res, err := whale.TransferErc20(suite.DeployedErc20.Address, toAddress, amount)
suite.NoError(err)
return res
}