Skip to content

Commit 2651ff2

Browse files
authored
feat: gmp handling create payment (#529)
1 parent b16218c commit 2651ff2

File tree

3 files changed

+78
-60
lines changed

3 files changed

+78
-60
lines changed

app/gmpmiddleware/handler.go

+25-15
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,24 @@ package gmpmiddleware
33
import (
44
"context"
55

6+
"cosmossdk.io/math"
67
sdk "github.com/cosmos/cosmos-sdk/types"
78
"github.com/ojo-network/ojo/x/gmp/types"
89
)
910

11+
// default values for new payments coming from axelar
12+
const (
13+
blocksPerDay = 14400
14+
)
15+
16+
var defaultDeviation = math.LegacyMustNewDecFromStr("1.0")
17+
1018
type GmpKeeper interface {
11-
RelayPrice(
12-
goCtx context.Context,
13-
msg *types.MsgRelayPrice,
14-
) (*types.MsgRelayPriceResponse, error)
1519
GetParams(ctx sdk.Context) (params types.Params)
20+
CreatePayment(
21+
goCtx context.Context,
22+
msg *types.MsgCreatePayment,
23+
) (*types.MsgCreatePaymentResponse, error)
1624
}
1725

1826
type GmpHandler struct {
@@ -56,22 +64,24 @@ func (h GmpHandler) HandleGeneralMessage(
5664
return err
5765
}
5866
ctx.Logger().Info("HandleGeneralMessage GMP Decoder", "msg", msg)
59-
tx := &types.MsgRelayPrice{
60-
Relayer: h.relayer,
61-
DestinationChain: srcChain,
62-
ClientContractAddress: msg.ContractAddress.Hex(),
63-
OjoContractAddress: srcAddress,
64-
Denoms: msg.GetDenoms(),
65-
CommandSelector: msg.CommandSelector[:],
66-
CommandParams: msg.CommandParams,
67-
Timestamp: msg.Timestamp.Int64(),
68-
Token: coin,
67+
denoms := msg.GetDenoms()
68+
69+
tx := &types.MsgCreatePayment{
70+
Relayer: h.relayer,
71+
Payment: &types.Payment{
72+
Relayer: h.relayer,
73+
DestinationChain: srcChain,
74+
Denom: denoms[0],
75+
Token: coin,
76+
Deviation: defaultDeviation,
77+
Heartbeat: blocksPerDay,
78+
},
6979
}
7080
err = tx.ValidateBasic()
7181
if err != nil {
7282
return err
7383
}
7484
ctx.Logger().Info("HandleGeneralMessage GMP Decoder", "tx", tx)
75-
_, err = h.gmp.RelayPrice(ctx, tx)
85+
_, err = h.gmp.CreatePayment(ctx, tx)
7686
return err
7787
}

x/gmp/keeper/keeper.go

+52
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"math/big"
88
"time"
99

10+
"cosmossdk.io/errors"
1011
"cosmossdk.io/log"
1112
"cosmossdk.io/math"
1213
"github.com/ethereum/go-ethereum/common"
@@ -21,6 +22,7 @@ import (
2122
"github.com/ojo-network/ojo/app/ibctransfer"
2223
"github.com/ojo-network/ojo/util"
2324
"github.com/ojo-network/ojo/x/gmp/types"
25+
oracletypes "github.com/ojo-network/ojo/x/oracle/types"
2426
)
2527

2628
type Keeper struct {
@@ -321,3 +323,53 @@ func (k Keeper) ProcessPayment(
321323

322324
return nil
323325
}
326+
327+
func (k Keeper) CreatePayment(
328+
goCtx context.Context,
329+
msg *types.MsgCreatePayment,
330+
) (*types.MsgCreatePaymentResponse, error) {
331+
ctx := sdk.UnwrapSDKContext(goCtx)
332+
333+
// make sure the destination chain is valid
334+
gasEstimateParams := k.GasEstimateKeeper.GetParams(ctx)
335+
isValidChain := false
336+
for _, chain := range gasEstimateParams.ContractRegistry {
337+
if chain.Network == msg.Payment.DestinationChain {
338+
isValidChain = true
339+
break
340+
}
341+
}
342+
if !isValidChain {
343+
return nil, errors.Wrapf(
344+
types.ErrInvalidDestinationChain,
345+
"destination chain %s not found in contract registry",
346+
msg.Payment.DestinationChain,
347+
)
348+
}
349+
350+
// make sure the denom is active in the oracle
351+
_, err := k.oracleKeeper.GetExchangeRate(ctx, msg.Payment.Denom)
352+
if err != nil {
353+
return nil, errors.Wrapf(
354+
oracletypes.ErrUnknownDenom,
355+
"denom %s not active in the oracle",
356+
msg.Payment.Denom,
357+
)
358+
}
359+
360+
// send tokens from msg to the module account
361+
address, err := sdk.AccAddressFromBech32(msg.Relayer)
362+
if err != nil {
363+
return nil, err
364+
}
365+
coins := sdk.NewCoins(msg.Payment.Token)
366+
err = k.BankKeeper.SendCoinsFromAccountToModule(ctx, address, types.ModuleName, coins)
367+
if err != nil {
368+
return nil, err
369+
}
370+
371+
// Create a payment record in the KV store
372+
msg.Payment.Relayer = msg.Relayer
373+
k.SetPayment(ctx, *msg.Payment)
374+
return &types.MsgCreatePaymentResponse{}, nil
375+
}

x/gmp/keeper/msg_server.go

+1-45
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
1010
"github.com/ojo-network/ojo/x/gmp/types"
11-
oracletypes "github.com/ojo-network/ojo/x/oracle/types"
1211
)
1312

1413
type msgServer struct {
@@ -57,48 +56,5 @@ func (ms msgServer) CreatePayment(
5756
goCtx context.Context,
5857
msg *types.MsgCreatePayment,
5958
) (*types.MsgCreatePaymentResponse, error) {
60-
ctx := sdk.UnwrapSDKContext(goCtx)
61-
62-
// make sure the destination chain is valid
63-
gasEstimateParams := ms.keeper.GasEstimateKeeper.GetParams(ctx)
64-
isValidChain := false
65-
for _, chain := range gasEstimateParams.ContractRegistry {
66-
if chain.Network == msg.Payment.DestinationChain {
67-
isValidChain = true
68-
break
69-
}
70-
}
71-
if !isValidChain {
72-
return nil, errors.Wrapf(
73-
types.ErrInvalidDestinationChain,
74-
"destination chain %s not found in contract registry",
75-
msg.Payment.DestinationChain,
76-
)
77-
}
78-
79-
// make sure the denom is active in the oracle
80-
_, err := ms.keeper.oracleKeeper.GetExchangeRate(ctx, msg.Payment.Denom)
81-
if err != nil {
82-
return nil, errors.Wrapf(
83-
oracletypes.ErrUnknownDenom,
84-
"denom %s not active in the oracle",
85-
msg.Payment.Denom,
86-
)
87-
}
88-
89-
// send tokens from msg to the module account
90-
address, err := sdk.AccAddressFromBech32(msg.Relayer)
91-
if err != nil {
92-
return nil, err
93-
}
94-
coins := sdk.NewCoins(msg.Payment.Token)
95-
err = ms.keeper.BankKeeper.SendCoinsFromAccountToModule(ctx, address, types.ModuleName, coins)
96-
if err != nil {
97-
return nil, err
98-
}
99-
100-
// Create a payment record in the KV store
101-
msg.Payment.Relayer = msg.Relayer
102-
ms.keeper.SetPayment(ctx, *msg.Payment)
103-
return &types.MsgCreatePaymentResponse{}, nil
59+
return ms.keeper.CreatePayment(goCtx, msg)
10460
}

0 commit comments

Comments
 (0)