forked from NethermindEth/starknet.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accountrpcv02_test.go
145 lines (131 loc) · 3.78 KB
/
accountrpcv02_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
package caigo
import (
"context"
"fmt"
"math/big"
"os"
"strings"
"testing"
"time"
rpc "github.com/dontpanicdao/caigo/rpcv02"
"github.com/dontpanicdao/caigo/types"
)
// TestAccountNonce tests the account Nonce
func TestRPCv02Account_Nonce(t *testing.T) {
testConfig := beforeRPCEach(t)
type testSetType struct {
Provider *rpc.Provider
Address string
PrivateKeyEnvVar string
}
testSet := map[string][]testSetType{
"devnet": {
{
Address: DevNetAccount032Address,
PrivateKeyEnvVar: "TESTNET_ACCOUNT_PRIVATE_KEY",
},
},
"testnet": {
{
Address: TestNetAccount032Address,
PrivateKeyEnvVar: "TESTNET_ACCOUNT_PRIVATE_KEY",
},
},
"mainnet": {},
}[testEnv]
for _, test := range testSet {
account, err := NewRPCAccount(os.Getenv(test.PrivateKeyEnvVar), test.Address, testConfig.providerv02)
if err != nil {
t.Fatal(err)
}
_, err = account.Nonce(context.Background())
if err != nil {
t.Fatal(err)
}
}
}
// TestAccountEstimateFee tests the account EstimateFee
func TestRPCv02Account_EstimateFee(t *testing.T) {
testConfig := beforeRPCEach(t)
type testSetType struct {
Address string
PrivateKeyEnvVar string
Call types.FunctionCall
}
testSet := map[string][]testSetType{
"devnet": {
{
Address: DevNetAccount032Address,
PrivateKeyEnvVar: "TESTNET_ACCOUNT_PRIVATE_KEY",
Call: types.FunctionCall{
ContractAddress: types.HexToHash("0x07704fb2d72fcdae1e6f658ef8521415070a01a3bd3cc5788f7b082126922b7b"),
EntryPointSelector: "increment",
Calldata: []string{},
},
},
},
"testnet": {
{
Address: TestNetAccount032Address,
PrivateKeyEnvVar: "TESTNET_ACCOUNT_PRIVATE_KEY",
Call: types.FunctionCall{
ContractAddress: types.HexToHash("0x357b37bf12f59dd04c4da4933dcadf4a104e158365886d64ca0e554ada68fef"),
EntryPointSelector: "increment",
Calldata: []string{},
},
},
},
"mainnet": {},
}[testEnv]
for _, test := range testSet {
account, err := NewRPCAccount(os.Getenv(test.PrivateKeyEnvVar), test.Address, testConfig.providerv02)
if err != nil {
t.Fatal(err)
}
estimate, err := account.EstimateFee(context.Background(), []types.FunctionCall{test.Call}, types.ExecuteDetails{})
if err != nil {
t.Fatal(err)
}
if types.HexToBN(string(estimate.OverallFee)).Cmp(big.NewInt(1000000)) < 0 {
t.Fatal("OverallFee should be > 1000000, instead:", estimate.OverallFee)
}
}
}
// TestRPCAccount_Execute tests the account Execute method
func TestRPCv02Account_Execute(t *testing.T) {
testConfig := beforeRPCEach(t)
type testSetType struct {
Address string
PrivateKeyEnvVar string
Call types.FunctionCall
}
testSet := map[string][]testSetType{
"devnet": {},
"testnet": {},
"mainnet": {},
}[testEnv]
for _, test := range testSet {
account, err := NewRPCAccount(os.Getenv(test.PrivateKeyEnvVar), test.Address, testConfig.providerv02)
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
execute, err := account.Execute(ctx, []types.FunctionCall{test.Call}, types.ExecuteDetails{})
if err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(execute.TransactionHash, "0x") {
t.Fatal("TransactionHash start with 0x, instead:", execute.TransactionHash)
}
fmt.Println("transaction_hash:", execute.TransactionHash)
ctx, cancel := context.WithTimeout(ctx, 600*time.Second)
defer cancel()
status, err := account.rpcv01.WaitForTransaction(ctx, types.HexToHash(execute.TransactionHash), 8*time.Second)
if err != nil {
t.Fatal("declare should succeed, instead:", err)
}
if status != "PENDING" && status != "ACCEPTED_ON_L1" && status != "ACCEPTED_ON_L2" {
t.Fatalf("tx %s wrong status: %s", execute.TransactionHash, status)
}
}
}