-
Notifications
You must be signed in to change notification settings - Fork 5
/
abci.go
162 lines (131 loc) · 4.57 KB
/
abci.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
package keeper
import (
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
vestexported "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"
"github.com/echelonfoundation/echelon/v3/x/claims/types"
)
// EndBlocker checks if the airdrop claiming period has ended in order to
// process the clawback of unclaimed tokens
func (k Keeper) EndBlocker(ctx sdk.Context) {
params := k.GetParams(ctx)
// NOTE: ignore end of airdrop period check if claiming is disabled
if !params.EnableClaims {
return
}
// check if the time to claim airdrop tokens has passed
elapsedAirdropTime := ctx.BlockTime().Sub(params.AirdropStartTime)
if elapsedAirdropTime <= params.DurationUntilDecay+params.DurationOfDecay {
return
}
if err := k.EndAirdrop(ctx, params); err != nil {
panic(err)
}
}
// EndAirdrop transfers the unclaimed tokens from the airdrop to the community
// pool, removes all claims records from state and disables the claims.
func (k Keeper) EndAirdrop(ctx sdk.Context, params types.Params) error {
logger := k.Logger(ctx)
logger.Info("beginning EndAirdrop logic")
if err := k.ClawbackEscrowedTokens(ctx); err != nil {
return err
}
// transfer unclaimed tokens from accounts to community pool and clean up the
// claims record state
k.ClawbackEmptyAccounts(ctx, params.ClaimsDenom)
// set the EnableClaims param to false so that we don't have to compute
// duration every block
params.EnableClaims = false
k.SetParams(ctx, params)
logger.Info("end EndAirdrop logic")
return nil
}
// ClawbackEscrowedTokens transfers all the escrowed airdrop tokens on the
// ModuleAccount to the community pool
func (k Keeper) ClawbackEscrowedTokens(ctx sdk.Context) error {
logger := k.Logger(ctx)
moduleAccAddr := k.GetModuleAccountAddress()
balances := k.bankKeeper.GetAllBalances(ctx, moduleAccAddr)
if balances.IsZero() {
logger.Debug("clawback aborted, airdrop escrow account is empty")
return nil
}
if err := k.distrKeeper.FundCommunityPool(ctx, balances, moduleAccAddr); err != nil {
return sdkerrors.Wrap(err, "failed to transfer escrowed airdrop tokens")
}
logger.Info(
"clawback of funds to community pool treasury",
"total", balances.String(),
)
return nil
}
// ClawbackEmptyAccounts performs the clawback of all allocated tokens
// from airdrop recipient accounts with a sequence number of 0 (i.e the account
// hasn't performed a single tx during the claim window).
// Once the account is clawbacked, the claims record is deleted from state.
func (k Keeper) ClawbackEmptyAccounts(ctx sdk.Context, claimsDenom string) {
totalClawback := sdk.Coin{Denom: claimsDenom, Amount: sdk.ZeroInt()}
logger := k.Logger(ctx)
accClawbacked := int64(0)
var addresses []sdk.AccAddress
k.IterateClaimsRecords(ctx, func(addr sdk.AccAddress, _ types.ClaimsRecord) (stop bool) {
// NOTE: we cannot delete the record while iterating over it
defer func() {
addresses = append(addresses, addr)
}()
acc := k.accountKeeper.GetAccount(ctx, addr)
if acc == nil {
logger.Debug(
"airdrop account not found during clawback",
"address", addr.String(),
)
return false
}
// ignore vesting accounts since some of the funds might be locked
if _, isVesting := acc.(vestexported.VestingAccount); isVesting {
return false
}
seq, err := k.accountKeeper.GetSequence(ctx, addr)
if err != nil {
logger.Debug(
"airdrop account nonce not found during clawback",
"address", addr.String(),
)
return false
}
if seq != 0 {
return false
}
balance := k.bankKeeper.GetBalance(ctx, addr, claimsDenom)
// prune empty accounts from the airdrop
if balance.IsZero() {
return false
}
// Send all unclaimed airdropped coins back to the community pool
// and prune those inactive wallets from current state.
// "Unclaimed" tokens are defined as being in wallets which have a sequence
// number = 0, which means the address has NOT performed a single action
// during the airdrop claim window.
if err := k.distrKeeper.FundCommunityPool(ctx, sdk.Coins{balance}, addr); err != nil {
logger.Debug(
"not enough balance to clawback account",
"address", addr.String(),
"amount", balance.String(),
)
return false
}
totalClawback = totalClawback.Add(balance)
accClawbacked++
return false
})
// delete claims record once the account balance is clawed back
for _, addr := range addresses {
k.DeleteClaimsRecord(ctx, addr)
}
logger.Info(
"clawed back funds into community pool",
"total", totalClawback.String(),
"clawbacked-accounts", strconv.FormatInt(accClawbacked, 10),
)
}