Skip to content

Commit

Permalink
improve scale encode, event/extrinsic params add params raw
Browse files Browse the repository at this point in the history
  • Loading branch information
freehere107 committed Jul 17, 2024
1 parent 439d799 commit 0031c15
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 42 deletions.
4 changes: 4 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type EventRecord struct {
Params []EventParam `json:"params"`
Event scaleType.MetadataEvents `json:"event"`
Topic []string `json:"topic"`
ParamsRaw string `json:"params_raw"`
}

func (e *EventRecord) Process() map[string]interface{} {
Expand All @@ -79,6 +80,7 @@ func (e *EventRecord) Process() map[string]interface{} {

e.Event = call.Call
e.Module = call.Module.Name
offset := e.Data.Offset
for index, argType := range e.Event.Args {
value := e.ProcessAndUpdateData(argType)
param := EventParam{Type: argType, Value: value}
Expand All @@ -90,6 +92,7 @@ func (e *EventRecord) Process() map[string]interface{} {
}
e.Params = append(e.Params, param)
}
e.ParamsRaw = utiles.BytesToHex(e.Data.Data[offset:e.Data.Offset])

if e.Metadata.MetadataVersion >= 5 {
if topic := e.ProcessAndUpdateData("Vec<Hash>"); topic != nil {
Expand All @@ -108,6 +111,7 @@ func (e *EventRecord) Process() map[string]interface{} {
"event_id": e.Event.Name,
"params": e.Params,
"topic": e.Topic,
"params_raw": e.ParamsRaw,
}

}
6 changes: 5 additions & 1 deletion extrinsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type ExtrinsicDecoder struct {
Era string `json:"era"`
CallIndex string `json:"call_index"`
Params []ExtrinsicParam `json:"params"`
ParamsRaw string `json:"params_raw"`
Metadata *scaleType.MetadataStruct
SignedExtensions []scaleType.SignedExtension `json:"signed_extensions"`
AdditionalCheck []string
Expand Down Expand Up @@ -96,6 +97,7 @@ type GenericExtrinsic struct {
CallCode string `json:"call_code"`
CallModule string `json:"call_module"`
Params []ExtrinsicParam `json:"params"`
ParamsRaw string `json:"params_raw"`
}

func (e *ExtrinsicDecoder) Process() {
Expand Down Expand Up @@ -181,10 +183,11 @@ func (e *ExtrinsicDecoder) Process() {
panic(fmt.Sprintf("Not find Extrinsic Lookup %s, please check metadata info", e.CallIndex))
}
e.Module = call.Module.Name

offset := e.Data.Offset
for _, arg := range call.Call.Args {
e.Params = append(e.Params, ExtrinsicParam{Name: arg.Name, Type: arg.Type, Value: e.ProcessAndUpdateData(arg.Type), TypeName: arg.TypeName})
}
e.ParamsRaw = utiles.BytesToHex(e.Data.Data[offset:e.Data.Offset])

if e.ContainsTransaction {
result.AccountId = e.Address
Expand All @@ -197,6 +200,7 @@ func (e *ExtrinsicDecoder) Process() {
result.CallModuleFunction = call.Call.Name
result.CallModule = call.Module.Name
result.Params = e.Params
result.ParamsRaw = e.ParamsRaw
e.Value = &result
}

Expand Down
5 changes: 3 additions & 2 deletions extrinsic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package scalecodec_test

import (
"fmt"
"io/ioutil"
"os"
"testing"

"github.com/itering/scale.go"
Expand All @@ -18,7 +18,7 @@ func TestV14ExtrinsicDecoder(t *testing.T) {
m := scalecodec.MetadataDecoder{}
m.Init(utiles.HexToBytes(kusamaV14))
_ = m.Process()
c, err := ioutil.ReadFile(fmt.Sprintf("%s.json", "network/kusama"))
c, err := os.ReadFile(fmt.Sprintf("%s.json", "network/kusama"))

Check failure on line 21 in extrinsic_test.go

View workflow job for this annotation

GitHub Actions / Build and Publish

undefined: os.ReadFile
if err != nil {
panic(err)
}
Expand All @@ -28,6 +28,7 @@ func TestV14ExtrinsicDecoder(t *testing.T) {
extrinsicRaw := "0x1d03840018c7717a3c5d2930f10eb5b0f67c191210e018bc55481bc44c1c1c7e810e945c01922c584c1c205b9747e76aad28cf2e73f729a9b3180072c47cd3abd205bb4b54f78a2627fa62a799f363fde25b5db74e5f8d8f3bde7a9a7f2ea8c95033d84e8585030800630301000400000000070088526a74080700000000070088526a74005ed0b200000000005ed0b20000000000000101000100000000010100707fd754e80e531ad411eb8b433548acbe669bf46a7e896e440feadc5ef3530800bca06501000000"
e.Init(scaleBytes.ScaleBytes{Data: utiles.HexToBytes(extrinsicRaw)}, &option)
e.Process()
assert.Equal(t, e.Value.(*scalecodec.GenericExtrinsic).ParamsRaw, "01000400000000070088526a74080700000000070088526a74005ed0b200000000005ed0b20000000000000101000100000000010100707fd754e80e531ad411eb8b433548acbe669bf46a7e896e440feadc5ef3530800bca06501000000")
}

func TestCallEncode(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion source/base.go

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion source/grandpa/definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@
],
"type": "enum"
},
"NextAuthority": "(AuthorityId, AuthorityWeight)",
"AuthorityIndex": "u64",
"GrandpaEquivocationProof": {
"type_mapping": [
Expand Down
4 changes: 4 additions & 0 deletions types/Call.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (s *Call) Process() {
type BoxCall struct {
CallIndex string `json:"call_Index"`
Params []ExtrinsicParam `json:"params"`
CallArgs []ExtrinsicParam `json:"call_args"`
}

func (s *Call) Encode(value interface{}) string {
Expand All @@ -51,6 +52,9 @@ func (s *Call) Encode(value interface{}) string {
default:
panic("input value is not valid boxCall")
}
if len(boxCall.CallArgs) > 0 {
boxCall.Params = boxCall.CallArgs
}
callIndex := utiles.TrimHex(boxCall.CallIndex)
raw := callIndex
callModule, ok := s.Metadata.CallIndex[callIndex]
Expand Down
37 changes: 23 additions & 14 deletions types/Enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,32 @@ func (e *Enum) Encode(data interface{}) string {
}
}
}
for enumKey, value := range data.(map[string]interface{}) {
index := 0
for k, v := range e.TypeMapping.Names {
if v == enumKey {
subType := e.TypeMapping.Types[k]
var typeMap [][]string
if len(subType) > 4 && subType[0:2] == "[[" && json.Unmarshal([]byte(subType), &typeMap) == nil && len(typeMap) > 0 && len(typeMap[0]) == 2 {
var raw string
valueStruct := value.(map[string]interface{})
for _, st := range typeMap {
raw += EncodeWithOpt(st[1], valueStruct[st[0]], &ScaleDecoderOption{Spec: e.Spec, Metadata: e.Metadata})
switch dataValue := data.(type) {
case map[string]interface{}:
for enumKey, value := range dataValue {
index := 0
for k, v := range e.TypeMapping.Names {
if v == enumKey {
subType := e.TypeMapping.Types[k]
var typeMap [][]string
if len(subType) > 4 && subType[0:2] == "[[" && json.Unmarshal([]byte(subType), &typeMap) == nil && len(typeMap) > 0 && len(typeMap[0]) == 2 {
var raw string
valueStruct := value.(map[string]interface{})
for _, st := range typeMap {
raw += EncodeWithOpt(st[1], valueStruct[st[0]], &ScaleDecoderOption{Spec: e.Spec, Metadata: e.Metadata})
}
return utiles.U8Encode(index) + raw
}
return utiles.U8Encode(index) + raw
return utiles.U8Encode(index) + EncodeWithOpt(subType, value, &ScaleDecoderOption{Spec: e.Spec, Metadata: e.Metadata})
}
return utiles.U8Encode(index) + EncodeWithOpt(subType, value, &ScaleDecoderOption{Spec: e.Spec, Metadata: e.Metadata})
index++
}
}
case string:
for index, v := range e.TypeMapping.Names {
if v == dataValue {
return utiles.U8Encode(index)
}
index++
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions types/FixedU8.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"fmt"
"strings"

"github.com/itering/scale.go/utiles"
)
Expand All @@ -21,15 +22,19 @@ func (s *FixedU8) Process() {
}

func (s *FixedU8) Encode(value interface{}) string {
switch value.(type) {
switch v := value.(type) {
case string:
return utiles.TrimHex(value.(string))
valueStr := v
if strings.HasPrefix(valueStr, "0x") {
return utiles.TrimHex(valueStr)
} else {
return utiles.BytesToHex([]byte(valueStr))
}
case []byte:
return utiles.TrimHex(utiles.BytesToHex(value.([]byte)))
return utiles.TrimHex(utiles.BytesToHex(v))
default:
panic("type error,only support string or []byte")
}
return ""
}

func (s *FixedU8) TypeStructString() string {
Expand Down
7 changes: 5 additions & 2 deletions types/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ func (s *ScaleDecoder) Init(data scaleBytes.ScaleBytes, option *ScaleDecoderOpti

func (s *ScaleDecoder) Process() {}

func (s *ScaleDecoder) Encode(interface{}) string { return "" }
func (s *ScaleDecoder) Encode(interface{}) string {
panic(fmt.Sprintf("not found base type %s", s.TypeName))
}

// TypeStructString Type Struct string
func (s *ScaleDecoder) TypeStructString() string {
Expand Down Expand Up @@ -190,12 +192,13 @@ func Encode(typeString string, data interface{}) string {

func EncodeWithOpt(typeString string, data interface{}, opt *ScaleDecoderOption) string {
r := RuntimeType{}
if typeString == "Null" {
if strings.EqualFold(typeString, "Null") {
return ""
}
if opt == nil {
opt = &ScaleDecoderOption{Spec: -1}
}
opt.TypeName = typeString
class, value, subType := r.GetCodecClass(typeString, opt.Spec)
if class == nil {
panic(fmt.Sprintf("Not found decoder class %s", typeString))
Expand Down
10 changes: 4 additions & 6 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func (e *Empty) Process() {
e.Value = "NULL"
}

func (e *Empty) Encode(interface{}) string { return "" }

func (h *H160) Process() {
h.Value = utiles.AddHex(utiles.BytesToHex(h.NextBytes(20)))
}
Expand Down Expand Up @@ -170,11 +172,7 @@ func (a *GenericAddress) Process() {
}

type Signature struct {
ScaleDecoder
}

func (s *Signature) Process() {
s.Value = utiles.BytesToHex(s.NextBytes(64))
H512
}

type AccountId struct {
Expand Down Expand Up @@ -330,7 +328,7 @@ func (e *EcdsaSignature) Process() {
}

type EthereumAddress struct {
ScaleDecoder
H160
}

func (e *EthereumAddress) Process() {
Expand Down
24 changes: 13 additions & 11 deletions types/v15.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ func (m *MetadataV15Decoder) Process() {
}
}

_ = utiles.UnmarshalAny(m.ProcessAndUpdateData("OuterEnumsMetadataV15").(interface{}), &result.OuterEnums)
_ = utiles.UnmarshalAny(m.ProcessAndUpdateData("CustomMetadataV15").(interface{}), &result.Customer)
_ = utiles.UnmarshalAny(m.ProcessAndUpdateData("OuterEnumsMetadataV15"), &result.OuterEnums)
_ = utiles.UnmarshalAny(m.ProcessAndUpdateData("CustomMetadataV15"), &result.Customer)
m.Value = result
}

Expand All @@ -198,11 +198,12 @@ type RuntimeApiMetadataV15 struct {
}

// Process
// "RuntimeApiMetadataV15": {
// "name": "Text",
// "methods": "Vec<RuntimeApiMethodMetadataV15>",
// "docs": "Vec<Text>"
// }
//
// "RuntimeApiMetadataV15": {
// "name": "Text",
// "methods": "Vec<RuntimeApiMethodMetadataV15>",
// "docs": "Vec<Text>"
// }
func (m *RuntimeApiMetadataV15) Process() {
runtimeApiMetadata := RuntimeApiMetadata{}
runtimeApiMetadata.Name = m.ProcessAndUpdateData("Text").(string)
Expand All @@ -216,10 +217,11 @@ type RuntimeApiMethodParamMetadataV15 struct {
}

// Process
// "RuntimeApiMethodParamMetadataV15": {
// "name": "Text",
// "type": "SiLookupTypeId"
// },
//
// "RuntimeApiMethodParamMetadataV15": {
// "name": "Text",
// "type": "SiLookupTypeId"
// },
func (r *RuntimeApiMethodParamMetadataV15) Process() {
ra := RuntimeApiMethodParamMetadata{}
ra.Name = r.ProcessAndUpdateData("Text").(string)
Expand Down

0 comments on commit 0031c15

Please sign in to comment.