-
Notifications
You must be signed in to change notification settings - Fork 200
/
msg_server_clear_balance.go
62 lines (53 loc) · 2.21 KB
/
msg_server_clear_balance.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
package keeper
import (
"context"
"fmt"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
ibctransfertypes "github.com/cosmos/ibc-go/v5/modules/apps/transfer/types"
"github.com/spf13/cast"
"github.com/Stride-Labs/stride/v9/x/stakeibc/types"
)
func (k msgServer) ClearBalance(goCtx context.Context, msg *types.MsgClearBalance) (*types.MsgClearBalanceResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
zone, found := k.GetHostZone(ctx, msg.ChainId)
if !found {
return nil, errorsmod.Wrapf(types.ErrInvalidHostZone, "chainId: %s", msg.ChainId)
}
feeAccount := zone.GetFeeAccount()
if feeAccount == nil {
return nil, errorsmod.Wrapf(types.ErrFeeAccountNotRegistered, "chainId: %s", msg.ChainId)
}
sourcePort := ibctransfertypes.PortID
// Should this be a param?
// I think as long as we have a timeout on this, it should be hard to attack (even if someone send a tx on a bad channel, it would be reverted relatively quickly)
sourceChannel := msg.Channel
coinString := cast.ToString(msg.Amount) + zone.GetHostDenom()
tokens, err := sdk.ParseCoinNormalized(coinString)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("failed to parse coin (%s)", coinString))
return nil, errorsmod.Wrapf(err, "failed to parse coin (%s)", coinString)
}
sender := feeAccount.GetAddress()
// KeyICATimeoutNanos are for our Stride ICA calls, KeyFeeTransferTimeoutNanos is for the IBC transfer
feeTransferTimeoutNanos := k.GetParam(ctx, types.KeyFeeTransferTimeoutNanos)
timeoutTimestamp := cast.ToUint64(ctx.BlockTime().UnixNano()) + feeTransferTimeoutNanos
msgs := []sdk.Msg{
&ibctransfertypes.MsgTransfer{
SourcePort: sourcePort,
SourceChannel: sourceChannel,
Token: tokens,
Sender: sender,
Receiver: types.FeeAccount,
TimeoutTimestamp: timeoutTimestamp,
},
}
connectionId := zone.GetConnectionId()
icaTimeoutNanos := k.GetParam(ctx, types.KeyICATimeoutNanos)
icaTimeoutNanos = cast.ToUint64(ctx.BlockTime().UnixNano()) + icaTimeoutNanos
_, err = k.SubmitTxs(ctx, connectionId, msgs, *feeAccount, icaTimeoutNanos, "", nil)
if err != nil {
return nil, errorsmod.Wrapf(err, "failed to submit txs")
}
return &types.MsgClearBalanceResponse{}, nil
}