From 04a60c4076b1d41c02187c9f99fde039beff0328 Mon Sep 17 00:00:00 2001 From: freehere107 Date: Fri, 28 Jul 2023 14:37:58 +0800 Subject: [PATCH] Fix some tips not deciaml type --- extrinsic.go | 4 ++-- utiles/tools.go | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/extrinsic.go b/extrinsic.go index b113bd5..6785333 100644 --- a/extrinsic.go +++ b/extrinsic.go @@ -139,10 +139,10 @@ func (e *ExtrinsicDecoder) Process() { e.Nonce = int(e.ProcessAndUpdateData("Compact").(uint64)) if e.Metadata.Extrinsic != nil { if utiles.SliceIndex("ChargeTransactionPayment", e.Metadata.Extrinsic.SignedIdentifier) != -1 { - result.Tip = e.ProcessAndUpdateData("Compact").(decimal.Decimal) + result.Tip = utiles.DecimalFromInterface(e.ProcessAndUpdateData("Compact")) } } else { - result.Tip = e.ProcessAndUpdateData("Compact").(decimal.Decimal) + result.Tip = utiles.DecimalFromInterface(e.ProcessAndUpdateData("Compact")) } // spec SignedExtensions result.SignedExtensions = make(map[string]interface{}) diff --git a/utiles/tools.go b/utiles/tools.go index 6b6ca77..a7f555b 100644 --- a/utiles/tools.go +++ b/utiles/tools.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/shopspring/decimal" "math/big" "reflect" "strconv" @@ -168,3 +169,24 @@ func GetEnumValue(e map[string]interface{}) (string, interface{}, error) { } return "", "", errors.New("empty enum") } + +func DecimalFromInterface(i interface{}) decimal.Decimal { + switch i := i.(type) { + case int: + return decimal.New(int64(i), 0) + case int64: + return decimal.New(i, 0) + case uint64: + return decimal.New(int64(i), 0) + case float64: + return decimal.NewFromFloat(i) + case string: + r, _ := decimal.NewFromString(i) + return r + case decimal.Decimal: + return i + case *big.Int: + return decimal.NewFromBigInt(i, 0) + } + return decimal.Zero +}