-
Notifications
You must be signed in to change notification settings - Fork 40
/
client_main_test.go
217 lines (182 loc) · 7.07 KB
/
client_main_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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package seth_test
import (
"context"
"math/big"
"os"
"testing"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"github.com/smartcontractkit/chainlink-testing-framework/seth"
network_debug_contract "github.com/smartcontractkit/chainlink-testing-framework/seth/contracts/bind/NetworkDebugContract"
network_sub_contract "github.com/smartcontractkit/chainlink-testing-framework/seth/contracts/bind/NetworkDebugSubContract"
link_token "github.com/smartcontractkit/chainlink-testing-framework/seth/contracts/bind/link"
)
/*
Some tests should be run on testnets/mainnets, so we are deploying the contract only once,
for these types of tests it's always a choice between funds/speed of tests
If you need unique setup, just use NewDebugContractSetup in tests
*/
func init() {
_ = os.Setenv("SETH_CONFIG_PATH", "seth.toml")
}
var (
TestEnv TestEnvironment
)
type TestEnvironment struct {
Client *seth.Client
DebugContract *network_debug_contract.NetworkDebugContract
DebugSubContract *network_sub_contract.NetworkDebugSubContract
LinkTokenContract *link_token.LinkToken
DebugContractAddress common.Address
DebugSubContractAddress common.Address
DebugContractRaw *bind.BoundContract
ContractMap seth.ContractMap
}
func newClient(t *testing.T) *seth.Client {
c, err := seth.NewClient()
require.NoError(t, err, "failed to initialize seth")
return c
}
func newClientWithEphemeralAddresses(t *testing.T) *seth.Client {
cfg, err := seth.ReadConfig()
require.NoError(t, err, "failed to read config")
var sixty int64 = 60
cfg.EphemeralAddrs = &sixty
c, err := seth.NewClientWithConfig(cfg)
require.NoError(t, err, "failed to initialize seth")
return c
}
func TestDeploymentLinkTokenFromGethWrapperExample(t *testing.T) {
c, err := seth.NewClient()
require.NoError(t, err, "failed to initialize seth")
abi, err := link_token.LinkTokenMetaData.GetAbi()
require.NoError(t, err, "failed to get ABI")
contractData, err := c.DeployContract(c.NewTXOpts(), "LinkToken", *abi, []byte(link_token.LinkTokenMetaData.Bin))
require.NoError(t, err, "failed to deploy link token contract from wrapper's ABI/BIN")
contract, err := link_token.NewLinkToken(contractData.Address, c.Client)
require.NoError(t, err, "failed to create debug contract instance")
_, err = c.Decode(contract.Mint(c.NewTXOpts(), common.Address{}, big.NewInt(1)))
require.NoError(t, err, "failed to decode transaction")
}
func TestDeploymentAbortedWhenContextHasError(t *testing.T) {
c, err := seth.NewClient()
require.NoError(t, err, "failed to initialize seth")
abi, err := link_token.LinkTokenMetaData.GetAbi()
require.NoError(t, err, "failed to get ABI")
opts := c.NewTXOpts()
opts.Context = context.WithValue(context.Background(), seth.ContextErrorKey{}, errors.New("context error"))
_, err = c.DeployContract(opts, "LinkToken", *abi, []byte(link_token.LinkTokenMetaData.Bin))
require.Error(t, err, "did not abort deployment of link token contract due to context error")
require.Contains(t, err.Error(), "aborted contract deployment for", "incorrect context error")
}
func newClientWithContractMapFromEnv(t *testing.T) *seth.Client {
c := newClient(t)
if TestEnv.ContractMap.Size() == 0 {
t.Fatal("contract map is empty")
}
// create a copy of the map, so we don't have problem with side effects of modifying client's map
// impacting the global, underlying one
contractMap := seth.NewEmptyContractMap()
for k, v := range TestEnv.ContractMap.GetContractMap() {
contractMap.AddContract(k, v)
}
c.ContractAddressToNameMap = contractMap
// now let's recreate the Tracer, so that it has the same contract map
tracer, err := seth.NewTracer(c.ContractStore, c.ABIFinder, c.Cfg, contractMap, c.Addresses)
require.NoError(t, err, "failed to create tracer")
c.Tracer = tracer
c.ABIFinder.ContractMap = contractMap
return c
}
func NewDebugContractSetup() (
*seth.Client,
*network_debug_contract.NetworkDebugContract,
common.Address,
common.Address,
*bind.BoundContract,
error,
) {
cfg, err := seth.ReadConfig()
if err != nil {
return nil, nil, common.Address{}, common.Address{}, nil, err
}
cs, err := seth.NewContractStore("./contracts/abi", "./contracts/bin", nil)
if err != nil {
return nil, nil, common.Address{}, common.Address{}, nil, err
}
addrs, pkeys, err := cfg.ParseKeys()
if err != nil {
return nil, nil, common.Address{}, common.Address{}, nil, err
}
contractMap := seth.NewEmptyContractMap()
abiFinder := seth.NewABIFinder(contractMap, cs)
tracer, err := seth.NewTracer(cs, &abiFinder, cfg, contractMap, addrs)
if err != nil {
return nil, nil, common.Address{}, common.Address{}, nil, err
}
nm, err := seth.NewNonceManager(cfg, addrs, pkeys)
if err != nil {
return nil, nil, common.Address{}, common.Address{}, nil, errors.Wrap(err, seth.ErrCreateNonceManager)
}
c, err := seth.NewClientRaw(cfg, addrs, pkeys, seth.WithContractStore(cs), seth.WithTracer(tracer), seth.WithNonceManager(nm))
if err != nil {
return nil, nil, common.Address{}, common.Address{}, nil, err
}
subData, err := c.DeployContractFromContractStore(c.NewTXOpts(), "NetworkDebugSubContract.abi")
if err != nil {
return nil, nil, common.Address{}, common.Address{}, nil, err
}
data, err := c.DeployContractFromContractStore(c.NewTXOpts(), "NetworkDebugContract.abi", subData.Address)
if err != nil {
return nil, nil, common.Address{}, common.Address{}, nil, err
}
contract, err := network_debug_contract.NewNetworkDebugContract(data.Address, c.Client)
if err != nil {
return nil, nil, common.Address{}, common.Address{}, nil, err
}
return c, contract, data.Address, subData.Address, data.BoundContract, nil
}
func TestMain(m *testing.M) {
if skip := os.Getenv("SKIP_MAIN_CONFIG"); skip == "" {
var err error
client, debugContract, debugContractAddress, debugSubContractAddress, debugContractRaw, err := NewDebugContractSetup()
if err != nil {
panic(err)
}
linkTokenAbi, err := link_token.LinkTokenMetaData.GetAbi()
if err != nil {
panic(err)
}
linkDeploymentData, err := client.DeployContract(client.NewTXOpts(), "LinkToken", *linkTokenAbi, common.FromHex(link_token.LinkTokenMetaData.Bin))
if err != nil {
panic(err)
}
linkToken, err := link_token.NewLinkToken(linkDeploymentData.Address, client.Client)
if err != nil {
panic(err)
}
linkAbi, err := link_token.LinkTokenMetaData.GetAbi()
if err != nil {
panic(err)
}
client.ContractStore.AddABI("LinkToken", *linkAbi)
contractMap := seth.NewEmptyContractMap()
for k, v := range client.ContractAddressToNameMap.GetContractMap() {
contractMap.AddContract(k, v)
}
TestEnv = TestEnvironment{
Client: client,
DebugContract: debugContract,
LinkTokenContract: linkToken,
DebugContractAddress: debugContractAddress,
DebugSubContractAddress: debugSubContractAddress,
DebugContractRaw: debugContractRaw,
ContractMap: contractMap,
}
} else {
seth.L.Warn().Msg("Skipping main suite setup")
}
os.Exit(m.Run())
}