diff --git a/rpc/handlers.go b/rpc/handlers.go index 39f9f5a911..aed038d6d5 100644 --- a/rpc/handlers.go +++ b/rpc/handlers.go @@ -1340,6 +1340,15 @@ func (h *Handler) EstimateMessageFee(msg MsgFromL1, id BlockID) (*FeeEstimate, * return &estimates[0], nil } +func (h *Handler) LegacyEstimateMessageFee(msg MsgFromL1, id BlockID) (*FeeEstimate, *jsonrpc.Error) { //nolint:gocritic + estimate, rpcErr := h.EstimateMessageFee(msg, id) + if rpcErr != nil { + return nil, rpcErr + } + estimate.Unit = nil + return estimate, nil +} + // TraceTransaction returns the trace for a given executed transaction, including internal calls // // It follows the specification defined here: @@ -2007,7 +2016,7 @@ func (h *Handler) LegacyMethods() ([]jsonrpc.Method, string) { //nolint: funlen { Name: "starknet_estimateMessageFee", Params: []jsonrpc.Parameter{{Name: "message"}, {Name: "block_id"}}, - Handler: h.EstimateMessageFee, + Handler: h.LegacyEstimateMessageFee, }, { Name: "starknet_traceTransaction", diff --git a/rpc/handlers_test.go b/rpc/handlers_test.go index f40bcb5e0d..6394a75e43 100644 --- a/rpc/handlers_test.go +++ b/rpc/handlers_test.go @@ -3075,6 +3075,53 @@ func TestEstimateMessageFee(t *testing.T) { }, *estimateFee) } +func TestLegacyEstimateMessageFee(t *testing.T) { + mockCtrl := gomock.NewController(t) + t.Cleanup(mockCtrl.Finish) + + mockReader := mocks.NewMockReader(mockCtrl) + mockVM := mocks.NewMockVM(mockCtrl) + log := utils.NewNopZapLogger() + + handler := rpc.New(mockReader, nil, utils.Mainnet, nil, nil, mockVM, "", log) + msg := rpc.MsgFromL1{ + From: common.HexToAddress("0xDEADBEEF"), + To: *new(felt.Felt).SetUint64(1337), + Payload: []felt.Felt{*new(felt.Felt).SetUint64(1), *new(felt.Felt).SetUint64(2)}, + Selector: *new(felt.Felt).SetUint64(44), + } + + latestHeader := &core.Header{ + Number: 123, + Timestamp: 456, + GasPrice: new(felt.Felt).SetUint64(42), + } + mockState := mocks.NewMockStateHistoryReader(mockCtrl) + + mockReader.EXPECT().HeadState().Return(mockState, nopCloser, nil) + mockReader.EXPECT().HeadsHeader().Return(latestHeader, nil) + + expectedGasConsumed := new(felt.Felt).SetUint64(37) + mockVM.EXPECT().Execute(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), + gomock.Any(), utils.Mainnet, gomock.Any(), gomock.Any(), gomock.Any(), latestHeader.GasPrice, latestHeader.GasPriceSTRK, false).DoAndReturn( + func(txns []core.Transaction, declaredClasses []core.Class, blockNumber, blockTimestamp uint64, + sequencerAddress *felt.Felt, state core.StateReader, network utils.Network, paidFeesOnL1 []*felt.Felt, + skipChargeFee, skipValidate bool, gasPriceWei, gasPriceSTRK *felt.Felt, legacyTraceJson bool, + ) ([]*felt.Felt, []json.RawMessage, error) { + actualFee := new(felt.Felt).Mul(expectedGasConsumed, gasPriceWei) + return []*felt.Felt{actualFee}, []json.RawMessage{{}}, nil + }, + ) + + estimateFee, err := handler.LegacyEstimateMessageFee(msg, rpc.BlockID{Latest: true}) + require.Nil(t, err) + require.Equal(t, rpc.FeeEstimate{ + GasConsumed: expectedGasConsumed, + GasPrice: latestHeader.GasPrice, + OverallFee: new(felt.Felt).Mul(expectedGasConsumed, latestHeader.GasPrice), + }, *estimateFee) +} + func TestTraceTransaction(t *testing.T) { mockCtrl := gomock.NewController(t) t.Cleanup(mockCtrl.Finish)