forked from NethermindEth/starknet.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accountgw_test.go
90 lines (84 loc) · 2.63 KB
/
accountgw_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
package caigo
import (
"context"
"fmt"
"testing"
"time"
"github.com/dontpanicdao/caigo/types"
)
type TestAccountType struct {
PrivateKey string `json:"private_key"`
PublicKey string `json:"public_key"`
Address string `json:"address"`
Transactions []types.FunctionCall `json:"transactions,omitempty"`
}
func TestGatewayAccount_EstimateAndExecute(t *testing.T) {
testConfig := beforeGatewayEach(t)
type testSetType struct {
ExecuteCalls []types.FunctionCall
QueryCall types.FunctionCall
}
testSet := map[string][]testSetType{
"devnet": {{
ExecuteCalls: []types.FunctionCall{{
EntryPointSelector: "increment",
ContractAddress: types.HexToHash(testConfig.CounterAddress),
}},
QueryCall: types.FunctionCall{
EntryPointSelector: "get_count",
ContractAddress: types.HexToHash(testConfig.CounterAddress),
},
}},
"testnet": {{
ExecuteCalls: []types.FunctionCall{{
EntryPointSelector: "increment",
ContractAddress: types.HexToHash(testConfig.CounterAddress),
}},
QueryCall: types.FunctionCall{
EntryPointSelector: "get_count",
ContractAddress: types.HexToHash(testConfig.CounterAddress),
},
}},
}[testEnv]
for _, test := range testSet {
account, err := NewGatewayAccount(
testConfig.AccountPrivateKey,
testConfig.AccountAddress,
testConfig.client,
AccountVersion1)
if err != nil {
t.Fatal("should access the existing accounts", err)
}
if err != nil {
t.Fatal("should access the existing accounts", err)
}
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Second*60)
defer cancel()
estimateFee, err := account.EstimateFee(ctx, test.ExecuteCalls, types.ExecuteDetails{})
if err != nil {
t.Fatal("should succeed with EstimateFee, instead:", err)
}
fmt.Printf("estimate fee is %+v\n", estimateFee)
tx, err := account.Execute(ctx, test.ExecuteCalls, types.ExecuteDetails{})
if err != nil {
t.Fatal("should succeed with Execute, instead:", err)
}
fmt.Printf("Execute txHash: %v\n", tx.TransactionHash)
_, state, err := testConfig.client.WaitForTransaction(ctx, tx.TransactionHash, 3, 10)
if err != nil {
t.Fatal("should succeed with Execute, instead:", err)
}
if state.Status != types.TransactionAcceptedOnL1 && state.Status != types.TransactionAcceptedOnL2 {
t.Fatal("should be final, instead:", state.Status)
}
result, err := account.Call(ctx, test.QueryCall)
if err != nil {
t.Fatal("should succeed with Call, instead:", err)
}
if len(result) == 0 {
t.Fatal("should return data, instead 0")
}
fmt.Println("count is now: ", result[0])
}
}