-
Notifications
You must be signed in to change notification settings - Fork 2
/
msg_server.go
377 lines (319 loc) · 12.8 KB
/
msg_server.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
package keeper
import (
"context"
"strconv"
"time"
errorsmod "cosmossdk.io/errors"
"github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
vestingexported "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"
sdkvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
cvntypes "github.com/cvn-network/cvn/v2/types"
"github.com/cvn-network/cvn/v2/x/vesting/types"
)
var _ types.MsgServer = &Keeper{}
// CreateClawbackVestingAccount creates a new ClawbackVestingAccount, or merges
// a grant into an existing one.
func (k Keeper) CreateClawbackVestingAccount(
goCtx context.Context,
msg *types.MsgCreateClawbackVestingAccount,
) (*types.MsgCreateClawbackVestingAccountResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
ak := k.accountKeeper
bk := k.bankKeeper
// Error checked during msg validation
from := sdk.MustAccAddressFromBech32(msg.FromAddress)
to := sdk.MustAccAddressFromBech32(msg.ToAddress)
if bk.BlockedAddr(to) {
return nil, errorsmod.Wrapf(errortypes.ErrUnauthorized,
"%s is not allowed to receive funds", msg.ToAddress,
)
}
vestingCoins := msg.VestingPeriods.TotalAmount()
lockupCoins := msg.LockupPeriods.TotalAmount()
// If lockup absent, default to an instant unlock schedule
if !vestingCoins.IsZero() && len(msg.LockupPeriods) == 0 {
msg.LockupPeriods = sdkvesting.Periods{
{Length: 0, Amount: vestingCoins},
}
lockupCoins = vestingCoins
}
// If vesting absent, default to an instant vesting schedule
if !lockupCoins.IsZero() && len(msg.VestingPeriods) == 0 {
msg.VestingPeriods = sdkvesting.Periods{
{Length: 0, Amount: lockupCoins},
}
vestingCoins = lockupCoins
}
// The vesting and lockup schedules must describe the same total amount.
// IsEqual can panic, so use (a == b) <=> (a <= b && b <= a).
if !(vestingCoins.IsAllLTE(lockupCoins) && lockupCoins.IsAllLTE(vestingCoins)) {
return nil, errorsmod.Wrapf(errortypes.ErrInvalidRequest,
"lockup and vesting amounts must be equal",
)
}
// Add Grant if vesting account exists, "merge" is true and funder is correct.
// Otherwise create a new Clawback Vesting Account
madeNewAcc := false
acc := ak.GetAccount(ctx, to)
var vestingAcc *types.ClawbackVestingAccount
if acc != nil {
var isClawback bool
vestingAcc, isClawback = acc.(*types.ClawbackVestingAccount)
switch {
case !msg.Merge && isClawback:
return nil, errorsmod.Wrapf(errortypes.ErrInvalidRequest, "account %s already exists; consider using --merge", msg.ToAddress)
case !msg.Merge && !isClawback:
return nil, errorsmod.Wrapf(errortypes.ErrInvalidRequest, "account %s already exists", msg.ToAddress)
case msg.Merge && !isClawback:
return nil, errorsmod.Wrapf(errortypes.ErrNotSupported, "account %s must be a clawback vesting account", msg.ToAddress)
case msg.FromAddress != vestingAcc.FunderAddress:
return nil, errorsmod.Wrapf(errortypes.ErrInvalidRequest, "account %s can only accept grants from account %s", msg.ToAddress, vestingAcc.FunderAddress)
}
err := k.addGrant(ctx, vestingAcc, msg.GetStartTime().Unix(), msg.GetLockupPeriods(), msg.GetVestingPeriods(), vestingCoins)
if err != nil {
return nil, err
}
ak.SetAccount(ctx, vestingAcc)
} else {
baseAcc := authtypes.NewBaseAccountWithAddress(to)
vestingAcc = types.NewClawbackVestingAccount(
baseAcc,
from,
vestingCoins,
msg.StartTime,
msg.LockupPeriods,
msg.VestingPeriods,
)
acc := ak.NewAccount(ctx, vestingAcc)
ak.SetAccount(ctx, acc)
madeNewAcc = true
}
if madeNewAcc {
defer func() {
telemetry.IncrCounter(1, "new", "account")
for _, a := range vestingCoins {
if a.Amount.IsInt64() {
telemetry.SetGaugeWithLabels(
[]string{"tx", "msg", "create_clawback_vesting_account"},
float32(a.Amount.Int64()),
[]metrics.Label{telemetry.NewLabel("denom", a.Denom)},
)
}
}
}()
}
// Send coins from the funder to vesting account
if err := bk.SendCoins(ctx, from, to, vestingCoins); err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(
sdk.Events{
sdk.NewEvent(
types.EventTypeCreateClawbackVestingAccount,
sdk.NewAttribute(sdk.AttributeKeySender, msg.FromAddress),
sdk.NewAttribute(types.AttributeKeyCoins, vestingCoins.String()),
sdk.NewAttribute(types.AttributeKeyStartTime, msg.StartTime.String()),
sdk.NewAttribute(types.AttributeKeyMerge, strconv.FormatBool(msg.Merge)),
sdk.NewAttribute(types.AttributeKeyAccount, msg.ToAddress),
),
},
)
return &types.MsgCreateClawbackVestingAccountResponse{}, nil
}
// Clawback removes the unvested amount from a ClawbackVestingAccount.
// The destination defaults to the funder address, but can be overridden.
func (k Keeper) Clawback(
goCtx context.Context,
msg *types.MsgClawback,
) (*types.MsgClawbackResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
ak := k.accountKeeper
bk := k.bankKeeper
// NOTE: ignore error in case dest address is not defined
dest, _ := sdk.AccAddressFromBech32(msg.DestAddress)
// NOTE: error checked during msg validation
addr := sdk.MustAccAddressFromBech32(msg.AccountAddress)
// Default destination to funder address
if msg.DestAddress == "" {
dest, _ = sdk.AccAddressFromBech32(msg.FunderAddress)
}
if bk.BlockedAddr(dest) {
return nil, errorsmod.Wrapf(errortypes.ErrUnauthorized,
"%s is not allowed to receive funds", msg.DestAddress,
)
}
// Check if account exists
acc := ak.GetAccount(ctx, addr)
if acc == nil {
return nil, errorsmod.Wrapf(errortypes.ErrNotFound, "account %s does not exist", msg.AccountAddress)
}
// Check if account has a clawback account
va, ok := acc.(*types.ClawbackVestingAccount)
if !ok {
return nil, errorsmod.Wrapf(errortypes.ErrInvalidRequest, "account not subject to clawback: %s", msg.AccountAddress)
}
// Check if account funder is same as in msg
if va.FunderAddress != msg.FunderAddress {
return nil, errorsmod.Wrapf(errortypes.ErrInvalidRequest, "clawback can only be requested by original funder %s", va.FunderAddress)
}
// Return error if clawback is attempted before start time
if ctx.BlockTime().Before(va.StartTime) {
return nil, errorsmod.Wrapf(errortypes.ErrInvalidRequest, "clawback can only be executed after vesting begins: %s", va.FunderAddress)
}
// Perform clawback transfer
if err := k.transferClawback(ctx, *va, dest); err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(
sdk.Events{
sdk.NewEvent(
types.EventTypeClawback,
sdk.NewAttribute(types.AttributeKeyFunder, msg.FunderAddress),
sdk.NewAttribute(types.AttributeKeyAccount, msg.AccountAddress),
sdk.NewAttribute(types.AttributeKeyDestination, msg.DestAddress),
),
},
)
return &types.MsgClawbackResponse{}, nil
}
// UpdateVestingFunder updates the funder account of a ClawbackVestingAccount.
func (k Keeper) UpdateVestingFunder(
goCtx context.Context,
msg *types.MsgUpdateVestingFunder,
) (*types.MsgUpdateVestingFunderResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
ak := k.accountKeeper
bk := k.bankKeeper
// NOTE: errors checked during msg validation
newFunder := sdk.MustAccAddressFromBech32(msg.NewFunderAddress)
vesting := sdk.MustAccAddressFromBech32(msg.VestingAddress)
// Need to check if new funder can receive funds because in
// Clawback function, destination defaults to funder address
if bk.BlockedAddr(newFunder) {
return nil, errorsmod.Wrapf(errortypes.ErrUnauthorized,
"%s is not allowed to receive funds", msg.NewFunderAddress,
)
}
// Check if vesting account exists
vestingAcc := ak.GetAccount(ctx, vesting)
if vestingAcc == nil {
return nil, errorsmod.Wrapf(errortypes.ErrNotFound, "account %s does not exist", msg.VestingAddress)
}
// Check if account is a clawback vesting account
va, ok := vestingAcc.(*types.ClawbackVestingAccount)
if !ok {
return nil, errorsmod.Wrapf(errortypes.ErrInvalidRequest, "account not subject to clawback: %s", msg.VestingAddress)
}
// Check if account current funder is same as in msg
if va.FunderAddress != msg.FunderAddress {
return nil, errorsmod.Wrapf(errortypes.ErrInvalidRequest, "clawback can only be requested by original funder %s", va.FunderAddress)
}
// Perform clawback account update
va.FunderAddress = msg.NewFunderAddress
// set the account with the updated funder
ak.SetAccount(ctx, va)
ctx.EventManager().EmitEvents(
sdk.Events{
sdk.NewEvent(
types.EventTypeUpdateVestingFunder,
sdk.NewAttribute(types.AttributeKeyFunder, msg.FunderAddress),
sdk.NewAttribute(types.AttributeKeyAccount, msg.VestingAddress),
sdk.NewAttribute(types.AttributeKeyNewFunder, msg.NewFunderAddress),
),
},
)
return &types.MsgUpdateVestingFunderResponse{}, nil
}
// ConvertVestingAccount converts a ClawbackVestingAccount to the default chain account
// after its lock and vesting periods have concluded.
func (k Keeper) ConvertVestingAccount(
goCtx context.Context,
msg *types.MsgConvertVestingAccount,
) (*types.MsgConvertVestingAccountResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
address := sdk.MustAccAddressFromBech32(msg.VestingAddress)
account := k.accountKeeper.GetAccount(ctx, address)
if account == nil {
return nil, errorsmod.Wrapf(errortypes.ErrNotFound, "account %s does not exist", msg.VestingAddress)
}
// Check if account is of VestingAccount interface
if _, ok := account.(vestingexported.VestingAccount); !ok {
return nil, errorsmod.Wrapf(errortypes.ErrInvalidRequest, "account not subject to vesting: %s", msg.VestingAddress)
}
// check if account is of type ClawbackVestingAccount
vestingAcc, ok := account.(*types.ClawbackVestingAccount)
if !ok {
return nil, errorsmod.Wrapf(errortypes.ErrInvalidRequest, "account %s is not a ClawbackVestingAccount", msg.VestingAddress)
}
// check if account has any vesting coins left
if vestingAcc.GetVestingCoins(ctx.BlockTime()) != nil {
return nil, errorsmod.Wrapf(errortypes.ErrInvalidRequest, "vesting coins still left in account: %s", msg.VestingAddress)
}
ethAccount := cvntypes.ProtoAccount().(*cvntypes.EthAccount)
ethAccount.BaseAccount = vestingAcc.BaseAccount
k.accountKeeper.SetAccount(ctx, ethAccount)
return &types.MsgConvertVestingAccountResponse{}, nil
}
// addGrant merges a new clawback vesting grant into an existing
// ClawbackVestingAccount.
func (k Keeper) addGrant(
ctx sdk.Context,
va *types.ClawbackVestingAccount,
grantStartTime int64,
grantLockupPeriods, grantVestingPeriods sdkvesting.Periods,
grantCoins sdk.Coins,
) error {
// how much is really delegated?
bondedAmt := k.stakingKeeper.GetDelegatorBonded(ctx, va.GetAddress())
unbondingAmt := k.stakingKeeper.GetDelegatorUnbonding(ctx, va.GetAddress())
delegatedAmt := bondedAmt.Add(unbondingAmt)
delegated := sdk.NewCoins(sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), delegatedAmt))
// modify schedules for the new grant
newLockupStart, newLockupEnd, newLockupPeriods := types.DisjunctPeriods(va.GetStartTime(), grantStartTime, va.LockupPeriods, grantLockupPeriods)
newVestingStart, newVestingEnd, newVestingPeriods := types.DisjunctPeriods(va.GetStartTime(), grantStartTime,
va.GetVestingPeriods(), grantVestingPeriods)
if newLockupStart != newVestingStart {
return errorsmod.Wrapf(
types.ErrVestingLockup,
"vesting start time calculation should match lockup start (%d ≠ %d)",
newVestingStart, newLockupStart,
)
}
va.StartTime = time.Unix(newLockupStart, 0)
va.EndTime = types.Max64(newLockupEnd, newVestingEnd)
va.LockupPeriods = newLockupPeriods
va.VestingPeriods = newVestingPeriods
va.OriginalVesting = va.OriginalVesting.Add(grantCoins...)
// cap DV at the current unvested amount, DF rounds out to current delegated
unvested := va.GetVestingCoins(ctx.BlockTime())
va.DelegatedVesting = delegated.Min(unvested)
va.DelegatedFree = delegated.Sub(va.DelegatedVesting...)
return nil
}
// transferClawback transfers unvested tokens in a ClawbackVestingAccount to
// dest address, updates the lockup schedule and removes future vesting events.
func (k Keeper) transferClawback(
ctx sdk.Context,
va types.ClawbackVestingAccount,
dest sdk.AccAddress,
) error {
// Compute clawback amount, unlock unvested tokens and remove future vesting events
updatedAcc, toClawBack := va.ComputeClawback(ctx.BlockTime().Unix())
if toClawBack.IsZero() {
// no-op, nothing to transfer
return nil
}
// set the account with the updated values of the vesting schedule
k.accountKeeper.SetAccount(ctx, &updatedAcc)
addr := updatedAcc.GetAddress()
// NOTE: don't use `SpendableCoins` to get the minimum value to clawback since
// the amount is retrieved from `ComputeClawback`, which ensures correctness.
// `SpendableCoins` can result in gas exhaustion if the user has too many
// different denoms (because of store iteration).
// Transfer clawback to the destination (funder)
return k.bankKeeper.SendCoins(ctx, addr, dest, toClawBack)
}