github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/metadataV4_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/stafiprotocol/go-substrate-rpc-client/types" 23 24 "github.com/stretchr/testify/assert" 25 ) 26 27 var exampleMetadataV4 = Metadata{ 28 MagicNumber: 0x6174656d, 29 Version: 4, 30 IsMetadataV4: true, 31 AsMetadataV4: exampleRuntimeMetadataV4, 32 } 33 34 var exampleRuntimeMetadataV4 = MetadataV4{ 35 Modules: []ModuleMetadataV4{exampleModuleMetadataV4Empty, exampleModuleMetadataV41, exampleModuleMetadataV42}, 36 } 37 38 var exampleCallIndex = CallIndex{ 39 SectionIndex: 123, 40 MethodIndex: 234, 41 } 42 43 var exampleModuleMetadataV4Empty = ModuleMetadataV4{ 44 Name: "EmptyModule", 45 Prefix: "EmptyModule", 46 HasStorage: false, 47 Storage: nil, 48 HasCalls: false, 49 Calls: nil, 50 HasEvents: false, 51 Events: nil, 52 } 53 54 var exampleModuleMetadataV41 = ModuleMetadataV4{ 55 Name: "Module1", 56 Prefix: "Module1", 57 HasStorage: true, 58 Storage: []StorageFunctionMetadataV4{exampleStorageFunctionMetadataV4DoubleMap}, 59 HasCalls: true, 60 Calls: []FunctionMetadataV4{exampleFunctionMetadataV4}, 61 HasEvents: true, 62 Events: []EventMetadataV4{exampleEventMetadataV4}, 63 } 64 65 var exampleModuleMetadataV42 = ModuleMetadataV4{ 66 Name: "Module2", 67 Prefix: "Module2", 68 HasStorage: true, 69 Storage: []StorageFunctionMetadataV4{exampleStorageFunctionMetadataV4DoubleMap}, 70 HasCalls: true, 71 Calls: []FunctionMetadataV4{exampleFunctionMetadataV4}, 72 HasEvents: true, 73 Events: []EventMetadataV4{exampleEventMetadataV4}, 74 } 75 76 var exampleStorageFunctionMetadataV4Type = StorageFunctionMetadataV4{ 77 Name: "myStorageFunc", 78 Modifier: StorageFunctionModifierV0{IsOptional: true}, 79 Type: StorageFunctionTypeV4{IsType: true, AsType: "U8"}, 80 Fallback: []byte{23, 14}, 81 Documentation: []Text{"My", "storage func", "doc"}, 82 } 83 84 var exampleStorageFunctionMetadataV4Map = StorageFunctionMetadataV4{ 85 Name: "myStorageFunc2", 86 Modifier: StorageFunctionModifierV0{IsOptional: true}, 87 Type: StorageFunctionTypeV4{IsMap: true, AsMap: exampleMapTypeV4}, 88 Fallback: []byte{23, 14}, 89 Documentation: []Text{"My", "storage func", "doc"}, 90 } 91 92 var exampleStorageFunctionMetadataV4DoubleMap = StorageFunctionMetadataV4{ 93 Name: "myStorageFunc3", 94 Modifier: StorageFunctionModifierV0{IsOptional: true}, 95 Type: StorageFunctionTypeV4{IsDoubleMap: true, AsDoubleMap: exampleDoubleMapTypeV4}, 96 Fallback: []byte{23, 14}, 97 Documentation: []Text{"My", "storage func", "doc"}, 98 } 99 100 var exampleFunctionMetadataV4 = FunctionMetadataV4{ 101 Name: "my function", 102 Args: []FunctionArgumentMetadata{exampleFunctionArgumentMetadata}, 103 Documentation: []Text{"My", "doc"}, 104 } 105 106 var exampleEventMetadataV4 = EventMetadataV4{ 107 Name: "myEvent", 108 Args: []Type{"arg1", "arg2"}, 109 Documentation: []Text{"My", "doc"}, 110 } 111 112 var exampleMapTypeV4 = MapTypeV4{ 113 Hasher: StorageHasher{IsBlake2_256: true}, 114 Key: "my key", 115 Value: "and my value", 116 Linked: false, 117 } 118 119 var exampleDoubleMapTypeV4 = DoubleMapTypeV4{ 120 Hasher: StorageHasher{IsBlake2_256: true}, 121 Key1: "myKey", 122 Key2: "otherKey", 123 Value: "and a value", 124 Key2Hasher: "and a hasher", 125 } 126 127 var exampleFunctionArgumentMetadata = FunctionArgumentMetadata{Name: "myFunctionName", Type: "myType"} 128 129 func TestMetadataV4_Decode(t *testing.T) { 130 metadata := NewMetadataV4() 131 132 err := DecodeFromBytes(MustHexDecodeString(ExamplaryMetadataV4String), metadata) 133 assert.NoError(t, err) 134 135 assert.Equal(t, *ExamplaryMetadataV4, *metadata) 136 } 137 138 func TestMetadataV4_EncodeDecode(t *testing.T) { 139 assertRoundtrip(t, exampleMetadataV4) 140 } 141 142 func TestCallIndex_EncodeDecode(t *testing.T) { 143 assertRoundtrip(t, exampleCallIndex) 144 } 145 146 func TestModuleMetadataV4_EncodeDecode(t *testing.T) { 147 assertRoundtrip(t, exampleModuleMetadataV42) 148 } 149 150 func TestStorageFunctionMetadataV4Type_EncodeDecode(t *testing.T) { 151 assertRoundtrip(t, exampleStorageFunctionMetadataV4Type) 152 } 153 154 func TestStorageFunctionMetadataV4Map_EncodeDecode(t *testing.T) { 155 assertRoundtrip(t, exampleStorageFunctionMetadataV4Map) 156 } 157 158 func TestStorageFunctionMetadataV4DoubleMap_EncodeDecode(t *testing.T) { 159 assertRoundtrip(t, exampleStorageFunctionMetadataV4DoubleMap) 160 } 161 162 func TestFunctionMetadataV4_EncodeDecode(t *testing.T) { 163 assertRoundtrip(t, exampleFunctionMetadataV4) 164 } 165 166 func TestEventMetadataV4_EncodeDecode(t *testing.T) { 167 assertRoundtrip(t, exampleEventMetadataV4) 168 } 169 170 func TestMapTypeV4_EncodeDecode(t *testing.T) { 171 assertRoundtrip(t, exampleMapTypeV4) 172 } 173 174 func TestDoubleMapTypeV4_EncodeDecode(t *testing.T) { 175 assertRoundtrip(t, exampleDoubleMapTypeV4) 176 } 177 178 func TestFunctionArgumentMetadata_EncodeDecode(t *testing.T) { 179 assertRoundtrip(t, exampleFunctionArgumentMetadata) 180 } 181 182 func TestFindEventNamesForEventIDV4(t *testing.T) { 183 module, event, err := exampleMetadataV4.FindEventNamesForEventID(EventID([2]byte{1, 0})) 184 185 assert.NoError(t, err) 186 assert.Equal(t, exampleModuleMetadataV42.Name, module) 187 assert.Equal(t, exampleEventMetadataV4.Name, event) 188 }