|
1 | | -use std::sync::Arc; |
2 | | -use crate::{ |
3 | | - error::{ApiError, Error, NotFoundKind}, |
4 | | - model::RawTx, |
5 | | - repository::RepositoryOps, |
6 | | - storage::SortOrder, |
7 | | - Result, |
8 | | -}; |
9 | | -use super::{ |
10 | | - query::PaginationQuery, |
11 | | - response::{ApiPagedResponse, Response}, |
12 | | - AppContext, |
13 | | -}; |
| 1 | +use std::{str::FromStr, sync::Arc}; |
| 2 | + |
| 3 | +use ain_macros::ocean_endpoint; |
14 | 4 | use axum::{ |
15 | | - extract::{Path, Query}, |
16 | | - routing::{get,post}, |
| 5 | + extract::Path, |
| 6 | + response::Json, |
| 7 | + routing::{get, post}, |
17 | 8 | Extension, Router, |
18 | 9 | }; |
19 | | -use defichain_rpc::{Client, RpcApi}; |
| 10 | +use bitcoin::{consensus::encode::deserialize, Transaction, Txid}; |
| 11 | +use defichain_rpc::{ |
| 12 | + json::{GetTransactionResult, TestMempoolAcceptResult}, |
| 13 | + RpcApi, |
| 14 | +}; |
| 15 | +use rust_decimal::Decimal; |
| 16 | +use rust_decimal_macros::dec; |
20 | 17 |
|
21 | | -use ain_macros::ocean_endpoint; |
| 18 | +use super::{response::Response, AppContext}; |
| 19 | +use crate::{api::response::ApiPagedResponse, error::ApiError, model::RawTxDto, Result}; |
| 20 | +const DEFAULT_MAX_FEE_RATE: Decimal = dec!(0.1); |
22 | 21 |
|
23 | 22 | #[ocean_endpoint] |
24 | 23 | async fn send_rawtx( |
25 | | - Path(tx): Path<RawTx>, |
| 24 | + Path(tx): Path<RawTxDto>, |
26 | 25 | Extension(ctx): Extension<Arc<AppContext>>, |
27 | 26 | ) -> Result<Response<String>> { |
28 | | - |
29 | | - Ok(Response::new("Sending raw transaction".to_string())) |
| 27 | + let mut tx = tx.clone(); |
| 28 | + if tx.max_fee_rate.is_none() { |
| 29 | + tx.max_fee_rate = Some(DEFAULT_MAX_FEE_RATE); |
| 30 | + }; |
| 31 | + let trx = defichain_rpc::RawTx::raw_hex(tx.hex); |
| 32 | + let tx_hash = ctx.client.send_raw_transaction(trx).await?; |
| 33 | + Ok(Response::new(tx_hash.to_string())) |
30 | 34 | } |
31 | 35 |
|
32 | 36 | #[ocean_endpoint] |
33 | 37 | async fn test_rawtx( |
34 | | - Path(tx): Path<RawTx>, |
| 38 | + Path(tx): Path<RawTxDto>, |
35 | 39 | Extension(ctx): Extension<Arc<AppContext>>, |
36 | | -) -> Result<Response<String>> { |
37 | | - |
38 | | -Ok(Response::new("Testing raw transaction".to_string())) |
| 40 | +) -> Result<Response<Vec<TestMempoolAcceptResult>>> { |
| 41 | + let mut tx = tx.clone(); |
| 42 | + if tx.max_fee_rate.is_none() { |
| 43 | + tx.max_fee_rate = Some(DEFAULT_MAX_FEE_RATE); |
| 44 | + }; |
| 45 | + let trx = defichain_rpc::RawTx::raw_hex(tx.hex); |
| 46 | + let mempool_tx = ctx.client.test_mempool_accept(&[trx]).await?; |
| 47 | + Ok(Response::new(mempool_tx)) |
39 | 48 | } |
40 | 49 | #[ocean_endpoint] |
41 | | -async fn get_rawtx( |
| 50 | +async fn get_raw_tx( |
42 | 51 | Path(txid): Path<String>, |
43 | 52 | Extension(ctx): Extension<Arc<AppContext>>, |
44 | | -) -> Result<Response<String>> { |
45 | | - format!("Details of raw transaction with txid {}", txid); |
46 | | - Ok(Response::new("Testing raw transaction".to_string())) |
| 53 | +) -> Result<Response<GetTransactionResult>> { |
| 54 | + format!("Details of raw transaction with txid {}", txid); |
| 55 | + let tx_hash = Txid::from_str(&txid)?; |
| 56 | + let tx_result = ctx.client.get_transaction(&tx_hash, Some(true)).await?; |
| 57 | + Ok(Response::new(tx_result)) |
47 | 58 | } |
48 | 59 |
|
49 | | -async fn validate(hex:String) { |
50 | | - if !hex.starts_with("040000000001") { |
51 | | - return |
| 60 | +async fn validate(hex: String) { |
| 61 | + if !hex.starts_with("040000000001") { |
| 62 | + return; |
| 63 | + } |
| 64 | + let buffer = hex::decode(hex).expect("Decoding failed"); |
| 65 | + let transaction: Transaction = deserialize(&buffer).expect("Failed to deserialize transaction"); |
| 66 | + if transaction.output.len() != 2 { |
| 67 | + return; |
52 | 68 | } |
53 | | - |
54 | 69 | } |
55 | 70 |
|
56 | 71 | pub fn router(ctx: Arc<AppContext>) -> Router { |
57 | 72 | Router::new() |
58 | 73 | .route("/send", post(send_rawtx)) |
59 | 74 | .route("/test", get(test_rawtx)) |
60 | | - .route("/:txid", get(get_rawtx)) |
| 75 | + // .route("/:txid", get(get_raw_tx)) |
61 | 76 | .layer(Extension(ctx)) |
62 | 77 | } |
0 commit comments