1
1
package agent_test
2
2
3
3
import (
4
+ "encoding/hex"
4
5
"encoding/json"
5
6
"fmt"
6
7
"testing"
@@ -19,20 +20,38 @@ var (
19
20
20
21
var _ = new (testLogger )
21
22
22
- func Example_anonymous_query () {
23
+ func Example_anonymous_call () {
23
24
a , _ := agent .New (agent .DefaultConfig )
24
- type Account struct {
25
- Account string `ic:"account "`
25
+ var balance struct {
26
+ E8S uint64 `ic:"e8s "`
26
27
}
28
+
29
+ accountID , _ := hex .DecodeString ("9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d" )
30
+ err := a .Call (LEDGER_PRINCIPAL , "account_balance" , []any {
31
+ struct {
32
+ Account []byte `ic:"account"`
33
+ }{Account : accountID },
34
+ }, []any {& balance })
35
+ fmt .Println (balance .E8S , err )
36
+ // Output:
37
+ // 0 <nil>
38
+ }
39
+
40
+ func Example_anonymous_query () {
41
+ a , _ := agent .New (agent .DefaultConfig )
27
42
var balance struct {
28
43
E8S uint64 `ic:"e8s"`
29
44
}
30
- _ = a .Query (LEDGER_PRINCIPAL , "account_balance_dfx" , []any {
31
- Account {"9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d" },
45
+
46
+ accountID , _ := hex .DecodeString ("9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d" )
47
+ err := a .Query (LEDGER_PRINCIPAL , "account_balance" , []any {
48
+ struct {
49
+ Account []byte `ic:"account"`
50
+ }{Account : accountID },
32
51
}, []any {& balance })
33
- fmt .Println (balance .E8S )
52
+ fmt .Println (balance .E8S , err )
34
53
// Output:
35
- // 0
54
+ // 0 <nil>
36
55
}
37
56
38
57
func Example_json () {
@@ -45,10 +64,11 @@ func Example_json() {
45
64
fmt .Println (balance .E8S )
46
65
47
66
a , _ := agent .New (agent .DefaultConfig )
48
- if err := a .Query (LEDGER_PRINCIPAL , "account_balance_dfx" , []any {struct {
49
- Account string `json:"account"`
67
+ accountID , _ := hex .DecodeString ("9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d" )
68
+ if err := a .Query (LEDGER_PRINCIPAL , "account_balance" , []any {struct {
69
+ Account []byte `json:"account"`
50
70
}{
51
- Account : "9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d" ,
71
+ Account : accountID ,
52
72
}}, []any {& balance }); err != nil {
53
73
fmt .Println (err )
54
74
}
@@ -66,8 +86,10 @@ func Example_query_ed25519() {
66
86
var balance struct {
67
87
E8S uint64 `ic:"e8s"`
68
88
}
69
- _ = a .Query (ledgerID , "account_balance_dfx" , []any {map [string ]any {
70
- "account" : "9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d" ,
89
+
90
+ accountID , _ := hex .DecodeString ("9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d" )
91
+ _ = a .Query (ledgerID , "account_balance" , []any {map [string ]any {
92
+ "account" : accountID ,
71
93
}}, []any {& balance })
72
94
fmt .Println (balance .E8S )
73
95
// Output:
@@ -76,13 +98,14 @@ func Example_query_ed25519() {
76
98
77
99
func Example_query_prime256v1 () {
78
100
id , _ := identity .NewRandomPrime256v1Identity ()
79
- ledgerID := principal .MustDecode ("ryjl3-tyaaa-aaaaa-aaaba-cai" )
80
101
a , _ := agent .New (agent.Config {Identity : id })
81
102
var balance struct {
82
103
E8S uint64 `ic:"e8s"`
83
104
}
84
- _ = a .Query (ledgerID , "account_balance_dfx" , []any {map [string ]any {
85
- "account" : "9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d" ,
105
+
106
+ accountID , _ := hex .DecodeString ("9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d" )
107
+ _ = a .Query (LEDGER_PRINCIPAL , "account_balance" , []any {map [string ]any {
108
+ "account" : accountID ,
86
109
}}, []any {& balance })
87
110
fmt .Println (balance .E8S )
88
111
// Output:
@@ -91,13 +114,14 @@ func Example_query_prime256v1() {
91
114
92
115
func Example_query_secp256k1 () {
93
116
id , _ := identity .NewRandomSecp256k1Identity ()
94
- ledgerID := principal .MustDecode ("ryjl3-tyaaa-aaaaa-aaaba-cai" )
95
117
a , _ := agent .New (agent.Config {Identity : id })
96
118
var balance struct {
97
119
E8S uint64 `ic:"e8s"`
98
120
}
99
- _ = a .Query (ledgerID , "account_balance_dfx" , []any {map [string ]any {
100
- "account" : "9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d" ,
121
+
122
+ accountID , _ := hex .DecodeString ("9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d" )
123
+ _ = a .Query (LEDGER_PRINCIPAL , "account_balance" , []any {map [string ]any {
124
+ "account" : accountID ,
101
125
}}, []any {& balance })
102
126
fmt .Println (balance .E8S )
103
127
// Output:
@@ -129,14 +153,15 @@ func TestAgent_Query_Ed25519(t *testing.T) {
129
153
a , _ := agent .New (agent.Config {
130
154
Identity : id ,
131
155
})
132
- type Account struct {
133
- Account string `ic:"account"`
134
- }
135
156
var balance struct {
136
157
E8S uint64 `ic:"e8s"`
137
158
}
138
- if err := a .Query (LEDGER_PRINCIPAL , "account_balance_dfx" , []any {
139
- Account {"9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d" },
159
+
160
+ accountID , _ := hex .DecodeString ("9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d" )
161
+ if err := a .Query (LEDGER_PRINCIPAL , "account_balance" , []any {
162
+ struct {
163
+ Account []byte `ic:"account"`
164
+ }{Account : accountID },
140
165
}, []any {& balance }); err != nil {
141
166
t .Fatal (err )
142
167
}
@@ -150,14 +175,15 @@ func TestAgent_Query_Secp256k1(t *testing.T) {
150
175
a , _ := agent .New (agent.Config {
151
176
Identity : id ,
152
177
})
153
- type Account struct {
154
- Account string `ic:"account"`
155
- }
156
178
var balance struct {
157
179
E8S uint64 `ic:"e8s"`
158
180
}
159
- if err := a .Query (LEDGER_PRINCIPAL , "account_balance_dfx" , []any {
160
- Account {"9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d" },
181
+
182
+ accountID , _ := hex .DecodeString ("9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d" )
183
+ if err := a .Query (LEDGER_PRINCIPAL , "account_balance" , []any {
184
+ struct {
185
+ Account []byte `ic:"account"`
186
+ }{Account : accountID },
161
187
}, []any {& balance }); err != nil {
162
188
t .Fatal (err )
163
189
}
@@ -168,7 +194,6 @@ func TestAgent_Query_callback(t *testing.T) {
168
194
if err != nil {
169
195
t .Fatal (err )
170
196
}
171
- ledgerCanisterID := principal .MustDecode ("ryjl3-tyaaa-aaaaa-aaaba-cai" )
172
197
173
198
type GetBlocksArgs struct {
174
199
Start uint64 `ic:"start" json:"start"`
@@ -195,7 +220,7 @@ func TestAgent_Query_callback(t *testing.T) {
195
220
}
196
221
req , err := a .CreateCandidAPIRequest (
197
222
agent .RequestTypeQuery ,
198
- ledgerCanisterID ,
223
+ LEDGER_PRINCIPAL ,
199
224
"query_blocks" ,
200
225
args ,
201
226
)
@@ -301,6 +326,15 @@ func TestAgent_Query_callback(t *testing.T) {
301
326
}
302
327
}
303
328
329
+ func TestCall_invalid (t * testing.T ) {
330
+ a , _ := agent .New (agent .DefaultConfig )
331
+ qErr := a .Query (LEDGER_PRINCIPAL , "account_balance" , []any {}, []any {})
332
+ cErr := a .Call (LEDGER_PRINCIPAL , "account_balance" , []any {}, []any {})
333
+ if qErr != cErr {
334
+ t .Error (qErr , cErr )
335
+ }
336
+ }
337
+
304
338
type testLogger struct {}
305
339
306
340
func (t testLogger ) Printf (format string , v ... any ) {
0 commit comments