Skip to content

Commit

Permalink
refactor: use MsgSendPacket
Browse files Browse the repository at this point in the history
This is the type used in solidity-ibc-eureka e2e tests for sending
tokens from a Cosmos chain back to an EVM chain
  • Loading branch information
rootulp committed Dec 18, 2024
1 parent c7ecdbf commit 23477cb
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 48 deletions.
13 changes: 8 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ require (
cosmossdk.io/x/circuit v0.1.1
cosmossdk.io/x/evidence v0.1.1
cosmossdk.io/x/feegrant v0.1.1
cosmossdk.io/x/tx v0.13.5
cosmossdk.io/x/tx v0.13.6
cosmossdk.io/x/upgrade v0.1.4
github.com/cometbft/cometbft v0.38.15
github.com/cosmos/cosmos-db v1.1.0
github.com/cosmos/cosmos-sdk v0.50.10
github.com/cosmos/gogoproto v1.7.0
// e9b5b33def38 is the commit hash of feat/ibc-eureka branch - https://github.com/cosmos/ibc-go/tree/feat/ibc-eureka
github.com/cosmos/ibc-go/v9 v9.0.0-20241202053124-ce8dcf235e81
github.com/cosmos/ibc-go/v9 v9.0.1
github.com/spf13/cast v1.7.0
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
Expand All @@ -30,6 +29,7 @@ require (

require (
cosmossdk.io/errors v1.0.1
cosmossdk.io/math v1.4.0
github.com/celestiaorg/celestia-openrpc v0.5.0
github.com/consensys/gnark v0.11.0
github.com/consensys/gnark-crypto v0.14.0
Expand All @@ -39,7 +39,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/hashicorp/go-multierror v1.1.1
github.com/miguelmota/go-ethereum-hdwallet v0.1.2
google.golang.org/grpc v1.68.0
google.golang.org/grpc v1.68.1
google.golang.org/protobuf v1.35.2
)

Expand All @@ -52,7 +52,6 @@ require (
cloud.google.com/go/storage v1.41.0 // indirect
cosmossdk.io/collections v0.4.0 // indirect
cosmossdk.io/depinject v1.0.0 // indirect
cosmossdk.io/math v1.4.0 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.2 // indirect
Expand Down Expand Up @@ -280,6 +279,10 @@ require (
)

replace (
// Replace ibc-go with the most recent commit on the feat/ibc-eureka branch.
// ea4e73629c55e35537467d9f032da5be8082b468 is the recent commit as of this writing.
// See https://github.com/cosmos/ibc-go/tree/feat/ibc-eureka
github.com/cosmos/ibc-go/v9 => github.com/cosmos/ibc-go/v9 v9.0.0-20241218142801-ea4e73629c55
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ cosmossdk.io/x/evidence v0.1.1 h1:Ks+BLTa3uftFpElLTDp9L76t2b58htjVbSZ86aoK/E4=
cosmossdk.io/x/evidence v0.1.1/go.mod h1:OoDsWlbtuyqS70LY51aX8FBTvguQqvFrt78qL7UzeNc=
cosmossdk.io/x/feegrant v0.1.1 h1:EKFWOeo/pup0yF0svDisWWKAA9Zags6Zd0P3nRvVvw8=
cosmossdk.io/x/feegrant v0.1.1/go.mod h1:2GjVVxX6G2fta8LWj7pC/ytHjryA6MHAJroBWHFNiEQ=
cosmossdk.io/x/tx v0.13.5 h1:FdnU+MdmFWn1pTsbfU0OCf2u6mJ8cqc1H4OMG418MLw=
cosmossdk.io/x/tx v0.13.5/go.mod h1:V6DImnwJMTq5qFjeGWpXNiT/fjgE4HtmclRmTqRVM3w=
cosmossdk.io/x/tx v0.13.6 h1:qCiZJ+yK5MsSdUByjOUrfm3sk7aZk1AuYufX22VLC1M=
cosmossdk.io/x/tx v0.13.6/go.mod h1:V6DImnwJMTq5qFjeGWpXNiT/fjgE4HtmclRmTqRVM3w=
cosmossdk.io/x/upgrade v0.1.4 h1:/BWJim24QHoXde8Bc64/2BSEB6W4eTydq0X/2f8+g38=
cosmossdk.io/x/upgrade v0.1.4/go.mod h1:9v0Aj+fs97O+Ztw+tG3/tp5JSlrmT7IcFhAebQHmOPo=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
Expand Down Expand Up @@ -411,8 +411,8 @@ github.com/cosmos/gogoproto v1.7.0 h1:79USr0oyXAbxg3rspGh/m4SWNyoz/GLaAh0QlCe2fr
github.com/cosmos/gogoproto v1.7.0/go.mod h1:yWChEv5IUEYURQasfyBW5ffkMHR/90hiHgbNgrtp4j0=
github.com/cosmos/iavl v1.2.0 h1:kVxTmjTh4k0Dh1VNL046v6BXqKziqMDzxo93oh3kOfM=
github.com/cosmos/iavl v1.2.0/go.mod h1:HidWWLVAtODJqFD6Hbne2Y0q3SdxByJepHUOeoH4LiI=
github.com/cosmos/ibc-go/v9 v9.0.0-20241202053124-ce8dcf235e81 h1:zZHKLBh9RGKZCIOuMZQLz5kigiwzoEi3dYE0/8ju/Zg=
github.com/cosmos/ibc-go/v9 v9.0.0-20241202053124-ce8dcf235e81/go.mod h1:uC5almPKxKsUnWQR0CFG2at9ZWcJ/0Zsa5GiThEU96k=
github.com/cosmos/ibc-go/v9 v9.0.0-20241218142801-ea4e73629c55 h1:tvvb3EXXVrYaCOFGuS1awUBEfoI9a0qjhJaQnbkagZ4=
github.com/cosmos/ibc-go/v9 v9.0.0-20241218142801-ea4e73629c55/go.mod h1:6+3if3nHW38kA8P2Lcb4UIDpt5BIUPbrCGAD8kGIli8=
github.com/cosmos/ics23/go v0.11.0 h1:jk5skjT0TqX5e5QJbEnwXIS2yI2vnmLOgpQPeM5RtnU=
github.com/cosmos/ics23/go v0.11.0/go.mod h1:A8OjxPE67hHST4Icw94hOxxFEJMBG031xIGF/JHNIY0=
github.com/cosmos/ledger-cosmos-go v0.13.3 h1:7ehuBGuyIytsXbd4MP43mLeoN2LTOEnk5nvue4rK+yM=
Expand Down Expand Up @@ -1870,8 +1870,8 @@ google.golang.org/grpc v1.48.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACu
google.golang.org/grpc v1.49.0/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI=
google.golang.org/grpc v1.50.0/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI=
google.golang.org/grpc v1.50.1/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI=
google.golang.org/grpc v1.68.0 h1:aHQeeJbo8zAkAa3pRzrVjZlbz6uSfeOXlJNQM0RAbz0=
google.golang.org/grpc v1.68.0/go.mod h1:fmSPC5AsjSBCK54MyHRx48kpOti1/jRfOlwEWywNjWA=
google.golang.org/grpc v1.68.1 h1:oI5oTa11+ng8r8XMMN7jAOmWfPZWbYpCFaMUTACxkM0=
google.golang.org/grpc v1.68.1/go.mod h1:+q1XYFJjShcqn0QZHvCyeR4CXPA+llXIeUIfIe00waw=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
Expand Down
77 changes: 40 additions & 37 deletions testing/demo/pkg/transfer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@ package main
import (
"fmt"
"os"
"time"

"cosmossdk.io/math"
"github.com/celestiaorg/celestia-zkevm-ibc-demo/testing/demo/pkg/utils"
"github.com/cosmos/cosmos-sdk/client"
sdktypes "github.com/cosmos/cosmos-sdk/types"
transfertypes "github.com/cosmos/ibc-go/v9/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
channeltypesv2 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types"
ibctesting "github.com/cosmos/ibc-go/v9/testing"
"github.com/cosmos/solidity-ibc-eureka/abigen/ics20lib"
)

const (
// relayer is an address registered in simapp
relayer = "cosmos1ltvzpwf3eg8e9s7wzleqdmw02lesrdex9jgt0q"

// ethereumUserAddress is an address registered in the ethereum chain
ethereumUserAddress = "0x7f39c581f595b53c5cb19b5a6e5b8f3a0b1f2f6e"

// denom is the denomination of the token on simapp
denom = "stake"
)
Expand Down Expand Up @@ -42,54 +49,50 @@ func SubmitMsgTransfer() error {
}

func submitMsgTransfer(clientCtx client.Context) error {
portID := "transfer"
channelID := "channel-0"
version := "ics20-2"
tokens := sdktypes.NewCoins(sdktypes.NewInt64Coin(denom, 100))
sender := relayer
recipient := relayer
timeoutHeight := clienttypes.NewHeight(0, 100)
timeoutTimestamp := uint64(0)
memo := "test transfer"
forwarding := &transfertypes.Forwarding{}
msgTransfer, err := createMsgTransfer(portID, channelID, version, tokens, sender, recipient, timeoutHeight, timeoutTimestamp, memo, forwarding)

msgTransfer, err := createMsgTransfer()
if err != nil {
return fmt.Errorf("failed to create MsgTransfer: %w", err)
}

response, err := utils.BroadcastMessages(clientCtx, relayer, 200_000, msgTransfer)
response, err := utils.BroadcastMessages(clientCtx, relayer, 200_000, &msgTransfer)
if err != nil {
return fmt.Errorf("failed to broadcast MsgTransfer", err)
return fmt.Errorf("failed to broadcast MsgTransfer %w", err)
}

if response.Code != 0 {
return fmt.Errorf("failed to execute MsgTransfer", response.RawLog)
return fmt.Errorf("failed to execute MsgTransfer %v", response.RawLog)
}

return nil
}

// createMsgTransfer returns a MsgTransfer that is constructed based on the version of the IBC transfer module in-use.
// Note: the IBC transfer module version should be specified by the channel metadata: v1 is ics20-1. v2 is ics20-2.
// TODO: I thought IBC Eureka eliminated the notion of portID so maybe this uses an outdated type.
func createMsgTransfer(portID string, channelID string, version string, tokens sdktypes.Coins, sender string, receiver string, timeoutHeight clienttypes.Height, timeoutTimestamp uint64, memo string, forwarding *transfertypes.Forwarding) (*transfertypes.MsgTransfer, error) {
switch version {
case transfertypes.V1:
return &transfertypes.MsgTransfer{
SourcePort: portID,
SourceChannel: channelID,
Token: tokens[0],
Sender: sender,
Receiver: receiver,
TimeoutHeight: timeoutHeight,
TimeoutTimestamp: timeoutTimestamp,
Memo: memo,
Tokens: sdktypes.NewCoins(),
}, nil
case transfertypes.V2:
return transfertypes.NewMsgTransfer(portID, channelID, tokens, sender, receiver, timeoutHeight, timeoutTimestamp, memo, forwarding), nil
default:
return nil, fmt.Errorf("unsupported transfer version: %s", version)
func createMsgTransfer() (channeltypesv2.MsgSendPacket, error) {
timeout := uint64(time.Now().Add(30 * time.Minute).Unix())
coin := sdktypes.NewCoin(denom, math.NewInt(100))

transferPayload := ics20lib.ICS20LibFungibleTokenPacketData{
Denom: coin.Denom,
Amount: coin.Amount.BigInt(),
Sender: relayer,
Receiver: ethereumUserAddress,
Memo: "test transfer",
}
transferBz, err := ics20lib.EncodeFungibleTokenPacketData(transferPayload)
if err != nil {
return channeltypesv2.MsgSendPacket{}, err
}
payload := channeltypesv2.Payload{
SourcePort: transfertypes.PortID,
DestinationPort: transfertypes.PortID,
Version: transfertypes.V1,
Encoding: transfertypes.EncodingABI,
Value: transferBz,
}
msg := channeltypesv2.MsgSendPacket{
SourceChannel: ibctesting.FirstChannelID,
TimeoutTimestamp: timeout,
Payloads: []channeltypesv2.Payload{payload},
Signer: relayer,
}
return msg, nil
}

0 comments on commit 23477cb

Please sign in to comment.