-
Notifications
You must be signed in to change notification settings - Fork 82
/
account.go
308 lines (257 loc) · 7.17 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
300
301
302
303
304
305
306
307
308
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 passage account
type Account struct {
Address sdk.AccAddress
TotalPassage Dec
Distributions []Distribution
}
// Distribution is an internal representation of a genesis vesting distribution of passage
type Distribution struct {
Time time.Time
Passage Dec
}
func (a Account) String() string {
return fmt.Sprintf("Account{%s, %spassage, %s}", a.Address, a.TotalPassage.String(), a.Distributions)
}
func (d Distribution) String() string {
return fmt.Sprintf("Distribution{%s, %spassage}", d.Time.Format(time.RFC3339), d.Passage.String())
}
func (acc Account) Validate() error {
if acc.Address.Empty() {
return fmt.Errorf("empty address")
}
if !acc.TotalPassage.IsPositive() {
return fmt.Errorf("expected positive balance, got %s", acc.TotalPassage.String())
}
if len(acc.Distributions) > 0 {
var calcTotal Dec
for _, dist := range acc.Distributions {
err := dist.Validate()
if err != nil {
return err
}
calcTotal, err = calcTotal.Add(dist.Passage)
if err != nil {
return err
}
}
if !acc.TotalPassage.IsEqual(calcTotal) {
return fmt.Errorf("incorrect balance, expected %s, got %s", acc.TotalPassage.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() {
fmt.Println("recordToAcc", rec.Address)
return Account{
Address: rec.Address,
Distributions: nil,
TotalPassage: amount,
}, nil
}
numDist := rec.NumWeeklyDistributions
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,
TotalPassage: amount,
Distributions: []Distribution{
{
Time: genesisTime,
Passage: amount,
},
},
}, nil
}
// calculate dust, which represents an upasg-integral remainder, represented as a decimal value of passage
// 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(OneWeek)
}
// if there is a genesis distribution add it
if !genesisAmount.IsZero() {
distributions = append(distributions, Distribution{
Time: genesisTime,
Passage: genesisAmount,
})
}
// add post genesis distributions
for ; numDist > 0; numDist-- {
distributions = append(distributions, Distribution{
Time: distTime,
Passage: distAmount,
})
// adding one week
distTime = distTime.Add(OneWeek)
}
// add dust to first distribution
distributions[0].Passage, err = distributions[0].Passage.Add(dust)
if err != nil {
return Account{}, err
}
return Account{
Address: rec.Address,
Distributions: distributions,
TotalPassage: 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 passage to upasg, so we can perform integral arithmetic on upasg
amount, err = amount.Mul(tenE6)
if err != nil {
return distAmount, dust, err
}
// each distribution is an integral amount of upasg
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 upasg back to passage
distAmount, err = distAmount.Quo(tenE6)
if err != nil {
return distAmount, dust, err
}
// convert dust from upasg back to passage
dust, err = dust.Quo(tenE6)
if err != nil {
return distAmount, dust, err
}
return distAmount, dust, nil
}
const (
UPassageDenom = "upasg"
)
var tenE6 = 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 := Passage3DToCoins(acc.TotalPassage)
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 := Passage3DToCoins(dist.Passage)
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 Passage3DToCoins(passageAmount Dec) (sdk.Coins, error) {
upasg, err := passageAmount.Mul(tenE6)
if err != nil {
return nil, err
}
upasgInt64, err := upasg.Int64()
if err != nil {
return nil, err
}
return sdk.NewCoins(sdk.NewCoin(UPassageDenom, sdk.NewInt(upasgInt64))), 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
}