additional sql table for active offers

This commit is contained in:
f321x
2024-06-27 10:23:58 +00:00
parent 6dacb2433d
commit cbaa665140
6 changed files with 52 additions and 3 deletions

View File

@ -177,6 +177,7 @@ dependencies = [
"async-trait",
"bdk-macros",
"bitcoin",
"bitcoinconsensus",
"electrum-client",
"getrandom",
"js-sys",
@ -237,6 +238,16 @@ dependencies = [
"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]]
name = "bitflags"
version = "1.3.2"

View File

@ -6,7 +6,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0.86"
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"
hex = "0.4.3"
reqwest = { version = "0.12.4", features = ["blocking", "json"] }

View File

@ -51,10 +51,13 @@ async fn submit_maker_bond(
// validate bond (check amounts, valid inputs, correct addresses, valid signature, feerate)
wallet
.validate_bond_tx_hex(&payload.signed_bond_hex)
.validate_bond_tx_hex(&payload.signed_bond_hex, &bond_requirements)
.await?;
// insert bond into sql database
database
.move_offer_to_active(&payload, &bond_requirements)
.await?;
// begin monitoring bond

View File

@ -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.

View File

@ -39,6 +39,22 @@ impl CoordinatorDB {
)
.execute(&db_pool)
.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");
let shared_db_pool = Arc::new(db_pool);
Ok(Self {
@ -84,4 +100,6 @@ impl CoordinatorDB {
locking_amount_sat: maker_request.try_get::<i64, _>("bond_amount_sat")? as u64,
})
}
pub async fn move_offer_to_active(&self) -> Result<()> {}
}

View File

@ -6,6 +6,7 @@ use bdk::{
electrum_client::Client,
sled::{self, Tree},
template::Bip86,
wallet::verify::*,
KeychainKind, SyncOptions, Wallet,
};
use std::str::FromStr;
@ -13,6 +14,7 @@ use std::str::FromStr;
#[derive(Clone, Debug)]
pub struct CoordinatorWallet {
pub wallet: Arc<Mutex<Wallet<Tree>>>,
// database: Arc<Mutex<Tree>>,
}
impl CoordinatorWallet {
@ -38,6 +40,7 @@ impl CoordinatorWallet {
dbg!(wallet.get_balance()?);
Ok(CoordinatorWallet {
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)
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 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)
}
}