rpc_transactions.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use log::error;
  19. use tinyjson::JsonValue;
  20. use darkfi::rpc::jsonrpc::{
  21. ErrorCode::{InternalError, InvalidParams},
  22. JsonError, JsonResponse, JsonResult,
  23. };
  24. use crate::BlockchainExplorer;
  25. impl BlockchainExplorer {
  26. // RPCAPI:
  27. // Queries the database to retrieve the transactions corresponding to the provided block header hash.
  28. // Returns the readable transactions upon success.
  29. //
  30. // **Params:**
  31. // * `array[0]`: `String` Block header hash
  32. //
  33. // **Returns:**
  34. // * Array of `TransactionRecord` encoded into a JSON.
  35. //
  36. // --> {"jsonrpc": "2.0", "method": "transactions.get_transactions_by_header_hash", "params": ["5cc...2f9"], "id": 1}
  37. // <-- {"jsonrpc": "2.0", "result": {...}, "id": 1}
  38. pub async fn transactions_get_transactions_by_header_hash(
  39. &self,
  40. id: u16,
  41. params: JsonValue,
  42. ) -> JsonResult {
  43. let params = params.get::<Vec<JsonValue>>().unwrap();
  44. if params.len() != 1 || !params[0].is_string() {
  45. return JsonError::new(InvalidParams, None, id).into()
  46. }
  47. let header_hash = params[0].get::<String>().unwrap();
  48. let transactions = match self.get_transactions_by_header_hash(header_hash) {
  49. Ok(v) => v,
  50. Err(e) => {
  51. error!(target: "blockchain-explorer::rpc_transactions::transactions_get_transaction_by_header_hash", "Failed fetching block transactions: {}", e);
  52. return JsonError::new(InternalError, None, id).into()
  53. }
  54. };
  55. let mut ret = vec![];
  56. for transaction in transactions {
  57. ret.push(transaction.to_json_array());
  58. }
  59. JsonResponse::new(JsonValue::Array(ret), id).into()
  60. }
  61. // RPCAPI:
  62. // Queries the database to retrieve the transaction corresponding to the provided hash.
  63. // Returns the readable transaction upon success.
  64. //
  65. // **Params:**
  66. // * `array[0]`: `String` Transaction hash
  67. //
  68. // **Returns:**
  69. // * `TransactionRecord` encoded into a JSON.
  70. //
  71. // --> {"jsonrpc": "2.0", "method": "transactions.get_transaction_by_hash", "params": ["7e7...b4d"], "id": 1}
  72. // <-- {"jsonrpc": "2.0", "result": {...}, "id": 1}
  73. pub async fn transactions_get_transaction_by_hash(
  74. &self,
  75. id: u16,
  76. params: JsonValue,
  77. ) -> JsonResult {
  78. let params = params.get::<Vec<JsonValue>>().unwrap();
  79. if params.len() != 1 || !params[0].is_string() {
  80. return JsonError::new(InvalidParams, None, id).into()
  81. }
  82. let transaction_hash = params[0].get::<String>().unwrap();
  83. let transaction = match self.get_transaction_by_hash(transaction_hash) {
  84. Ok(v) => v,
  85. Err(e) => {
  86. error!(target: "blockchain-explorer::rpc_transactions::transactions_get_transaction_by_hash", "Failed fetching transaction: {}", e);
  87. return JsonError::new(InternalError, None, id).into()
  88. }
  89. };
  90. JsonResponse::new(transaction.to_json_array(), id).into()
  91. }
  92. }