-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlocked-tokens.go
421 lines (396 loc) · 13.4 KB
/
locked-tokens.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package fio
import (
"encoding/json"
"errors"
"github.com/fioprotocol/fio-go/eos"
"math"
"sort"
"strconv"
"strings"
"time"
)
/*
Genesis lock types
*/
// set as variables to allow override on development nets
var (
LockedInitial = 90 * 24 * 60 // initial minutes before first unlock period
LockedInitialPct float64 = 0.06 // percentage unlocked after first period
LockedIncrement = 180 * 24 * 60 // each additional unlock period, 2nd unlock = LockedInitial + LockedIncrement, 3rd = LockedInitial + (2 * LockedIncrement), etc
LockedIncrementPct float64 = 0.188 // percent unlocked each additional period: 1st = 6%, 2nd = 24.8% etc.
LockedPeriods = 6 // number of unlock periods
)
const (
LockedFounder uint32 = iota + 1 // founder tokens: cannot vote until unlocked, can use for fees.
LockedMember // foundation member (wallets/exchanges) incentives: if inhibit unlocking is set, cannot be used, votable until 2nd unlock or if uninhibited.
LockedPresale // presale tokens, can vote while locked
LockedGiveaway // foundation tokens used for giveaways, can only be used to register addresses
)
// GenesisLockedTokens holds information about tokens that were locked at chain genesis.
type GenesisLockedTokens struct {
Name eos.AccountName `json:"owner"`
TotalGrantAmount uint64 `json:"total_grant_amount"`
UnlockedPeriodCount uint32 `json:"unlocked_period_count"`
GrantType uint32 `json:"grant_type"`
InhibitUnlocking uint32 `json:"inhibit_unlocking"`
RemainingLockedAmount uint64 `json:"remaining_locked_amount"` // Do not trust on-chain value, only calculated on activity.
Timestamp uint32 `json:"timestamp"`
}
// GetGenesisLockedTokens gives details about the locked tokens for a specific account
func (api *API) GetGenesisLockedTokens(accountOrPubkey string) (hasLocked bool, locked *GenesisLockedTokens, err error) {
var actor eos.AccountName
switch len(accountOrPubkey) {
case 12:
actor = eos.AccountName(accountOrPubkey)
case 53:
actor, err = ActorFromPub(accountOrPubkey)
if err != nil {
return false, nil, err
}
default:
return false, nil, errors.New("invalid account or pubkey provided for looking up locked tokens")
}
gtr, err := api.GetTableRows(eos.GetTableRowsRequest{
Code: "eosio",
Scope: "eosio",
Table: "lockedtokens",
LowerBound: string(actor),
UpperBound: string(actor),
Limit: 1,
KeyType: "name",
Index: "1",
JSON: true,
})
if err != nil {
return false, nil, err
}
lts := make([]GenesisLockedTokens, 0)
err = json.Unmarshal(gtr.Rows, <s)
if err != nil {
return false, nil, err
}
if len(lts) == 0 {
return false, nil, nil
}
return true, <s[0], nil
}
// ActualRemaining calculates the outstanding locked tokens, this is needed because RemainingLockedAmount is only updated
// when an account spends or receives tokens. This should be subtracted from the current balance to calculate available
// tokens.
func (g *GenesisLockedTokens) ActualRemaining() (tokens uint64, err error) {
lockStart := time.Unix(int64(g.Timestamp), 0).UTC()
switch g.GrantType {
case LockedGiveaway:
// every transaction will cause RemainingLockedAmount to get calculated, so it is always accurate.
return g.RemainingLockedAmount, nil
case LockedMember:
// Members had until the 2nd unlock to integrate FIO into their wallet or exchange, if it was done the tokens
// would have the InhibitUnlock set to 0, meaning the tokens continue to unlock as scheduled.
if g.InhibitUnlocking == 1 {
// they may have spent fees before the tokens were locked forever, so total_grant_amount could too high
return g.RemainingLockedAmount, nil
}
// otherwise same unlock schedule applies
fallthrough
case LockedFounder, LockedPresale:
minutes := int(time.Now().Sub(lockStart).Minutes())
// have not passed first period
if minutes < LockedInitial {
return g.RemainingLockedAmount, nil
}
// first unlock passed
pct := LockedInitialPct
// add percentage for each additional
for i := 1; i <= LockedPeriods; i++ {
if minutes >= (i*LockedIncrement)+LockedInitial {
pct += LockedIncrementPct
} else {
break
}
}
unlocked := uint64(math.Round(float64(g.TotalGrantAmount) * pct))
if g.RemainingLockedAmount < (g.TotalGrantAmount - unlocked) {
return g.RemainingLockedAmount, nil
}
return g.TotalGrantAmount - unlocked, nil
default:
return 0, errors.New("unknown token lock type")
}
}
// GetTotalGenesisLockTokens tallies the remaining locked tokens based upon the values in the lockedtokens table
func (api *API) GetTotalGenesisLockTokens() (total uint64, founder uint64, member uint64, presale uint64, giveaway uint64, err error) {
nameQueries := splitNames(10)
for i := range nameQueries {
gtr, err := api.GetTableRows(eos.GetTableRowsRequest{
Code: "eosio",
Scope: "eosio",
Table: "lockedtokens",
LowerBound: nameQueries[i].LowerName,
UpperBound: nameQueries[i].UpperName,
KeyType: "name",
Index: "1",
JSON: true,
Limit: math.MaxUint32,
})
if err != nil {
return 0, 0, 0, 0, 0, err
}
rows := make([]GenesisLockedTokens, 0)
err = json.Unmarshal(gtr.Rows, &rows)
if err != nil {
return 0, 0, 0, 0, 0, err
}
var locked uint64
for j := range rows {
locked, err = rows[j].ActualRemaining()
if err != nil {
return 0, 0, 0, 0, 0, err
}
total += locked
switch rows[j].GrantType {
case LockedFounder:
founder += locked
case LockedMember:
member += locked
case LockedPresale:
presale += locked
case LockedGiveaway:
giveaway += locked
}
}
}
return
}
type nameRange struct {
LowerI64 uint64
LowerName string
UpperI64 uint64
UpperName string
}
// splitNames is a handy helper for looping over name primary indexes, while not efficient there isn't
// any way to know how many records there are, and this will break it into smaller chunks
func splitNames(size int) []nameRange {
names := make([]nameRange, 0)
iter := math.MaxUint64 / uint64(size)
var high, low uint64
for s := size; s > 0; s -= 1 {
if s == size {
high, _ = eos.StringToName("zzzzzzzzzzzz") // highest name is actually 615 less than max uint64
} else {
high = uint64(s) * iter
}
if s == 1 || high < iter {
low = 1 // can't be 0
} else {
low = high - iter + 1
}
names = append(names, nameRange{
LowerI64: low,
LowerName: eos.NameToString(low),
UpperI64: high,
UpperName: eos.NameToString(high),
})
}
sort.Slice(names, func(i, j int) bool {
return names[i].LowerI64 < names[j].LowerI64
})
return names
}
/*
FIP6 lock types and actions: https://github.com/fioprotocol/fips/blob/master/fip-0006.md
*/
// LockPeriods specifies how long a portion of the locked tokens will be locked by seconds and percentage, a slice
// of these is provided when locking tokens and must total 100%. It's also important to know that when staking tokens
// only certain durations will be valid, see https://github.com/fioprotocol/fips/blob/master/fip-0021.md
type LockPeriods struct {
Duration uint64 `json:"duration"`
Percent float64 `json:"percent"`
}
const (
CanVoteNone int32 = iota
CanVoteAll
)
type TransferLockedTokens struct {
PayeePublicKey string `json:"payee_public_key"`
CanVote int32 `json:"can_vote"`
Periods []LockPeriods `json:"periods"`
Amount uint64 `json:"amount"` // differs from ABI, not sure why this call is unsigned, but making consistent in SDK.
MaxFee uint64 `json:"max_fee"` // ABI also differs here.
Actor eos.AccountName `json:"actor"`
Tpid string `json:"tpid"`
}
// NewTransferLockedTokens creates an action used to transfer locked tokens to an account. This must be a new account, and cannot have existing tokens or addresses.
func NewTransferLockedTokens(actor eos.AccountName, recipientPubKey string, canVote bool, periods []LockPeriods, amount uint64) *Action {
can := CanVoteNone
if canVote {
can = CanVoteAll
}
return NewAction(
"fio.token", "trnsloctoks", actor,
TransferLockedTokens{
PayeePublicKey: recipientPubKey,
CanVote: can,
Periods: periods,
Amount: amount,
MaxFee: Tokens(GetMaxFee(FeeTransferLockedTokens)),
Actor: actor,
Tpid: CurrentTpid(),
},
)
}
// NewValidTransferLockedTokens is the same as NewTransferLockedTokens, but adds checks to ensure the account does not exist, and the periods are legit
func (api *API) NewValidTransferLockedTokens(actor eos.AccountName, recipientPubKey string, canVote bool, periods []LockPeriods, amount uint64) (*Action, error) {
can := CanVoteNone
if canVote {
can = CanVoteAll
}
tlt := TransferLockedTokens{
PayeePublicKey: recipientPubKey,
CanVote: can,
Periods: periods,
Amount: amount,
MaxFee: Tokens(GetMaxFee(FeeTransferLockedTokens)),
Actor: actor,
Tpid: CurrentTpid(),
}
if e := tlt.valid(api); e != nil {
return nil, e
}
return NewAction("fio.token", "trnsloctoks", actor, tlt), nil
}
func (tlt *TransferLockedTokens) valid(api *API) error {
switch true {
case eos.CheckUnderOver(tlt.MaxFee) != nil:
return eos.CheckUnderOver(tlt.MaxFee)
case eos.CheckUnderOver(tlt.Amount) != nil:
return eos.CheckUnderOver(tlt.Amount)
case tlt.Amount == 0:
return errors.New("must transfer a positive amount")
case tlt.CanVote > CanVoteAll:
return errors.New("invalid value for can_vote, must be 0 or 1")
case len(tlt.Periods) == 0:
return errors.New("no periods provided")
}
act, err := ActorFromPub(tlt.PayeePublicKey)
if err != nil {
return err
}
var pct float64
for i := range tlt.Periods {
pct += tlt.Periods[i].Percent
}
if pct != 100 {
return errors.New("percentage must equal 100%")
}
resp, _ := api.GetFioAccount(string(act))
if resp != nil {
return errors.New("cannot transfer to an existing account")
}
return nil
}
type LockTokensResp struct {
Id uint32 `json:"id"`
OwnerAccount eos.AccountName `json:"owner_account"`
LockAmount uint64 `json:"lock_amount"`
PayoutsPerformed uint32 `json:"payouts_performed"`
CanVote int32 `json:"can_vote"`
Periods []*LockPeriods `json:"periods"`
RemainingLockAmount uint64 `json:"remaining_lock_amount"`
TimeStamp int64 `json:"time_stamp"`
}
// GetTotalLockTokens provides the total number of (FIP6) locked tokens by iterating through the locktokens table.
func (api *API) GetTotalLockTokens() (uint64, error) {
var total uint64
const iter int64 = math.MaxInt64
now := time.Now().UTC().Unix()
lower := "0"
more := true
for more {
gtr, err := api.GetTableRows(eos.GetTableRowsRequest{
Code: "eosio",
Scope: "eosio",
Table: "locktokens",
LowerBound: lower,
Limit: 5000,
KeyType: "i64",
Index: "1",
JSON: true,
})
if err != nil {
if err.Error() == "Internal Service Error - (fc): Contract Table Query Exception: Table locktokens is not specified in the ABI" {
// this may be running before FIP6 has been deployed
return 0, nil
}
return 0, err
}
ltr := make([]LockTokensResp, 0)
err = json.Unmarshal(gtr.Rows, <r)
if err != nil {
return 0, err
}
for i := range ltr {
total += ltr[i].LockAmount
for j := range ltr[i].Periods {
if int64(ltr[i].Periods[j].Duration)+ltr[i].TimeStamp < now {
total -= ltr[i].LockAmount - uint64((math.Round(ltr[i].Periods[j].Percent*100.0)*float64(ltr[i].LockAmount))/10000)
}
}
}
if gtr.More {
lower = strconv.FormatUint(uint64(ltr[len(ltr)-1].Id), 10)
}
more = gtr.More
}
return total, nil
}
/*
Circulating Supply
*/
// GetCirculatingSupply returns the number of spendable tokens based on current supply - genesis locks - locked tokens
// this is a very busy call, requiring multiple requests to calculate the result, and it is recommended to cache the
// output if needed frequently.
func (api *API) GetCirculatingSupply() (circulating uint64, minted uint64, locked uint64, err error) {
var supply, genesis uint64
gcr := &eos.GetCurrencyStatsResp{}
gcr, err = api.GetCurrencyStats("fio.token", "FIO")
if err != nil {
return
}
// not getting valid values from gcr.Supply.ToUint64()
sf, err := strconv.ParseFloat(strings.Split(gcr.Supply.String(), " ")[0], 64)
if err != nil {
return
}
supply = uint64(sf * 1_000_000_000.0)
genesis, _, _, _, _, err = api.GetTotalGenesisLockTokens()
if err != nil {
return
}
return supply - genesis, supply, genesis, nil
}
// GetLockedBpRewards gets the unpaid rewards for block producers: Fees collected for FIO Address/Domain
// registration are not immediately distributed, but rather locked and distributed evenly every day over
// a period of one year
func (api *API) GetLockedBpRewards() (locked uint64, err error) {
gtr, err := api.GetTableRows(eos.GetTableRowsRequest{
Code: "fio.treasury",
Scope: "fio.treasury",
Table: "bpbucketpool",
JSON: true,
})
if err != nil {
return
}
type lockedRew struct {
Rewards uint64 `json:"rewards"`
}
rows := make([]lockedRew, 0)
err = json.Unmarshal(gtr.Rows, &rows)
if err != nil {
return
}
if len(rows) == 0 {
return 0, errors.New("unknown error getting bpbucketpool")
}
return rows[0].Rewards, nil
}