-
Notifications
You must be signed in to change notification settings - Fork 26
/
extrinsic.go
63 lines (56 loc) · 1.7 KB
/
extrinsic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package substrate
import (
"fmt"
"math"
scale "github.com/itering/scale.go"
"github.com/itering/scale.go/types"
"github.com/itering/scale.go/types/scaleBytes"
"github.com/itering/substrate-api-rpc/metadata"
"github.com/itering/substrate-api-rpc/util"
"github.com/shopspring/decimal"
)
// Extrinsic decode
func DecodeExtrinsic(rawList []string, metadata *metadata.Instant, spec int) (r []map[string]interface{}, err error) {
defer func() {
if fatal := recover(); fatal != nil {
err = fmt.Errorf("Recovering from panic in DecodeExtrinsic error is: %v \n", fatal)
}
}()
m := types.MetadataStruct(*metadata)
for _, extrinsicRaw := range rawList {
e := scale.ExtrinsicDecoder{}
option := types.ScaleDecoderOption{Metadata: &m, Spec: spec}
e.Init(scaleBytes.ScaleBytes{Data: util.HexToBytes(extrinsicRaw)}, &option)
e.Process()
r = append(r, e.Value.(*scale.GenericExtrinsic).ToMap())
}
return
}
type Mortal struct {
Period uint64
Phase uint64
}
func DecodeMortal(era string) *Mortal {
if era == "" || era == "00" {
return nil
}
eraU8a := util.HexToBytes(era)
first := uint64(eraU8a[0])
second := uint64(eraU8a[1])
encoded := first + (second << 8)
var period uint64 = 2 << (encoded % (1 << 4))
quantizeFactor := math.Max(float64(period>>12), 1)
phase := (encoded >> 4) * uint64(quantizeFactor)
return &Mortal{
Period: period,
Phase: phase,
}
}
func (m *Mortal) Birth(current uint64) uint64 {
s := (decimal.Max(decimal.New(int64(current), 0), decimal.New(int64(m.Phase), 0)).
Sub(decimal.New(int64(m.Phase), 0))).Div(decimal.New(int64(m.Period), 0)).IntPart()
return uint64(s)*m.Period + m.Phase
}
func (m *Mortal) Death(current uint64) uint64 {
return m.Birth(current) + m.Period
}