Skip to content

Commit

Permalink
Fix some tips not deciaml type
Browse files Browse the repository at this point in the history
  • Loading branch information
freehere107 committed Jul 28, 2023
1 parent abb8824 commit 04a60c4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions extrinsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ func (e *ExtrinsicDecoder) Process() {
e.Nonce = int(e.ProcessAndUpdateData("Compact<U64>").(uint64))
if e.Metadata.Extrinsic != nil {
if utiles.SliceIndex("ChargeTransactionPayment", e.Metadata.Extrinsic.SignedIdentifier) != -1 {
result.Tip = e.ProcessAndUpdateData("Compact<Balance>").(decimal.Decimal)
result.Tip = utiles.DecimalFromInterface(e.ProcessAndUpdateData("Compact<Balance>"))
}
} else {
result.Tip = e.ProcessAndUpdateData("Compact<Balance>").(decimal.Decimal)
result.Tip = utiles.DecimalFromInterface(e.ProcessAndUpdateData("Compact<Balance>"))
}
// spec SignedExtensions
result.SignedExtensions = make(map[string]interface{})
Expand Down
22 changes: 22 additions & 0 deletions utiles/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/shopspring/decimal"
"math/big"
"reflect"
"strconv"
Expand Down Expand Up @@ -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
}

0 comments on commit 04a60c4

Please sign in to comment.