diff --git a/protocol/messenger_store_node_request_manager.go b/protocol/messenger_store_node_request_manager.go index fe725093f..362e1767a 100644 --- a/protocol/messenger_store_node_request_manager.go +++ b/protocol/messenger_store_node_request_manager.go @@ -8,10 +8,11 @@ import ( "sync" "time" + "github.com/waku-org/go-waku/waku/v2/api/history" + gocommon "github.com/status-im/status-go/common" "github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/protocol/common/shard" - "github.com/waku-org/go-waku/waku/v2/api/history" "go.uber.org/zap" diff --git a/services/wallet/router/router_helper.go b/services/wallet/router/router_helper.go index 7a6c68bb3..404a4dcdc 100644 --- a/services/wallet/router/router_helper.go +++ b/services/wallet/router/router_helper.go @@ -14,6 +14,7 @@ import ( "github.com/status-im/status-go/contracts" gaspriceoracle "github.com/status-im/status-go/contracts/gas-price-oracle" "github.com/status-im/status-go/params" + "github.com/status-im/status-go/rpc/chain" "github.com/status-im/status-go/services/wallet/bigint" "github.com/status-im/status-go/services/wallet/collectibles" walletCommon "github.com/status-im/status-go/services/wallet/common" @@ -88,12 +89,16 @@ func (r *Router) estimateGasForApproval(params pathprocessor.ProcessorInputParam } func (r *Router) calculateApprovalL1Fee(amountIn *big.Int, chainID uint64, approvalContractAddress *common.Address) (uint64, error) { - data, err := walletCommon.PackApprovalInputData(amountIn, approvalContractAddress) + ethClient, err := r.rpcClient.EthClient(chainID) if err != nil { return 0, err } - ethClient, err := r.rpcClient.EthClient(chainID) + return CalculateApprovalL1Fee(amountIn, chainID, approvalContractAddress, ethClient) +} + +func CalculateApprovalL1Fee(amountIn *big.Int, chainID uint64, approvalContractAddress *common.Address, ethClient chain.ClientInterface) (uint64, error) { + data, err := walletCommon.PackApprovalInputData(amountIn, approvalContractAddress) if err != nil { return 0, err } @@ -108,10 +113,13 @@ func (r *Router) calculateApprovalL1Fee(amountIn *big.Int, chainID uint64, appro callOpt := &bind.CallOpts{} - l1FeeResult, _ := oracleContract.GetL1Fee(callOpt, data) - l1Fee = l1FeeResult.Uint64() + l1FeeResult, err := oracleContract.GetL1Fee(callOpt, data) + if err == nil { + l1Fee = l1FeeResult.Uint64() + } } + // return 0 if we failed to get the fee return l1Fee, nil } diff --git a/services/wallet/router/router_helper_test.go b/services/wallet/router/router_helper_test.go new file mode 100644 index 000000000..63c771d19 --- /dev/null +++ b/services/wallet/router/router_helper_test.go @@ -0,0 +1,86 @@ +package router_test + +import ( + "context" + "errors" + "math/big" + "testing" + + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + "go.uber.org/mock/gomock" + + "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/common" + + mock_client "github.com/status-im/status-go/rpc/chain/mock/client" + walletCommon "github.com/status-im/status-go/services/wallet/common" + "github.com/status-im/status-go/services/wallet/router" +) + +type CalculateFeesTestSuite struct { + suite.Suite + ethClient *mock_client.MockClientInterface // Mock client implementing ContractCaller + mockCtrl *gomock.Controller + chainID uint64 +} + +func (s *CalculateFeesTestSuite) SetupTest() { + s.mockCtrl = gomock.NewController(s.T()) + s.ethClient = mock_client.NewMockClientInterface(s.mockCtrl) + s.chainID = walletCommon.OptimismMainnet +} + +func (s *CalculateFeesTestSuite) TearDownTest() { + s.mockCtrl.Finish() +} + +func (s *CalculateFeesTestSuite) TestCalculateApprovalL1Fee_Success() { + // Test inputs + amountIn := big.NewInt(1000) + approvalContractAddress := common.HexToAddress("0xApprovalAddress") + expectedFee := big.NewInt(500) + + // Prepare mock return data + expectedReturnData := expectedFee.FillBytes(make([]byte, 32)) // Mocked return as ABI encoded uint256 + + // Mock CallContract to simulate contract interaction + s.ethClient.EXPECT(). + CallContract(gomock.Any(), gomock.Any(), gomock.Nil()). + DoAndReturn(func(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) { + // Check that the call message matches expectations + require.NotEmpty(s.T(), call.Data) + + // Return encoded data + return expectedReturnData, nil + }) + + // Call the function + fee, err := router.CalculateApprovalL1Fee(amountIn, s.chainID, &approvalContractAddress, s.ethClient) + + // Assertions + require.NoError(s.T(), err) + require.Equal(s.T(), expectedFee.Uint64(), fee) +} + +func (s *CalculateFeesTestSuite) TestCalculateApprovalL1Fee_ZeroFeeOnContractCallError() { + // Test inputs + amountIn := big.NewInt(1000) + approvalContractAddress := common.HexToAddress("0xApprovalAddress") + + // Mock CallContract to return an error + s.ethClient.EXPECT(). + CallContract(gomock.Any(), gomock.Any(), gomock.Nil()). + Return(nil, errors.New("contract call failed")) + + // Call the function + fee, err := router.CalculateApprovalL1Fee(amountIn, s.chainID, &approvalContractAddress, s.ethClient) + + // Assertions + require.Nil(s.T(), err) + require.Equal(s.T(), uint64(0), fee) +} + +func TestCalculateFeesTestSuite(t *testing.T) { + suite.Run(t, new(CalculateFeesTestSuite)) +} diff --git a/wakuv2/history_processor_wrapper.go b/wakuv2/history_processor_wrapper.go index 25cee9e8f..eba4b3809 100644 --- a/wakuv2/history_processor_wrapper.go +++ b/wakuv2/history_processor_wrapper.go @@ -3,9 +3,10 @@ package wakuv2 import ( "github.com/libp2p/go-libp2p/core/peer" - "github.com/status-im/status-go/wakuv2/common" "github.com/waku-org/go-waku/waku/v2/api/history" "github.com/waku-org/go-waku/waku/v2/protocol" + + "github.com/status-im/status-go/wakuv2/common" ) type HistoryProcessorWrapper struct {