Skip to content

Commit

Permalink
changing openack api
Browse files Browse the repository at this point in the history
  • Loading branch information
spoo-bar committed Mar 4, 2024
1 parent 825e9c2 commit c3aea14
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 47 deletions.
Binary file modified interchaintest/artifacts/cwica.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion x/cwica/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (im IBCModule) OnChanOpenInit(_ sdk.Context, _ channeltypes.Order, _ []stri
}

// OnChanOpenAck implements the IBCModule interface. This handler is called after we create an
// account on a remote zone (because icaControllerKeeper.RegisterInterchainAccount opens a channel).
// account on the counterparty chain (because icaControllerKeeper.RegisterInterchainAccount opens a channel).
func (im IBCModule) OnChanOpenAck(ctx sdk.Context, portID, channelID, counterPartyChannelID, counterpartyVersion string) error {
return im.keeper.HandleChanOpenAck(ctx, portID, channelID, counterPartyChannelID, counterpartyVersion)
}
Expand Down
77 changes: 40 additions & 37 deletions x/cwica/keeper/ibc_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,51 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"

"github.com/archway-network/archway/x/cwica/types"
)

// HandleChanOpenAck passes the data about a successfully created channel to the appropriate contract via sudo call
func (k *Keeper) HandleChanOpenAck(
ctx sdk.Context,
portID,
channelID,
counterpartyChannelID,
counterpartyVersion string,
) error {
icaOwner := types.ICAOwnerFromPort(portID)
contractAddress, err := sdk.AccAddressFromBech32(icaOwner)
if err != nil {
return errors.Wrapf(sdkerrors.ErrInvalidAddress, "failed to parse contract address: %s", icaOwner)
}

var metadata icatypes.Metadata
if err := icatypes.ModuleCdc.UnmarshalJSON([]byte(counterpartyVersion), &metadata); err != nil {
return errors.Wrapf(icatypes.ErrUnknownDataType, "cannot unmarshal ICS-27 interchain accounts metadata")
}

successMsg := types.SudoPayload{
ICA: &types.MessageICASuccess{
AccountRegistered: &types.AccountRegistered{
CounterpartyAddress: metadata.Address,
},
},
}
sudoPayload, err := json.Marshal(successMsg)
if err != nil {
return fmt.Errorf("failed to marshal MessageSuccess: %v", err)
}

_, err = k.sudoKeeper.Sudo(ctx, contractAddress, sudoPayload)
if err != nil {
k.Logger(ctx).Debug("HandleChanOpenAck: failed to sudo contract on channel open acknowledgement", "error", err)
}

return nil
}

// HandleAcknowledgement passes the acknowledgement data to the appropriate contract via a sudo call.
func (k *Keeper) HandleAcknowledgement(ctx sdk.Context, packet channeltypes.Packet, acknowledgement []byte, relayer sdk.AccAddress) error {
icaOwner := types.ICAOwnerFromPort(packet.SourcePort)
Expand Down Expand Up @@ -97,43 +137,6 @@ func (k *Keeper) HandleTimeout(ctx sdk.Context, packet channeltypes.Packet, rela
return nil
}

// HandleChanOpenAck passes the data about a successfully created channel to the appropriate contract via sudo call
func (k *Keeper) HandleChanOpenAck(
ctx sdk.Context,
portID,
channelID,
counterpartyChannelID,
counterpartyVersion string,
) error {
icaOwner := types.ICAOwnerFromPort(portID)
contractAddress, err := sdk.AccAddressFromBech32(icaOwner)
if err != nil {
return errors.Wrapf(sdkerrors.ErrInvalidAddress, "failed to parse contract address: %s", icaOwner)
}

successMsg := types.SudoPayload{
ICA: &types.MessageICASuccess{
AccountRegistered: &types.OpenAckDetails{
PortID: portID,
ChannelID: channelID,
CounterpartyChannelID: counterpartyChannelID,
CounterpartyVersion: counterpartyVersion,
},
},
}
sudoPayload, err := json.Marshal(successMsg)
if err != nil {
return fmt.Errorf("failed to marshal MessageSuccess: %v", err)
}

_, err = k.sudoKeeper.Sudo(ctx, contractAddress, sudoPayload)
if err != nil {
k.Logger(ctx).Debug("HandleChanOpenAck: failed to sudo contract on channel open acknowledgement", "error", err)
}

return nil
}

// HandleChanCloseConfirm passes the data about a successfully closed channel to the appropriate contract
func (k *Keeper) HandleChanCloseConfirm(ctx sdk.Context, portID string, channelID string) error {
icaOwner := types.ICAOwnerFromPort(portID)
Expand Down
15 changes: 6 additions & 9 deletions x/cwica/types/sudo.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,19 @@ type SudoPayload struct {

// MessageICASuccess is the success message for the sudo call
type MessageICASuccess struct {
AccountRegistered *OpenAckDetails `json:"account_registered,omitempty"`
AccountClosed *ChannelClosed `json:"account_closed,omitempty"`
TxExecuted *ICATxResponse `json:"tx_executed,omitempty"`
AccountRegistered *AccountRegistered `json:"account_registered,omitempty"`
AccountClosed *ChannelClosed `json:"account_closed,omitempty"`
TxExecuted *ICATxResponse `json:"tx_executed,omitempty"`
}

// MessageICAError is the error message for the sudo call
type MessageICAError struct {
Error *SudoErrorMsg `json:"error,omitempty"`
}

// OpenAckDetails is the details of the open ack message - when an interchain account is registered
type OpenAckDetails struct {
PortID string `json:"port_id"`
ChannelID string `json:"channel_id"`
CounterpartyChannelID string `json:"counterparty_channel_id"`
CounterpartyVersion string `json:"counterparty_version"`
// AccountRegistered is contains the address of the registered account on the counterparty chain
type AccountRegistered struct {
CounterpartyAddress string `json:"counterparty_address"`
}

type ChannelClosed struct {
Expand Down

0 comments on commit c3aea14

Please sign in to comment.