rpc_transactions.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 darkfi_sdk::tx::TransactionHash;
  25. use crate::Explorerd;
  26. impl Explorerd {
  27. // RPCAPI:
  28. // Queries the database to retrieve the transactions corresponding to the provided block header hash.
  29. // Returns the readable transactions upon success.
  30. //
  31. // **Params:**
  32. // * `array[0]`: `String` Block header hash
  33. //
  34. // **Returns:**
  35. // * Array of `TransactionRecord` encoded into a JSON.
  36. //
  37. // --> {"jsonrpc": "2.0", "method": "transactions.get_transactions_by_header_hash", "params": ["5cc...2f9"], "id": 1}
  38. // <-- {"jsonrpc": "2.0", "result": {...}, "id": 1}
  39. pub async fn transactions_get_transactions_by_header_hash(
  40. &self,
  41. id: u16,
  42. params: JsonValue,
  43. ) -> JsonResult {
  44. let params = params.get::<Vec<JsonValue>>().unwrap();
  45. if params.len() != 1 || !params[0].is_string() {
  46. return JsonError::new(InvalidParams, None, id).into()
  47. }
  48. let header_hash = params[0].get::<String>().unwrap();
  49. let transactions = match self.db.get_transactions_by_header_hash(header_hash) {
  50. Ok(v) => v,
  51. Err(e) => {
  52. error!(target: "blockchain-explorer::rpc_transactions::transactions_get_transaction_by_header_hash", "Failed fetching block transactions: {}", e);
  53. return JsonError::new(InternalError, None, id).into()
  54. }
  55. };
  56. let mut ret = vec![];
  57. for transaction in transactions {
  58. ret.push(transaction.to_json_array());
  59. }
  60. JsonResponse::new(JsonValue::Array(ret), id).into()
  61. }
  62. // RPCAPI:
  63. // Queries the database to retrieve the transaction corresponding to the provided hash.
  64. // Returns the readable transaction upon success.
  65. //
  66. // **Params:**
  67. // * `array[0]`: `String` Transaction hash
  68. //
  69. // **Returns:**
  70. // * `TransactionRecord` encoded into a JSON.
  71. //
  72. // --> {"jsonrpc": "2.0", "method": "transactions.get_transaction_by_hash", "params": ["7e7...b4d"], "id": 1}
  73. // <-- {"jsonrpc": "2.0", "result": {...}, "id": 1}
  74. pub async fn transactions_get_transaction_by_hash(
  75. &self,
  76. id: u16,
  77. params: JsonValue,
  78. ) -> JsonResult {
  79. let params = params.get::<Vec<JsonValue>>().unwrap();
  80. if params.len() != 1 || !params[0].is_string() {
  81. return JsonError::new(InvalidParams, None, id).into()
  82. }
  83. // Validate provided hash and store it for later use
  84. let tx_hash_str = params[0].get::<String>().unwrap();
  85. let tx_hash = match tx_hash_str.parse::<TransactionHash>() {
  86. Ok(hash) => hash,
  87. Err(e) => return JsonError::new(InternalError, Some(e.to_string()), id).into(),
  88. };
  89. // Retrieve transaction by hash and return result
  90. match self.db.get_transaction_by_hash(&tx_hash) {
  91. Ok(Some(transaction)) => JsonResponse::new(transaction.to_json_array(), id).into(),
  92. Ok(None) => JsonResponse::new(JsonValue::Array(vec![]), id).into(),
  93. Err(e) => {
  94. error!(target: "blockchain-explorer::rpc_transactions::transactions_get_transaction_by_hash", "Failed fetching transaction: {}", e);
  95. JsonError::new(InternalError, None, id).into()
  96. }
  97. }
  98. }
  99. }