Skip to content

Commit 7f98835

Browse files
committed
fix rawtx.ctrl.send validate_composite_swap_tx
1 parent b9fd69c commit 7f98835

1 file changed

Lines changed: 47 additions & 41 deletions

File tree

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

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{result::Result as StdResult, str::FromStr, sync::Arc};
22

3-
use ain_dftx::{deserialize, DfTx, COIN};
3+
use ain_dftx::{deserialize, DfTx, Stack, COIN};
44
use ain_macros::ocean_endpoint;
55
use axum::{
66
extract::{Json, Path},
@@ -9,7 +9,6 @@ use axum::{
99
};
1010
use bitcoin::{Transaction, Txid};
1111
use defichain_rpc::{PoolPairRPC, RpcApi};
12-
use log::trace;
1312
use rust_decimal::prelude::ToPrimitive;
1413
use serde::{Deserialize, Serialize, Serializer};
1514
use snafu::location;
@@ -36,7 +35,8 @@ async fn send_raw_tx(
3635
Extension(ctx): Extension<Arc<AppContext>>,
3736
Json(raw_tx_dto): Json<RawTxDto>,
3837
) -> Result<String> {
39-
validate(ctx.clone(), raw_tx_dto.hex.clone()).await?;
38+
validate_composite_swap_tx(&ctx, &raw_tx_dto.hex).await?;
39+
4040
let max_fee = match raw_tx_dto.max_fee_rate {
4141
Some(fee_rate) => {
4242
let fee_in_satoshis = fee_rate.checked_mul(COIN.into());
@@ -162,54 +162,60 @@ async fn get_raw_tx(
162162
}
163163
}
164164

165-
async fn validate(ctx: Arc<AppContext>, hex: String) -> Result<()> {
165+
async fn validate_composite_swap_tx(ctx: &Arc<AppContext>, hex: &String) -> Result<()> {
166166
if !hex.starts_with("040000000001") {
167167
return Ok(());
168168
}
169169
let data = hex::decode(hex)?;
170-
let trx = deserialize::<Transaction>(&data)?;
171-
let bytes = trx.output[0].clone().script_pubkey.into_bytes();
172-
let tx: Option<DfTx> = if bytes.len() > 2 && bytes[0] == 0x6a && bytes[1] <= 0x4e {
173-
let offset = 1 + match bytes[1] {
174-
0x4c => 2,
175-
0x4d => 3,
176-
0x4e => 4,
177-
_ => 1,
178-
};
170+
let tx = deserialize::<Transaction>(&data)?;
179171

180-
let raw_tx = &bytes[offset..];
181-
Some(deserialize::<DfTx>(raw_tx)?)
182-
} else {
172+
let bytes = tx.output[0].script_pubkey.as_bytes();
173+
if bytes.len() <= 6 || bytes[0] != 0x6a || bytes[1] > 0x4e {
183174
return Ok(());
175+
}
176+
177+
let offset = 1 + match bytes[1] {
178+
0x4c => 2,
179+
0x4d => 3,
180+
0x4e => 4,
181+
_ => 1,
182+
};
183+
let raw_tx = &bytes[offset..];
184+
let dftx = match deserialize::<Stack>(raw_tx) {
185+
Err(bitcoin::consensus::encode::Error::ParseFailed("Invalid marker")) => None,
186+
Err(e) => return Err(e.into()),
187+
Ok(Stack { dftx, .. }) => Some(dftx),
184188
};
185189

186-
if let Some(tx) = tx {
187-
if let DfTx::CompositeSwap(composite_swap) = tx {
188-
if composite_swap.pools.as_ref().is_empty() {
189-
return Ok(());
190-
}
191-
let pool_id = composite_swap.pools.iter().last().unwrap();
192-
let tokio_id = composite_swap.pool_swap.to_token_id.0.to_string();
193-
let pool_pair = ctx
194-
.client
195-
.get_pool_pair(pool_id.to_string(), Some(true))
196-
.await?;
197-
for (_, pool_pair_info) in pool_pair.0 {
198-
if pool_pair_info.id_token_a.eq(&tokio_id)
199-
|| pool_pair_info.id_token_b.eq(&tokio_id)
200-
{
201-
trace!("Found a match: {pool_pair_info:?}");
202-
}
190+
let Some(dftx) = dftx else {
191+
return Ok(())
192+
};
193+
194+
if let DfTx::CompositeSwap(swap) = dftx {
195+
let Some(last_pool_id) = swap.pools.iter().last() else {
196+
return Ok(())
197+
};
198+
199+
let pool_pair = ctx
200+
.client
201+
.get_pool_pair(last_pool_id.to_string(), Some(true))
202+
.await?;
203+
204+
let to_token_id = swap.pool_swap.to_token_id.0.to_string();
205+
206+
for (_, info) in pool_pair.0 {
207+
if info.id_token_a == to_token_id || info.id_token_b == to_token_id {
208+
return Ok(())
203209
}
204-
Ok(())
205-
} else {
206-
Err(Error::BadRequest {
207-
msg: "Transaction is not a composite swap".to_string(),
208-
})
209210
}
210-
} else {
211-
Ok(())
212-
}
211+
212+
return Err(Error::BadRequest {
213+
msg: "Transaction is not a composite swap".to_string(),
214+
})
215+
};
216+
217+
Ok(())
218+
213219
}
214220

215221
pub fn router(ctx: Arc<AppContext>) -> Router {

0 commit comments

Comments
 (0)