-
Notifications
You must be signed in to change notification settings - Fork 621
/
Copy pathmsg_server.go
184 lines (152 loc) · 6.19 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
package keeper
import (
"context"
"fmt"
"github.com/osmosis-labs/osmosis/v11/x/gamm/utils"
"github.com/osmosis-labs/osmosis/v11/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, utils.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, utils.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())
}
lock, err = server.keeper.GetLockByID(ctx, msg.ID)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
ctx.EventManager().EmitEvents(sdk.Events{
createBeginUnlockEvent(lock),
})
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, utils.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, utils.Uint64ToString(lock.ID)),
sdk.NewAttribute(types.AttributePeriodLockOwner, lock.Owner),
sdk.NewAttribute(types.AttributePeriodLockDuration, lock.Duration.String()),
),
})
return &types.MsgExtendLockupResponse{}, nil
}