Skip to content

Commit

Permalink
[BP: v7 <- #1069] feat!: using ica controller instead of intertx (#…
Browse files Browse the repository at this point in the history
…1074)

* feat!: using ica controller instead of intertx (#1069)

* replacing intertx with interchain account controller

* adding generic icatx support

* updating SendICABankTransfer to use interchain account controller module

* adding chain level wrappers for ica controller

* go mod

* Revert "go mod"

This reverts commit 2638272.

(cherry picked from commit 94f966a)

# Conflicts:
#	chain/cosmos/chain_node.go
#	chain/cosmos/cosmos_chain.go

* fix changes in v7 from v8

---------

Co-authored-by: Spoorthi <9302666+spoo-bar@users.noreply.github.com>
Co-authored-by: Reece Williams <reecepbcups@gmail.com>
  • Loading branch information
3 people authored Apr 17, 2024
1 parent 537f056 commit 0f83fea
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 22 deletions.
76 changes: 54 additions & 22 deletions chain/cosmos/chain_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ import (
coretypes "github.com/cometbft/cometbft/rpc/core/types"
libclient "github.com/cometbft/cometbft/rpc/jsonrpc/client"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authTx "github.com/cosmos/cosmos-sdk/x/auth/tx"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
paramsutils "github.com/cosmos/cosmos-sdk/x/params/client/utils"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/gogoproto/proto"
volumetypes "github.com/docker/docker/api/types/volume"
dockerclient "github.com/docker/docker/client"
"github.com/docker/go-connections/nat"
Expand All @@ -37,6 +42,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types"
ccvclient "github.com/cosmos/interchain-security/v3/x/ccv/provider/client"
"github.com/strangelove-ventures/interchaintest/v7/ibc"
"github.com/strangelove-ventures/interchaintest/v7/internal/blockdb"
Expand Down Expand Up @@ -1597,22 +1603,21 @@ func (tn *ChainNode) logger() *zap.Logger {
// RegisterICA will attempt to register an interchain account on the counterparty chain.
func (tn *ChainNode) RegisterICA(ctx context.Context, keyName, connectionID string) (string, error) {
return tn.ExecTx(ctx, keyName,
"intertx", "register",
"--connection-id", connectionID,
"interchain-accounts", "controller", "register", connectionID,
)
}

// QueryICA will query for an interchain account controlled by the specified address on the counterparty chain.
func (tn *ChainNode) QueryICA(ctx context.Context, connectionID, address string) (string, error) {
stdout, _, err := tn.ExecQuery(ctx,
"intertx", "interchainaccounts", connectionID, address,
"interchain-accounts", "controller", "interchain-account", address, connectionID,
)
if err != nil {
return "", err
}

// at this point stdout should look like this:
// interchain_account_address: cosmos1p76n3mnanllea4d3av0v0e42tjj03cae06xq8fwn9at587rqp23qvxsv0j
// address: cosmos1p76n3mnanllea4d3av0v0e42tjj03cae06xq8fwn9at587rqp23qvxsv0j
// we split the string at the : and then just grab the address before returning.
parts := strings.SplitN(string(stdout), ":", 2)
if len(parts) < 2 {
Expand All @@ -1621,27 +1626,54 @@ func (tn *ChainNode) QueryICA(ctx context.Context, connectionID, address string)
return strings.TrimSpace(parts[1]), nil
}

// SendICABankTransfer builds a bank transfer message for a specified address and sends it to the specified
// SendICATx sends an interchain account transaction for a specified address and sends it to the specified
// interchain account.
func (tn *ChainNode) SendICABankTransfer(ctx context.Context, connectionID, fromAddr string, amount ibc.WalletAmount) error {
msg, err := json.Marshal(map[string]any{
"@type": "/cosmos.bank.v1beta1.MsgSend",
"from_address": fromAddr,
"to_address": amount.Address,
"amount": []map[string]any{
{
"denom": amount.Denom,
"amount": amount.Amount.String(),
},
},
})
func (tn *ChainNode) SendICATx(ctx context.Context, keyName, connectionID string, registry codectypes.InterfaceRegistry, msgs []sdk.Msg, icaTxMemo string) (string, error) {
cdc := codec.NewProtoCodec(registry)

messages := make([]proto.Message, len(msgs))
for _, msg := range msgs {
protoMsg, ok := msg.(proto.Message)
if !ok {
return "", fmt.Errorf("failed to convert sdk.Msg to proto.Message")
}
messages = append(messages, protoMsg)
}

icaPacketDataBytes, err := icatypes.SerializeCosmosTx(cdc, messages)
if err != nil {
return err
return "", err
}

_, err = tn.ExecTx(ctx, fromAddr,
"intertx", "submit", string(msg),
"--connection-id", connectionID,
)
icaPacketData := icatypes.InterchainAccountPacketData{
Type: icatypes.EXECUTE_TX,
Data: icaPacketDataBytes,
Memo: icaTxMemo,
}

if err := icaPacketData.ValidateBasic(); err != nil {
return "", err
}

icaPacketBytes, err := cdc.MarshalJSON(&icaPacketData)
if err != nil {
return "", err
}

return tn.ExecTx(ctx, keyName, "interchain-accounts", "controller", "send-tx", connectionID, string(icaPacketBytes))
}

// SendICABankTransfer builds a bank transfer message for a specified address and sends it to the specified
// interchain account.
func (tn *ChainNode) SendICABankTransfer(ctx context.Context, connectionID, fromAddr string, amount ibc.WalletAmount) error {
fromAddress := sdk.MustAccAddressFromBech32(fromAddr)
toAddress := sdk.MustAccAddressFromBech32(amount.Address)
coin := sdk.NewCoin(amount.Denom, amount.Amount)
msg := banktypes.NewMsgSend(fromAddress, toAddress, sdk.NewCoins(coin))
msgs := []sdk.Msg{msg}

ir := tn.Chain.Config().EncodingConfig.InterfaceRegistry
icaTxMemo := "ica bank transfer"
_, err := tn.SendICATx(ctx, fromAddr, connectionID, ir, msgs, icaTxMemo)
return err
}
19 changes: 19 additions & 0 deletions chain/cosmos/cosmos_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankTypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
Expand Down Expand Up @@ -409,6 +410,24 @@ func (c *CosmosChain) QueryProposal(ctx context.Context, proposalID string) (*Pr
return c.getFullNode().QueryProposal(ctx, proposalID)
}

// RegisterICA will attempt to register an interchain account on the given counterparty chain.
func (c *CosmosChain) RegisterICA(ctx context.Context, keyName string, connectionID string) (string, error) {
return c.getFullNode().RegisterICA(ctx, keyName, connectionID)
}

// QueryICA will query for an interchain account controlled by the given address on the counterparty chain.
func (c *CosmosChain) QueryICAAddress(ctx context.Context, connectionID, address string) (string, error) {
return c.getFullNode().QueryICA(ctx, connectionID, address)
}

// SendICATx sends an interchain account transaction for a specified address and sends it to the respective
// interchain account on the counterparty chain.
func (c *CosmosChain) SendICATx(ctx context.Context, keyName, connectionID string, msgs []sdk.Msg, icaTxMemo string) (string, error) {
node := c.getFullNode()
registry := node.Chain.Config().EncodingConfig.InterfaceRegistry
return node.SendICATx(ctx, keyName, connectionID, registry, msgs, icaTxMemo)
}

// PushNewWasmClientProposal submits a new wasm client governance proposal to the chain
func (c *CosmosChain) PushNewWasmClientProposal(ctx context.Context, keyName string, fileName string, prop TxProposalv1) (TxProposal, string, error) {
tx := TxProposal{}
Expand Down

0 comments on commit 0f83fea

Please sign in to comment.