-
Notifications
You must be signed in to change notification settings - Fork 837
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(erc20): OnRecv automatic conversion (#1086)
* erc20 OnRecv conversion * suggestions from pr * apply suggestions from pr * Change outward conversion to only accept coin denom * fix test inconsistency * suggestions from pr * suggestions from pr Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
- Loading branch information
1 parent
601cc4d
commit 694a8cd
Showing
21 changed files
with
1,976 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package tests | ||
|
||
import ( | ||
transfertypes "github.com/cosmos/ibc-go/v5/modules/apps/transfer/types" | ||
) | ||
|
||
var ( | ||
UosmoDenomtrace = transfertypes.DenomTrace{ | ||
Path: "transfer/channel-0", | ||
BaseDenom: "uosmo", | ||
} | ||
UosmoIbcdenom = UosmoDenomtrace.IBCDenom() | ||
|
||
UatomDenomtrace = transfertypes.DenomTrace{ | ||
Path: "transfer/channel-1", | ||
BaseDenom: "uatom", | ||
} | ||
UatomIbcdenom = UatomDenomtrace.IBCDenom() | ||
|
||
UevmosDenomtrace = transfertypes.DenomTrace{ | ||
Path: "transfer/channel-0", | ||
BaseDenom: "aevmos", | ||
} | ||
UevmosIbcdenom = UevmosDenomtrace.IBCDenom() | ||
|
||
UatomOsmoDenomtrace = transfertypes.DenomTrace{ | ||
Path: "transfer/channel-0/transfer/channel-1", | ||
BaseDenom: "uatom", | ||
} | ||
UatomOsmoIbcdenom = UatomOsmoDenomtrace.IBCDenom() | ||
|
||
AevmosDenomtrace = transfertypes.DenomTrace{ | ||
Path: "transfer/channel-0", | ||
BaseDenom: "aevmos", | ||
} | ||
AevmosIbcdenom = AevmosDenomtrace.IBCDenom() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package erc20 | ||
|
||
import ( | ||
errorsmod "cosmossdk.io/errors" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
transfertypes "github.com/cosmos/ibc-go/v5/modules/apps/transfer/types" | ||
channeltypes "github.com/cosmos/ibc-go/v5/modules/core/04-channel/types" | ||
porttypes "github.com/cosmos/ibc-go/v5/modules/core/05-port/types" | ||
"github.com/cosmos/ibc-go/v5/modules/core/exported" | ||
|
||
errortypes "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/evmos/evmos/v10/ibc" | ||
"github.com/evmos/evmos/v10/x/erc20/keeper" | ||
) | ||
|
||
var _ porttypes.IBCModule = &IBCMiddleware{} | ||
|
||
// IBCMiddleware implements the ICS26 callbacks for the transfer middleware given | ||
// the erc20 keeper and the underlying application. | ||
type IBCMiddleware struct { | ||
*ibc.Module | ||
keeper keeper.Keeper | ||
} | ||
|
||
// NewIBCMiddleware creates a new IBCMiddleware given the keeper and underlying application | ||
func NewIBCMiddleware(k keeper.Keeper, app porttypes.IBCModule) IBCMiddleware { | ||
return IBCMiddleware{ | ||
Module: ibc.NewModule(app), | ||
keeper: k, | ||
} | ||
} | ||
|
||
// OnRecvPacket implements the IBCModule interface. | ||
// It receives the tokens through the default ICS20 OnRecvPacket callback logic | ||
// and then automatically converts the Cosmos Coin to their ERC20 token | ||
// representation. | ||
// If the acknowledgement fails, this callback will default to the ibc-core | ||
// packet callback. | ||
func (im IBCMiddleware) OnRecvPacket( | ||
ctx sdk.Context, | ||
packet channeltypes.Packet, | ||
relayer sdk.AccAddress, | ||
) exported.Acknowledgement { | ||
ack := im.Module.OnRecvPacket(ctx, packet, relayer) | ||
|
||
// return if the acknowledgement is an error ACK | ||
if !ack.Success() { | ||
return ack | ||
} | ||
|
||
return im.keeper.OnRecvPacket(ctx, packet, ack) | ||
} | ||
|
||
// OnAcknowledgementPacket implements the IBCModule interface. | ||
// It refunds the token transferred and then automatically converts the | ||
// Cosmos Coin to their ERC20 token representation. | ||
func (im IBCMiddleware) OnAcknowledgementPacket( | ||
ctx sdk.Context, | ||
packet channeltypes.Packet, | ||
acknowledgement []byte, | ||
relayer sdk.AccAddress, | ||
) error { | ||
var ack channeltypes.Acknowledgement | ||
if err := transfertypes.ModuleCdc.UnmarshalJSON(acknowledgement, &ack); err != nil { | ||
return errorsmod.Wrapf(errortypes.ErrUnknownRequest, "cannot unmarshal ICS-20 transfer packet acknowledgement: %v", err) | ||
} | ||
|
||
var data transfertypes.FungibleTokenPacketData | ||
if err := transfertypes.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil { | ||
return errorsmod.Wrapf(errortypes.ErrUnknownRequest, "cannot unmarshal ICS-20 transfer packet data: %s", err.Error()) | ||
} | ||
|
||
if err := im.Module.OnAcknowledgementPacket(ctx, packet, acknowledgement, relayer); err != nil { | ||
return err | ||
} | ||
|
||
return im.keeper.OnAcknowledgementPacket(ctx, packet, data, ack) | ||
} | ||
|
||
// OnTimeoutPacket implements the IBCModule interface. | ||
// It refunds the token transferred and then automatically converts the | ||
// Cosmos Coin to their ERC20 token representation. | ||
func (im IBCMiddleware) OnTimeoutPacket( | ||
ctx sdk.Context, | ||
packet channeltypes.Packet, | ||
relayer sdk.AccAddress, | ||
) error { | ||
var data transfertypes.FungibleTokenPacketData | ||
if err := transfertypes.ModuleCdc.UnmarshalJSON(packet.GetData(), &data); err != nil { | ||
return errorsmod.Wrapf(errortypes.ErrUnknownRequest, "cannot unmarshal ICS-20 transfer packet data: %s", err.Error()) | ||
} | ||
|
||
if err := im.Module.OnTimeoutPacket(ctx, packet, relayer); err != nil { | ||
return err | ||
} | ||
|
||
return im.keeper.OnTimeoutPacket(ctx, packet, data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.