-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
323 lines (295 loc) · 7.03 KB
/
client_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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package client
import (
"fmt"
"testing"
)
type exampleWallet struct {
address string
name string
}
type exampleBlock struct {
ID int `json:"id"`
Hash string `json:"hash"`
Size int `json:"size"`
TransactionsCount int `json:"transactions_count"`
Header *BlockHeader
}
type exampleTXN struct {
ID int `json:"id"`
BlockID int `json:"block_id"`
Attributes string `json:"attributes"`
Fee int `json:"fee"`
Hash string `json:"hash"`
Nonce string `json:"nonce"`
TxType string `json:"txType"`
BlockHeight int `json:"block_height"`
CreatedAt string `json:"created_at"`
Payload *TransactionPayload
}
var (
wallet exampleWallet
block exampleBlock
txn exampleTXN
c Client
)
func getClient() Client {
c := New()
c.SetAddress("https://openapi.nkn.org/api/v1/")
return c
}
func TestGetRegisteredNames(t *testing.T) {
c := getClient()
resp, err := c.GetRegisteredNames()
if err != nil {
t.Fatal(err)
}
fmt.Printf("[TestGetRegisteredNames] Address: %s - Name: %s\n", resp.Data[0].Address, resp.Data[0].Name)
if len(resp.Data) > 0 {
wallet.address = resp.Data[0].Address
wallet.name = resp.Data[0].Name
}
if !resp.HasMore() {
return
}
// now fetch next page
err = c.Next(resp)
if err != nil {
t.Fatal(err)
}
fmt.Printf("[TestGetRegisteredNames] Address: %s - Name: %s\n", resp.Data[0].Address, resp.Data[0].Name)
if len(resp.Data) > 0 {
wallet.address = resp.Data[0].Address
wallet.name = resp.Data[0].Name
}
}
func TestGetRegisteredNamesByAddress(t *testing.T) {
c := getClient()
resp, err := c.GetRegisteredNamesByAddress(wallet.address)
if err != nil {
t.Fatal(err)
}
for _, r := range resp {
fmt.Printf("[TestGetRegisteredNamesByAddress] Address: %s - Name: %s\n", r.Address, r.Name)
}
}
func TestGetRegisteredNamesByName(t *testing.T) {
c := getClient()
resp, err := c.GetAddressByRegisteredName(wallet.name)
if err != nil {
t.Fatal(err)
}
if resp != nil {
fmt.Printf("[TestGetRegisteredNamesByName] Address: %s - Name: %s\n", resp.Address, wallet.name)
}
}
func TestGetAddresses(t *testing.T) {
c := getClient()
resp, err := c.GetAddresses()
if err != nil {
t.Error(err)
}
if resp == nil {
t.Fatalf("Response has no data")
}
if len(resp.Addresses.Data) == 0 {
t.Fatalf("Response has no data")
}
fmt.Printf("[TestGetAddresses] %#v\n", resp.Addresses.Data[0])
if !resp.Addresses.HasMore() {
return
}
// now fetch next page
err = c.Next(resp)
if err != nil {
t.Fatal(err)
}
fmt.Printf("[TestGetAddresses] %#v\n", resp.Addresses.Data[0])
}
func TestGetSingleAddress(t *testing.T) {
c := getClient()
resp, err := c.GetSingleAddress(wallet.address)
if err != nil {
t.Error(err)
}
if resp == nil {
t.Fatalf("Response has no data")
}
fmt.Printf("[TestGetAddresses] %#v\n", resp)
}
func TestGetAddressTransactions(t *testing.T) {
c := getClient()
resp, err := c.GetAddressTransactions(wallet.address)
if err != nil {
t.Error(err)
}
if resp == nil {
t.Fatalf("Response has no data")
}
fmt.Printf("[TestGetAddressesTransactions] %#v\n", resp)
}
func TestGetAllBlocks(t *testing.T) {
c := getClient()
resp, err := c.GetAllBlocks()
if err != nil {
t.Error(err)
}
if resp == nil {
t.Fatalf("Response has no data")
}
fmt.Println(len(resp.Blocks.Data))
fmt.Printf("[TestGetAllBlocks] %#v\n", resp.Blocks.Data[0])
block = resp.Blocks.Data[0]
if !resp.Blocks.HasMore() {
return
}
// now fetch next page
err = c.Next(resp)
if err != nil {
t.Fatal(err)
}
fmt.Printf("[TestGetAllBlocks] %#v\n", resp.Blocks.Data[0])
block = resp.Blocks.Data[0]
}
func TestGetBlockByHeight(t *testing.T) {
c := getClient()
resp, err := c.GetBlockByHeight(block.ID)
if err != nil {
t.Error(err)
}
if resp == nil {
t.Fatalf("Response has no data")
}
fmt.Printf("[TestGetBlockByHeight] %#v\n", resp)
}
func TestGetBlockByHash(t *testing.T) {
c := getClient()
resp, err := c.GetBlockByHash(block.Hash)
if err != nil {
t.Error(err)
}
if resp == nil {
t.Fatalf("Response has no data")
}
fmt.Printf("[TestGetBlockByHash] %#v\n", resp)
}
func TestGetTransactionsByBlockHeight(t *testing.T) {
c := getClient()
resp, err := c.GetTransactionsByBlockHeight(block.ID)
if err != nil {
t.Error(err)
}
if resp == nil {
t.Fatalf("Response has no data")
}
fmt.Printf("[TestGetTransactionsByBlockHeight] %#v\n", resp)
}
// according to docs querying txn by block hash is possible. but it isn't.
// reported bug to rule110 team on discord https://discord.com/channels/443413382737952778/724296220222160946
// func TestGetTransactionsByBlockHash(t *testing.T) {
// c := getClient()
// resp, err := c.GetTransactionsByBlockHash(block.Hash)
// if err != nil {
// t.Error(err)
// }
//
// if resp == nil {
// t.Fatalf("Response has no data")
// }
//
// fmt.Printf("[TestGetTransactionsByBlockHash] %#v\n", resp)
// }
func TestGetAllSigchains(t *testing.T) {
c := getClient()
resp, err := c.GetAllSigchains()
if err != nil {
t.Error(err)
}
if resp == nil {
t.Fatalf("Response has no data")
}
fmt.Printf("[TestGetAllSigchains] %#v\n", resp.Data[0])
if !resp.MetaData.HasMore() {
return
}
// now fetch next page
err = c.Next(resp)
if err != nil {
t.Fatal(err)
}
fmt.Printf("[TestGetAllSigchains] %#v\n", resp.Data[0])
}
func TestGetAllTransactions(t *testing.T) {
c := getClient()
resp, err := c.GetAllTransactions()
if err != nil {
t.Error(err)
}
if resp == nil {
t.Fatalf("Response has no data")
}
fmt.Printf("[TestGetAllTransactions] %#v\n", resp.Transactions.Data[0])
txn = resp.Transactions.Data[0]
// test c.Next() implementation
// save a txn of first page
tmp := resp.Transactions.Data[0].ID
// check if next page is available
if !resp.Transactions.HasMore() {
return
}
// now fetch next page
err = c.Next(resp)
if err != nil {
t.Fatal(err)
}
// check if this txn is the same as txn above
// if NOT, c.Next() works
if tmp == resp.Transactions.Data[0].ID {
t.Fatal("EQUAL")
}
fmt.Printf("[TestGetAllTransactions] %#v\n", resp.Transactions.Data[0])
txn = resp.Transactions.Data[0]
}
func TestGetTransactionByHash(t *testing.T) {
c := getClient()
resp, err := c.GetTransactionByHash(txn.Hash)
if err != nil {
t.Error(err)
}
if resp == nil {
t.Fatalf("Response has no data")
}
fmt.Printf("[TestGetTransactionByHash] %#v\n", resp)
}
func TestStatsBlocksPerDay(t *testing.T) {
c := getClient()
resp, err := c.StatsBlocksPerDay()
if err != nil {
t.Error(err)
}
if resp == nil {
t.Fatalf("Response has no data")
}
fmt.Printf("[TestStatsBlocksPerDay] %#v\n", resp)
}
func TestStatsTransactionsPerDay(t *testing.T) {
c := getClient()
resp, err := c.StatsTransactionsPerDay()
if err != nil {
t.Error(err)
}
if resp == nil {
t.Fatalf("Response has no data")
}
fmt.Printf("[TestStatsTransactionsPerDay] %#v\n", resp)
}
func TestStatsSupplies(t *testing.T) {
c := getClient()
resp, err := c.StatsSupplies()
if err != nil {
t.Error(err)
}
if resp == nil {
t.Fatalf("Response has no data")
}
fmt.Printf("[TestStatsSupplies] %#v\n", resp)
}