Skip to content

Commit

Permalink
add MakePayment to e2e test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
adamewozniak committed Oct 2, 2024
1 parent 14e6e9f commit e26d2f4
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 3 deletions.
23 changes: 23 additions & 0 deletions client/query/gmp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package query

import (
"context"

gmptypes "github.com/ojo-network/ojo/x/gmp/types"
)

func (c *Client) GmpQueryClient() gmptypes.QueryClient {
return gmptypes.NewQueryClient(c.grpcConn)
}

func (c *Client) QueryPayments() (*gmptypes.AllPaymentsResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), queryTimeout)
defer cancel()

queryResponse, err := c.GmpQueryClient().AllPayments(
ctx,
&gmptypes.AllPaymentsRequest{},
)

return queryResponse, err
}
26 changes: 26 additions & 0 deletions client/tx/gmp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package tx

import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
gmptypes "github.com/ojo-network/ojo/x/gmp/types"
)

// TxCreatePayment creates a gmp payment transaction
func (c *Client) TxCreatePayment() (*sdk.TxResponse, error) {
fromAddr, err := c.keyringRecord.GetAddress()
if err != nil {
return nil, err
}

msg := gmptypes.NewMsgCreatePayment(
fromAddr.String(),
"Arbitrum",
"BTC",
sdk.NewCoin("uojo", math.NewInt(10_000_000_000)),
math.LegacyOneDec(),
100,
)

return c.BroadcastTx(msg)
}
27 changes: 27 additions & 0 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,30 @@ func (s *IntegrationTestSuite) TestClaimAirdrop() {

s.Require().Equal(airdropAccount.ClaimAmount, amount.Uint64())
}

func (s *IntegrationTestSuite) TestPaymentTx() {
// wait for exchange rates to be populated
ratesFound := false
for i := 0; i < 10; i++ {
rates, err := s.orchestrator.OjoClient.QueryClient.QueryExchangeRates()
if err == nil && len(rates) > 0 {
ratesFound = true
break
}
time.Sleep(time.Second * 2)
}
s.Require().True(ratesFound)

c := s.orchestrator.OjoClient
payment, err := c.TxClient.TxCreatePayment()
s.Require().NoError(err)
s.Require().Equal(payment.Code, uint32(0))

// sleep to make sure payment is created
time.Sleep(time.Second * 6)

// check to make sure payment was created
payments, err := c.QueryClient.QueryPayments()
s.Require().NoError(err)
s.Require().Equal(len(payments.Payments), 1)
}
5 changes: 2 additions & 3 deletions x/gmp/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,17 @@ func (am AppModule) EndBlock(goCtx context.Context) ([]abci.ValidatorUpdate, err
for _, payment := range payments {
rate, err := am.oracleKeeper.GetExchangeRate(ctx, payment.Denom)
if err != nil {
ctx.Logger().Error("failed to get exchange rate while processing payment", "error", err)
continue
}
// if the last price has deviated by more than the deviation percentage, trigger an update
if payment.LastPrice.Sub(rate).Abs().GT(payment.Deviation) ||
payment.LastBlock < ctx.BlockHeight()-payment.Heartbeat {
if payment.TriggerUpdate(rate, ctx) {
err := am.keeper.ProcessPayment(goCtx, payment)
if err != nil {
ctx.Logger().Error("failed to process payment", "error", err)
continue
}
}

}
return []abci.ValidatorUpdate{}, nil
}
Expand Down
11 changes: 11 additions & 0 deletions x/gmp/types/payment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package types

import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (p Payment) TriggerUpdate(rate math.LegacyDec, ctx sdk.Context) bool {
return p.LastPrice.Sub(rate).Abs().GT(p.Deviation) ||
p.LastBlock < ctx.BlockHeight()-p.Heartbeat
}
62 changes: 62 additions & 0 deletions x/gmp/types/payments_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package types

import (
"testing"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
)

func TestPayment_TriggerUpdate(t *testing.T) {
tests := []struct {
name string
payment Payment
rate math.LegacyDec
ctx sdk.Context
want bool
}{
{
name: "should trigger update - price deviation",
payment: Payment{
LastPrice: math.LegacyMustNewDecFromStr("100"),
Deviation: math.LegacyMustNewDecFromStr("1"),
Heartbeat: 100,
LastBlock: 1,
},
rate: math.LegacyMustNewDecFromStr("101.1"),
ctx: sdk.Context{}.WithBlockHeight(1),
want: true,
},
{
name: "should not trigger update - no expiration",
payment: Payment{
LastPrice: math.LegacyMustNewDecFromStr("100"),
Deviation: math.LegacyMustNewDecFromStr("1"),
Heartbeat: 100,
LastBlock: 100,
},
rate: math.LegacyMustNewDecFromStr("101"),
ctx: sdk.Context{},
want: false,
},
{
name: "should trigger update - heartbeat expired",
payment: Payment{
LastPrice: math.LegacyMustNewDecFromStr("100"),
Deviation: math.LegacyMustNewDecFromStr("1"),
Heartbeat: 100,
LastBlock: 1,
},
rate: math.LegacyMustNewDecFromStr("100"),
ctx: sdk.Context{}.WithBlockHeight(102),
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.payment.TriggerUpdate(tt.rate, tt.ctx)
require.Equal(t, tt.want, got)
})
}
}

0 comments on commit e26d2f4

Please sign in to comment.