Skip to content

Commit

Permalink
fix ibc logic
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-nguy committed Jul 26, 2022
1 parent 2f3a349 commit 51c7583
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion x/cronos/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (suite *CronosTestSuite) TestMsgTransferTokens() {
"Correct address with non supported coin denom",
types.NewMsgTransferTokens(suite.address.String(), "to", sdk.NewCoins(sdk.NewCoin("fake", sdk.NewInt(1)))),
func() {},
errors.New("coin fake is not supported"),
errors.New("the coin fake is neither an ibc voucher or a cronos token"),
},
}
for _, tc := range testCases {
Expand Down
18 changes: 16 additions & 2 deletions x/cronos/keeper/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ func (k Keeper) IbcTransferCoins(ctx sdk.Context, from, destination string, coin
}

default:
if !types.IsValidIBCDenom(c.Denom) && !types.IsValidCronosDenom(c.Denom) {
return fmt.Errorf("the coin %s is neither an ibc voucher or a cronos token", c.Denom)
}
_, found := k.GetContractByDenom(ctx, c.Denom)
if !found {
return fmt.Errorf("coin %s is not supported", c.Denom)
Expand All @@ -153,8 +156,19 @@ func (k Keeper) IbcTransferCoins(ctx sdk.Context, from, destination string, coin
}

func (k Keeper) ibcSendTransfer(ctx sdk.Context, sender sdk.AccAddress, destination string, coin sdk.Coin) error {
// Coin needs to be a voucher so that we can extract the channel id from the denom
channelID, err := k.GetSourceChannelID(ctx, coin.Denom)
// We extract the channel id from the coin denom
channelDenom := ""
if types.IsSourceCoin(coin.Denom) {
// If coin is source, we can only transfer to crypto.org chain
// we are using the cro denom to extract the channel id
// TODO: support arbitrary channel?
channelDenom = k.GetParams(ctx).IbcCroDenom
} else {
// If it is not source, then coin is a voucher so we can extract the channel id from the denom
channelDenom = coin.Denom
}

channelID, err := k.GetSourceChannelID(ctx, channelDenom)
if err != nil {
return err
}
Expand Down
13 changes: 11 additions & 2 deletions x/cronos/keeper/ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,18 @@ func (suite *KeeperTestSuite) TestIbcTransferCoins() {
"Correct address with non supported coin denom",
address.String(),
"to",
sdk.NewCoins(sdk.NewCoin("ibc/BAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", sdk.NewInt(1))),
func() {},
errors.New("coin ibc/BAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA is not supported"),
func() {},
},
{
"Correct address with incorrect coin denom",
address.String(),
"to",
sdk.NewCoins(sdk.NewCoin("fake", sdk.NewInt(1))),
func() {},
errors.New("coin fake is not supported"),
errors.New("the coin fake is neither an ibc voucher or a cronos token"),
func() {},
},
{
Expand Down Expand Up @@ -227,7 +236,7 @@ func (suite *KeeperTestSuite) TestIbcTransferCoins() {
// Add support for the IBC token
suite.app.CronosKeeper.SetAutoContractForDenom(suite.ctx, "incorrect", common.HexToAddress("0x11"))
},
errors.New("incorrect is invalid: ibc cro denom is invalid"),
errors.New("the coin incorrect is neither an ibc voucher or a cronos token"),
func() {
},
},
Expand Down

0 comments on commit 51c7583

Please sign in to comment.