From 2651ff28b9eaa0e2f9e5ecaf8775ca964174aad7 Mon Sep 17 00:00:00 2001 From: Adam Wozniak <29418299+adamewozniak@users.noreply.github.com> Date: Wed, 2 Oct 2024 12:05:12 -0700 Subject: [PATCH] feat: gmp handling create payment (#529) --- app/gmpmiddleware/handler.go | 40 ++++++++++++++++----------- x/gmp/keeper/keeper.go | 52 ++++++++++++++++++++++++++++++++++++ x/gmp/keeper/msg_server.go | 46 +------------------------------ 3 files changed, 78 insertions(+), 60 deletions(-) diff --git a/app/gmpmiddleware/handler.go b/app/gmpmiddleware/handler.go index d943999d..27507b96 100644 --- a/app/gmpmiddleware/handler.go +++ b/app/gmpmiddleware/handler.go @@ -3,16 +3,24 @@ package gmpmiddleware import ( "context" + "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ojo-network/ojo/x/gmp/types" ) +// default values for new payments coming from axelar +const ( + blocksPerDay = 14400 +) + +var defaultDeviation = math.LegacyMustNewDecFromStr("1.0") + type GmpKeeper interface { - RelayPrice( - goCtx context.Context, - msg *types.MsgRelayPrice, - ) (*types.MsgRelayPriceResponse, error) GetParams(ctx sdk.Context) (params types.Params) + CreatePayment( + goCtx context.Context, + msg *types.MsgCreatePayment, + ) (*types.MsgCreatePaymentResponse, error) } type GmpHandler struct { @@ -56,22 +64,24 @@ func (h GmpHandler) HandleGeneralMessage( return err } ctx.Logger().Info("HandleGeneralMessage GMP Decoder", "msg", msg) - tx := &types.MsgRelayPrice{ - Relayer: h.relayer, - DestinationChain: srcChain, - ClientContractAddress: msg.ContractAddress.Hex(), - OjoContractAddress: srcAddress, - Denoms: msg.GetDenoms(), - CommandSelector: msg.CommandSelector[:], - CommandParams: msg.CommandParams, - Timestamp: msg.Timestamp.Int64(), - Token: coin, + denoms := msg.GetDenoms() + + tx := &types.MsgCreatePayment{ + Relayer: h.relayer, + Payment: &types.Payment{ + Relayer: h.relayer, + DestinationChain: srcChain, + Denom: denoms[0], + Token: coin, + Deviation: defaultDeviation, + Heartbeat: blocksPerDay, + }, } err = tx.ValidateBasic() if err != nil { return err } ctx.Logger().Info("HandleGeneralMessage GMP Decoder", "tx", tx) - _, err = h.gmp.RelayPrice(ctx, tx) + _, err = h.gmp.CreatePayment(ctx, tx) return err } diff --git a/x/gmp/keeper/keeper.go b/x/gmp/keeper/keeper.go index 333ec65d..697423a5 100644 --- a/x/gmp/keeper/keeper.go +++ b/x/gmp/keeper/keeper.go @@ -7,6 +7,7 @@ import ( "math/big" "time" + "cosmossdk.io/errors" "cosmossdk.io/log" "cosmossdk.io/math" "github.com/ethereum/go-ethereum/common" @@ -21,6 +22,7 @@ import ( "github.com/ojo-network/ojo/app/ibctransfer" "github.com/ojo-network/ojo/util" "github.com/ojo-network/ojo/x/gmp/types" + oracletypes "github.com/ojo-network/ojo/x/oracle/types" ) type Keeper struct { @@ -321,3 +323,53 @@ func (k Keeper) ProcessPayment( return nil } + +func (k Keeper) CreatePayment( + goCtx context.Context, + msg *types.MsgCreatePayment, +) (*types.MsgCreatePaymentResponse, error) { + ctx := sdk.UnwrapSDKContext(goCtx) + + // make sure the destination chain is valid + gasEstimateParams := k.GasEstimateKeeper.GetParams(ctx) + isValidChain := false + for _, chain := range gasEstimateParams.ContractRegistry { + if chain.Network == msg.Payment.DestinationChain { + isValidChain = true + break + } + } + if !isValidChain { + return nil, errors.Wrapf( + types.ErrInvalidDestinationChain, + "destination chain %s not found in contract registry", + msg.Payment.DestinationChain, + ) + } + + // make sure the denom is active in the oracle + _, err := k.oracleKeeper.GetExchangeRate(ctx, msg.Payment.Denom) + if err != nil { + return nil, errors.Wrapf( + oracletypes.ErrUnknownDenom, + "denom %s not active in the oracle", + msg.Payment.Denom, + ) + } + + // send tokens from msg to the module account + address, err := sdk.AccAddressFromBech32(msg.Relayer) + if err != nil { + return nil, err + } + coins := sdk.NewCoins(msg.Payment.Token) + err = k.BankKeeper.SendCoinsFromAccountToModule(ctx, address, types.ModuleName, coins) + if err != nil { + return nil, err + } + + // Create a payment record in the KV store + msg.Payment.Relayer = msg.Relayer + k.SetPayment(ctx, *msg.Payment) + return &types.MsgCreatePaymentResponse{}, nil +} diff --git a/x/gmp/keeper/msg_server.go b/x/gmp/keeper/msg_server.go index 4bc43ddc..03bb8db8 100644 --- a/x/gmp/keeper/msg_server.go +++ b/x/gmp/keeper/msg_server.go @@ -8,7 +8,6 @@ import ( govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" "github.com/ojo-network/ojo/x/gmp/types" - oracletypes "github.com/ojo-network/ojo/x/oracle/types" ) type msgServer struct { @@ -57,48 +56,5 @@ func (ms msgServer) CreatePayment( goCtx context.Context, msg *types.MsgCreatePayment, ) (*types.MsgCreatePaymentResponse, error) { - ctx := sdk.UnwrapSDKContext(goCtx) - - // make sure the destination chain is valid - gasEstimateParams := ms.keeper.GasEstimateKeeper.GetParams(ctx) - isValidChain := false - for _, chain := range gasEstimateParams.ContractRegistry { - if chain.Network == msg.Payment.DestinationChain { - isValidChain = true - break - } - } - if !isValidChain { - return nil, errors.Wrapf( - types.ErrInvalidDestinationChain, - "destination chain %s not found in contract registry", - msg.Payment.DestinationChain, - ) - } - - // make sure the denom is active in the oracle - _, err := ms.keeper.oracleKeeper.GetExchangeRate(ctx, msg.Payment.Denom) - if err != nil { - return nil, errors.Wrapf( - oracletypes.ErrUnknownDenom, - "denom %s not active in the oracle", - msg.Payment.Denom, - ) - } - - // send tokens from msg to the module account - address, err := sdk.AccAddressFromBech32(msg.Relayer) - if err != nil { - return nil, err - } - coins := sdk.NewCoins(msg.Payment.Token) - err = ms.keeper.BankKeeper.SendCoinsFromAccountToModule(ctx, address, types.ModuleName, coins) - if err != nil { - return nil, err - } - - // Create a payment record in the KV store - msg.Payment.Relayer = msg.Relayer - ms.keeper.SetPayment(ctx, *msg.Payment) - return &types.MsgCreatePaymentResponse{}, nil + return ms.keeper.CreatePayment(goCtx, msg) }