-
Notifications
You must be signed in to change notification settings - Fork 14
/
agent_test.go
192 lines (176 loc) · 4.9 KB
/
agent_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
package agent_test
import (
"encoding/json"
"fmt"
"testing"
"github.com/aviate-labs/agent-go"
"github.com/aviate-labs/agent-go/certification/hashtree"
"github.com/aviate-labs/agent-go/ic"
ic0 "github.com/aviate-labs/agent-go/ic/ic"
"github.com/aviate-labs/agent-go/ic/icpledger"
"github.com/aviate-labs/agent-go/identity"
"github.com/aviate-labs/agent-go/principal"
)
var _ = new(testLogger)
func Example_anonymous_query() {
a, _ := agent.New(agent.DefaultConfig)
type Account struct {
Account string `ic:"account"`
}
var balance struct {
E8S uint64 `ic:"e8s"`
}
_ = a.Query(ic.LEDGER_PRINCIPAL, "account_balance_dfx", []any{
Account{"9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d"},
}, []any{&balance})
fmt.Println(balance.E8S)
// Output:
// 0
}
func Example_json() {
raw := `{"e8s":1}`
var balance struct {
// Tags can be combined with json tags.
E8S uint64 `ic:"e8s" json:"e8s"`
}
_ = json.Unmarshal([]byte(raw), &balance)
fmt.Println(balance.E8S)
a, _ := agent.New(agent.DefaultConfig)
if err := a.Query(ic.LEDGER_PRINCIPAL, "account_balance_dfx", []any{struct {
Account string `json:"account"`
}{
Account: "9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d",
}}, []any{&balance}); err != nil {
fmt.Println(err)
}
rawJSON, _ := json.Marshal(balance)
fmt.Println(string(rawJSON))
// Output:
// 1
// {"e8s":0}
}
func Example_query_ed25519() {
id, _ := identity.NewRandomEd25519Identity()
ledgerID, _ := principal.Decode("ryjl3-tyaaa-aaaaa-aaaba-cai")
a, _ := agent.New(agent.Config{Identity: id})
var balance struct {
E8S uint64 `ic:"e8s"`
}
_ = a.Query(ledgerID, "account_balance_dfx", []any{map[string]any{
"account": "9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d",
}}, []any{&balance})
fmt.Println(balance.E8S)
// Output:
// 0
}
func Example_query_prime256v1() {
id, _ := identity.NewRandomPrime256v1Identity()
ledgerID, _ := principal.Decode("ryjl3-tyaaa-aaaaa-aaaba-cai")
a, _ := agent.New(agent.Config{Identity: id})
var balance struct {
E8S uint64 `ic:"e8s"`
}
_ = a.Query(ledgerID, "account_balance_dfx", []any{map[string]any{
"account": "9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d",
}}, []any{&balance})
fmt.Println(balance.E8S)
// Output:
// 0
}
func Example_query_secp256k1() {
id, _ := identity.NewRandomSecp256k1Identity()
ledgerID, _ := principal.Decode("ryjl3-tyaaa-aaaaa-aaaba-cai")
a, _ := agent.New(agent.Config{Identity: id})
var balance struct {
E8S uint64 `ic:"e8s"`
}
_ = a.Query(ledgerID, "account_balance_dfx", []any{map[string]any{
"account": "9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d",
}}, []any{&balance})
fmt.Println(balance.E8S)
// Output:
// 0
}
func TestAgent_Call(t *testing.T) {
a, err := agent.New(agent.DefaultConfig)
if err != nil {
t.Fatal(err)
}
n, err := a.ReadStateCertificate(ic.REGISTRY_PRINCIPAL, [][]hashtree.Label{{hashtree.Label("subnet")}})
if err != nil {
t.Fatal(err)
}
for _, path := range hashtree.ListPaths(n, nil) {
if len(path) == 3 && string(path[0]) == "subnet" && string(path[2]) == "public_key" {
subnetID := principal.Principal{Raw: []byte(path[1])}
_ = subnetID
}
}
}
func TestAgent_Call_provisionalTopUpCanister(t *testing.T) {
a, err := ic0.NewAgent(ic.MANAGEMENT_CANISTER_PRINCIPAL, agent.DefaultConfig)
if err != nil {
t.Fatal(err)
}
if err := a.ProvisionalTopUpCanister(ic0.ProvisionalTopUpCanisterArgs{
CanisterId: ic.LEDGER_PRINCIPAL,
}); err == nil {
t.Fatal()
}
}
func TestAgent_Query_Ed25519(t *testing.T) {
id, err := identity.NewRandomEd25519Identity()
if err != nil {
t.Fatal(err)
}
a, _ := agent.New(agent.Config{
Identity: id,
})
type Account struct {
Account string `ic:"account"`
}
var balance struct {
E8S uint64 `ic:"e8s"`
}
if err := a.Query(ic.LEDGER_PRINCIPAL, "account_balance_dfx", []any{
Account{"9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d"},
}, []any{&balance}); err != nil {
t.Fatal(err)
}
}
func TestAgent_Query_Secp256k1(t *testing.T) {
id, err := identity.NewRandomSecp256k1Identity()
if err != nil {
t.Fatal(err)
}
a, _ := agent.New(agent.Config{
Identity: id,
})
type Account struct {
Account string `ic:"account"`
}
var balance struct {
E8S uint64 `ic:"e8s"`
}
if err := a.Query(ic.LEDGER_PRINCIPAL, "account_balance_dfx", []any{
Account{"9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d"},
}, []any{&balance}); err != nil {
t.Fatal(err)
}
}
func TestICPLedger_queryBlocks(t *testing.T) {
a, err := icpledger.NewAgent(ic.LEDGER_PRINCIPAL, agent.DefaultConfig)
if err != nil {
t.Fatal(err)
}
if _, err := a.QueryBlocks(icpledger.GetBlocksArgs{
Start: 1, // Start from the first block.
Length: 10, // Get the last 10 blocks.
}); err != nil {
t.Fatal(err)
}
}
type testLogger struct{}
func (t testLogger) Printf(format string, v ...any) {
fmt.Printf("[TEST]"+format+"\n", v...)
}