github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/teste2e/state_subscribe_storage_test.go (about)

     1  // Go Substrate RPC Client (GSRPC) provides APIs and types around Polkadot and any Substrate-based chain RPC calls
     2  //
     3  // Copyright 2020 Stafi Protocol
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package teste2e
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/stafiprotocol/go-substrate-rpc-client/config"
    25  	"github.com/stafiprotocol/go-substrate-rpc-client/rpc"
    26  	"github.com/stafiprotocol/go-substrate-rpc-client/types"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestState_SubscribeStorage_EventsRaw(t *testing.T) {
    31  	if testing.Short() {
    32  		t.Skip("skipping end-to-end test in short mode.")
    33  	}
    34  
    35  	rpcs, err := rpc.NewRPCS(config.Default().RPCURL)
    36  	if err != nil {
    37  		panic(err)
    38  	}
    39  
    40  	meta, err := rpcs.State.GetMetadataLatest()
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  
    45  	key, err := types.CreateStorageKey(meta, "System", "Events", nil, nil)
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  
    50  	sub, err := rpcs.State.SubscribeStorageRaw([]types.StorageKey{key})
    51  	if err != nil {
    52  		panic(err)
    53  	}
    54  	defer sub.Unsubscribe()
    55  
    56  	timeout := time.After(10 * time.Second)
    57  	received := 0
    58  
    59  	for {
    60  		select {
    61  		case set := <-sub.Chan():
    62  			fmt.Printf("%#v\n", set)
    63  			received++
    64  
    65  			if received >= 2 {
    66  				return
    67  			}
    68  		case <-timeout:
    69  			assert.FailNow(t, "timeout reached without getting 2 notifications from subscription")
    70  			return
    71  		}
    72  	}
    73  }
    74  
    75  func TestState_SubscribeStorage_Events(t *testing.T) {
    76  	if testing.Short() {
    77  		t.Skip("skipping end-to-end test in short mode.")
    78  	}
    79  
    80  	rpcs, err := rpc.NewRPCS(config.Default().RPCURL)
    81  	if err != nil {
    82  		panic(err)
    83  	}
    84  
    85  	meta, err := rpcs.State.GetMetadataLatest()
    86  	if err != nil {
    87  		panic(err)
    88  	}
    89  
    90  	key, err := types.CreateStorageKey(meta, "System", "Events", nil, nil)
    91  	if err != nil {
    92  		panic(err)
    93  	}
    94  
    95  	sub, err := rpcs.State.SubscribeStorageRaw([]types.StorageKey{key})
    96  	if err != nil {
    97  		panic(err)
    98  	}
    99  	defer sub.Unsubscribe()
   100  
   101  	timeout := time.After(10 * time.Second)
   102  	received := 0
   103  
   104  	for {
   105  		select {
   106  		case set := <-sub.Chan():
   107  			fmt.Printf("%#v\n", set)
   108  			for _, chng := range set.Changes {
   109  				if !types.Eq(chng.StorageKey, key) || !chng.HasStorageData {
   110  					// skip, we are only interested in events with content
   111  					continue
   112  				}
   113  
   114  				fmt.Printf("%s\n", chng.StorageKey.Hex())
   115  				fmt.Printf("%#x\n", chng.StorageData)
   116  
   117  				events := types.EventRecords{}
   118  				err = types.EventRecordsRaw(chng.StorageData).DecodeEventRecords(meta, &events)
   119  				if err != nil {
   120  					panic(err)
   121  				}
   122  
   123  				fmt.Printf("%#v\n", events)
   124  			}
   125  
   126  			received++
   127  
   128  			if received >= 2 {
   129  				return
   130  			}
   131  		case <-timeout:
   132  			assert.FailNow(t, "timeout reached without getting 2 notifications from subscription")
   133  			return
   134  		}
   135  	}
   136  }