github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/types/metadataV10_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 "github.com/stretchr/testify/assert" 24 ) 25 26 var exampleMetadataV10 = Metadata{ 27 MagicNumber: 0x6174656d, 28 Version: 10, 29 IsMetadataV10: true, 30 AsMetadataV10: exampleRuntimeMetadataV10, 31 } 32 33 var exampleRuntimeMetadataV10 = MetadataV10{ 34 Modules: []ModuleMetadataV10{exampleModuleMetadataV10Empty, exampleModuleMetadataV101, exampleModuleMetadataV102}, 35 } 36 37 var exampleModuleMetadataV10Empty = ModuleMetadataV10{ 38 Name: "EmptyModule", 39 HasStorage: false, 40 Storage: StorageMetadataV10{}, 41 HasCalls: false, 42 Calls: nil, 43 HasEvents: false, 44 Events: nil, 45 Constants: nil, 46 Errors: nil, 47 } 48 49 var exampleModuleMetadataV101 = ModuleMetadataV10{ 50 Name: "Module1", 51 HasStorage: true, 52 Storage: exampleStorageMetadataV10, 53 HasCalls: true, 54 Calls: []FunctionMetadataV4{exampleFunctionMetadataV4}, 55 HasEvents: true, 56 Events: []EventMetadataV4{exampleEventMetadataV4}, 57 Constants: []ModuleConstantMetadataV6{exampleModuleConstantMetadataV6}, 58 Errors: []ErrorMetadataV8{exampleErrorMetadataV8}, 59 } 60 61 var exampleModuleMetadataV102 = ModuleMetadataV10{ 62 Name: "Module2", 63 HasStorage: true, 64 Storage: exampleStorageMetadataV10, 65 HasCalls: true, 66 Calls: []FunctionMetadataV4{exampleFunctionMetadataV4}, 67 HasEvents: true, 68 Events: []EventMetadataV4{exampleEventMetadataV4}, 69 Constants: []ModuleConstantMetadataV6{exampleModuleConstantMetadataV6}, 70 Errors: []ErrorMetadataV8{exampleErrorMetadataV8}, 71 } 72 73 var exampleStorageMetadataV10 = StorageMetadataV10{ 74 Prefix: "myStoragePrefix", 75 Items: []StorageFunctionMetadataV10{exampleStorageFunctionMetadataV10Type, exampleStorageFunctionMetadataV10Map, 76 exampleStorageFunctionMetadataV10DoubleMap}, 77 } 78 79 var exampleStorageFunctionMetadataV10Type = StorageFunctionMetadataV10{ 80 Name: "myStorageFunc", 81 Modifier: StorageFunctionModifierV0{IsOptional: true}, 82 Type: StorageFunctionTypeV10{IsType: true, AsType: "U8"}, 83 Fallback: []byte{23, 14}, 84 Documentation: []Text{"My", "storage func", "doc"}, 85 } 86 87 var exampleStorageFunctionMetadataV10Map = StorageFunctionMetadataV10{ 88 Name: "myStorageFunc2", 89 Modifier: StorageFunctionModifierV0{IsOptional: true}, 90 Type: StorageFunctionTypeV10{IsMap: true, AsMap: exampleMapTypeV10}, 91 Fallback: []byte{23, 14}, 92 Documentation: []Text{"My", "storage func", "doc"}, 93 } 94 95 var exampleStorageFunctionMetadataV10DoubleMap = StorageFunctionMetadataV10{ 96 Name: "myStorageFunc3", 97 Modifier: StorageFunctionModifierV0{IsOptional: true}, 98 Type: StorageFunctionTypeV10{IsDoubleMap: true, AsDoubleMap: exampleDoubleMapTypeV10}, 99 Fallback: []byte{23, 14}, 100 Documentation: []Text{"My", "storage func", "doc"}, 101 } 102 103 var exampleMapTypeV10 = MapTypeV10{ 104 Hasher: StorageHasherV10{IsBlake2_256: true}, 105 Key: "my key", 106 Value: "and my value", 107 Linked: false, 108 } 109 110 var exampleDoubleMapTypeV10 = DoubleMapTypeV10{ 111 Hasher: StorageHasherV10{IsBlake2_256: true}, 112 Key1: "myKey", 113 Key2: "otherKey", 114 Value: "and a value", 115 Key2Hasher: StorageHasherV10{IsTwox256: true}, 116 } 117 118 func TestMetadataV10_EncodeDecode(t *testing.T) { 119 assertRoundtrip(t, exampleMetadataV10) 120 } 121 122 func TestFindEventNamesForEventIDV10(t *testing.T) { 123 module, event, err := exampleMetadataV10.FindEventNamesForEventID(EventID([2]byte{1, 0})) 124 125 assert.NoError(t, err) 126 assert.Equal(t, exampleModuleMetadataV102.Name, module) 127 assert.Equal(t, exampleEventMetadataV4.Name, event) 128 } 129 130 func TestFindStorageEntryMetadataV10(t *testing.T) { 131 _, err := exampleMetadataV10.FindStorageEntryMetadata("myStoragePrefix", "myStorageFunc2") 132 assert.NoError(t, err) 133 } 134 135 func TestMetadataV10_Decode(t *testing.T) { 136 metadata := NewMetadataV10() 137 138 err := DecodeFromBytes(MustHexDecodeString(ExamplaryMetadataV10String), metadata) 139 assert.NoError(t, err) 140 141 assert.Equal(t, *ExamplaryMetadataV10, *metadata) 142 } 143 144 func TestMetadataV10Polkadot_Decode(t *testing.T) { 145 metadata := NewMetadataV10() 146 147 err := DecodeFromBytes(MustHexDecodeString(ExamplaryMetadataV10PolkadotString), metadata) 148 assert.NoError(t, err) 149 150 assert.Equal(t, *ExamplaryMetadataV10Polkadot, *metadata) 151 } 152 153 func TestMetadataV10_ExistsModuleMetadata(t *testing.T) { 154 assert.True(t, exampleMetadataV10.ExistsModuleMetadata("EmptyModule")) 155 assert.False(t, exampleMetadataV10.ExistsModuleMetadata("NotExistModule")) 156 }