/
vestingpolicy.go
116 lines (92 loc) · 3 KB
/
vestingpolicy.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package types
//go:generate ffjson $GOFILE
import (
"github.com/denkhaus/bitshares/util"
"github.com/juju/errors"
"github.com/pquerna/ffjson/ffjson"
)
//TODO: CoinSecondsEarned is UInt128! Since golang has no
//128 bit uint, check marshal and implement this later.
type CCDVestingPolicy struct {
StartClaim Time `json:"start_claim"`
CoinSecondsEarnedLastUpdate Time `json:"coin_seconds_earned_last_update"`
VestingSeconds UInt32 `json:"vesting_seconds"`
CoinSecondsEarned UInt64 `json:"coin_seconds_earned"` //UInt128!!
}
// TODO: check order!
func (p CCDVestingPolicy) Marshal(enc *util.TypeEncoder) error {
if err := enc.Encode(p.StartClaim); err != nil {
return errors.Annotate(err, "encode StartClaim")
}
if err := enc.Encode(p.VestingSeconds); err != nil {
return errors.Annotate(err, "encode VestingSeconds")
}
// if err := enc.Encode(p.CoinSecondsEarnedLastUpdate); err != nil {
// return errors.Annotate(err, "encode CoinSecondsEarnedLastUpdate")
// }
// if err := enc.Encode(p.CoinSecondsEarned); err != nil {
// return errors.Annotate(err, "encode CoinSecondsEarned")
// }
return nil
}
type LinearVestingPolicy struct {
BeginTimestamp Time `json:"begin_timestamp"`
VestingCliffSeconds UInt32 `json:"vesting_cliff_seconds"`
VestingDurationSeconds UInt32 `json:"vesting_duration_seconds"`
}
func (p LinearVestingPolicy) Marshal(enc *util.TypeEncoder) error {
if err := enc.Encode(p.BeginTimestamp); err != nil {
return errors.Annotate(err, "encode BeginTimestamp")
}
if err := enc.Encode(p.VestingCliffSeconds); err != nil {
return errors.Annotate(err, "encode VestingCliffSeconds")
}
if err := enc.Encode(p.VestingDurationSeconds); err != nil {
return errors.Annotate(err, "encode VestingDurationSeconds")
}
return nil
}
type VestingPolicy struct {
typ VestingPolicyType
data util.TypeMarshaler
}
func (p *VestingPolicy) UnmarshalJSON(data []byte) error {
var res []interface{}
if err := ffjson.Unmarshal(data, &res); err != nil {
return errors.Annotate(err, "Unmarshal")
}
if len(res) != 2 {
return ErrInvalidInputLength
}
p.typ = VestingPolicyType(res[0].(float64))
switch p.typ {
case VestingPolicyTypeLinear:
pol := LinearVestingPolicy{}
if err := ffjson.Unmarshal(util.ToBytes(res[1]), &pol); err != nil {
return errors.Annotate(err, "unmarshal LinearVestingPolicy")
}
p.data = pol
case VestingPolicyTypeCCD:
pol := CCDVestingPolicy{}
if err := ffjson.Unmarshal(util.ToBytes(res[1]), &pol); err != nil {
return errors.Annotate(err, "unmarshal CCDVestingPolicy")
}
p.data = pol
}
return nil
}
func (p VestingPolicy) MarshalJSON() ([]byte, error) {
return ffjson.Marshal([]interface{}{
p.typ,
p.data,
})
}
func (p VestingPolicy) Marshal(enc *util.TypeEncoder) error {
if err := enc.EncodeUVarint(uint64(p.typ)); err != nil {
return errors.Annotate(err, "encode PolicyType")
}
if err := enc.Encode(p.data); err != nil {
return errors.Annotate(err, "encode Policy")
}
return nil
}