forked from NethermindEth/starknet.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
providers_test.go
280 lines (258 loc) · 8.9 KB
/
providers_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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package caigo
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"math/big"
"os"
"strings"
"testing"
"time"
"github.com/dontpanicdao/caigo/artifacts"
"github.com/dontpanicdao/caigo/gateway"
"github.com/dontpanicdao/caigo/rpcv01"
"github.com/dontpanicdao/caigo/rpcv02"
devtest "github.com/dontpanicdao/caigo/test"
"github.com/dontpanicdao/caigo/types"
ethrpc "github.com/ethereum/go-ethereum/rpc"
"github.com/joho/godotenv"
)
const (
TestPublicKey = "0x783318b2cc1067e5c06d374d2bb9a0382c39aabd009b165d7a268b882971d6"
DevNetETHAddress = "0x62230ea046a9a5fbc261ac77d03c8d41e5d442db2284587570ab46455fd2488"
TestNetETHAddress = "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"
DevNetAccount032Address = "0x0536244bba4dc9bb219d964b477af6d18f7096635a96284bb0e008bf137650ec"
TestNetAccount032Address = "0x6ca4fdd437dffde5253ba7021ef7265c88b07789aa642eafda37791626edf00"
DevNetAccount040Address = "0x058079067104f58fd9f1ef949cd2d2b482d7bca39b793983f077edaf51d979e9"
TestNetAccount040Address = "0x6cbfa37f409610fee26eeb427ed854b3a4b24580d9b9ef6c3e38db7b3f7322c"
TestnetCounterAddress = "0x51e94d515df16ecae5be4a377666121494eb54193d854fcf5baba2b0da679c6"
)
// testGatewayConfiguration is a type that is used to configure tests
type testGatewayConfiguration struct {
client *gateway.GatewayProvider
base string
CounterAddress string
AccountAddress string
AccountPrivateKey string
}
var (
// set the environment for the test, default: mock
testEnv = "mock"
// testConfigurations are predefined test configurations
testRPCConfigurations = map[string]testRPCConfiguration{
// Requires a Mainnet StarkNet JSON-RPC compliant node (e.g. pathfinder)
// (ref: https://github.com/eqlabs/pathfinder)
"mainnet": {
base: "http://localhost:9545",
},
// Requires a Testnet StarkNet JSON-RPC compliant node (e.g. pathfinder)
// (ref: https://github.com/eqlabs/pathfinder)
"testnet": {
base: "http://localhost:9545",
},
// Requires a Devnet configuration running locally
// (ref: https://github.com/Shard-Labs/starknet-devnet)
"devnet": {
base: "http://localhost:5050/rpc",
},
// Used with a mock as a standard configuration, see `mock_test.go``
"mock": {},
}
testGatewayConfigurations = map[string]testGatewayConfiguration{
"mainnet": {
base: "https://alpha4-mainnet.starknet.io",
},
// Requires a Testnet StarkNet JSON-RPC compliant node (e.g. pathfinder)
// (ref: https://github.com/eqlabs/pathfinder)
"testnet": {
base: "https://alpha4.starknet.io",
CounterAddress: TestnetCounterAddress,
AccountAddress: TestNetAccount040Address,
},
// Requires a Devnet configuration running locally
// (ref: https://github.com/Shard-Labs/starknet-devnet)
"devnet": {
base: "http://localhost:5050",
},
// Used with a mock as a standard configuration, see `mock_test.go``
"mock": {},
}
)
func InstallCounterContract(provider *gateway.GatewayProvider) (string, error) {
class := types.ContractClass{}
if err := json.Unmarshal(artifacts.CounterCompiled, &class); err != nil {
return "", err
}
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Second*60)
defer cancel()
tx, err := provider.Deploy(context.Background(), class, types.DeployRequest{
ContractAddressSalt: "0x0",
ConstructorCalldata: []string{},
})
if err != nil {
return "", err
}
fmt.Println("deploy counter txHash", tx.TransactionHash)
_, receipt, err := provider.WaitForTransaction(ctx, tx.TransactionHash, 3, 20)
if err != nil {
log.Printf("contract Address: %s\n", tx.ContractAddress)
log.Printf("transaction Hash: %s\n", tx.TransactionHash)
return "", err
}
if !receipt.Status.IsTransactionFinal() ||
receipt.Status == types.TransactionRejected {
return "", fmt.Errorf("installation status: %s", receipt.Status)
}
return tx.ContractAddress, nil
}
// beforeEach checks the configuration and initializes it before running the script
func beforeGatewayEach(t *testing.T) *testGatewayConfiguration {
t.Helper()
godotenv.Load(fmt.Sprintf(".env.%s", testEnv), ".env")
testConfig, ok := testGatewayConfigurations[testEnv]
if !ok {
t.Fatal("env supports testnet, mainnet or devnet")
}
switch testEnv {
default:
testConfig.client = gateway.NewProvider(gateway.WithBaseURL(testConfig.base))
}
t.Cleanup(func() {
})
return &testConfig
}
// testConfiguration is a type that is used to configure tests
type testRPCConfiguration struct {
providerv01 *rpcv01.Provider
providerv02 *rpcv02.Provider
base string
}
// TestMain is used to trigger the tests and, in that case, check for the environment to use.
func TestMain(m *testing.M) {
baseURL := ""
flag.StringVar(&testEnv, "env", "mock", "set the test environment")
flag.StringVar(&baseURL, "base-url", "", "change the baseUrl")
flag.Parse()
godotenv.Load(fmt.Sprintf(".env.%s", testEnv), ".env")
if baseURL != "" {
gwLocalConfig := testGatewayConfigurations[testEnv]
gwLocalConfig.base = baseURL
testGatewayConfigurations[testEnv] = gwLocalConfig
rpcLocalConfig := testRPCConfigurations[testEnv]
rpcLocalConfig.base = baseURL
testRPCConfigurations[testEnv] = rpcLocalConfig
}
switch testEnv {
case "devnet":
provider := gateway.NewProvider(gateway.WithBaseURL(testGatewayConfigurations["devnet"].base))
counterAddress, err := InstallCounterContract(provider)
if err != nil {
fmt.Println("error installing counter contract", err)
os.Exit(1)
}
localConfig := testGatewayConfigurations[testEnv]
accounts, err := devtest.NewDevNet().Accounts()
if err != nil {
fmt.Println("error getting devnet accounts", err)
os.Exit(1)
}
localConfig.AccountAddress = accounts[0].Address
localConfig.AccountPrivateKey = accounts[0].PrivateKey
localConfig.CounterAddress = counterAddress
testGatewayConfigurations[testEnv] = localConfig
case "testnet":
localConfig := testGatewayConfigurations[testEnv]
localConfig.AccountPrivateKey = os.Getenv("TESTNET_ACCOUNT_PRIVATE_KEY")
testGatewayConfigurations[testEnv] = localConfig
}
os.Exit(m.Run())
}
// beforeEach checks the configuration and initializes it before running the script
func beforeRPCEach(t *testing.T) *testRPCConfiguration {
t.Helper()
godotenv.Load(fmt.Sprintf(".env.%s", testEnv), ".env")
testConfig, ok := testRPCConfigurations[testEnv]
if !ok {
t.Fatal("env supports mock, testnet, mainnet or devnet")
}
testConfig.base = "https://starknet-goerli.cartridge.gg"
base := os.Getenv("INTEGRATION_BASE")
if base != "" {
testConfig.base = base
}
c, err := ethrpc.DialContext(context.Background(), testConfig.base)
if err != nil {
t.Fatal("connect should succeed, instead:", err)
}
clientv01 := rpcv01.NewProvider(c)
testConfig.providerv01 = clientv01
clientv02 := rpcv02.NewProvider(c)
testConfig.providerv02 = clientv02
return &testConfig
}
// TestChainID checks the chainId matches the one for the environment
func TestGeneral_ChainID(t *testing.T) {
testConfig := beforeRPCEach(t)
type testSetType struct {
ChainID string
}
testSet := map[string][]testSetType{
"devnet": {{ChainID: "SN_GOERLI"}},
"mainnet": {{ChainID: "SN_MAIN"}},
"mock": {{ChainID: "MOCK"}},
"testnet": {{ChainID: "SN_GOERLI"}},
}[testEnv]
fmt.Printf("----------------------------\n")
fmt.Printf("Env: %s\n", testEnv)
fmt.Printf("Url: %s\n", testConfig.base)
fmt.Printf("----------------------------\n")
for _, test := range testSet {
chain, err := testConfig.providerv01.ChainID(context.Background())
if err != nil {
t.Fatal(err)
}
if chain != test.ChainID {
t.Fatalf("expecting %s, instead: %s", test.ChainID, chain)
}
}
}
// TestSyncing checks the values returned are consistent
func TestGeneral_Syncing(t *testing.T) {
testConfig := beforeRPCEach(t)
type testSetType struct {
ChainID string
}
testSet := map[string][]testSetType{
"devnet": {},
"mainnet": {{ChainID: "SN_MAIN"}},
"mock": {{ChainID: "MOCK"}},
"testnet": {{ChainID: "SN_GOERLI"}},
}[testEnv]
for range testSet {
syncv01, err := testConfig.providerv01.Syncing(context.Background())
if err != nil {
t.Fatal("BlockWithTxHashes match the expected error:", err)
}
i, ok := big.NewInt(0).SetString(syncv01.CurrentBlockNum, 0)
if !ok || i.Cmp(big.NewInt(0)) <= 0 {
t.Fatal("CurrentBlockNum should be positive number, instead: ", syncv01.CurrentBlockNum)
}
if !strings.HasPrefix(syncv01.CurrentBlockHash, "0x") {
t.Fatal("current block hash should return a string starting with 0x")
}
syncv02, err := testConfig.providerv02.Syncing(context.Background())
if err != nil {
t.Fatal("BlockWithTxHashes match the expected error:", err)
}
i, ok = big.NewInt(0).SetString(string(syncv02.CurrentBlockNum), 0)
if !ok || i.Cmp(big.NewInt(0)) <= 0 {
t.Fatal("CurrentBlockNum should be positive number, instead: ", syncv02.CurrentBlockNum)
}
if !strings.HasPrefix(syncv02.CurrentBlockHash, "0x") {
t.Fatal("current block hash should return a string starting with 0x")
}
}
}