|
39 | 39 | #include <silkrpc/types/block.hpp> |
40 | 40 | #include <silkrpc/types/call.hpp> |
41 | 41 | #include <silkrpc/types/filter.hpp> |
| 42 | +#include <silkrpc/types/transaction.hpp> |
42 | 43 |
|
43 | 44 | namespace silkrpc::commands { |
44 | 45 |
|
@@ -456,12 +457,36 @@ asio::awaitable<void> EthereumRpcApi::handle_eth_get_transaction_by_hash(const n |
456 | 457 |
|
457 | 458 | // https://eth.wiki/json-rpc/API#eth_gettransactionbyblockhashandindex |
458 | 459 | asio::awaitable<void> EthereumRpcApi::handle_eth_get_transaction_by_block_hash_and_index(const nlohmann::json& request, nlohmann::json& reply) { |
| 460 | + auto params = request["params"]; |
| 461 | + if (params.size() != 2) { |
| 462 | + auto error_msg = "invalid eth_getTransactionByBlockHashAndIndex params: " + params.dump(); |
| 463 | + SILKRPC_ERROR << error_msg << "\n"; |
| 464 | + reply = make_json_error(request["id"], 100, error_msg); |
| 465 | + co_return; |
| 466 | + } |
| 467 | + auto block_hash = params[0].get<evmc::bytes32>(); |
| 468 | + auto index_string = params[1].get<std::string>(); |
| 469 | + SILKRPC_DEBUG << "block_hash: " << block_hash << " index: " << index_string << "\n"; |
| 470 | + |
459 | 471 | auto tx = co_await database_->begin(); |
460 | 472 |
|
461 | 473 | try { |
462 | 474 | ethdb::TransactionDatabase tx_database{*tx}; |
| 475 | + const auto block_with_hash = co_await core::rawdb::read_block_by_hash(tx_database, block_hash); |
463 | 476 |
|
464 | | - reply = make_json_content(request["id"], "0x" + to_hex_no_leading_zeros(0)); |
| 477 | + const auto transactions = block_with_hash.block.transactions; |
| 478 | + |
| 479 | + auto index = std::stoul(index_string, 0, 16); |
| 480 | + if (index >= transactions.size()) { |
| 481 | + const auto error_msg = "Requested transaction not found " + index_string; |
| 482 | + SILKRPC_DEBUG << error_msg << "\n"; |
| 483 | + reply = make_json_content(request["id"], nullptr); |
| 484 | + co_return; |
| 485 | + } |
| 486 | + |
| 487 | + silkrpc::Transaction new_transaction{{transactions[index]}, {block_with_hash.hash}, {block_with_hash.block.header.number}, {index}}; |
| 488 | + |
| 489 | + reply = make_json_content(request["id"], new_transaction); |
465 | 490 | } catch (const std::exception& e) { |
466 | 491 | SILKRPC_ERROR << "exception: " << e.what() << "\n"; |
467 | 492 | reply = make_json_error(request["id"], 100, e.what()); |
|
0 commit comments