-
Notifications
You must be signed in to change notification settings - Fork 36
/
v15.go
262 lines (227 loc) · 8.71 KB
/
v15.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package types
import (
"encoding/json"
"fmt"
"github.com/huandu/xstrings"
"github.com/itering/scale.go/types/convert"
"github.com/itering/scale.go/utiles"
)
type MetadataV15Decoder struct {
ScaleDecoder
}
// "MetadataV15": {
// "lookup": "PortableRegistry",
// "pallets": "Vec<PalletMetadataV15>",
// "extrinsic": "ExtrinsicMetadataV15",
// "type": "SiLookupTypeId",
// "apis": "Vec<RuntimeApiMetadataV15>"
// }
func (m *MetadataV15Decoder) Process() {
result := MetadataStruct{
Metadata: MetadataTag{
Modules: nil,
},
}
// custom type lookup
portable := initPortableRaw(m.ProcessAndUpdateData("PortableRegistry").([]interface{}))
// utiles.Debug(portable)
scaleInfo := ScaleInfo{ScaleDecoder: &m.ScaleDecoder, V14: true}
scaleInfo.ProcessSiType(portable)
metadataSiType := m.RegisteredSiType
MetadataV15ModuleCall := m.ProcessAndUpdateData("Vec<MetadataV15Module>").([]interface{})
bm, _ := json.Marshal(MetadataV15ModuleCall)
var modulesType []MetadataModules
_ = json.Unmarshal(bm, &modulesType)
result.CallIndex = make(map[string]CallIndex)
result.EventIndex = make(map[string]EventIndex)
var originCallers []OriginCaller
for k, module := range modulesType {
originCallers = append(originCallers, OriginCaller{Name: module.Name, Index: module.Index})
// calls look up
if module.CallsValue != nil {
variants := portable[module.CallsValue.Type].Def.Variant
if variants == nil {
panic(fmt.Sprintf("%d call value not variant", module.CallsValue.Type))
}
for _, variant := range variants.Variants {
call := MetadataCalls{Name: variant.Name, Docs: variant.Docs, LookupIndex: variant.Index}
for _, field := range variant.Fields {
call.Args = append(call.Args, MetadataModuleCallArgument{
Name: field.Name,
Type: metadataSiType[field.Type],
TypeName: convert.ConvertType(field.TypeName),
})
}
module.Calls = append(module.Calls, call)
}
}
modulesType[k].Calls = module.Calls
for callIndex, call := range module.Calls {
modulesType[k].Calls[callIndex].Lookup = xstrings.RightJustify(utiles.IntToHex(module.Index), 2, "0") + xstrings.RightJustify(utiles.IntToHex(call.LookupIndex), 2, "0")
result.CallIndex[modulesType[k].Calls[callIndex].Lookup] = CallIndex{Module: MetadataModules{Name: module.Name}, Call: call}
}
// Events
if module.EventsValue != nil {
variants := portable[module.EventsValue.Type].Def.Variant
if variants == nil {
panic(fmt.Sprintf("%d event value not variant", module.EventsValue.Type))
}
for _, variant := range variants.Variants {
event := MetadataEvents{Name: variant.Name, Docs: variant.Docs}
for _, field := range variant.Fields {
event.Args = append(event.Args, metadataSiType[field.Type])
event.ArgsTypeName = append(event.ArgsTypeName, convert.ConvertType(field.TypeName))
event.ArgsName = append(event.ArgsName, field.Name)
}
module.Events = append(module.Events, event)
}
}
modulesType[k].Events = module.Events
if module.Events != nil {
for eventIndex, event := range module.Events {
modulesType[k].Events[eventIndex].Lookup = xstrings.RightJustify(utiles.IntToHex(module.Index), 2, "0") + xstrings.RightJustify(utiles.IntToHex(eventIndex), 2, "0")
result.EventIndex[modulesType[k].Events[eventIndex].Lookup] = EventIndex{Module: MetadataModules{Name: module.Name}, Call: event}
}
}
// Error
if module.ErrorsValue != nil {
variants := portable[module.ErrorsValue.Type].Def.Variant
if variants == nil {
panic(fmt.Sprintf("%d error value not variant", module.EventsValue.Type))
}
for _, variant := range variants.Variants {
moduleErr := MetadataModuleError{Name: variant.Name, Doc: variant.Docs}
for _, field := range variant.Fields {
moduleErr.Fields = append(moduleErr.Fields, ModuleErrorField{Doc: field.Docs, TypeName: field.TypeName, Type: metadataSiType[field.Type]})
}
module.Errors = append(module.Errors, moduleErr)
}
}
modulesType[k].Errors = module.Errors
// Constant
for index, constant := range module.Constants {
variant := metadataSiType[constant.TypeValue]
if variant == "" {
panic(fmt.Sprintf("%d constant value not variant", constant.TypeValue))
}
module.Constants[index].Type = variant
}
// Storage
for index, storage := range module.Storage {
if storage.Type.Origin == "PlainType" {
variant := metadataSiType[*storage.Type.PlainTypeValue]
module.Storage[index].Type.PlainType = &variant
} else {
if maps := storage.Type.NMapType; maps != nil {
NMapTypeValue := &NMapType{
Hashers: maps.Hashers,
Value: metadataSiType[maps.ValueId],
KeysId: maps.KeysId,
ValueId: maps.ValueId,
}
if t := portable[maps.KeysId].Def.Tuple; t != nil {
for _, v := range *t {
NMapTypeValue.KeyVec = append(NMapTypeValue.KeyVec, metadataSiType[v])
}
} else {
NMapTypeValue.KeyVec = TupleDisassemble(metadataSiType[maps.KeysId])
}
module.Storage[index].Type.NMapType = NMapTypeValue
}
}
}
}
result.Metadata.Modules = modulesType
extrinsicMetadata := m.ProcessAndUpdateData("ExtrinsicMetadataV15").(map[string]interface{})
bm, _ = json.Marshal(extrinsicMetadata)
_ = json.Unmarshal(bm, &result.Extrinsic)
for index, extension := range result.Extrinsic.SignedExtensions {
result.Extrinsic.SignedExtensions[index].TypeString = metadataSiType[extension.Type]
result.Extrinsic.SignedIdentifier = append(result.Extrinsic.SignedIdentifier, extension.Identifier)
}
registerOriginCaller(originCallers)
// type
metadataType := m.ProcessAndUpdateData("SiLookupTypeId").(int)
result.Type = &metadataType
// apis
_ = utiles.UnmarshalAny(m.ProcessAndUpdateData("Vec<RuntimeApiMetadataV15>").([]interface{}), &result.Apis)
for index, api := range result.Apis {
for methodIndex, method := range api.Methods {
result.Apis[index].Methods[methodIndex].Outputs = metadataSiType[method.OutputsId]
for inputIndex, arg := range method.Inputs {
result.Apis[index].Methods[methodIndex].Inputs[inputIndex].Type = metadataSiType[arg.TypeId]
}
}
}
_ = utiles.UnmarshalAny(m.ProcessAndUpdateData("OuterEnumsMetadataV15").(interface{}), &result.OuterEnums)
_ = utiles.UnmarshalAny(m.ProcessAndUpdateData("CustomMetadataV15").(interface{}), &result.Customer)
m.Value = result
}
type MetadataV15Module struct {
MetadataV14Module
Docs []string `json:"docs"`
}
func (m *MetadataV15Module) Process() {
m.MetadataV14Module.Process()
result := MetadataV15Module{}
_ = utiles.UnmarshalAny(m.Value, &result)
_ = utiles.UnmarshalAny(m.ProcessAndUpdateData("Vec<Text>").([]interface{}), &result.Docs)
m.Value = result
}
type RuntimeApiMetadataV15 struct {
ScaleDecoder
}
// Process
// "RuntimeApiMetadataV15": {
// "name": "Text",
// "methods": "Vec<RuntimeApiMethodMetadataV15>",
// "docs": "Vec<Text>"
// }
func (m *RuntimeApiMetadataV15) Process() {
runtimeApiMetadata := RuntimeApiMetadata{}
runtimeApiMetadata.Name = m.ProcessAndUpdateData("Text").(string)
_ = utiles.UnmarshalAny(m.ProcessAndUpdateData("Vec<RuntimeApiMethodMetadataV15>").([]interface{}), &runtimeApiMetadata.Methods)
_ = utiles.UnmarshalAny(m.ProcessAndUpdateData("Vec<Text>").([]interface{}), &runtimeApiMetadata.Docs)
m.Value = runtimeApiMetadata
}
type RuntimeApiMethodParamMetadataV15 struct {
ScaleDecoder
}
// Process
// "RuntimeApiMethodParamMetadataV15": {
// "name": "Text",
// "type": "SiLookupTypeId"
// },
func (r *RuntimeApiMethodParamMetadataV15) Process() {
ra := RuntimeApiMethodParamMetadata{}
ra.Name = r.ProcessAndUpdateData("Text").(string)
ra.TypeId = r.ProcessAndUpdateData("SiLookupTypeId").(int)
r.Value = ra
}
type RuntimeApiMethodMetadataV15 struct {
ScaleDecoder
}
func (r *RuntimeApiMethodMetadataV15) Process() {
ra := RuntimeApiMethodMetadata{}
ra.Name = r.ProcessAndUpdateData("Text").(string)
_ = utiles.UnmarshalAny(r.ProcessAndUpdateData("Vec<RuntimeApiMethodParamMetadataV15>").([]interface{}), &ra.Inputs)
ra.OutputsId = r.ProcessAndUpdateData("SiLookupTypeId").(int)
_ = utiles.UnmarshalAny(r.ProcessAndUpdateData("Vec<Text>").([]interface{}), &ra.Docs)
r.Value = ra
}
type OuterEnumsMetadataV15 struct{ ScaleDecoder }
func (o *OuterEnumsMetadataV15) Process() {
ra := OuterEnumsMetadata{}
ra.CallType = o.ProcessAndUpdateData("SiLookupTypeId").(int)
ra.EventType = o.ProcessAndUpdateData("SiLookupTypeId").(int)
ra.ErrorType = o.ProcessAndUpdateData("SiLookupTypeId").(int)
// fmt.Println(ra.CallType, ra.ErrorType, ra.EventType)
o.Value = ra
}
type CustomMetadataV15 struct{ ScaleDecoder }
func (c *CustomMetadataV15) Process() {
ra := CustomMetadata{}
mapping := c.ProcessAndUpdateData("BTreeMap<Text, CustomValueMetadata15>").([]interface{})
_ = utiles.UnmarshalAny(mapping, &ra.Map)
c.Value = ra
}