Skip to content

Commit

Permalink
refactor: add null check for return value of GetDefaultOracleQuote (#939
Browse files Browse the repository at this point in the history
)
  • Loading branch information
zakir-code authored Jan 22, 2025
1 parent 1cb185c commit 1f3ec77
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 1 deletion.
6 changes: 6 additions & 0 deletions contract/bridge_fee_quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func (k BridgeFeeQuoteKeeper) GetDefaultOracleQuote(ctx context.Context, chainNa
if err := k.QueryContract(sdk.UnwrapSDKContext(ctx), k.from, k.contract, k.abi, "getDefaultOracleQuote", &res, chainName, tokenName); err != nil {
return nil, err
}
for i := 0; i < len(res.Quotes); i++ {
if res.Quotes[i].Id.Sign() <= 0 {
res.Quotes = append(res.Quotes[:i], res.Quotes[i+1:]...)
i--
}
}
return res.Quotes, nil
}

Expand Down
20 changes: 20 additions & 0 deletions contract/bridge_fee_quote_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package contract_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/pundiai/fx-core/v8/contract"
"github.com/pundiai/fx-core/v8/testutil/helpers"
fxtypes "github.com/pundiai/fx-core/v8/types"
ethtypes "github.com/pundiai/fx-core/v8/x/eth/types"
)

func TestBridgeFeeQuoteKeeper_GetDefaultOracleQuote(t *testing.T) {
app, ctx := helpers.NewAppWithValNumber(t, 1)
keeper := contract.NewBridgeFeeQuoteKeeper(app.EvmKeeper)
quote, err := keeper.GetDefaultOracleQuote(ctx, contract.MustStrToByte32(ethtypes.ModuleName), contract.MustStrToByte32(fxtypes.DefaultDenom))
require.NoError(t, err)
require.Empty(t, quote)
}
2 changes: 2 additions & 0 deletions precompiles/crosschain/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type CrosschainPrecompileTestSuite struct {
chainName string

helpers.CrosschainPrecompileSuite
bridgeFeeSuite helpers.BridgeFeeSuite
}

func TestCrosschainPrecompileTestSuite(t *testing.T) {
Expand Down Expand Up @@ -54,6 +55,7 @@ func (suite *CrosschainPrecompileTestSuite) SetupTest() {
}

suite.CrosschainPrecompileSuite = helpers.NewCrosschainPrecompileSuite(suite.Require(), suite.signer, suite.App.EvmKeeper, suite.crosschainAddr)
suite.bridgeFeeSuite = helpers.NewBridgeFeeSuite(suite.Require(), suite.App.EvmKeeper)

chainNames := fxtypes.GetSupportChains()
suite.chainName = chainNames[tmrand.Intn(len(chainNames))]
Expand Down
10 changes: 10 additions & 0 deletions precompiles/crosschain/crosschain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package crosschain_test
import (
"math/big"
"testing"
"time"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -30,6 +31,15 @@ func (suite *CrosschainPrecompileTestSuite) TestContract_Crosschain() {
suite.App.CrosschainKeepers.GetKeeper(suite.chainName).
SetLastObservedBlockHeight(suite.Ctx, 100, 100)

suite.bridgeFeeSuite.Quote(suite.Ctx, contract.IBridgeFeeQuoteQuoteInput{
Cap: 0,
GasLimit: 21000,
Expiry: uint64(time.Now().Add(time.Hour).Unix()),
ChainName: contract.MustStrToByte32(suite.chainName),
TokenName: contract.MustStrToByte32(fxtypes.DefaultDenom),
Amount: big.NewInt(1),
})

balance := suite.Balance(suite.signer.AccAddress())

txResponse := suite.Crosschain(suite.Ctx, big.NewInt(2), suite.signer.Address(),
Expand Down
49 changes: 49 additions & 0 deletions testutil/helpers/bridge_fee_suite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package helpers

import (
"context"

evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/stretchr/testify/require"

"github.com/pundiai/fx-core/v8/contract"
)

type BridgeFeeSuite struct {
require *require.Assertions
err error

contract.BridgeFeeQuoteKeeper
contract.BridgeFeeOracleKeeper
}

func NewBridgeFeeSuite(require *require.Assertions, caller contract.Caller) BridgeFeeSuite {
return BridgeFeeSuite{
require: require,
BridgeFeeQuoteKeeper: contract.NewBridgeFeeQuoteKeeper(caller),
BridgeFeeOracleKeeper: contract.NewBridgeFeeOracleKeeper(caller),
}
}

func (s BridgeFeeSuite) WithError(err error) BridgeFeeSuite {
suite := s
suite.err = err
return suite
}

func (s BridgeFeeSuite) requireError(err error) {
if s.err != nil {
s.require.ErrorIs(err, evmtypes.ErrVMExecution.Wrap(s.err.Error()))
return
}
s.require.NoError(err)
}

func (s BridgeFeeSuite) Quote(ctx context.Context, args contract.IBridgeFeeQuoteQuoteInput) *evmtypes.MsgEthereumTxResponse {
defOracle, err := s.BridgeFeeOracleKeeper.DefaultOracle(ctx)
s.require.NoError(err)

res, err := s.BridgeFeeQuoteKeeper.Quote(ctx, defOracle, []contract.IBridgeFeeQuoteQuoteInput{args})
s.requireError(err)
return res
}
5 changes: 4 additions & 1 deletion x/crosschain/keeper/outgoing_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ func (k Keeper) BuildOutgoingTxBatch(ctx sdk.Context, caller contract.Caller, se
}
var quoteInfo *contract.IBridgeFeeQuoteQuoteInfo
for _, quote := range quoteInfos {
if quote.Id.Sign() <= 0 {
continue
}
if fee.Amount.GTE(sdkmath.NewIntFromBigInt(quote.Amount)) && !quote.IsTimeout(ctx.BlockTime()) {
quoteInfo = &quote
break
}
}
if quoteInfo == nil {
if quoteInfo == nil || quoteInfo.Id.Sign() <= 0 {
return 0, types.ErrInvalid.Wrapf("bridge fee is too small or expired")
}

Expand Down

0 comments on commit 1f3ec77

Please sign in to comment.