github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/metadataV7_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 types_test
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  
    24  	. "github.com/stafiprotocol/go-substrate-rpc-client/types"
    25  )
    26  
    27  var exampleMetadataV7 = Metadata{
    28  	MagicNumber:  0x6174656d,
    29  	Version:      7,
    30  	IsMetadataV7: true,
    31  	AsMetadataV7: exampleRuntimeMetadataV7,
    32  }
    33  
    34  var exampleRuntimeMetadataV7 = MetadataV7{
    35  	Modules: []ModuleMetadataV7{exampleModuleMetadataV7Empty, exampleModuleMetadataV71, exampleModuleMetadataV72},
    36  }
    37  
    38  var exampleModuleMetadataV7Empty = ModuleMetadataV7{
    39  	Name:       "EmptyModule",
    40  	HasStorage: false,
    41  	Storage:    StorageMetadata{},
    42  	HasCalls:   false,
    43  	Calls:      nil,
    44  	HasEvents:  false,
    45  	Events:     nil,
    46  	Constants:  nil,
    47  }
    48  
    49  var exampleModuleMetadataV71 = ModuleMetadataV7{
    50  	Name:       "Module1",
    51  	HasStorage: true,
    52  	Storage:    exampleStorageMetadata,
    53  	HasCalls:   true,
    54  	Calls:      []FunctionMetadataV4{exampleFunctionMetadataV4},
    55  	HasEvents:  true,
    56  	Events:     []EventMetadataV4{exampleEventMetadataV4},
    57  	Constants:  []ModuleConstantMetadataV6{exampleModuleConstantMetadataV6},
    58  }
    59  
    60  var exampleModuleMetadataV72 = ModuleMetadataV7{
    61  	Name:       "Module2",
    62  	HasStorage: true,
    63  	Storage:    exampleStorageMetadata,
    64  	HasCalls:   true,
    65  	Calls:      []FunctionMetadataV4{exampleFunctionMetadataV4},
    66  	HasEvents:  true,
    67  	Events:     []EventMetadataV4{exampleEventMetadataV4},
    68  	Constants:  []ModuleConstantMetadataV6{exampleModuleConstantMetadataV6},
    69  }
    70  
    71  var exampleStorageMetadata = StorageMetadata{
    72  	Prefix: "myStoragePrefix",
    73  	Items: []StorageFunctionMetadataV5{exampleStorageFunctionMetadataV5Type, exampleStorageFunctionMetadataV5Map,
    74  		exampleStorageFunctionMetadataV5DoubleMap},
    75  }
    76  
    77  var exampleStorageFunctionMetadataV5Type = StorageFunctionMetadataV5{
    78  	Name:          "myStorageFunc",
    79  	Modifier:      StorageFunctionModifierV0{IsOptional: true},
    80  	Type:          StorageFunctionTypeV5{IsType: true, AsType: "U8"},
    81  	Fallback:      []byte{23, 14},
    82  	Documentation: []Text{"My", "storage func", "doc"},
    83  }
    84  
    85  var exampleStorageFunctionMetadataV5Map = StorageFunctionMetadataV5{
    86  	Name:          "myStorageFunc2",
    87  	Modifier:      StorageFunctionModifierV0{IsOptional: true},
    88  	Type:          StorageFunctionTypeV5{IsMap: true, AsMap: exampleMapTypeV4},
    89  	Fallback:      []byte{23, 14},
    90  	Documentation: []Text{"My", "storage func", "doc"},
    91  }
    92  
    93  var exampleStorageFunctionMetadataV5DoubleMap = StorageFunctionMetadataV5{
    94  	Name:          "myStorageFunc3",
    95  	Modifier:      StorageFunctionModifierV0{IsOptional: true},
    96  	Type:          StorageFunctionTypeV5{IsDoubleMap: true, AsDoubleMap: exampleDoubleMapTypeV5},
    97  	Fallback:      []byte{23, 14},
    98  	Documentation: []Text{"My", "storage func", "doc"},
    99  }
   100  
   101  var exampleDoubleMapTypeV5 = DoubleMapTypeV5{
   102  	Hasher:     StorageHasher{IsBlake2_256: true},
   103  	Key1:       "myKey",
   104  	Key2:       "otherKey",
   105  	Value:      "and a value",
   106  	Key2Hasher: StorageHasher{IsTwox256: true},
   107  }
   108  
   109  var exampleModuleConstantMetadataV6 = ModuleConstantMetadataV6{
   110  	Name:          Text("My name"),
   111  	Type:          Type("My Type"),
   112  	Value:         Bytes{123, 23},
   113  	Documentation: []Text{"Doca", "Docb"},
   114  }
   115  
   116  func TestMetadataV7_EncodeDecode(t *testing.T) {
   117  	assertRoundtrip(t, exampleMetadataV7)
   118  }
   119  
   120  func TestFindEventNamesForEventIDV7(t *testing.T) {
   121  	module, event, err := exampleMetadataV7.FindEventNamesForEventID(EventID([2]byte{1, 0}))
   122  
   123  	assert.NoError(t, err)
   124  	assert.Equal(t, exampleModuleMetadataV72.Name, module)
   125  	assert.Equal(t, exampleEventMetadataV4.Name, event)
   126  }
   127  
   128  func TestFindStorageEntryMetadataV7(t *testing.T) {
   129  	_, err := exampleMetadataV7.FindStorageEntryMetadata("myStoragePrefix", "myStorageFunc2")
   130  	assert.NoError(t, err)
   131  }