@ -8,8 +8,7 @@ use crate::model::cfd::{
} ;
use crate ::model ::{ TakerId , Usd } ;
use crate ::monitor ::MonitorParams ;
use crate ::wallet ::Wallet ;
use crate ::{ log_error , maker_inc_connections , monitor , oracle , setup_contract , wire } ;
use crate ::{ log_error , maker_inc_connections , monitor , oracle , setup_contract , wallet , wire } ;
use anyhow ::{ Context as _ , Result } ;
use async_trait ::async_trait ;
use bdk ::bitcoin ::secp256k1 ::schnorrsig ;
@ -59,9 +58,9 @@ pub struct FromTaker {
pub msg : wire ::TakerToMaker ,
}
pub struct Actor < O , M , T > {
pub struct Actor < O , M , T , W > {
db : sqlx ::SqlitePool ,
wallet : Wallet ,
wallet : Address < W > ,
term : Duration ,
oracle_pk : schnorrsig ::PublicKey ,
cfd_feed_actor_inbox : watch ::Sender < Vec < Cfd > > ,
@ -94,11 +93,11 @@ enum RollOverState {
None ,
}
impl < O , M , T > Actor < O , M , T > {
impl < O , M , T , W > Actor < O , M , T , W > {
#[ allow(clippy::too_many_arguments) ]
pub fn new (
db : sqlx ::SqlitePool ,
wallet : Wallet ,
wallet : Address < W > ,
term : Duration ,
oracle_pk : schnorrsig ::PublicKey ,
cfd_feed_actor_inbox : watch ::Sender < Vec < Cfd > > ,
@ -127,19 +126,6 @@ impl<O, M, T> Actor<O, M, T> {
}
}
async fn handle_commit ( & mut self , order_id : OrderId ) -> Result < ( ) > {
let mut conn = self . db . acquire ( ) . await ? ;
cfd_actors ::handle_commit (
order_id ,
& mut conn ,
& self . wallet ,
& self . cfd_feed_actor_inbox ,
)
. await ? ;
Ok ( ( ) )
}
async fn handle_propose_roll_over (
& mut self ,
proposal : RollOverProposal ,
@ -204,30 +190,6 @@ impl<O, M, T> Actor<O, M, T> {
Ok ( ( ) )
}
async fn handle_monitoring_event ( & mut self , event : monitor ::Event ) -> Result < ( ) > {
let mut conn = self . db . acquire ( ) . await ? ;
cfd_actors ::handle_monitoring_event (
event ,
& mut conn ,
& self . wallet ,
& self . cfd_feed_actor_inbox ,
)
. await ? ;
Ok ( ( ) )
}
async fn handle_oracle_attestation ( & mut self , attestation : oracle ::Attestation ) -> Result < ( ) > {
let mut conn = self . db . acquire ( ) . await ? ;
cfd_actors ::handle_oracle_attestation (
attestation ,
& mut conn ,
& self . wallet ,
& self . cfd_feed_actor_inbox ,
)
. await ? ;
Ok ( ( ) )
}
async fn handle_inc_protocol_msg (
& mut self ,
taker_id : TakerId ,
@ -311,7 +273,49 @@ impl<O, M, T> Actor<O, M, T> {
}
}
impl < O , M , T > Actor < O , M , T >
impl < O , M , T , W > Actor < O , M , T , W >
where
W : xtra ::Handler < wallet ::TryBroadcastTransaction > ,
{
async fn handle_commit ( & mut self , order_id : OrderId ) -> Result < ( ) > {
let mut conn = self . db . acquire ( ) . await ? ;
cfd_actors ::handle_commit (
order_id ,
& mut conn ,
& self . wallet ,
& self . cfd_feed_actor_inbox ,
)
. await ? ;
Ok ( ( ) )
}
async fn handle_monitoring_event ( & mut self , event : monitor ::Event ) -> Result < ( ) > {
let mut conn = self . db . acquire ( ) . await ? ;
cfd_actors ::handle_monitoring_event (
event ,
& mut conn ,
& self . wallet ,
& self . cfd_feed_actor_inbox ,
)
. await ? ;
Ok ( ( ) )
}
async fn handle_oracle_attestation ( & mut self , attestation : oracle ::Attestation ) -> Result < ( ) > {
let mut conn = self . db . acquire ( ) . await ? ;
cfd_actors ::handle_oracle_attestation (
attestation ,
& mut conn ,
& self . wallet ,
& self . cfd_feed_actor_inbox ,
)
. await ? ;
Ok ( ( ) )
}
}
impl < O , M , T , W > Actor < O , M , T , W >
where
T : xtra ::Handler < maker_inc_connections ::TakerMessage > ,
{
@ -401,7 +405,7 @@ where
}
}
impl < O , M , T > Actor < O , M , T >
impl < O , M , T , W > Actor < O , M , T , W >
where
T : xtra ::Handler < maker_inc_connections ::TakerMessage >
+ xtra ::Handler < maker_inc_connections ::BroadcastOrder > ,
@ -530,11 +534,12 @@ where
}
}
impl < O , M , T > Actor < O , M , T >
impl < O , M , T , W > Actor < O , M , T , W >
where
Self : xtra ::Handler < CfdSetupCompleted > ,
O : xtra ::Handler < oracle ::GetAnnouncement > ,
T : xtra ::Handler < maker_inc_connections ::TakerMessage > ,
W : xtra ::Handler < wallet ::Sign > + xtra ::Handler < wallet ::BuildPartyParams > ,
{
async fn handle_accept_order (
& mut self ,
@ -622,7 +627,7 @@ where
}
}
impl < O , M , T > Actor < O , M , T >
impl < O , M , T , W > Actor < O , M , T , W >
where
O : xtra ::Handler < oracle ::FetchAnnouncement > ,
T : xtra ::Handler < maker_inc_connections ::BroadcastOrder > ,
@ -667,10 +672,11 @@ where
}
}
impl < O , M , T > Actor < O , M , T >
impl < O , M , T , W > Actor < O , M , T , W >
where
O : xtra ::Handler < oracle ::MonitorAttestation > ,
M : xtra ::Handler < monitor ::StartMonitoring > ,
W : xtra ::Handler < wallet ::TryBroadcastTransaction > ,
{
async fn handle_cfd_setup_completed (
& mut self ,
@ -694,8 +700,10 @@ where
let txid = self
. wallet
. try_broadcast_transaction ( dlc . lock . 0. clone ( ) )
. await ? ;
. send ( wallet ::TryBroadcastTransaction {
tx : dlc . lock . 0. clone ( ) ,
} )
. await ? ? ;
tracing ::info ! ( "Lock transaction published with txid {}" , txid ) ;
@ -716,7 +724,7 @@ where
}
}
impl < O , M , T > Actor < O , M , T >
impl < O , M , T , W > Actor < O , M , T , W >
where
Self : xtra ::Handler < CfdRollOverCompleted > ,
O : xtra ::Handler < oracle ::MonitorAttestation > + xtra ::Handler < oracle ::GetAnnouncement > ,
@ -809,7 +817,7 @@ where
}
}
impl < O , M , T > Actor < O , M , T >
impl < O , M , T , W > Actor < O , M , T , W >
where
M : xtra ::Handler < monitor ::StartMonitoring > ,
{
@ -842,9 +850,10 @@ where
}
}
impl < O , M , T > Actor < O , M , T >
impl < O , M , T , W > Actor < O , M , T , W >
where
M : xtra ::Handler < monitor ::CollaborativeSettlement > ,
W : xtra ::Handler < wallet ::TryBroadcastTransaction > ,
{
async fn handle_initiate_settlement (
& mut self ,
@ -888,8 +897,10 @@ where
let txid = self
. wallet
. try_broadcast_transaction ( spend_tx . clone ( ) )
. await
. send ( wallet ::TryBroadcastTransaction {
tx : spend_tx . clone ( ) ,
} )
. await ?
. context ( "Broadcasting spend transaction" ) ? ;
tracing ::info ! ( "Close transaction published with txid {}" , txid ) ;
@ -909,12 +920,15 @@ where
}
#[ async_trait ]
impl < O : 'static , M : 'static , T : 'static > Handler < CfdAction > for Actor < O , M , T >
impl < O : 'static , M : 'static , T : 'static , W : 'static > Handler < CfdAction > for Actor < O , M , T , W >
where
Self : xtra ::Handler < CfdSetupCompleted > + xtra ::Handler < CfdRollOverCompleted > ,
O : xtra ::Handler < oracle ::MonitorAttestation > + xtra ::Handler < oracle ::GetAnnouncement > ,
T : xtra ::Handler < maker_inc_connections ::TakerMessage >
+ xtra ::Handler < maker_inc_connections ::BroadcastOrder > ,
W : xtra ::Handler < wallet ::Sign >
+ xtra ::Handler < wallet ::BuildPartyParams >
+ xtra ::Handler < wallet ::TryBroadcastTransaction > ,
{
async fn handle ( & mut self , msg : CfdAction , ctx : & mut Context < Self > ) {
use CfdAction ::* ;
@ -933,7 +947,7 @@ where
}
#[ async_trait ]
impl < O : 'static , M : 'static , T : 'static > Handler < NewOrder > for Actor < O , M , T >
impl < O : 'static , M : 'static , T : 'static , W : 'static > Handler < NewOrder > for Actor < O , M , T , W >
where
O : xtra ::Handler < oracle ::FetchAnnouncement > ,
T : xtra ::Handler < maker_inc_connections ::BroadcastOrder > ,
@ -944,7 +958,7 @@ where
}
#[ async_trait ]
impl < O : 'static , M : 'static , T : 'static > Handler < NewTakerOnline > for Actor < O , M , T >
impl < O : 'static , M : 'static , T : 'static , W : 'static > Handler < NewTakerOnline > for Actor < O , M , T , W >
where
T : xtra ::Handler < maker_inc_connections ::TakerMessage > ,
{
@ -954,10 +968,12 @@ where
}
#[ async_trait ]
impl < O : 'static , M : 'static , T : 'static > Handler < CfdSetupCompleted > for Actor < O , M , T >
impl < O : 'static , M : 'static , T : 'static , W : 'static > Handler < CfdSetupCompleted >
for Actor < O , M , T , W >
where
O : xtra ::Handler < oracle ::MonitorAttestation > ,
M : xtra ::Handler < monitor ::StartMonitoring > ,
W : xtra ::Handler < wallet ::TryBroadcastTransaction > ,
{
async fn handle ( & mut self , msg : CfdSetupCompleted , _ctx : & mut Context < Self > ) {
log_error ! ( self . handle_cfd_setup_completed ( msg . order_id , msg . dlc ) ) ;
@ -965,7 +981,8 @@ where
}
#[ async_trait ]
impl < O : 'static , M : 'static , T : 'static > Handler < CfdRollOverCompleted > for Actor < O , M , T >
impl < O : 'static , M : 'static , T : 'static , W : 'static > Handler < CfdRollOverCompleted >
for Actor < O , M , T , W >
where
M : xtra ::Handler < monitor ::StartMonitoring > ,
{
@ -975,18 +992,22 @@ where
}
#[ async_trait ]
impl < O : 'static , M : 'static , T : 'static > Handler < monitor ::Event > for Actor < O , M , T > {
impl < O : 'static , M : 'static , T : 'static , W : 'static > Handler < monitor ::Event > for Actor < O , M , T , W >
where
W : xtra ::Handler < wallet ::TryBroadcastTransaction > ,
{
async fn handle ( & mut self , msg : monitor ::Event , _ctx : & mut Context < Self > ) {
log_error ! ( self . handle_monitoring_event ( msg ) )
}
}
#[ async_trait ]
impl < O : 'static , M : 'static , T : 'static > Handler < FromTaker > for Actor < O , M , T >
impl < O : 'static , M : 'static , T : 'static , W : 'static > Handler < FromTaker > for Actor < O , M , T , W >
where
T : xtra ::Handler < maker_inc_connections ::BroadcastOrder >
+ xtra ::Handler < maker_inc_connections ::TakerMessage > ,
M : xtra ::Handler < monitor ::CollaborativeSettlement > ,
W : xtra ::Handler < wallet ::TryBroadcastTransaction > ,
{
async fn handle ( & mut self , FromTaker { taker_id , msg } : FromTaker , _ctx : & mut Context < Self > ) {
match msg {
@ -1041,7 +1062,11 @@ where
}
#[ async_trait ]
impl < O : 'static , M : 'static , T : 'static > Handler < oracle ::Attestation > for Actor < O , M , T > {
impl < O : 'static , M : 'static , T : 'static , W : 'static > Handler < oracle ::Attestation >
for Actor < O , M , T , W >
where
W : xtra ::Handler < wallet ::TryBroadcastTransaction > ,
{
async fn handle ( & mut self , msg : oracle ::Attestation , _ctx : & mut Context < Self > ) {
log_error ! ( self . handle_oracle_attestation ( msg ) )
}
@ -1071,4 +1096,4 @@ impl Message for FromTaker {
type Result = ( ) ;
}
impl < O : 'static , M : 'static , T : 'static > xtra ::Actor for Actor < O , M , T > { }
impl < O : 'static , M : 'static , T : 'static , W : 'static > xtra ::Actor for Actor < O , M , T , W > { }