@ -1,24 +1,20 @@
use anyhow ::{ Context , Result } ;
use anyhow ::{ Context , Result } ;
use bdk ::bitcoin ;
use bdk ::bitcoin ::secp256k1 ::schnorrsig ;
use bdk ::bitcoin ::secp256k1 ::schnorrsig ;
use clap ::Parser ;
use bdk ::bitcoin ::{ Address , Amount } ;
use bdk ::{ bitcoin , FeeRate } ;
use clap ::{ Parser , Subcommand } ;
use daemon ::db ::{ self } ;
use daemon ::db ::{ self } ;
use daemon ::model ::WalletInfo ;
use daemon ::model ::WalletInfo ;
use daemon ::seed ::Seed ;
use daemon ::seed ::Seed ;
use daemon ::{
use daemon ::{
bitmex_price_feed , connection , housekeeping , logger , monitor , oracle , taker_cfd , wallet ,
bitmex_price_feed , connection , housekeeping , logger , monitor , oracle , taker_cfd , wallet ,
wallet_sync , TakerActorSystem ,
wallet_sync , TakerActorSystem ,
} ;
} ;
use sqlx ::sqlite ::SqliteConnectOptions ;
use sqlx ::sqlite ::SqliteConnectOptions ;
use sqlx ::SqlitePool ;
use sqlx ::SqlitePool ;
use std ::net ::SocketAddr ;
use std ::net ::SocketAddr ;
use std ::path ::PathBuf ;
use std ::path ::PathBuf ;
use std ::str ::FromStr ;
use std ::str ::FromStr ;
use tokio ::sync ::watch ;
use tokio ::sync ::watch ;
use tracing_subscriber ::filter ::LevelFilter ;
use tracing_subscriber ::filter ::LevelFilter ;
use xtra ::prelude ::MessageChannel ;
use xtra ::prelude ::MessageChannel ;
@ -61,26 +57,52 @@ enum Network {
/// URL to the electrum backend to use for the wallet.
/// URL to the electrum backend to use for the wallet.
#[ clap(long, default_value = " ssl://electrum.blockstream.info:50002 " ) ]
#[ clap(long, default_value = " ssl://electrum.blockstream.info:50002 " ) ]
electrum : String ,
electrum : String ,
#[ clap(subcommand) ]
withdraw : Option < Withdraw > ,
} ,
} ,
Testnet {
Testnet {
/// URL to the electrum backend to use for the wallet.
/// URL to the electrum backend to use for the wallet.
#[ clap(long, default_value = " ssl://electrum.blockstream.info:60002 " ) ]
#[ clap(long, default_value = " ssl://electrum.blockstream.info:60002 " ) ]
electrum : String ,
electrum : String ,
#[ clap(subcommand) ]
withdraw : Option < Withdraw > ,
} ,
} ,
/// Run on signet
/// Run on signet
Signet {
Signet {
/// URL to the electrum backend to use for the wallet.
/// URL to the electrum backend to use for the wallet.
#[ clap(long) ]
#[ clap(long) ]
electrum : String ,
electrum : String ,
#[ clap(subcommand) ]
withdraw : Option < Withdraw > ,
} ,
}
#[ derive(Subcommand) ]
enum Withdraw {
Withdraw {
/// Optionally specify the amount of Bitcoin to be withdrawn. If not specified the wallet
/// will be drained. Amount is to be specified with denomination, e.g. "0.1 BTC"
#[ clap(long) ]
amount : Option < Amount > ,
/// Optionally specify the fee-rate for the transaction. The fee-rate is specified as btc
/// per kvb, e.g. 0.00001
#[ clap(long) ]
fee : Option < f32 > ,
/// The address to receive the Bitcoin.
#[ clap(long) ]
address : Address ,
} ,
} ,
}
}
impl Network {
impl Network {
fn electrum ( & self ) -> & str {
fn electrum ( & self ) -> & str {
match self {
match self {
Network ::Mainnet { electrum } = > electrum ,
Network ::Mainnet { electrum , . . } = > electrum ,
Network ::Testnet { electrum } = > electrum ,
Network ::Testnet { electrum , . . } = > electrum ,
Network ::Signet { electrum } = > electrum ,
Network ::Signet { electrum , . . } = > electrum ,
}
}
}
}
@ -99,6 +121,14 @@ impl Network {
Network ::Signet { . . } = > base . join ( "signet" ) ,
Network ::Signet { . . } = > base . join ( "signet" ) ,
}
}
}
}
fn withdraw ( & self ) -> & Option < Withdraw > {
match self {
Network ::Mainnet { withdraw , . . } = > withdraw ,
Network ::Testnet { withdraw , . . } = > withdraw ,
Network ::Signet { withdraw , . . } = > withdraw ,
}
}
}
}
#[ rocket::main ]
#[ rocket::main ]
@ -132,8 +162,29 @@ async fn main() -> Result<()> {
. await ?
. await ?
. create ( None )
. create ( None )
. spawn_global ( ) ;
. spawn_global ( ) ;
// do this before withdraw to ensure the wallet is synced
let wallet_info = wallet . send ( wallet ::Sync ) . await ? ? ;
let wallet_info = wallet . send ( wallet ::Sync ) . await ? ? ;
if let Some ( Withdraw ::Withdraw {
amount ,
address ,
fee ,
} ) = opts . network . withdraw ( )
{
let txid = wallet
. send ( wallet ::Withdraw {
amount : * amount ,
address : address . clone ( ) ,
fee : fee . map ( FeeRate ::from_btc_per_kvb ) ,
} )
. await ? ? ;
tracing ::info ! ( % txid , "Withdraw successful" ) ;
return Ok ( ( ) ) ;
}
// TODO: Actually fetch it from Olivia
// TODO: Actually fetch it from Olivia
let oracle = schnorrsig ::PublicKey ::from_str (
let oracle = schnorrsig ::PublicKey ::from_str (
"ddd4636845a90185991826be5a494cde9f4a6947b1727217afedc6292fa4caf7" ,
"ddd4636845a90185991826be5a494cde9f4a6947b1727217afedc6292fa4caf7" ,