Skip to content

Commit 0031c15

Browse files
committed
improve scale encode, event/extrinsic params add params raw
1 parent 439d799 commit 0031c15

File tree

11 files changed

+71
-42
lines changed

11 files changed

+71
-42
lines changed

event.go

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type EventRecord struct {
5353
Params []EventParam `json:"params"`
5454
Event scaleType.MetadataEvents `json:"event"`
5555
Topic []string `json:"topic"`
56+
ParamsRaw string `json:"params_raw"`
5657
}
5758

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

8081
e.Event = call.Call
8182
e.Module = call.Module.Name
83+
offset := e.Data.Offset
8284
for index, argType := range e.Event.Args {
8385
value := e.ProcessAndUpdateData(argType)
8486
param := EventParam{Type: argType, Value: value}
@@ -90,6 +92,7 @@ func (e *EventRecord) Process() map[string]interface{} {
9092
}
9193
e.Params = append(e.Params, param)
9294
}
95+
e.ParamsRaw = utiles.BytesToHex(e.Data.Data[offset:e.Data.Offset])
9396

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

113117
}

extrinsic.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type ExtrinsicDecoder struct {
3131
Era string `json:"era"`
3232
CallIndex string `json:"call_index"`
3333
Params []ExtrinsicParam `json:"params"`
34+
ParamsRaw string `json:"params_raw"`
3435
Metadata *scaleType.MetadataStruct
3536
SignedExtensions []scaleType.SignedExtension `json:"signed_extensions"`
3637
AdditionalCheck []string
@@ -96,6 +97,7 @@ type GenericExtrinsic struct {
9697
CallCode string `json:"call_code"`
9798
CallModule string `json:"call_module"`
9899
Params []ExtrinsicParam `json:"params"`
100+
ParamsRaw string `json:"params_raw"`
99101
}
100102

101103
func (e *ExtrinsicDecoder) Process() {
@@ -181,10 +183,11 @@ func (e *ExtrinsicDecoder) Process() {
181183
panic(fmt.Sprintf("Not find Extrinsic Lookup %s, please check metadata info", e.CallIndex))
182184
}
183185
e.Module = call.Module.Name
184-
186+
offset := e.Data.Offset
185187
for _, arg := range call.Call.Args {
186188
e.Params = append(e.Params, ExtrinsicParam{Name: arg.Name, Type: arg.Type, Value: e.ProcessAndUpdateData(arg.Type), TypeName: arg.TypeName})
187189
}
190+
e.ParamsRaw = utiles.BytesToHex(e.Data.Data[offset:e.Data.Offset])
188191

189192
if e.ContainsTransaction {
190193
result.AccountId = e.Address
@@ -197,6 +200,7 @@ func (e *ExtrinsicDecoder) Process() {
197200
result.CallModuleFunction = call.Call.Name
198201
result.CallModule = call.Module.Name
199202
result.Params = e.Params
203+
result.ParamsRaw = e.ParamsRaw
200204
e.Value = &result
201205
}
202206

extrinsic_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package scalecodec_test
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"os"
66
"testing"
77

88
"github.com/itering/scale.go"
@@ -18,7 +18,7 @@ func TestV14ExtrinsicDecoder(t *testing.T) {
1818
m := scalecodec.MetadataDecoder{}
1919
m.Init(utiles.HexToBytes(kusamaV14))
2020
_ = m.Process()
21-
c, err := ioutil.ReadFile(fmt.Sprintf("%s.json", "network/kusama"))
21+
c, err := os.ReadFile(fmt.Sprintf("%s.json", "network/kusama"))
2222
if err != nil {
2323
panic(err)
2424
}
@@ -28,6 +28,7 @@ func TestV14ExtrinsicDecoder(t *testing.T) {
2828
extrinsicRaw := "0x1d03840018c7717a3c5d2930f10eb5b0f67c191210e018bc55481bc44c1c1c7e810e945c01922c584c1c205b9747e76aad28cf2e73f729a9b3180072c47cd3abd205bb4b54f78a2627fa62a799f363fde25b5db74e5f8d8f3bde7a9a7f2ea8c95033d84e8585030800630301000400000000070088526a74080700000000070088526a74005ed0b200000000005ed0b20000000000000101000100000000010100707fd754e80e531ad411eb8b433548acbe669bf46a7e896e440feadc5ef3530800bca06501000000"
2929
e.Init(scaleBytes.ScaleBytes{Data: utiles.HexToBytes(extrinsicRaw)}, &option)
3030
e.Process()
31+
assert.Equal(t, e.Value.(*scalecodec.GenericExtrinsic).ParamsRaw, "01000400000000070088526a74080700000000070088526a74005ed0b200000000005ed0b20000000000000101000100000000010100707fd754e80e531ad411eb8b433548acbe669bf46a7e896e440feadc5ef3530800bca06501000000")
3132
}
3233

3334
func TestCallEncode(t *testing.T) {

source/base.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/grandpa/definitions.json

-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@
107107
],
108108
"type": "enum"
109109
},
110-
"NextAuthority": "(AuthorityId, AuthorityWeight)",
111110
"AuthorityIndex": "u64",
112111
"GrandpaEquivocationProof": {
113112
"type_mapping": [

types/Call.go

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func (s *Call) Process() {
3333
type BoxCall struct {
3434
CallIndex string `json:"call_Index"`
3535
Params []ExtrinsicParam `json:"params"`
36+
CallArgs []ExtrinsicParam `json:"call_args"`
3637
}
3738

3839
func (s *Call) Encode(value interface{}) string {
@@ -51,6 +52,9 @@ func (s *Call) Encode(value interface{}) string {
5152
default:
5253
panic("input value is not valid boxCall")
5354
}
55+
if len(boxCall.CallArgs) > 0 {
56+
boxCall.Params = boxCall.CallArgs
57+
}
5458
callIndex := utiles.TrimHex(boxCall.CallIndex)
5559
raw := callIndex
5660
callModule, ok := s.Metadata.CallIndex[callIndex]

types/Enum.go

+23-14
Original file line numberDiff line numberDiff line change
@@ -91,23 +91,32 @@ func (e *Enum) Encode(data interface{}) string {
9191
}
9292
}
9393
}
94-
for enumKey, value := range data.(map[string]interface{}) {
95-
index := 0
96-
for k, v := range e.TypeMapping.Names {
97-
if v == enumKey {
98-
subType := e.TypeMapping.Types[k]
99-
var typeMap [][]string
100-
if len(subType) > 4 && subType[0:2] == "[[" && json.Unmarshal([]byte(subType), &typeMap) == nil && len(typeMap) > 0 && len(typeMap[0]) == 2 {
101-
var raw string
102-
valueStruct := value.(map[string]interface{})
103-
for _, st := range typeMap {
104-
raw += EncodeWithOpt(st[1], valueStruct[st[0]], &ScaleDecoderOption{Spec: e.Spec, Metadata: e.Metadata})
94+
switch dataValue := data.(type) {
95+
case map[string]interface{}:
96+
for enumKey, value := range dataValue {
97+
index := 0
98+
for k, v := range e.TypeMapping.Names {
99+
if v == enumKey {
100+
subType := e.TypeMapping.Types[k]
101+
var typeMap [][]string
102+
if len(subType) > 4 && subType[0:2] == "[[" && json.Unmarshal([]byte(subType), &typeMap) == nil && len(typeMap) > 0 && len(typeMap[0]) == 2 {
103+
var raw string
104+
valueStruct := value.(map[string]interface{})
105+
for _, st := range typeMap {
106+
raw += EncodeWithOpt(st[1], valueStruct[st[0]], &ScaleDecoderOption{Spec: e.Spec, Metadata: e.Metadata})
107+
}
108+
return utiles.U8Encode(index) + raw
105109
}
106-
return utiles.U8Encode(index) + raw
110+
return utiles.U8Encode(index) + EncodeWithOpt(subType, value, &ScaleDecoderOption{Spec: e.Spec, Metadata: e.Metadata})
107111
}
108-
return utiles.U8Encode(index) + EncodeWithOpt(subType, value, &ScaleDecoderOption{Spec: e.Spec, Metadata: e.Metadata})
112+
index++
113+
}
114+
}
115+
case string:
116+
for index, v := range e.TypeMapping.Names {
117+
if v == dataValue {
118+
return utiles.U8Encode(index)
109119
}
110-
index++
111120
}
112121
}
113122
}

types/FixedU8.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package types
22

33
import (
44
"fmt"
5+
"strings"
56

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

2324
func (s *FixedU8) Encode(value interface{}) string {
24-
switch value.(type) {
25+
switch v := value.(type) {
2526
case string:
26-
return utiles.TrimHex(value.(string))
27+
valueStr := v
28+
if strings.HasPrefix(valueStr, "0x") {
29+
return utiles.TrimHex(valueStr)
30+
} else {
31+
return utiles.BytesToHex([]byte(valueStr))
32+
}
2733
case []byte:
28-
return utiles.TrimHex(utiles.BytesToHex(value.([]byte)))
34+
return utiles.TrimHex(utiles.BytesToHex(v))
2935
default:
3036
panic("type error,only support string or []byte")
3137
}
32-
return ""
3338
}
3439

3540
func (s *FixedU8) TypeStructString() string {

types/base.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ func (s *ScaleDecoder) Init(data scaleBytes.ScaleBytes, option *ScaleDecoderOpti
9898

9999
func (s *ScaleDecoder) Process() {}
100100

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

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

191193
func EncodeWithOpt(typeString string, data interface{}, opt *ScaleDecoderOption) string {
192194
r := RuntimeType{}
193-
if typeString == "Null" {
195+
if strings.EqualFold(typeString, "Null") {
194196
return ""
195197
}
196198
if opt == nil {
197199
opt = &ScaleDecoderOption{Spec: -1}
198200
}
201+
opt.TypeName = typeString
199202
class, value, subType := r.GetCodecClass(typeString, opt.Spec)
200203
if class == nil {
201204
panic(fmt.Sprintf("Not found decoder class %s", typeString))

types/types.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ func (e *Empty) Process() {
4141
e.Value = "NULL"
4242
}
4343

44+
func (e *Empty) Encode(interface{}) string { return "" }
45+
4446
func (h *H160) Process() {
4547
h.Value = utiles.AddHex(utiles.BytesToHex(h.NextBytes(20)))
4648
}
@@ -170,11 +172,7 @@ func (a *GenericAddress) Process() {
170172
}
171173

172174
type Signature struct {
173-
ScaleDecoder
174-
}
175-
176-
func (s *Signature) Process() {
177-
s.Value = utiles.BytesToHex(s.NextBytes(64))
175+
H512
178176
}
179177

180178
type AccountId struct {
@@ -330,7 +328,7 @@ func (e *EcdsaSignature) Process() {
330328
}
331329

332330
type EthereumAddress struct {
333-
ScaleDecoder
331+
H160
334332
}
335333

336334
func (e *EthereumAddress) Process() {

types/v15.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ func (m *MetadataV15Decoder) Process() {
175175
}
176176
}
177177

178-
_ = utiles.UnmarshalAny(m.ProcessAndUpdateData("OuterEnumsMetadataV15").(interface{}), &result.OuterEnums)
179-
_ = utiles.UnmarshalAny(m.ProcessAndUpdateData("CustomMetadataV15").(interface{}), &result.Customer)
178+
_ = utiles.UnmarshalAny(m.ProcessAndUpdateData("OuterEnumsMetadataV15"), &result.OuterEnums)
179+
_ = utiles.UnmarshalAny(m.ProcessAndUpdateData("CustomMetadataV15"), &result.Customer)
180180
m.Value = result
181181
}
182182

@@ -198,11 +198,12 @@ type RuntimeApiMetadataV15 struct {
198198
}
199199

200200
// Process
201-
// "RuntimeApiMetadataV15": {
202-
// "name": "Text",
203-
// "methods": "Vec<RuntimeApiMethodMetadataV15>",
204-
// "docs": "Vec<Text>"
205-
// }
201+
//
202+
// "RuntimeApiMetadataV15": {
203+
// "name": "Text",
204+
// "methods": "Vec<RuntimeApiMethodMetadataV15>",
205+
// "docs": "Vec<Text>"
206+
// }
206207
func (m *RuntimeApiMetadataV15) Process() {
207208
runtimeApiMetadata := RuntimeApiMetadata{}
208209
runtimeApiMetadata.Name = m.ProcessAndUpdateData("Text").(string)
@@ -216,10 +217,11 @@ type RuntimeApiMethodParamMetadataV15 struct {
216217
}
217218

218219
// Process
219-
// "RuntimeApiMethodParamMetadataV15": {
220-
// "name": "Text",
221-
// "type": "SiLookupTypeId"
222-
// },
220+
//
221+
// "RuntimeApiMethodParamMetadataV15": {
222+
// "name": "Text",
223+
// "type": "SiLookupTypeId"
224+
// },
223225
func (r *RuntimeApiMethodParamMetadataV15) Process() {
224226
ra := RuntimeApiMethodParamMetadata{}
225227
ra.Name = r.ProcessAndUpdateData("Text").(string)

0 commit comments

Comments
 (0)