github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/teste2e/main_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  
    23  	"github.com/stafiprotocol/go-substrate-rpc-client/config"
    24  	"github.com/stafiprotocol/go-substrate-rpc-client/rpc"
    25  	"github.com/stafiprotocol/go-substrate-rpc-client/types"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestEnd2end(t *testing.T) {
    30  	if testing.Short() {
    31  		t.Skip("skipping end-to-end test in short mode.")
    32  	}
    33  
    34  	rpcs, err := rpc.NewRPCS(config.Default().RPCURL)
    35  	assert.NoError(t, err)
    36  
    37  	fmt.Println()
    38  	fmt.Printf("Connected to node: %v\n", rpcs.Client.URL())
    39  	fmt.Println()
    40  
    41  	runtimeVersion, err := rpcs.State.GetRuntimeVersionLatest()
    42  	assert.NoError(t, err)
    43  	fmt.Printf("authoringVersion: %v\n", runtimeVersion.AuthoringVersion)
    44  	fmt.Printf("specVersion: %v\n", runtimeVersion.SpecVersion)
    45  	fmt.Printf("implVersion: %v\n", runtimeVersion.ImplVersion)
    46  	fmt.Println()
    47  
    48  	hash, err := rpcs.Chain.GetBlockHashLatest()
    49  	assert.NoError(t, err)
    50  	fmt.Printf("Latest block: %v\n", hash.Hex())
    51  	fmt.Printf("\tView in Polkadot/Substrate Apps: https://polkadot.js.org/apps/#/explorer/query/%v?"+
    52  		"rpc=wss://serinus-5.kusama.network\n", hash.Hex())
    53  	fmt.Printf("\tView in polkascan.io: https://polkascan.io/pre/kusama-cc2/block/%v\n", hash.Hex())
    54  	fmt.Println()
    55  
    56  	header, err := rpcs.Chain.GetHeader(hash)
    57  	assert.NoError(t, err)
    58  	fmt.Printf("Block number: %v\n", header.Number)
    59  	fmt.Printf("Parent hash: %v\n", header.ParentHash.Hex())
    60  	fmt.Printf("State root: %v\n", header.StateRoot.Hex())
    61  	fmt.Printf("Extrinsics root: %v\n", header.ExtrinsicsRoot.Hex())
    62  	fmt.Println()
    63  
    64  	block, err := rpcs.Chain.GetBlock(hash)
    65  	assert.NoError(t, err)
    66  	fmt.Printf("Total extrinsics: %v\n", len(block.Block.Extrinsics))
    67  	fmt.Println()
    68  
    69  	finHead, err := rpcs.Chain.GetFinalizedHead()
    70  	assert.NoError(t, err)
    71  	fmt.Printf("Last finalized block in the canon chain: %v\n", finHead.Hex())
    72  	fmt.Println()
    73  
    74  	meta, err := rpcs.State.GetMetadataLatest()
    75  	assert.NoError(t, err)
    76  
    77  	key, err := types.CreateStorageKey(meta, "Session", "Validators", nil, nil)
    78  	assert.NoError(t, err)
    79  
    80  	var validators []types.AccountID
    81  	ok, err := rpcs.State.GetStorageLatest(key, &validators)
    82  	assert.NoError(t, err)
    83  	assert.True(t, ok)
    84  	fmt.Printf("Current validators:\n")
    85  	for i, v := range validators {
    86  		fmt.Printf("\tValidator %v: %#x\n", i, v)
    87  	}
    88  	fmt.Println()
    89  }