Skip to content

Commit

Permalink
fix IbcTransferCoins logic for hook case
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-nguy committed Sep 9, 2021
1 parent 6656fdd commit 982bd28
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
13 changes: 12 additions & 1 deletion x/cronos/keeper/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (k Keeper) ConvertVouchersToEvmCoins(ctx sdk.Context, from string, coins sd
return nil
}

func (k Keeper) IbcTransferCoins(ctx sdk.Context, from, destination string, coins sdk.Coins) error {
func (k Keeper) IbcTransferCoins(ctx sdk.Context, from, destination string, coins sdk.Coins, hook bool) error {
acc, err := sdk.AccAddressFromBech32(from)
if err != nil {
return err
Expand Down Expand Up @@ -131,6 +131,17 @@ func (k Keeper) IbcTransferCoins(ctx sdk.Context, from, destination string, coin
if !found {
return fmt.Errorf("coin %s is not supported", c.Denom)
}

if hook {
// if the method is called through a hook, we don't allow the conversion from CRC20 to native
// to be done without user consent (signature).
// Smart contract need to handle the CRC20 burn operation by themselves
err = k.ibcSendTransfer(ctx, contract.Bytes(), destination, c)
if err != nil {
return err
}
}

err := k.ConvertCoinFromCRC20ToNative(ctx, contract, common.BytesToAddress(acc.Bytes()), c.Amount)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion x/cronos/keeper/ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func (suite *KeeperTestSuite) TestIbcTransferCoins() {
suite.app.CronosKeeper = cronosKeeper

tc.malleate()
err := suite.app.CronosKeeper.IbcTransferCoins(suite.ctx, tc.from, tc.to, tc.coin)
err := suite.app.CronosKeeper.IbcTransferCoins(suite.ctx, tc.from, tc.to, tc.coin, false)
if tc.expectedError != nil {
suite.Require().True(strings.Contains(err.Error(),tc.expectedError.Error()))
} else {
Expand Down
2 changes: 1 addition & 1 deletion x/cronos/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (k msgServer) ConvertVouchers(goCtx context.Context, msg *types.MsgConvertV

func (k msgServer) TransferTokens(goCtx context.Context, msg *types.MsgTransferTokens) (*types.MsgTransferTokensResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
err := k.IbcTransferCoins(ctx, msg.From, msg.To, msg.Coins)
err := k.IbcTransferCoins(ctx, msg.From, msg.To, msg.Coins, false)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 982bd28

Please sign in to comment.