-
Notifications
You must be signed in to change notification settings - Fork 558
/
claim.go
249 lines (205 loc) · 7.21 KB
/
claim.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
package keeper
import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/gogo/protobuf/proto"
"github.com/osmosis-labs/osmosis/v4/x/claim/types"
)
// GetModuleAccountBalance gets the airdrop coin balance of module account
func (k Keeper) GetModuleAccountAddress(ctx sdk.Context) sdk.AccAddress {
return k.accountKeeper.GetModuleAddress(types.ModuleName)
}
// GetModuleAccountBalance gets the airdrop coin balance of module account
func (k Keeper) GetModuleAccountBalance(ctx sdk.Context) sdk.Coin {
moduleAccAddr := k.GetModuleAccountAddress(ctx)
params, _ := k.GetParams(ctx)
return k.bankKeeper.GetBalance(ctx, moduleAccAddr, params.ClaimDenom)
}
// SetModuleAccountBalance set balance of airdrop module
func (k Keeper) CreateModuleAccount(ctx sdk.Context, amount sdk.Coin) {
moduleAcc := authtypes.NewEmptyModuleAccount(types.ModuleName, authtypes.Minter)
k.accountKeeper.SetModuleAccount(ctx, moduleAcc)
k.bankKeeper.MintCoins(ctx, types.ModuleName, sdk.NewCoins(amount))
}
func (k Keeper) EndAirdrop(ctx sdk.Context) error {
err := k.fundRemainingsToCommunity(ctx)
if err != nil {
return err
}
k.clearInitialClaimables(ctx)
return nil
}
// ClearClaimables clear claimable amounts
func (k Keeper) clearInitialClaimables(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, []byte(types.ClaimRecordsStorePrefix))
for ; iterator.Valid(); iterator.Next() {
key := iterator.Key()
store.Delete(key)
}
iterator.Close()
}
// SetClaimables set claimable amount from balances object
func (k Keeper) SetClaimRecords(ctx sdk.Context, claimRecords []types.ClaimRecord) error {
for _, claimRecord := range claimRecords {
err := k.SetClaimRecord(ctx, claimRecord)
if err != nil {
return err
}
}
return nil
}
// GetClaimables get claimables for genesis export
func (k Keeper) GetClaimRecords(ctx sdk.Context) []types.ClaimRecord {
store := ctx.KVStore(k.storeKey)
prefixStore := prefix.NewStore(store, []byte(types.ClaimRecordsStorePrefix))
iterator := prefixStore.Iterator(nil, nil)
defer iterator.Close()
claimRecords := []types.ClaimRecord{}
for ; iterator.Valid(); iterator.Next() {
claimRecord := types.ClaimRecord{}
err := proto.Unmarshal(iterator.Value(), &claimRecord)
if err != nil {
panic(err)
}
claimRecords = append(claimRecords, claimRecord)
}
return claimRecords
}
// GetClaimRecord returns the claim record for a specific address
func (k Keeper) GetClaimRecord(ctx sdk.Context, addr sdk.AccAddress) (types.ClaimRecord, error) {
store := ctx.KVStore(k.storeKey)
prefixStore := prefix.NewStore(store, []byte(types.ClaimRecordsStorePrefix))
if !prefixStore.Has(addr) {
return types.ClaimRecord{}, nil
}
bz := prefixStore.Get(addr)
claimRecord := types.ClaimRecord{}
err := proto.Unmarshal(bz, &claimRecord)
if err != nil {
return types.ClaimRecord{}, err
}
return claimRecord, nil
}
// SetClaimRecord sets a claim record for an address in store
func (k Keeper) SetClaimRecord(ctx sdk.Context, claimRecord types.ClaimRecord) error {
store := ctx.KVStore(k.storeKey)
prefixStore := prefix.NewStore(store, []byte(types.ClaimRecordsStorePrefix))
bz, err := proto.Marshal(&claimRecord)
if err != nil {
return err
}
addr, err := sdk.AccAddressFromBech32(claimRecord.Address)
if err != nil {
return err
}
prefixStore.Set(addr, bz)
return nil
}
// GetClaimable returns claimable amount for a specific action done by an address
func (k Keeper) GetClaimableAmountForAction(ctx sdk.Context, addr sdk.AccAddress, action types.Action) (sdk.Coins, error) {
claimRecord, err := k.GetClaimRecord(ctx, addr)
if err != nil {
return nil, err
}
if claimRecord.Address == "" {
return sdk.Coins{}, nil
}
// if action already completed, nothing is claimable
if claimRecord.ActionCompleted[action] {
return sdk.Coins{}, nil
}
params, err := k.GetParams(ctx)
if err != nil {
return nil, err
}
// If we are before the start time, do nothing.
// This case _shouldn't_ occur on chain, since the
// start time ought to be chain start time.
if ctx.BlockTime().Before(params.AirdropStartTime) {
return sdk.Coins{}, nil
}
InitialClaimablePerAction := sdk.Coins{}
for _, coin := range claimRecord.InitialClaimableAmount {
InitialClaimablePerAction = InitialClaimablePerAction.Add(
sdk.NewCoin(coin.Denom,
coin.Amount.QuoRaw(int64(len(types.Action_name))),
),
)
}
elapsedAirdropTime := ctx.BlockTime().Sub(params.AirdropStartTime)
// Are we early enough in the airdrop s.t. theres no decay?
if elapsedAirdropTime <= params.DurationUntilDecay {
return InitialClaimablePerAction, nil
}
// The entire airdrop has completed
if elapsedAirdropTime > params.DurationUntilDecay+params.DurationOfDecay {
return sdk.Coins{}, nil
}
// Positive, since goneTime > params.DurationUntilDecay
decayTime := elapsedAirdropTime - params.DurationUntilDecay
decayPercent := sdk.NewDec(decayTime.Nanoseconds()).QuoInt64(params.DurationOfDecay.Nanoseconds())
claimablePercent := sdk.OneDec().Sub(decayPercent)
claimableCoins := sdk.Coins{}
for _, coin := range InitialClaimablePerAction {
claimableCoins = claimableCoins.Add(sdk.NewCoin(coin.Denom, coin.Amount.ToDec().Mul(claimablePercent).RoundInt()))
}
return claimableCoins, nil
}
// GetClaimable returns claimable amount for a specific action done by an address
func (k Keeper) GetUserTotalClaimable(ctx sdk.Context, addr sdk.AccAddress) (sdk.Coins, error) {
claimRecord, err := k.GetClaimRecord(ctx, addr)
if err != nil {
return sdk.Coins{}, err
}
if claimRecord.Address == "" {
return sdk.Coins{}, nil
}
totalClaimable := sdk.Coins{}
for action := range types.Action_name {
claimableForAction, err := k.GetClaimableAmountForAction(ctx, addr, types.Action(action))
if err != nil {
return sdk.Coins{}, err
}
totalClaimable = totalClaimable.Add(claimableForAction...)
}
return totalClaimable, nil
}
// ClaimCoins remove claimable amount entry and transfer it to user's account
func (k Keeper) ClaimCoinsForAction(ctx sdk.Context, addr sdk.AccAddress, action types.Action) (sdk.Coins, error) {
claimableAmount, err := k.GetClaimableAmountForAction(ctx, addr, action)
if err != nil {
return claimableAmount, err
}
if claimableAmount.Empty() {
return claimableAmount, nil
}
claimRecord, err := k.GetClaimRecord(ctx, addr)
if err != nil {
return nil, err
}
err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, addr, claimableAmount)
if err != nil {
return nil, err
}
claimRecord.ActionCompleted[action] = true
err = k.SetClaimRecord(ctx, claimRecord)
if err != nil {
return claimableAmount, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeClaim,
sdk.NewAttribute(sdk.AttributeKeySender, addr.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, claimableAmount.String()),
),
})
return claimableAmount, nil
}
// FundRemainingsToCommunity fund remainings to the community when airdrop period end
func (k Keeper) fundRemainingsToCommunity(ctx sdk.Context) error {
moduleAccAddr := k.GetModuleAccountAddress(ctx)
amt := k.GetModuleAccountBalance(ctx)
return k.distrKeeper.FundCommunityPool(ctx, sdk.NewCoins(amt), moduleAccAddr)
}