forked from regen-network/mainnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
299 lines (249 loc) · 6.96 KB
/
account.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package main
import (
"fmt"
"math"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
auth "github.com/cosmos/cosmos-sdk/x/auth/types"
vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
)
// Account is an internal representation of a genesis regen account
type Account struct {
Address sdk.AccAddress
TotalRegen Dec
Distributions []Distribution
}
// Distribution is an internal representation of a genesis vesting distribution of regen
type Distribution struct {
Time time.Time
Regen Dec
}
func (a Account) String() string {
return fmt.Sprintf("Account{%s, %sregen, %s}", a.Address, a.TotalRegen.String(), a.Distributions)
}
func (d Distribution) String() string {
return fmt.Sprintf("Distribution{%s, %sregen}", d.Time.Format(time.RFC3339), d.Regen.String())
}
func (acc Account) Validate() error {
if acc.Address.Empty() {
return fmt.Errorf("empty address")
}
if !acc.TotalRegen.IsPositive() {
return fmt.Errorf("expected positive balance, got %s", acc.TotalRegen.String())
}
var calcTotal Dec
for _, dist := range acc.Distributions {
err := dist.Validate()
if err != nil {
return err
}
calcTotal, err = calcTotal.Add(dist.Regen)
if err != nil {
return err
}
}
if !acc.TotalRegen.IsEqual(calcTotal) {
return fmt.Errorf("incorrect balance, expected %s, got %s", acc.TotalRegen.String(), calcTotal.String())
}
return nil
}
func (d Distribution) Validate() error {
if d.Time.IsZero() {
return fmt.Errorf("time is zero")
}
return nil
}
func RecordToAccount(rec Record, genesisTime time.Time) (Account, error) {
amount := rec.TotalAmount
distTime := rec.StartTime
if distTime.IsZero() {
return Account{}, fmt.Errorf("require a non-zero distribution time")
}
numDist := rec.NumMonthlyDistributions
if numDist < 1 {
return Account{}, fmt.Errorf("numDist must be >= 1, got %d", numDist)
}
if numDist == 1 && !distTime.After(genesisTime) {
return Account{
Address: rec.Address,
TotalRegen: amount,
Distributions: []Distribution{
{
Time: genesisTime,
Regen: amount,
},
},
}, nil
}
// calculate dust, which represents an uregen-integral remainder, represented as a decimal value of regen
// from dividing `amount` by `numDist`
distAmount, dust, err := distAmountAndDust(amount, numDist)
if err != nil {
return Account{}, err
}
var distributions []Distribution
var genesisAmount Dec
// collapse all pre-genesis distributions into a genesis distribution
for ; numDist > 0 && !distTime.After(genesisTime); numDist-- {
genesisAmount, err = genesisAmount.Add(distAmount)
if err != nil {
return Account{}, err
}
distTime = distTime.Add(OneMonth)
}
// if there is a genesis distribution add it
if !genesisAmount.IsZero() {
distributions = append(distributions, Distribution{
Time: genesisTime,
Regen: genesisAmount,
})
}
// add post genesis distributions
for ; numDist > 0; numDist-- {
distributions = append(distributions, Distribution{
Time: distTime,
Regen: distAmount,
})
distTime = distTime.Add(OneMonth)
}
// add dust to first distribution
distributions[0].Regen, err = distributions[0].Regen.Add(dust)
if err != nil {
return Account{}, err
}
return Account{
Address: rec.Address,
Distributions: distributions,
TotalRegen: amount,
}, nil
}
func distAmountAndDust(amount Dec, numDist int) (distAmount Dec, dust Dec, err error) {
if numDist < 1 {
return Dec{}, Dec{}, fmt.Errorf("num must be >= 1, got %d", numDist)
}
if numDist == 1 {
return amount, dust, nil
}
numDistDec := NewDecFromInt64(int64(numDist))
// convert amount from regen to uregen, so we can perform integral arithmetic on uregen
amount, err = amount.Mul(tenE6)
if err != nil {
return distAmount, dust, err
}
// each distribution is an integral amount of uregen
distAmount, err = amount.QuoInteger(numDistDec)
if err != nil {
return distAmount, dust, err
}
dust, err = amount.Rem(numDistDec)
if err != nil {
return distAmount, dust, err
}
// convert distAmount from uregen back to regen
distAmount, err = distAmount.Quo(tenE6)
if err != nil {
return distAmount, dust, err
}
// convert dust from uregen back to regen
dust, err = dust.Quo(tenE6)
if err != nil {
return distAmount, dust, err
}
return distAmount, dust, nil
}
const (
URegenDenom = "uregen"
)
var tenE6 Dec = NewDecFromInt64(1000000)
func ToCosmosAccount(acc Account, genesisTime time.Time) (auth.AccountI, *bank.Balance, error) {
err := acc.Validate()
if err != nil {
return nil, nil, err
}
totalCoins, err := RegenToCoins(acc.TotalRegen)
if err != nil {
return nil, nil, err
}
addrStr := acc.Address.String()
balance := &bank.Balance{
Address: addrStr,
Coins: totalCoins,
}
if len(acc.Distributions) == 0 {
return &auth.BaseAccount{Address: addrStr}, balance, nil
}
startTime := acc.Distributions[0].Time
// if we have one distribution and it happens before or at genesis return a basic BaseAccount
if len(acc.Distributions) == 1 {
if !acc.Distributions[0].Time.After(genesisTime) {
return &auth.BaseAccount{Address: addrStr}, balance, nil
} else {
return &vesting.DelayedVestingAccount{
BaseVestingAccount: &vesting.BaseVestingAccount{
BaseAccount: &auth.BaseAccount{Address: addrStr},
OriginalVesting: totalCoins,
EndTime: acc.Distributions[0].Time.Unix(),
},
}, balance, nil
}
} else {
periodStart := startTime
var periods []vesting.Period
for _, dist := range acc.Distributions {
coins, err := RegenToCoins(dist.Regen)
if err != nil {
return nil, nil, err
}
length := dist.Time.Sub(periodStart)
periodStart = dist.Time
seconds := int64(math.Floor(length.Seconds()))
periods = append(periods, vesting.Period{
Length: seconds,
Amount: coins,
})
}
return &vesting.PeriodicVestingAccount{
BaseVestingAccount: &vesting.BaseVestingAccount{
BaseAccount: &auth.BaseAccount{
Address: addrStr,
},
OriginalVesting: totalCoins,
EndTime: periodStart.Unix(),
},
StartTime: startTime.Unix(),
VestingPeriods: periods,
}, balance, nil
}
}
func RegenToCoins(regenAmount Dec) (sdk.Coins, error) {
uregen, err := regenAmount.Mul(tenE6)
if err != nil {
return nil, err
}
uregenInt64, err := uregen.Int64()
if err != nil {
return nil, err
}
return sdk.NewCoins(sdk.NewCoin(URegenDenom, sdk.NewInt(uregenInt64))), nil
}
func ValidateVestingAccount(acc auth.AccountI) error {
vacc, ok := acc.(*vesting.PeriodicVestingAccount)
if !ok {
return nil
}
orig := vacc.OriginalVesting
time := vacc.StartTime
var total sdk.Coins
for _, period := range vacc.VestingPeriods {
total = total.Add(period.Amount...)
time += period.Length
}
if !orig.IsEqual(total) {
return fmt.Errorf("vesting account error: expected %s coins, got %s", orig.String(), total.String())
}
if vacc.EndTime != time {
return fmt.Errorf("vesting account error: expected %d end time, got %d", vacc.EndTime, time)
}
return nil
}