Skip to content

Instantly share code, notes, and snippets.

@cdamian
Created July 22, 2022 14:22
Show Gist options
  • Save cdamian/0c6b1424e65d458b0a5b01c0bdb9e998 to your computer and use it in GitHub Desktop.
Save cdamian/0c6b1424e65d458b0a5b01c0bdb9e998 to your computer and use it in GitHub Desktop.
Reverse metadata lookup
func findCall(ext types.Extrinsic, m *types.Metadata) string {
meta := m.AsMetadataV14
call := ext.Method
var sb strings.Builder
for _, mod := range meta.Pallets {
if !mod.HasCalls {
continue
}
if mod.Index != types.U8(call.CallIndex.SectionIndex) {
continue
}
if mod.Name != "Proxy" {
continue
}
sb.WriteString("Pallet name - " + string(mod.Name))
sb.WriteString("\n")
callType := mod.Calls.Type.Int64()
if typ, ok := meta.EfficientLookup[callType]; ok {
if len(typ.Def.Variant.Variants) > 0 {
for _, vars := range typ.Def.Variant.Variants {
if vars.Name != "anonymous" {
continue
}
sb.WriteString("Method name - " + string(vars.Name))
sb.WriteString("\n")
if vars.Index == types.U8(call.CallIndex.MethodIndex) {
decoder := scale.NewDecoder(bytes.NewReader(call.Args))
sb.WriteString("Args:\n")
for _, field := range vars.Fields {
switch fn := field.Name; fn {
case "proxy_type":
var pt types.ProxyType
decoder.Decode(&pt)
sb.WriteString(fmt.Sprintf("%s - %d\n", fn, pt))
case "delay":
var bn types.BlockNumber
decoder.Decode(&bn)
sb.WriteString(fmt.Sprintf("%s - %d\n", fn, bn))
case "index":
var index types.U16
decoder.Decode(&index)
sb.WriteString(fmt.Sprintf("%s - %d\n", fn, index))
}
}
}
}
}
}
}
return sb.String()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment