mirror of
https://github.com/RoboSats/taptrade-core.git
synced 2025-08-02 16:11:45 +00:00
complete bond validation function
This commit is contained in:
@ -21,7 +21,7 @@ struct AwaitingBondOffer {
|
|||||||
bond_amount_sat: u64,
|
bond_amount_sat: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct AwaitinigTakerOffer {
|
struct AwaitingTakerOffer {
|
||||||
offer_id: String,
|
offer_id: String,
|
||||||
robohash_maker: Vec<u8>,
|
robohash_maker: Vec<u8>,
|
||||||
is_buy_order: bool,
|
is_buy_order: bool,
|
||||||
@ -160,7 +160,7 @@ impl CoordinatorDB {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn fetch_and_delete_offer_from_bond_table(
|
async fn fetch_and_delete_offer_from_bond_table(
|
||||||
&self,
|
&self,
|
||||||
robohash_hex: &str,
|
robohash_hex: &str,
|
||||||
) -> Result<AwaitingBondOffer> {
|
) -> Result<AwaitingBondOffer> {
|
||||||
@ -271,10 +271,10 @@ impl CoordinatorDB {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn fetch_and_delete_offer_from_public_offers_table(
|
async fn fetch_and_delete_offer_from_public_offers_table(
|
||||||
&self,
|
&self,
|
||||||
offer_id_hex: &str,
|
offer_id_hex: &str,
|
||||||
) -> Result<AwaitinigTakerOffer> {
|
) -> Result<AwaitingTakerOffer> {
|
||||||
let fetched_values = sqlx::query_as::<_, (Vec<u8>, bool, i64, i32, i64, String, i64, String, String, String, String)> (
|
let fetched_values = sqlx::query_as::<_, (Vec<u8>, bool, i64, i32, i64, String, i64, String, String, String, String)> (
|
||||||
"SELECT robohash, is_buy_order, amount_sat, bond_ratio, offer_duration_ts, bond_address, bond_amount_sat, bond_tx_hex, payout_address,
|
"SELECT robohash, is_buy_order, amount_sat, bond_ratio, offer_duration_ts, bond_address, bond_amount_sat, bond_tx_hex, payout_address,
|
||||||
musig_pub_nonce_hex, musig_pubkey_hex FROM active_maker_offers WHERE <unique_identifier_column> = ?",
|
musig_pub_nonce_hex, musig_pubkey_hex FROM active_maker_offers WHERE <unique_identifier_column> = ?",
|
||||||
@ -289,7 +289,7 @@ impl CoordinatorDB {
|
|||||||
.execute(&*self.db_pool)
|
.execute(&*self.db_pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(AwaitinigTakerOffer {
|
Ok(AwaitingTakerOffer {
|
||||||
offer_id: offer_id_hex.to_string(),
|
offer_id: offer_id_hex.to_string(),
|
||||||
robohash_maker: fetched_values.0,
|
robohash_maker: fetched_values.0,
|
||||||
is_buy_order: fetched_values.1,
|
is_buy_order: fetched_values.1,
|
||||||
@ -475,7 +475,9 @@ mod tests {
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Fetch and delete the order request
|
// Fetch and delete the order request
|
||||||
let fetched_offer = database.fetch_maker_request(&robohash_hex.to_string()).await?;
|
let fetched_offer = database
|
||||||
|
.fetch_maker_request(&robohash_hex.to_string())
|
||||||
|
.await?;
|
||||||
|
|
||||||
// Verify the result
|
// Verify the result
|
||||||
let expected = BondRequirementResponse {
|
let expected = BondRequirementResponse {
|
||||||
@ -519,7 +521,9 @@ mod tests {
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Fetch and delete the order request
|
// Fetch and delete the order request
|
||||||
let fetched_offer = database.fetch_and_delete_offer_from_bond_table(robohash_hex).await?;
|
let fetched_offer = database
|
||||||
|
.fetch_and_delete_offer_from_bond_table(robohash_hex)
|
||||||
|
.await?;
|
||||||
|
|
||||||
// Verify the fetched offer
|
// Verify the fetched offer
|
||||||
let expected_offer = AwaitingBondOffer {
|
let expected_offer = AwaitingBondOffer {
|
||||||
@ -548,7 +552,6 @@ mod tests {
|
|||||||
// Create a temporary SQLite database
|
// Create a temporary SQLite database
|
||||||
let database = create_coordinator().await?;
|
let database = create_coordinator().await?;
|
||||||
|
|
||||||
|
|
||||||
// Insert a test entry into maker_requests
|
// Insert a test entry into maker_requests
|
||||||
let robohash_hex = "a3f1f1f0e2f3f4f5";
|
let robohash_hex = "a3f1f1f0e2f3f4f5";
|
||||||
let order_request = (
|
let order_request = (
|
||||||
@ -676,7 +679,6 @@ mod tests {
|
|||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Create a sample OffersRequest
|
// Create a sample OffersRequest
|
||||||
let offers_request = OffersRequest {
|
let offers_request = OffersRequest {
|
||||||
buy_offers: true,
|
buy_offers: true,
|
||||||
@ -731,7 +733,9 @@ mod tests {
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Call the fetch_taker_bond_requirements function
|
// Call the fetch_taker_bond_requirements function
|
||||||
let result = database.fetch_taker_bond_requirements(&offer_id_hex.to_string()).await?;
|
let result = database
|
||||||
|
.fetch_taker_bond_requirements(&offer_id_hex.to_string())
|
||||||
|
.await?;
|
||||||
|
|
||||||
// Verify the result
|
// Verify the result
|
||||||
assert_eq!(result.bond_address, taker_bond_address);
|
assert_eq!(result.bond_address, taker_bond_address);
|
||||||
@ -778,7 +782,9 @@ mod tests {
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Call the fetch_and_delete_offer_from_public_offers_table function
|
// Call the fetch_and_delete_offer_from_public_offers_table function
|
||||||
let result = database.fetch_and_delete_offer_from_public_offers_table(offer_id_hex).await?;
|
let result = database
|
||||||
|
.fetch_and_delete_offer_from_public_offers_table(offer_id_hex)
|
||||||
|
.await?;
|
||||||
|
|
||||||
// Verify the result
|
// Verify the result
|
||||||
assert_eq!(result.offer_id, offer_id_hex);
|
assert_eq!(result.offer_id, offer_id_hex);
|
||||||
@ -795,7 +801,8 @@ mod tests {
|
|||||||
assert_eq!(result.musig_pubkey_hex_maker, musig_pubkey_hex);
|
assert_eq!(result.musig_pubkey_hex_maker, musig_pubkey_hex);
|
||||||
|
|
||||||
// Verify the deletion
|
// Verify the deletion
|
||||||
let remaining_offers = sqlx::query("SELECT COUNT(*) FROM active_maker_offers WHERE offer_id = ?")
|
let remaining_offers =
|
||||||
|
sqlx::query("SELECT COUNT(*) FROM active_maker_offers WHERE offer_id = ?")
|
||||||
.bind(offer_id_hex)
|
.bind(offer_id_hex)
|
||||||
.fetch_one(&*database.db_pool)
|
.fetch_one(&*database.db_pool)
|
||||||
.await?;
|
.await?;
|
||||||
@ -805,5 +812,4 @@ mod tests {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,2 +0,0 @@
|
|||||||
use super::*;
|
|
||||||
use bdk::wallet::verify::verify_tx;
|
|
@ -64,30 +64,51 @@ impl CoordinatorWallet {
|
|||||||
&self,
|
&self,
|
||||||
bond: &String,
|
bond: &String,
|
||||||
requirements: BondRequirements,
|
requirements: BondRequirements,
|
||||||
) -> Result<bool> {
|
) -> Result<()> {
|
||||||
|
let input_sum: u64;
|
||||||
let tx: Transaction = deserialize(&hex::decode(bond)?)?;
|
let tx: Transaction = deserialize(&hex::decode(bond)?)?;
|
||||||
let wallet = self.wallet.lock().await;
|
{
|
||||||
let blockchain = ElectrumBlockchain::from(Client::new(
|
let blockchain = ElectrumBlockchain::from(Client::new(
|
||||||
&env::var("ELECTRUM_BACKEND")
|
&env::var("ELECTRUM_BACKEND")
|
||||||
.context("Parsing ELECTRUM_BACKEND from .env failed, is it set?")?,
|
.context("Parsing ELECTRUM_BACKEND from .env failed, is it set?")?,
|
||||||
)?);
|
)?);
|
||||||
|
let wallet = self.wallet.lock().await;
|
||||||
|
|
||||||
// we need to test this with signed and invalid/unsigned transactions
|
// we need to test this with signed and invalid/unsigned transactions
|
||||||
// checks signatures and inputs
|
// checks signatures and inputs
|
||||||
if let Err(e) = verify_tx(&tx, &*wallet.database(), &blockchain) {
|
if let Err(e) = verify_tx(&tx, &*wallet.database(), &blockchain) {
|
||||||
dbg!(e);
|
return Err(anyhow!(e));
|
||||||
return Ok(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if the tx has the correct input amounts (have to be >= trading amount)
|
// check if the tx has the correct input amounts (have to be >= trading amount)
|
||||||
if tx.input_sum(&blockchain, &*wallet.database())? < requirements.min_input_sum_sat {
|
input_sum = match tx.input_sum(&blockchain, &*wallet.database()) {
|
||||||
return Ok(false);
|
Ok(amount) => {
|
||||||
|
if amount < requirements.min_input_sum_sat {
|
||||||
|
return Err(anyhow!("Bond input sum too small"));
|
||||||
|
}
|
||||||
|
amount
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
return Err(anyhow!(e));
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if bond output to us is big enough
|
// check if bond output to us is big enough
|
||||||
// trait bond_output_sum
|
let output_sum = match tx.bond_output_sum(&requirements.bond_address) {
|
||||||
|
Ok(amount) => {
|
||||||
|
if amount < requirements.locking_amount_sat {
|
||||||
|
return Err(anyhow!("Bond output sum too small"));
|
||||||
|
}
|
||||||
|
amount
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
return Err(anyhow!(e));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// let valid = tx.verify_tx();
|
if ((input_sum - output_sum) / tx.vsize() as u64) < 200 {
|
||||||
Ok(true)
|
return Err(anyhow!("Bond fee rate too low"));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
use bdk::{blockchain::GetTx, database::Database};
|
use bdk::{
|
||||||
|
bitcoin::{Address, Network},
|
||||||
|
blockchain::GetTx,
|
||||||
|
database::Database,
|
||||||
|
};
|
||||||
|
|
||||||
pub trait BondTx {
|
pub trait BondTx {
|
||||||
fn input_sum<D: Database, B: GetTx>(&self, blockchain: &B, db: &D) -> Result<u64>;
|
fn input_sum<D: Database, B: GetTx>(&self, blockchain: &B, db: &D) -> Result<u64>;
|
||||||
@ -35,10 +39,15 @@ impl BondTx for Transaction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn bond_output_sum(&self, bond_address: &str) -> Result<u64> {
|
fn bond_output_sum(&self, bond_address: &str) -> Result<u64> {
|
||||||
panic!("implement");
|
let bond_script = Address::from_str(bond_address)?
|
||||||
// let bond_script = ScriptBuf;
|
.require_network(Network::Testnet)?
|
||||||
|
.script_pubkey();
|
||||||
|
|
||||||
for output in self.output.iter() {}
|
for output in self.output.iter() {
|
||||||
Ok(0)
|
if output.script_pubkey == bond_script {
|
||||||
|
return Ok(output.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(anyhow!("No output to bond address in transaction"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user