diff --git a/ethereum/rpc/namespaces/eth/api.go b/ethereum/rpc/namespaces/eth/api.go index b58e7f4e..729484e8 100644 --- a/ethereum/rpc/namespaces/eth/api.go +++ b/ethereum/rpc/namespaces/eth/api.go @@ -604,7 +604,54 @@ func (e *PublicAPI) GetTransactionByHash(hash common.Hash) (*rpctypes.RPCTransac res, err := e.GetTxByEthHash(hash) if err != nil { e.logger.WithError(err).Debugln("tx not found", "hash", hash.Hex()) - return nil, nil + + // try to find tx in mempool + txs, err := e.backend.PendingTransactions() + if err != nil { + return nil, nil + } + + for _, tx := range txs { + if tx == nil { + return nil, fmt.Errorf("invalid tx in mempool") + } + + if len((*tx).GetMsgs()) != 1 { + continue + } + msg, ok := (*tx).GetMsgs()[0].(*evmtypes.MsgEthereumTx) + if !ok { + continue + } + + txhash := msg.AsTransaction().Hash() + if txhash != hash { + continue + } + + from, err := msg.GetSender(e.chainIDEpoch) + if err != nil { + return nil, err + } + + data, err := evmtypes.UnpackTxData(msg.Data) + if err != nil { + return nil, fmt.Errorf("failed to unpack tx data: %w", err) + } + + rpctx, err := rpctypes.NewTransactionFromData( + data, + from, + hash, + common.Hash{}, + uint64(0), + uint64(0), + ) + if err != nil { + return nil, err + } + return rpctx, nil + } } resBlock, err := e.clientCtx.Client.Block(e.ctx, &res.Height) diff --git a/tests/rpc/rpc_pending_test.go b/tests/rpc/rpc_pending_test.go index 474151c7..656e0051 100644 --- a/tests/rpc/rpc_pending_test.go +++ b/tests/rpc/rpc_pending_test.go @@ -38,6 +38,7 @@ import ( func TestEth_Pending_GetBalance(t *testing.T) { var res hexutil.Big + var resTxHash common.Hash rpcRes := Call(t, "eth_getBalance", []string{addrA, "latest"}) err := res.UnmarshalJSON(rpcRes.Result) require.NoError(t, err) @@ -65,6 +66,12 @@ func TestEth_Pending_GetBalance(t *testing.T) { rpcRes = Call(t, "eth_sendTransaction", param) require.Nil(t, rpcRes.Error) + err = resTxHash.UnmarshalJSON(rpcRes.Result) + require.NoError(t, err) + + rpcRes = Call(t, "eth_getTransactionByHash", []string{resTxHash.Hex()}) + require.Nil(t, rpcRes.Error) + rpcRes = Call(t, "eth_getBalance", []string{addrA, "pending"}) err = res.UnmarshalJSON(rpcRes.Result) require.NoError(t, err)