wormchain: tokenbridge: Send/Burn coins after truncation

Sending or burning coins before truncating the amount would lead to the
user losing the truncated coins.  Only send or burn the coins after
truncation.
This commit is contained in:
Chirantan Ekbote 2022-08-25 17:27:19 +09:00 committed by Chirantan Ekbote
parent c53813ce37
commit 5e7752c74b
1 changed files with 13 additions and 13 deletions

View File

@ -36,19 +36,6 @@ func (k msgServer) Transfer(goCtx context.Context, msg *types.MsgTransfer) (*typ
} }
moduleAddress := k.accountKeeper.GetModuleAddress(types.ModuleName) moduleAddress := k.accountKeeper.GetModuleAddress(types.ModuleName)
_, _, wrapped := types.GetWrappedCoinMeta(msg.Amount.Denom)
if wrapped {
// We previously minted these coins so just burn them now.
if err := k.bankKeeper.BurnCoins(ctx, types.ModuleName, sdk.Coins{msg.Amount}); err != nil {
return nil, sdkerrors.Wrap(err, "failed to burn wrapped coins")
}
} else {
// Collect coins in the module account.
if err := k.bankKeeper.SendCoins(ctx, userAcc, moduleAddress, sdk.Coins{msg.Amount}); err != nil {
return nil, sdkerrors.Wrap(err, "failed to send coins to module account")
}
}
bridgeBalance, err := types.Truncate(k.bankKeeper.GetBalance(ctx, moduleAddress, msg.Amount.Denom), meta) bridgeBalance, err := types.Truncate(k.bankKeeper.GetBalance(ctx, moduleAddress, msg.Amount.Denom), meta)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to truncate bridge balance: %w", err) return nil, fmt.Errorf("failed to truncate bridge balance: %w", err)
@ -76,6 +63,19 @@ func (k msgServer) Transfer(goCtx context.Context, msg *types.MsgTransfer) (*typ
return nil, types.ErrAmountTooHigh return nil, types.ErrAmountTooHigh
} }
_, _, wrapped := types.GetWrappedCoinMeta(msg.Amount.Denom)
if wrapped {
// We previously minted these coins so just burn them now.
if err := k.bankKeeper.BurnCoins(ctx, types.ModuleName, sdk.Coins{amount}); err != nil {
return nil, sdkerrors.Wrap(err, "failed to burn wrapped coins")
}
} else {
// Collect coins in the module account.
if err := k.bankKeeper.SendCoins(ctx, userAcc, moduleAddress, sdk.Coins{amount}); err != nil {
return nil, sdkerrors.Wrap(err, "failed to send coins to module account")
}
}
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
// PayloadID // PayloadID
buf.WriteByte(1) buf.WriteByte(1)