Skip to content

Commit 7aeb6cd

Browse files
committed
updated api for raw_trasaction
1 parent 180c77d commit 7aeb6cd

6 files changed

Lines changed: 58 additions & 46 deletions

File tree

lib/ain-ocean/src/api/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ use axum::{extract::Request, http::StatusCode, response::IntoResponse, Json, Rou
44

55
// mod address;
66
mod block;
7+
mod cache;
8+
pub mod common;
79
mod fee;
810
mod governance;
911
mod loan;
1012
mod masternode;
1113
mod oracle;
14+
mod path;
1215
mod pool_pair;
1316
pub mod prices;
14-
mod rawtx;
15-
mod cache;
16-
pub mod common;
17-
mod path;
1817
mod query;
18+
mod rawtx;
1919
mod response;
2020
mod stats;
2121
mod tokens;
@@ -80,10 +80,8 @@ pub async fn ocean_router(
8080
.nest("/masternodes", masternode::router(Arc::clone(&context)))
8181
.nest("/oracles", oracle::router(Arc::clone(&context)))
8282
.nest("/poolpairs", pool_pair::router(Arc::clone(&context)))
83-
// .nest("/prices", prices::router(Arc::clone(&context)))
84-
// .nest("/poolpairs", poolpairs::router(Arc::clone(&context)))
8583
.nest("/prices", prices::router(Arc::clone(&context)))
86-
// .nest("/rawtx", rawtx::router(Arc::clone(&context)))
84+
.nest("/rawtx", rawtx::router(Arc::clone(&context)))
8785
.nest("/stats", stats::router(Arc::clone(&context)))
8886
.nest("/tokens", tokens::router(Arc::clone(&context)))
8987
.nest("/transactions", transactions::router(Arc::clone(&context)))

lib/ain-ocean/src/api/pool_pair/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use path::{
2020
SwapPathsResponse,
2121
};
2222
use petgraph::graphmap::UnGraphMap;
23+
use price::DexPriceResponse;
2324
use rust_decimal::Decimal;
2425
use rust_decimal_macros::dec;
2526
use serde::{Deserialize, Serialize};
@@ -42,8 +43,6 @@ use crate::{
4243
Result, TokenIdentifier,
4344
};
4445

45-
use price::DexPriceResponse;
46-
4746
pub mod path;
4847
pub mod price;
4948
pub mod service;

lib/ain-ocean/src/api/pool_pair/price.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use serde::Serialize;
21
use std::{collections::HashMap, sync::Arc};
32

43
use defichain_rpc::json::token::TokenInfo;
4+
use serde::Serialize;
55

66
use super::{path::get_best_path, AppContext};
7-
87
use crate::{
98
api::{
109
cache::{get_token_cached, list_token_cached},

lib/ain-ocean/src/api/rawtx.rs

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,77 @@
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;
144
use axum::{
15-
extract::{Path, Query},
16-
routing::{get,post},
5+
extract::Path,
6+
response::Json,
7+
routing::{get, post},
178
Extension, Router,
189
};
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;
2017

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);
2221

2322
#[ocean_endpoint]
2423
async fn send_rawtx(
25-
Path(tx): Path<RawTx>,
24+
Path(tx): Path<RawTxDto>,
2625
Extension(ctx): Extension<Arc<AppContext>>,
2726
) -> 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()))
3034
}
3135

3236
#[ocean_endpoint]
3337
async fn test_rawtx(
34-
Path(tx): Path<RawTx>,
38+
Path(tx): Path<RawTxDto>,
3539
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))
3948
}
4049
#[ocean_endpoint]
41-
async fn get_rawtx(
50+
async fn get_raw_tx(
4251
Path(txid): Path<String>,
4352
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))
4758
}
4859

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;
5268
}
53-
5469
}
5570

5671
pub fn router(ctx: Arc<AppContext>) -> Router {
5772
Router::new()
5873
.route("/send", post(send_rawtx))
5974
.route("/test", get(test_rawtx))
60-
.route("/:txid", get(get_rawtx))
75+
// .route("/:txid", get(get_raw_tx))
6176
.layer(Extension(ctx))
6277
}

lib/ain-ocean/src/model/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub use oracle_token_currency::*;
3434
pub use poolswap::*;
3535
pub use poolswap_aggregated::*;
3636
pub use price_ticker::*;
37+
pub use raw_tx::*;
3738
// pub use raw_block::*;
3839
// pub use script_activity::*;
3940
// pub use script_aggregation::*;

lib/ain-ocean/src/model/raw_tx.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use rust_decimal::Decimal;
22
use serde::{Deserialize, Serialize};
33

4-
#[derive(Serialize, Deserialize, Debug, Default)]
4+
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
55
#[serde(rename_all = "camelCase")]
6-
pub struct RawTx {
6+
pub struct RawTxDto {
77
pub hex: String,
88
pub max_fee_rate: Option<Decimal>,
99
}

0 commit comments

Comments
 (0)