mirror of
https://github.com/RoboSats/taptrade-core.git
synced 2026-01-05 03:15:32 +00:00
additional sql table for active offers
This commit is contained in:
11
taptrade-cli-demo/coordinator/Cargo.lock
generated
11
taptrade-cli-demo/coordinator/Cargo.lock
generated
@ -177,6 +177,7 @@ dependencies = [
|
|||||||
"async-trait",
|
"async-trait",
|
||||||
"bdk-macros",
|
"bdk-macros",
|
||||||
"bitcoin",
|
"bitcoin",
|
||||||
|
"bitcoinconsensus",
|
||||||
"electrum-client",
|
"electrum-client",
|
||||||
"getrandom",
|
"getrandom",
|
||||||
"js-sys",
|
"js-sys",
|
||||||
@ -237,6 +238,16 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bitcoinconsensus"
|
||||||
|
version = "0.19.0-3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1a8aa43b5cd02f856cb126a9af819e77b8910fdd74dd1407be649f2f5fe3a1b5"
|
||||||
|
dependencies = [
|
||||||
|
"cc",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "1.3.2"
|
version = "1.3.2"
|
||||||
|
|||||||
@ -6,7 +6,7 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.86"
|
anyhow = "1.0.86"
|
||||||
axum = { version = "0.7.5", features = ["tokio", "json"] }
|
axum = { version = "0.7.5", features = ["tokio", "json"] }
|
||||||
bdk = { version = "0.29.0", features = ["key-value-db"] }
|
bdk = { version = "0.29.0", features = ["key-value-db", "bitcoinconsensus", "verify"] }
|
||||||
dotenv = "0.15.0"
|
dotenv = "0.15.0"
|
||||||
hex = "0.4.3"
|
hex = "0.4.3"
|
||||||
reqwest = { version = "0.12.4", features = ["blocking", "json"] }
|
reqwest = { version = "0.12.4", features = ["blocking", "json"] }
|
||||||
|
|||||||
@ -51,10 +51,13 @@ async fn submit_maker_bond(
|
|||||||
|
|
||||||
// validate bond (check amounts, valid inputs, correct addresses, valid signature, feerate)
|
// validate bond (check amounts, valid inputs, correct addresses, valid signature, feerate)
|
||||||
wallet
|
wallet
|
||||||
.validate_bond_tx_hex(&payload.signed_bond_hex)
|
.validate_bond_tx_hex(&payload.signed_bond_hex, &bond_requirements)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// insert bond into sql database
|
// insert bond into sql database
|
||||||
|
database
|
||||||
|
.move_offer_to_active(&payload, &bond_requirements)
|
||||||
|
.await?;
|
||||||
|
|
||||||
// begin monitoring bond
|
// begin monitoring bond
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,4 @@
|
|||||||
|
// we create an async function that loops trough the sqlite db table active_maker_offers and
|
||||||
|
// continoously verifies the bond inputs (mempool and chain), maybe with some caching in a hashmap to
|
||||||
|
// prevent querying the db all the time.
|
||||||
|
// Also needs to implement punishment logic in case a fraud is detected.
|
||||||
@ -39,6 +39,22 @@ impl CoordinatorDB {
|
|||||||
)
|
)
|
||||||
.execute(&db_pool)
|
.execute(&db_pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
sqlx::query(
|
||||||
|
// robohash is binary hash
|
||||||
|
"CREATE TABLE IF NOT EXISTS active_maker_offers (
|
||||||
|
trade_id BLOB PRIMARY KEY,
|
||||||
|
robohash BLOB,
|
||||||
|
is_buy_order INTEGER,
|
||||||
|
amount_sat INTEGER NOT NULL,
|
||||||
|
bond_ratio INTEGER NOT NULL,
|
||||||
|
offer_duration_ts INTEGER NOT NULL,
|
||||||
|
bond_address TEXT NOT NULL,
|
||||||
|
bond_amount_sat INTEGER NOT NULL
|
||||||
|
bond_tx_hex TEXT NOT NULL
|
||||||
|
)",
|
||||||
|
)
|
||||||
|
.execute(&db_pool)
|
||||||
|
.await?;
|
||||||
dbg!("Database initialized");
|
dbg!("Database initialized");
|
||||||
let shared_db_pool = Arc::new(db_pool);
|
let shared_db_pool = Arc::new(db_pool);
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
@ -84,4 +100,6 @@ impl CoordinatorDB {
|
|||||||
locking_amount_sat: maker_request.try_get::<i64, _>("bond_amount_sat")? as u64,
|
locking_amount_sat: maker_request.try_get::<i64, _>("bond_amount_sat")? as u64,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn move_offer_to_active(&self) -> Result<()> {}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,6 +6,7 @@ use bdk::{
|
|||||||
electrum_client::Client,
|
electrum_client::Client,
|
||||||
sled::{self, Tree},
|
sled::{self, Tree},
|
||||||
template::Bip86,
|
template::Bip86,
|
||||||
|
wallet::verify::*,
|
||||||
KeychainKind, SyncOptions, Wallet,
|
KeychainKind, SyncOptions, Wallet,
|
||||||
};
|
};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
@ -13,6 +14,7 @@ use std::str::FromStr;
|
|||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct CoordinatorWallet {
|
pub struct CoordinatorWallet {
|
||||||
pub wallet: Arc<Mutex<Wallet<Tree>>>,
|
pub wallet: Arc<Mutex<Wallet<Tree>>>,
|
||||||
|
// database: Arc<Mutex<Tree>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CoordinatorWallet {
|
impl CoordinatorWallet {
|
||||||
@ -38,6 +40,7 @@ impl CoordinatorWallet {
|
|||||||
dbg!(wallet.get_balance()?);
|
dbg!(wallet.get_balance()?);
|
||||||
Ok(CoordinatorWallet {
|
Ok(CoordinatorWallet {
|
||||||
wallet: Arc::new(Mutex::new(wallet)),
|
wallet: Arc::new(Mutex::new(wallet)),
|
||||||
|
// database: Arc::new(Mutex::new(sled_db)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,9 +51,19 @@ impl CoordinatorWallet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// validate bond (check amounts, valid inputs, correct addresses, valid signature, feerate)
|
// validate bond (check amounts, valid inputs, correct addresses, valid signature, feerate)
|
||||||
pub async fn validate_bond_tx_hex(&self, bond: &String) -> Result<bool> {
|
pub async fn validate_bond_tx_hex(
|
||||||
|
&self,
|
||||||
|
bond: &String,
|
||||||
|
requirements: &BondRequirementResponse,
|
||||||
|
) -> Result<bool> {
|
||||||
let tx: Transaction = deserialize(&hex::decode(bond)?)?;
|
let tx: Transaction = deserialize(&hex::decode(bond)?)?;
|
||||||
|
let wallet = self.wallet.lock().await;
|
||||||
|
|
||||||
|
// we need to test this with signed and invalid/unsigned transactions
|
||||||
|
// let result = verify_tx(&tx, wallet.database(), blockchain);
|
||||||
|
|
||||||
|
// let valid = tx.verify_tx();
|
||||||
|
panic!("Not implemented");
|
||||||
Ok(true)
|
Ok(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user