Skip to content

Commit

Permalink
GMP msg_server implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
luckychess committed Nov 27, 2024
1 parent 80b6e6d commit b6b9e1c
Show file tree
Hide file tree
Showing 7 changed files with 749 additions and 15 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@ func New(
app.IBCKeeper.ChannelKeeper,
app.IBCKeeper.PortKeeper,
scopedGmpKeeper,
app.TransferKeeper,
)
gmpModule := gmpmodule.NewAppModule(appCodec, app.GmpKeeper, app.AccountKeeper, app.BankKeeper)

Expand Down
12 changes: 11 additions & 1 deletion proto/ssc/gmp/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ syntax = "proto3";
package ssc.gmp;

option go_package = "github.com/sagaxyz/ssc/x/gmp/types";
import "cosmos/base/v1beta1/coin.proto";

// Msg defines the Msg service.
service Msg {}
service Msg { rpc Transfer(MsgMultiSend) returns (MsgMultiSendResponse); }

message MsgMultiSend {
string from_address = 1;
string to_address = 2;
cosmos.base.v1beta1.Coin amount = 3;
string destination_chain = 4;
}

message MsgMultiSendResponse {}
2 changes: 2 additions & 0 deletions testutil/keeper/gmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
typesparams "github.com/cosmos/cosmos-sdk/x/params/types"
capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
ibctransferkeeper "github.com/cosmos/ibc-go/v8/modules/apps/transfer/keeper"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -87,6 +88,7 @@ func GmpKeeper(t testing.TB) (*keeper.Keeper, sdk.Context) {
gmpChannelKeeper{},
gmpPortKeeper{},
capabilityKeeper.ScopeToModule("GmpScopedKeeper"),
ibctransferkeeper.Keeper{},
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, logger)
Expand Down
16 changes: 16 additions & 0 deletions x/gmp/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"context"
"fmt"

cosmossdkerrors "cosmossdk.io/errors"
Expand All @@ -10,6 +11,8 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
ibctransferkeeper "github.com/cosmos/ibc-go/v8/modules/apps/transfer/keeper"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
"github.com/cosmos/ibc-go/v8/modules/core/exported"
Expand All @@ -27,6 +30,7 @@ type (
channelKeeper types.ChannelKeeper
portKeeper types.PortKeeper
scopedKeeper exported.ScopedKeeper
ibcKeeper ibctransferkeeper.Keeper
}
)

Expand All @@ -38,6 +42,7 @@ func NewKeeper(
channelKeeper types.ChannelKeeper,
portKeeper types.PortKeeper,
scopedKeeper exported.ScopedKeeper,
ibcKeeper ibctransferkeeper.Keeper,

) *Keeper {
// set KeyTable if it has not already been set
Expand All @@ -54,6 +59,7 @@ func NewKeeper(
channelKeeper: channelKeeper,
portKeeper: portKeeper,
scopedKeeper: scopedKeeper,
ibcKeeper: ibcKeeper,
}
}

Expand Down Expand Up @@ -110,3 +116,13 @@ func (k Keeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}

func (k Keeper) Transfer(goCtx context.Context, msg *ibctransfertypes.MsgTransfer) (*types.MsgMultiSendResponse, error) {
// TODO: msg.Receiver == AxelarGMPAcc

_, err := k.ibcKeeper.Transfer(goCtx, msg)
if err != nil {
return nil, err
}
return &types.MsgMultiSendResponse{}, nil
}
93 changes: 91 additions & 2 deletions x/gmp/keeper/msg_server.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,106 @@
package keeper

import (
"context"
"encoding/json"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/sagaxyz/ssc/x/gmp/types"
)

// AxelarGMPAcc is the address that receives the message from a cosmos chain
const AxelarGMPAcc = "axelar1dv4u5k73pzqrxlzujxg3qp8kvc3pje7jtdvu72npnt5zhq05ejcsn5qme5"

type MessageType int

const (
// TypeUnrecognized means coin type is unrecognized
TypeUnrecognized = iota
// TypeGeneralMessage is a pure message
TypeGeneralMessage
// TypeGeneralMessageWithToken is a general message with token
TypeGeneralMessageWithToken
// TypeSendToken is a direct token transfer
TypeSendToken
)

// Message is attached in ICS20 packet memo field
type Message struct {
DestinationChain string `json:"destination_chain"`
DestinationAddress string `json:"destination_address"`
Payload []byte `json:"payload"`
Type int64 `json:"type"`
}

type msgServer struct {
Keeper
keeper Keeper
}

// NewMsgServerImpl returns an implementation of the MsgServer interface
// for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{Keeper: keeper}
return &msgServer{keeper: keeper}
}

func (k msgServer) Transfer(goCtx context.Context, msg *types.MsgMultiSend) (*types.MsgMultiSendResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// sender, err := sdk.AccAddressFromBech32(msg.Sender)
// if err != nil {
// return nil, err
// }

// build payload that can be decoded by solidity
addressesType, err := abi.NewType("address[]", "address[]", nil)
if err != nil {
return nil, err
}

var addresses []common.Address
addresses = append(addresses, common.HexToAddress(msg.ToAddress))
// for _, receiver := range msg.ToAddress {
// addresses = append(addresses, common.HexToAddress(receiver))
// }

payload, err := abi.Arguments{{Type: addressesType}}.Pack(addresses)
if err != nil {
return nil, err
}

message := Message{
DestinationChain: msg.DestinationChain,
DestinationAddress: msg.ToAddress,
Payload: payload,
Type: TypeGeneralMessageWithToken,
}

bz, err := json.Marshal(&message)
if err != nil {
return nil, err
}

ibcMessage := ibctransfertypes.NewMsgTransfer(
ibctransfertypes.PortID,
"channel-17", // hard-coded channel id for demo
*msg.Amount,
msg.FromAddress,
AxelarGMPAcc, // TODO: use config
clienttypes.ZeroHeight(),
uint64(ctx.BlockTime().Add(6*time.Hour).UnixNano()),
string(bz),
)

res, err := k.keeper.Transfer(goCtx, ibcMessage)
if err != nil {
return nil, err
}

return res, nil
}

var _ types.MsgServer = msgServer{}
5 changes: 5 additions & 0 deletions x/gmp/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
context "context"

sdk "github.com/cosmos/cosmos-sdk/types"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
)

// AccountKeeper defines the expected account keeper used for simulations (noalias)
Expand All @@ -17,3 +18,7 @@ type BankKeeper interface {
SpendableCoins(ctx context.Context, addr sdk.AccAddress) sdk.Coins
// Methods imported from bank should be defined here
}

type IBCTransferKeeper interface {
Transfer(goCtx context.Context, msg *ibctransfertypes.MsgTransfer) (*ibctransfertypes.MsgTransferResponse, error)
}
Loading

0 comments on commit b6b9e1c

Please sign in to comment.