Skip to content

Commit

Permalink
feat: gmp handling create payment (#529)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamewozniak authored Oct 2, 2024
1 parent b16218c commit 2651ff2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 60 deletions.
40 changes: 25 additions & 15 deletions app/gmpmiddleware/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
52 changes: 52 additions & 0 deletions x/gmp/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/big"
"time"

"cosmossdk.io/errors"
"cosmossdk.io/log"
"cosmossdk.io/math"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
46 changes: 1 addition & 45 deletions x/gmp/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

0 comments on commit 2651ff2

Please sign in to comment.