forked from strangelove-ventures/interchaintest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_user.go
96 lines (89 loc) · 2.87 KB
/
test_user.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
package ibctest
import (
"context"
"fmt"
"testing"
"github.com/strangelove-ventures/ibctest/v6/ibc"
"github.com/strangelove-ventures/ibctest/v6/internal/dockerutil"
"github.com/strangelove-ventures/ibctest/v6/testutil"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
)
// generateUserWallet creates a new user wallet with the given key name on the given chain.
// a user is recovered if a mnemonic is specified.
func generateUserWallet(ctx context.Context, keyName, mnemonic string, chain ibc.Chain) (*ibc.Wallet, error) {
if mnemonic != "" {
if err := chain.RecoverKey(ctx, keyName, mnemonic); err != nil {
return nil, fmt.Errorf("failed to recover key on source chain: %w", err)
}
} else {
if err := chain.CreateKey(ctx, keyName); err != nil {
return nil, fmt.Errorf("failed to create key on source chain: %w", err)
}
}
userAccountAddressBytes, err := chain.GetAddress(ctx, keyName)
if err != nil {
return nil, fmt.Errorf("failed to get source user account address: %w", err)
}
user := ibc.Wallet{
KeyName: keyName,
Address: string(userAccountAddressBytes),
}
return &user, nil
}
// GetAndFundTestUserWithMnemonic restores a user using the given mnemonic
// and funds it with the native chain denom.
// The caller should wait for some blocks to complete before the funds will be accessible.
func GetAndFundTestUserWithMnemonic(
ctx context.Context,
keyNamePrefix, mnemonic string,
amount int64,
chain ibc.Chain,
) (*ibc.Wallet, error) {
chainCfg := chain.Config()
keyName := fmt.Sprintf("%s-%s-%s", keyNamePrefix, chainCfg.ChainID, dockerutil.RandLowerCaseLetterString(3))
user, err := generateUserWallet(ctx, keyName, mnemonic, chain)
if err != nil {
return nil, fmt.Errorf("failed to get source user wallet: %w", err)
}
err = chain.SendFunds(ctx, FaucetAccountKeyName, ibc.WalletAmount{
Address: user.Bech32Address(chainCfg.Bech32Prefix),
Amount: amount,
Denom: chainCfg.Denom,
})
if err != nil {
return nil, fmt.Errorf("failed to get funds from faucet: %w", err)
}
return user, nil
}
// GetAndFundTestUsers generates and funds chain users with the native chain denom.
// The caller should wait for some blocks to complete before the funds will be accessible.
func GetAndFundTestUsers(
t *testing.T,
ctx context.Context,
keyNamePrefix string,
amount int64,
chains ...ibc.Chain,
) []*ibc.Wallet {
users := make([]*ibc.Wallet, len(chains))
var eg errgroup.Group
for i, chain := range chains {
i := i
chain := chain
eg.Go(func() error {
user, err := GetAndFundTestUserWithMnemonic(ctx, keyNamePrefix, "", amount, chain)
if err != nil {
return err
}
users[i] = user
return nil
})
}
require.NoError(t, eg.Wait())
// TODO(nix 05-17-2022): Map with generics once using go 1.18
chainHeights := make([]testutil.ChainHeighter, len(chains))
for i := range chains {
chainHeights[i] = chains[i]
}
return users
}