-
Notifications
You must be signed in to change notification settings - Fork 179
/
main_test.go
487 lines (416 loc) · 13.8 KB
/
main_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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
// Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
//
// Copyright 2019 Centrifuge GmbH
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gsrpc_test
import (
"fmt"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/extrinsic"
"math/big"
"time"
gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
"github.com/centrifuge/go-substrate-rpc-client/v4/config"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
)
func Example_simpleConnect() {
// The following example shows how to instantiate a Substrate API and use it to connect to a node
api, err := gsrpc.NewSubstrateAPI(config.Default().RPCURL)
if err != nil {
panic(err)
}
chain, err := api.RPC.System.Chain()
if err != nil {
panic(err)
}
nodeName, err := api.RPC.System.Name()
if err != nil {
panic(err)
}
nodeVersion, err := api.RPC.System.Version()
if err != nil {
panic(err)
}
fmt.Printf("You are connected to chain %v using %v v%v\n", chain, nodeName, nodeVersion)
// Output: You are connected to chain Development using Parity Polkadot v0.9.43-ba42b9ce51d
}
func Example_listenToNewBlocks() {
// This example shows how to subscribe to new blocks.
//
// It displays the block number every time a new block is seen by the node you are connected to.
//
// NOTE: The example runs until 10 blocks are received or until you stop it with CTRL+C
api, err := gsrpc.NewSubstrateAPI(config.Default().RPCURL)
if err != nil {
panic(err)
}
sub, err := api.RPC.Chain.SubscribeNewHeads()
if err != nil {
panic(err)
}
defer sub.Unsubscribe()
count := 0
for {
head := <-sub.Chan()
fmt.Printf("Chain is at block: #%v\n", head.Number)
count++
if count == 10 {
sub.Unsubscribe()
break
}
}
}
func Example_listenToBalanceChange() {
// This example shows how to instantiate a Substrate API and use it to connect to a node and retrieve balance
// updates
//
// NOTE: The example runs until you stop it with CTRL+C
api, err := gsrpc.NewSubstrateAPI(config.Default().RPCURL)
if err != nil {
panic(err)
}
meta, err := api.RPC.State.GetMetadataLatest()
if err != nil {
panic(err)
}
alice := signature.TestKeyringPairAlice.PublicKey
key, err := types.CreateStorageKey(meta, "System", "Account", alice)
if err != nil {
panic(err)
}
var accountInfo types.AccountInfo
ok, err := api.RPC.State.GetStorageLatest(key, &accountInfo)
if err != nil || !ok {
panic(err)
}
previous := accountInfo.Data.Free
fmt.Printf("%#x has a balance of %v\n", alice, previous)
fmt.Printf("You may leave this example running and transfer any value to %#x\n", alice)
// Here we subscribe to any balance changes
sub, err := api.RPC.State.SubscribeStorageRaw([]types.StorageKey{key})
if err != nil {
panic(err)
}
defer sub.Unsubscribe()
// outer for loop for subscription notifications
for {
// inner loop for the changes within one of those notifications
for _, chng := range (<-sub.Chan()).Changes {
if !chng.HasStorageData {
continue
}
var acc types.AccountInfo
if err = codec.Decode(chng.StorageData, &acc); err != nil {
panic(err)
}
// Calculate the delta
current := acc.Data.Free
var change = types.U128{Int: big.NewInt(0).Sub(current.Int, previous.Int)}
// Only display positive value changes (Since we are pulling `previous` above already,
// the initial balance change will also be zero)
if change.Cmp(big.NewInt(0)) != 0 {
fmt.Printf("New balance change of: %v %v %v\n", change, previous, current)
previous = current
return
}
}
}
}
func Example_unsubscribeFromListeningToUpdates() {
// This example shows how to subscribe to and later unsubscribe from listening to block updates.
//
// In this example we're calling the built-in unsubscribe() function after a timeOut of 20s to cleanup and
// unsubscribe from listening to updates.
api, err := gsrpc.NewSubstrateAPI(config.Default().RPCURL)
if err != nil {
panic(err)
}
sub, err := api.RPC.Chain.SubscribeNewHeads()
if err != nil {
panic(err)
}
defer sub.Unsubscribe()
timeout := time.After(20 * time.Second)
for {
select {
case head := <-sub.Chan():
fmt.Printf("Chain is at block: #%v\n", head.Number)
case <-timeout:
sub.Unsubscribe()
fmt.Println("Unsubscribed")
return
}
}
}
func Example_makeASimpleTransfer() {
// This sample shows how to create a transaction to make a transfer from one an account to another.
// Instantiate the API
api, err := gsrpc.NewSubstrateAPI(config.Default().RPCURL)
if err != nil {
panic(err)
}
meta, err := api.RPC.State.GetMetadataLatest()
if err != nil {
panic(err)
}
// Create a call, transferring 12345 units to Bob
bob, err := types.NewMultiAddressFromHexAccountID("0x8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48")
if err != nil {
panic(err)
}
// 1 unit of transfer
bal, ok := new(big.Int).SetString("100000000000000", 10)
if !ok {
panic(fmt.Errorf("failed to convert balance"))
}
c, err := types.NewCall(meta, "Balances.transfer", bob, types.NewUCompact(bal))
if err != nil {
panic(err)
}
// Create the extrinsic
ext := extrinsic.NewExtrinsic(c)
genesisHash, err := api.RPC.Chain.GetBlockHash(0)
if err != nil {
panic(err)
}
rv, err := api.RPC.State.GetRuntimeVersionLatest()
if err != nil {
panic(err)
}
key, err := types.CreateStorageKey(meta, "System", "Account", signature.TestKeyringPairAlice.PublicKey)
if err != nil {
panic(err)
}
var accountInfo types.AccountInfo
ok, err = api.RPC.State.GetStorageLatest(key, &accountInfo)
if err != nil || !ok {
panic(err)
}
nonce := uint32(accountInfo.Nonce)
// Sign the transaction using Alice's default account
err = ext.Sign(signature.TestKeyringPairAlice, meta, extrinsic.WithEra(types.ExtrinsicEra{IsImmortalEra: true}, genesisHash),
extrinsic.WithNonce(types.NewUCompactFromUInt(uint64(nonce))),
extrinsic.WithTip(types.NewUCompactFromUInt(0)),
extrinsic.WithSpecVersion(rv.SpecVersion),
extrinsic.WithTransactionVersion(rv.TransactionVersion),
extrinsic.WithGenesisHash(genesisHash),
)
if err != nil {
panic(err)
}
// Send the extrinsic
_, err = api.RPC.Author.SubmitExtrinsic(ext)
if err != nil {
panic(err)
}
fmt.Printf("Balance transferred from Alice to Bob: %v\n", bal.String())
// Output: Balance transferred from Alice to Bob: 100000000000000
}
func Example_displaySystemEvents() {
// Query the system events and extract information from them. This example runs until exited via Ctrl-C
// Create our API with a default connection to the local node
api, err := gsrpc.NewSubstrateAPI(config.Default().RPCURL)
if err != nil {
panic(err)
}
meta, err := api.RPC.State.GetMetadataLatest()
if err != nil {
panic(err)
}
// Subscribe to system events via storage
key, err := types.CreateStorageKey(meta, "System", "Events", nil)
if err != nil {
panic(err)
}
sub, err := api.RPC.State.SubscribeStorageRaw([]types.StorageKey{key})
if err != nil {
panic(err)
}
defer sub.Unsubscribe()
// outer for loop for subscription notifications
for {
set := <-sub.Chan()
// inner loop for the changes within one of those notifications
for _, chng := range set.Changes {
if !codec.Eq(chng.StorageKey, key) || !chng.HasStorageData {
// skip, we are only interested in events with content
continue
}
// Decode the event records
events := types.EventRecords{}
err = types.EventRecordsRaw(chng.StorageData).DecodeEventRecords(meta, &events)
if err != nil {
panic(err)
}
// Show what we are busy with
for _, e := range events.Balances_Endowed {
fmt.Printf("\tBalances:Endowed:: (phase=%#v)\n", e.Phase)
fmt.Printf("\t\t%#x, %v\n", e.Who, e.Balance)
}
for _, e := range events.Balances_DustLost {
fmt.Printf("\tBalances:DustLost:: (phase=%#v)\n", e.Phase)
fmt.Printf("\t\t%#x, %v\n", e.Who, e.Balance)
}
for _, e := range events.Balances_Transfer {
fmt.Printf("\tBalances:Transfer:: (phase=%#v)\n", e.Phase)
fmt.Printf("\t\t%v, %v, %v\n", e.From, e.To, e.Value)
}
for _, e := range events.Balances_BalanceSet {
fmt.Printf("\tBalances:BalanceSet:: (phase=%#v)\n", e.Phase)
fmt.Printf("\t\t%v, %v, %v\n", e.Who, e.Free, e.Reserved)
}
for _, e := range events.Balances_Deposit {
fmt.Printf("\tBalances:Deposit:: (phase=%#v)\n", e.Phase)
fmt.Printf("\t\t%v, %v\n", e.Who, e.Balance)
}
for _, e := range events.Grandpa_NewAuthorities {
fmt.Printf("\tGrandpa:NewAuthorities:: (phase=%#v)\n", e.Phase)
fmt.Printf("\t\t%v\n", e.NewAuthorities)
}
for _, e := range events.Grandpa_Paused {
fmt.Printf("\tGrandpa:Paused:: (phase=%#v)\n", e.Phase)
}
for _, e := range events.Grandpa_Resumed {
fmt.Printf("\tGrandpa:Resumed:: (phase=%#v)\n", e.Phase)
}
for _, e := range events.ImOnline_HeartbeatReceived {
fmt.Printf("\tImOnline:HeartbeatReceived:: (phase=%#v)\n", e.Phase)
fmt.Printf("\t\t%#x\n", e.AuthorityID)
}
for _, e := range events.ImOnline_AllGood {
fmt.Printf("\tImOnline:AllGood:: (phase=%#v)\n", e.Phase)
}
for _, e := range events.ImOnline_SomeOffline {
fmt.Printf("\tImOnline:SomeOffline:: (phase=%#v)\n", e.Phase)
fmt.Printf("\t\t%v\n", e.IdentificationTuples)
}
for _, e := range events.Indices_IndexAssigned {
fmt.Printf("\tIndices:IndexAssigned:: (phase=%#v)\n", e.Phase)
fmt.Printf("\t\t%#x%v\n", e.AccountID, e.AccountIndex)
}
for _, e := range events.Indices_IndexFreed {
fmt.Printf("\tIndices:IndexFreed:: (phase=%#v)\n", e.Phase)
fmt.Printf("\t\t%v\n", e.AccountIndex)
}
for _, e := range events.Offences_Offence {
fmt.Printf("\tOffences:Offence:: (phase=%#v)\n", e.Phase)
fmt.Printf("\t\t%v%v\n", e.Kind, e.OpaqueTimeSlot)
}
for _, e := range events.Session_NewSession {
fmt.Printf("\tSession:NewSession:: (phase=%#v)\n", e.Phase)
fmt.Printf("\t\t%v\n", e.SessionIndex)
}
for _, e := range events.Staking_Rewarded {
fmt.Printf("\tStaking:Reward:: (phase=%#v)\n", e.Phase)
fmt.Printf("\t\t%v\n", e.Amount)
}
for _, e := range events.Staking_Slashed {
fmt.Printf("\tStaking:Slash:: (phase=%#v)\n", e.Phase)
fmt.Printf("\t\t%#x%v\n", e.AccountID, e.Balance)
}
for _, e := range events.Staking_OldSlashingReportDiscarded {
fmt.Printf("\tStaking:OldSlashingReportDiscarded:: (phase=%#v)\n", e.Phase)
fmt.Printf("\t\t%v\n", e.SessionIndex)
}
for _, e := range events.System_ExtrinsicSuccess {
fmt.Printf("\tSystem:ExtrinsicSuccess:: (phase=%#v)\n", e.Phase)
}
for _, e := range events.System_ExtrinsicFailed {
fmt.Printf("\tSystem:ExtrinsicFailed:: (phase=%#v)\n", e.Phase)
fmt.Printf("\t\t%v\n", e.DispatchError)
}
for _, e := range events.System_CodeUpdated {
fmt.Printf("\tSystem:CodeUpdated:: (phase=%#v)\n", e.Phase)
}
for _, e := range events.System_NewAccount {
fmt.Printf("\tSystem:NewAccount:: (phase=%#v)\n", e.Phase)
fmt.Printf("\t\t%#x\n", e.Who)
}
for _, e := range events.System_KilledAccount {
fmt.Printf("\tSystem:KilledAccount:: (phase=%#v)\n", e.Phase)
fmt.Printf("\t\t%#X\n", e.Who)
}
}
}
}
func Example_transactionWithEvents() {
// Display the events that occur during a transfer by sending a value to bob
// Instantiate the API
api, err := gsrpc.NewSubstrateAPI(config.Default().RPCURL)
if err != nil {
panic(err)
}
meta, err := api.RPC.State.GetMetadataLatest()
if err != nil {
panic(err)
}
// Create a call, transferring 12345 units to Bob
bob, err := types.NewAddressFromHexAccountID("0x8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48")
if err != nil {
panic(err)
}
amount := types.NewUCompactFromUInt(12345)
c, err := types.NewCall(meta, "Balances.transfer", bob, amount)
if err != nil {
panic(err)
}
// Create the extrinsic
ext := extrinsic.NewExtrinsic(c)
genesisHash, err := api.RPC.Chain.GetBlockHash(0)
if err != nil {
panic(err)
}
rv, err := api.RPC.State.GetRuntimeVersionLatest()
if err != nil {
panic(err)
}
// Get the nonce for Alice
key, err := types.CreateStorageKey(meta, "System", "Account", signature.TestKeyringPairAlice.PublicKey)
if err != nil {
panic(err)
}
var accountInfo types.AccountInfo
ok, err := api.RPC.State.GetStorageLatest(key, &accountInfo)
if err != nil || !ok {
panic(err)
}
nonce := uint32(accountInfo.Nonce)
fmt.Printf("Sending %v from %#x to %#x with nonce %v", amount, signature.TestKeyringPairAlice.PublicKey, bob.AsAccountID, nonce)
// Sign the transaction using Alice's default account
err = ext.Sign(signature.TestKeyringPairAlice, meta, extrinsic.WithEra(types.ExtrinsicEra{IsImmortalEra: true}, genesisHash),
extrinsic.WithNonce(types.NewUCompactFromUInt(uint64(nonce))),
extrinsic.WithTip(types.NewUCompactFromUInt(0)),
extrinsic.WithSpecVersion(rv.SpecVersion),
extrinsic.WithTransactionVersion(rv.TransactionVersion),
extrinsic.WithGenesisHash(genesisHash),
)
if err != nil {
panic(err)
}
// Do the transfer and track the actual status
sub, err := api.RPC.Author.SubmitAndWatchExtrinsic(ext)
if err != nil {
panic(err)
}
defer sub.Unsubscribe()
for {
status := <-sub.Chan()
fmt.Printf("Transaction status: %#v\n", status)
if status.IsInBlock {
fmt.Printf("Completed at block hash: %#x\n", status.AsInBlock)
return
}
}
}