-
Notifications
You must be signed in to change notification settings - Fork 9
/
msg_server_single_asset_deposit.go
98 lines (80 loc) · 2.64 KB
/
msg_server_single_asset_deposit.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
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
errormod "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/sideprotocol/ibcswap/v6/modules/apps/101-interchain-swap/types"
)
func (k Keeper) SingleAssetDeposit(ctx context.Context, msg *types.MsgSingleAssetDepositRequest) (*types.MsgSingleAssetDepositResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
// Validate message
err := msg.ValidateBasic()
if err != nil {
return nil, err
}
pool, found := k.GetInterchainLiquidityPool(sdkCtx, msg.PoolId)
if !found {
return nil, errormod.Wrapf(types.ErrFailedDeposit, "%s", types.ErrNotFoundPool)
}
// Deposit token to Escrow account
accAddress, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return nil, errormod.Wrapf(types.ErrFailedDeposit, "%s", types.ErrInvalidAddress)
}
balance := k.bankKeeper.GetBalance(sdkCtx, accAddress, msg.Token.Denom)
if balance.Amount.Equal(sdk.NewInt(0)) {
return nil, types.ErrInvalidAmount
}
if pool.Status != types.PoolStatus_ACTIVE {
return nil, errormod.Wrapf(types.ErrFailedDeposit, "%s", types.ErrNotReadyForSwap)
}
// Deposit assets to the escrowed account
err = k.LockTokens(sdkCtx, pool.CounterPartyPort, pool.CounterPartyChannel, sdk.MustAccAddressFromBech32(msg.Sender), sdk.NewCoins(*msg.Token))
if err != nil {
return nil, errormod.Wrapf(types.ErrFailedDeposit, "%s", err)
}
amm := *types.NewInterchainMarketMaker(&pool)
poolToken, err := amm.DepositSingleAsset(*msg.Token)
if err != nil {
return nil, err
}
// Construct IBC packet
rawMsgData := types.ModuleCdc.MustMarshalJSON(msg)
rawStateChange := types.ModuleCdc.MustMarshalJSON(&types.StateChange{PoolTokens: []*sdk.Coin{poolToken}})
packet := types.IBCSwapPacketData{
Type: types.SINGLE_DEPOSIT,
Data: rawMsgData,
StateChange: rawStateChange,
}
timeoutHeight, timeoutStamp := types.GetDefaultTimeOut(&sdkCtx)
// Use input timeoutHeight, timeoutStamp
if msg.TimeoutHeight != nil {
timeoutHeight = *msg.TimeoutHeight
}
if msg.TimeoutTimeStamp != 0 {
timeoutStamp = msg.TimeoutTimeStamp
}
_, err = k.SendIBCSwapPacket(sdkCtx, msg.Port, msg.Channel, timeoutHeight, timeoutStamp, packet)
if err != nil {
return nil, err
}
sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeSingleDepositOrder,
sdk.Attribute{
Key: types.AttributeKeyPoolId,
Value: msg.PoolId,
},
sdk.Attribute{
Key: types.AttributeKeyTokenIn,
Value: msg.Token.String(),
},
sdk.Attribute{
Key: types.AttributeKeyLpToken,
Value: poolToken.String(),
},
))
return &types.MsgSingleAssetDepositResponse{
PoolToken: pool.Supply,
}, nil
}