-
Notifications
You must be signed in to change notification settings - Fork 36
/
v13.go
270 lines (246 loc) · 8.91 KB
/
v13.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
263
264
265
266
267
268
269
270
package types
import (
"encoding/json"
"fmt"
"sort"
"strings"
"github.com/huandu/xstrings"
"github.com/itering/scale.go/types/convert"
"github.com/itering/scale.go/types/scaleBytes"
"github.com/itering/scale.go/utiles"
)
type MetadataV13Decoder struct {
ScaleDecoder
}
func (m *MetadataV13Decoder) Init(data scaleBytes.ScaleBytes, option *ScaleDecoderOption) {
m.ScaleDecoder.Init(data, option)
}
// https://github.com/polkadot-js/api/blob/ddf6f574f616d28cc0c59354baaf58208a776274/packages/metadata/src/v13/toLatest.ts#L14
var KnownOrigins = map[string]string{
"Council": "CollectiveOrigin",
"System": "SystemOrigin",
"TechnicalCommittee": "CollectiveOrigin",
// Polkadot
"Xcm": "XcmOrigin",
"XcmPallet": "XcmOrigin",
// Acala
"Authority": "AuthorityOrigin",
"GeneralCouncil": "CollectiveOrigin",
}
type OriginCaller struct {
Name string
Index int
}
func (m *MetadataV13Decoder) Process() {
result := MetadataStruct{
Metadata: MetadataTag{
Modules: nil,
},
}
MetadataV11ModuleCall := m.ProcessAndUpdateData("Vec<MetadataV13Module>").([]interface{})
bm, _ := json.Marshal(MetadataV11ModuleCall)
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})
if module.Calls != nil {
for callIndex, call := range module.Calls {
modulesType[k].Calls[callIndex].Lookup = xstrings.RightJustify(utiles.IntToHex(module.Index), 2, "0") + xstrings.RightJustify(utiles.IntToHex(callIndex), 2, "0")
result.CallIndex[modulesType[k].Calls[callIndex].Lookup] = CallIndex{Module: MetadataModules{Name: module.Name}, Call: call}
}
}
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}
}
}
}
result.Metadata.Modules = modulesType
extrinsicMetadata := m.ProcessAndUpdateData("ExtrinsicMetadata").(map[string]interface{})
bm, _ = json.Marshal(extrinsicMetadata)
_ = json.Unmarshal(bm, &result.Extrinsic)
registerOriginCaller(originCallers)
var extrinsic ExtrinsicMetadataV12
bm, _ = json.Marshal(extrinsicMetadata)
_ = json.Unmarshal(bm, &extrinsic)
result.Extrinsic = &ExtrinsicMetadata{SignedIdentifier: extrinsic.SignedExtensions}
m.Value = result
}
// https://github.com/polkadot-js/api/blob/ddf6f574f616d28cc0c59354baaf58208a776274/packages/metadata/src/v13/toLatest.ts#L117
func registerOriginCaller(originCallers []OriginCaller) {
sort.Slice(originCallers[:], func(i, j int) bool { return originCallers[i].Index < originCallers[j].Index })
e := Enum{}
e.TypeMapping = &TypeMapping{}
for _, caller := range originCallers {
for i := len(e.TypeMapping.Names); i < caller.Index; i++ {
e.TypeMapping.Names = append(e.TypeMapping.Names, fmt.Sprintf("EMPTY%d", i))
e.TypeMapping.Types = append(e.TypeMapping.Types, "NULL")
}
e.TypeMapping.Names = append(e.TypeMapping.Names, caller.Name)
if t, ok := KnownOrigins[caller.Name]; ok {
e.TypeMapping.Types = append(e.TypeMapping.Types, t)
} else {
e.TypeMapping.Types = append(e.TypeMapping.Types, "NULL")
}
}
regCustomKey(strings.ToLower("OriginCaller"), &e)
}
type MetadataV13Module struct {
ScaleDecoder
Name string `json:"name"`
Prefix string `json:"prefix"`
CallIndex string `json:"call_index"`
HasStorage bool `json:"has_storage"`
Storage []MetadataStorage `json:"storage"`
HasCalls bool `json:"has_calls"`
Calls []MetadataModuleCall `json:"calls"`
HasEvents bool `json:"has_events"`
Events []MetadataEvents `json:"events"`
Constants []map[string]interface{} `json:"constants"`
Errors []MetadataModuleError `json:"errors"`
Index int `json:"index"`
}
func (m *MetadataV13Module) Process() {
cm := MetadataV13Module{}
cm.Name = m.ProcessAndUpdateData("String").(string)
// storage
cm.HasStorage = m.ProcessAndUpdateData("bool").(bool)
if cm.HasStorage {
storageValue := m.ProcessAndUpdateData("MetadataV13ModuleStorage").(MetadataV7ModuleStorage)
cm.Storage = storageValue.Items
cm.Prefix = storageValue.Prefix
}
// call
cm.HasCalls = m.ProcessAndUpdateData("bool").(bool)
if cm.HasCalls {
callValue := m.ProcessAndUpdateData("Vec<MetadataModuleCall>").([]interface{})
var calls []MetadataModuleCall
for _, v := range callValue {
calls = append(calls, v.(MetadataModuleCall))
}
cm.Calls = calls
}
// event
cm.HasEvents = m.ProcessAndUpdateData("bool").(bool)
if cm.HasEvents {
eventValue := m.ProcessAndUpdateData("Vec<MetadataModuleEvent>").([]interface{})
var events []MetadataEvents
for _, v := range eventValue {
events = append(events, v.(MetadataEvents))
}
cm.Events = events
}
// constant
constantValue := m.ProcessAndUpdateData("Vec<MetadataV7ModuleConstants>").([]interface{})
var constants []map[string]interface{}
for _, v := range constantValue {
constants = append(constants, v.(map[string]interface{}))
}
cm.Constants = constants
errorValue := m.ProcessAndUpdateData("Vec<MetadataModuleError>").([]interface{})
var errors []MetadataModuleError
for _, v := range errorValue {
errors = append(errors, v.(MetadataModuleError))
}
cm.Errors = errors
cm.Index = m.ProcessAndUpdateData("U8").(int)
m.Value = cm
}
type MetadataV13ModuleStorage struct {
MetadataV6ModuleStorage
Prefix string `json:"prefix"`
Items []MetadataStorage `json:"items"`
}
func (m *MetadataV13ModuleStorage) Process() {
cm := MetadataV7ModuleStorage{}
cm.Prefix = m.ProcessAndUpdateData("String").(string)
itemsValue := m.ProcessAndUpdateData("Vec<MetadataV13ModuleStorageEntry>").([]interface{})
var items []MetadataStorage
for _, v := range itemsValue {
items = append(items, v.(MetadataStorage))
}
cm.Items = items
m.Value = cm
}
type MetadataV13ModuleStorageEntry struct {
ScaleDecoder
Name string `json:"name"`
Modifier string `json:"modifier"`
Type StorageType `json:"type"`
Fallback string `json:"fallback"`
Docs []string `json:"docs"`
Hasher string `json:"hasher"`
}
func (m *MetadataV13ModuleStorageEntry) Init(data scaleBytes.ScaleBytes, option *ScaleDecoderOption) {
m.ScaleDecoder.Init(data, option)
}
func (m *MetadataV13ModuleStorageEntry) Process() {
m.Name = m.ProcessAndUpdateData("String").(string)
m.Modifier = m.ProcessAndUpdateData("StorageModify").(string)
storageFunctionType := m.ProcessAndUpdateData("StorageFunctionType").(string)
switch storageFunctionType {
case "MapType":
m.Hasher = m.ProcessAndUpdateData("StorageHasher").(string)
m.Type = StorageType{
Origin: "MapType",
MapType: &MapType{
Hasher: m.Hasher,
Key: convert.ConvertType(m.ProcessAndUpdateData("String").(string)),
Value: convert.ConvertType(m.ProcessAndUpdateData("String").(string)),
IsLinked: m.ProcessAndUpdateData("bool").(bool)},
}
case "DoubleMapType":
m.Hasher = m.ProcessAndUpdateData("StorageHasher").(string)
key1 := convert.ConvertType(m.ProcessAndUpdateData("String").(string))
key2 := convert.ConvertType(m.ProcessAndUpdateData("String").(string))
value := convert.ConvertType(m.ProcessAndUpdateData("String").(string))
key2Hasher := m.ProcessAndUpdateData("StorageHasher").(string)
m.Type = StorageType{
Origin: "DoubleMapType",
DoubleMapType: &MapType{
Hasher: m.Hasher,
Key: key1,
Key2: key2,
Value: value,
Key2Hasher: key2Hasher,
},
}
case "PlainType":
plainType := convert.ConvertType(m.ProcessAndUpdateData("String").(string))
m.Type = StorageType{
Origin: "PlainType",
PlainType: &plainType}
case "NMap":
var KeyVec []string
for _, v := range m.ProcessAndUpdateData("Vec<String>").([]interface{}) {
KeyVec = append(KeyVec, convert.ConvertType(v.(string)))
}
var hasherVec []string
for _, v := range m.ProcessAndUpdateData("Vec<StorageHasher>").([]interface{}) {
hasherVec = append(hasherVec, convert.ConvertType(v.(string)))
}
m.Type = StorageType{
Origin: "NMapType",
NMapType: &NMapType{
Hashers: hasherVec,
KeyVec: KeyVec,
Value: convert.ConvertType(m.ProcessAndUpdateData("String").(string)),
}}
}
m.Fallback = m.ProcessAndUpdateData("HexBytes").(string)
docs := m.ProcessAndUpdateData("Vec<String>").([]interface{})
for _, v := range docs {
m.Docs = append(m.Docs, v.(string))
}
m.Value = MetadataStorage{
Name: m.Name,
Modifier: m.Modifier,
Type: m.Type,
Fallback: m.Fallback,
Docs: m.Docs,
}
}