-
Notifications
You must be signed in to change notification settings - Fork 52
/
msg_server_commit_liquid_tokens.go
61 lines (48 loc) · 1.99 KB
/
msg_server_commit_liquid_tokens.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
package keeper
import (
"context"
"fmt"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
aptypes "github.com/elys-network/elys/x/assetprofile/types"
"github.com/elys-network/elys/x/commitment/types"
)
// CommitLiquidTokens commit the tokens from user's balance
func (k msgServer) CommitLiquidTokens(goCtx context.Context, msg *types.MsgCommitLiquidTokens) (*types.MsgCommitLiquidTokensResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
assetProfile, found := k.assetProfileKeeper.GetEntry(ctx, msg.Denom)
if !found {
return nil, errorsmod.Wrapf(aptypes.ErrAssetProfileNotFound, "denom: %s", msg.Denom)
}
if !assetProfile.CommitEnabled {
return nil, errorsmod.Wrapf(types.ErrCommitDisabled, "denom: %s", msg.Denom)
}
depositCoins := sdk.NewCoins(sdk.NewCoin(msg.Denom, msg.Amount))
addr, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidAddress, "unable to convert address from bech32")
}
// send the deposited coins to the module
err = k.bankKeeper.SendCoinsFromAccountToModule(ctx, addr, types.ModuleName, depositCoins)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrInsufficientFunds, fmt.Sprintf("unable to send deposit tokens: %v", depositCoins))
}
// Get the Commitments for the creator
commitments := k.GetCommitments(ctx, msg.Creator)
// Update the commitments
commitments.AddCommittedTokens(msg.Denom, msg.Amount, msg.LockUntil)
k.SetCommitments(ctx, commitments)
// Emit Hook commitment changed
k.AfterCommitmentChange(ctx, msg.Creator, sdk.Coins{sdk.NewCoin(msg.Denom, msg.Amount)})
// Emit blockchain event
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeCommitmentChanged,
sdk.NewAttribute(types.AttributeCreator, msg.Creator),
sdk.NewAttribute(types.AttributeAmount, msg.Amount.String()),
sdk.NewAttribute(types.AttributeDenom, msg.Denom),
),
)
return &types.MsgCommitLiquidTokensResponse{}, nil
}