/
msg_server.go
224 lines (186 loc) · 8.15 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
package keeper
import (
"context"
"fmt"
"github.com/osmosis-labs/osmosis/osmoutils"
"github.com/tessornetwork/mokita/x/lockup/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
type msgServer struct {
keeper *Keeper
}
// NewMsgServerImpl returns an instance of MsgServer.
func NewMsgServerImpl(keeper *Keeper) types.MsgServer {
return &msgServer{
keeper: keeper,
}
}
var _ types.MsgServer = msgServer{}
// LockTokens locks tokens in either two ways.
// 1. Add to an existing lock if a lock with the same owner and same duration exists.
// 2. Create a new lock if not.
// A sanity check to ensure given tokens is a single token is done in ValidateBaic.
// That is, a lock with multiple tokens cannot be created.
func (server msgServer) LockTokens(goCtx context.Context, msg *types.MsgLockTokens) (*types.MsgLockTokensResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
owner, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
return nil, err
}
// check if there's an existing lock from the same owner with the same duration.
// If so, simply add tokens to the existing lock.
lockExists := server.keeper.HasLock(ctx, owner, msg.Coins[0].Denom, msg.Duration)
if lockExists {
lockID, err := server.keeper.AddToExistingLock(ctx, owner, msg.Coins[0], msg.Duration)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtAddTokensToLock,
sdk.NewAttribute(types.AttributePeriodLockID, osmoutils.Uint64ToString(lockID)),
sdk.NewAttribute(types.AttributePeriodLockOwner, msg.Owner),
sdk.NewAttribute(types.AttributePeriodLockAmount, msg.Coins.String()),
),
})
return &types.MsgLockTokensResponse{ID: lockID}, nil
}
// if the owner + duration combination is new, create a new lock.
lock, err := server.keeper.CreateLock(ctx, owner, msg.Coins, msg.Duration)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtLockTokens,
sdk.NewAttribute(types.AttributePeriodLockID, osmoutils.Uint64ToString(lock.ID)),
sdk.NewAttribute(types.AttributePeriodLockOwner, lock.Owner),
sdk.NewAttribute(types.AttributePeriodLockAmount, lock.Coins.String()),
sdk.NewAttribute(types.AttributePeriodLockDuration, lock.Duration.String()),
sdk.NewAttribute(types.AttributePeriodLockUnlockTime, lock.EndTime.String()),
),
})
return &types.MsgLockTokensResponse{ID: lock.ID}, nil
}
// BeginUnlocking begins unlocking of the specified lock.
// The lock would enter the unlocking queue, with the endtime of the lock set as block time + duration.
func (server msgServer) BeginUnlocking(goCtx context.Context, msg *types.MsgBeginUnlocking) (*types.MsgBeginUnlockingResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
lock, err := server.keeper.GetLockByID(ctx, msg.ID)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
if msg.Owner != lock.Owner {
return nil, sdkerrors.Wrap(types.ErrNotLockOwner, fmt.Sprintf("msg sender (%s) and lock owner (%s) does not match", msg.Owner, lock.Owner))
}
err = server.keeper.BeginUnlock(ctx, lock.ID, msg.Coins)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
// N.B. begin unlock event is emitted downstream in the keeper method.
return &types.MsgBeginUnlockingResponse{}, nil
}
// BeginUnlockingAll begins unlocking for all the locks that the account has by iterating all the not-unlocking locks the account holds.
func (server msgServer) BeginUnlockingAll(goCtx context.Context, msg *types.MsgBeginUnlockingAll) (*types.MsgBeginUnlockingAllResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
owner, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
return nil, err
}
unlocks, err := server.keeper.BeginUnlockAllNotUnlockings(ctx, owner)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
// Create the events for this message
unlockedCoins := server.keeper.getCoinsFromLocks(unlocks)
events := sdk.Events{
sdk.NewEvent(
types.TypeEvtBeginUnlockAll,
sdk.NewAttribute(types.AttributePeriodLockOwner, msg.Owner),
sdk.NewAttribute(types.AttributeUnlockedCoins, unlockedCoins.String()),
),
}
for _, lock := range unlocks {
lock := lock
events = events.AppendEvent(createBeginUnlockEvent(&lock))
}
ctx.EventManager().EmitEvents(events)
return &types.MsgBeginUnlockingAllResponse{}, nil
}
func createBeginUnlockEvent(lock *types.PeriodLock) sdk.Event {
return sdk.NewEvent(
types.TypeEvtBeginUnlock,
sdk.NewAttribute(types.AttributePeriodLockID, osmoutils.Uint64ToString(lock.ID)),
sdk.NewAttribute(types.AttributePeriodLockOwner, lock.Owner),
sdk.NewAttribute(types.AttributePeriodLockDuration, lock.Duration.String()),
sdk.NewAttribute(types.AttributePeriodLockUnlockTime, lock.EndTime.String()),
)
}
// ExtendLockup extends the duration of the existing lock.
// ExtendLockup would fail if the original lock's duration is longer than the new duration,
// OR if the lock is currently unlocking OR if the original lock has a synthetic lock.
func (server msgServer) ExtendLockup(goCtx context.Context, msg *types.MsgExtendLockup) (*types.MsgExtendLockupResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
owner, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
return nil, err
}
err = server.keeper.ExtendLockup(ctx, msg.ID, owner, msg.Duration)
if err != nil {
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, err.Error())
}
lock, err := server.keeper.GetLockByID(ctx, msg.ID)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeEvtLockTokens,
sdk.NewAttribute(types.AttributePeriodLockID, osmoutils.Uint64ToString(lock.ID)),
sdk.NewAttribute(types.AttributePeriodLockOwner, lock.Owner),
sdk.NewAttribute(types.AttributePeriodLockDuration, lock.Duration.String()),
),
})
return &types.MsgExtendLockupResponse{}, nil
}
// ForceUnlock ignores unlock duration and immediately unlocks the lock.
// This message is only allowed for governance-passed accounts that are kept as parameter in the lockup module.
// Locks that has been superfluid delegated is not supported.
func (server msgServer) ForceUnlock(goCtx context.Context, msg *types.MsgForceUnlock) (*types.MsgForceUnlockResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
lock, err := server.keeper.GetLockByID(ctx, msg.ID)
if err != nil {
return &types.MsgForceUnlockResponse{Success: false}, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
// check if message sender matches lock owner
if lock.Owner != msg.Owner {
return &types.MsgForceUnlockResponse{Success: false}, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "Sender (%s) does not match lock owner (%s)", msg.Owner, lock.Owner)
}
// check for chain parameter that the address is allowed to force unlock
forceUnlockAllowedAddresses := server.keeper.GetParams(ctx).ForceUnlockAllowedAddresses
found := false
for _, addr := range forceUnlockAllowedAddresses {
// defense in depth, double checking the message owner and lock owner are both the same and is one of the allowed force unlock addresses
if addr == lock.Owner && addr == msg.Owner {
found = true
break
}
}
if !found {
return &types.MsgForceUnlockResponse{Success: false}, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "Sender (%s) not allowed to force unlock", lock.Owner)
}
// check that given lock is not superfluid staked
synthLocks := server.keeper.GetAllSyntheticLockupsByLockup(ctx, lock.ID)
if len(synthLocks) > 0 {
return &types.MsgForceUnlockResponse{Success: false}, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "superfluid delegation exists for lock")
}
// force unlock given lock
// This also supports the case of force unlocking lock as a whole when msg.Coins
// provided is empty.
err = server.keeper.PartialForceUnlock(ctx, *lock, msg.Coins)
if err != nil {
return &types.MsgForceUnlockResponse{Success: false}, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
return &types.MsgForceUnlockResponse{Success: true}, nil
}