1
+ use crate :: db;
1
2
use crate :: dlc_protocol;
2
3
use crate :: dlc_protocol:: ProtocolId ;
3
4
use crate :: schema:: dlc_protocols;
4
5
use crate :: schema:: sql_types:: ProtocolStateType ;
6
+ use crate :: schema:: sql_types:: ProtocolTypeType ;
5
7
use bitcoin:: secp256k1:: PublicKey ;
6
8
use diesel:: query_builder:: QueryId ;
7
9
use diesel:: AsExpression ;
@@ -37,6 +39,26 @@ impl QueryId for ProtocolStateType {
37
39
}
38
40
}
39
41
42
+ #[ derive( Debug , Clone , Copy , PartialEq , FromSqlRow , AsExpression , Eq , Hash ) ]
43
+ #[ diesel( sql_type = ProtocolTypeType ) ]
44
+ pub ( crate ) enum DlcProtocolType {
45
+ Open ,
46
+ Renew ,
47
+ Settle ,
48
+ Close ,
49
+ ForceClose ,
50
+ Rollover ,
51
+ }
52
+
53
+ impl QueryId for ProtocolTypeType {
54
+ type QueryId = ProtocolTypeType ;
55
+ const HAS_STATIC_QUERY_ID : bool = false ;
56
+
57
+ fn query_id ( ) -> Option < TypeId > {
58
+ None
59
+ }
60
+ }
61
+
40
62
#[ derive( Queryable , Debug ) ]
41
63
#[ diesel( table_name = protocols) ]
42
64
#[ allow( dead_code) ] // We have to allow dead code here because diesel needs the fields to be able to derive queryable.
@@ -49,17 +71,52 @@ pub(crate) struct DlcProtocol {
49
71
pub protocol_state : DlcProtocolState ,
50
72
pub trader_pubkey : String ,
51
73
pub timestamp : OffsetDateTime ,
74
+ pub protocol_type : DlcProtocolType ,
52
75
}
53
76
54
77
pub ( crate ) fn get_dlc_protocol (
55
78
conn : & mut PgConnection ,
56
79
protocol_id : ProtocolId ,
57
80
) -> QueryResult < dlc_protocol:: DlcProtocol > {
58
- let contract_transaction : DlcProtocol = dlc_protocols:: table
81
+ let dlc_protocol : DlcProtocol = dlc_protocols:: table
59
82
. filter ( dlc_protocols:: protocol_id. eq ( protocol_id. to_uuid ( ) ) )
60
83
. first ( conn) ?;
61
84
62
- Ok ( dlc_protocol:: DlcProtocol :: from ( contract_transaction) )
85
+ let protocol_type = match dlc_protocol. protocol_type {
86
+ DlcProtocolType :: Open => {
87
+ let trade_params = db:: trade_params:: get ( conn, protocol_id) ?;
88
+ dlc_protocol:: DlcProtocolType :: Open { trade_params }
89
+ }
90
+ DlcProtocolType :: Renew => {
91
+ let trade_params = db:: trade_params:: get ( conn, protocol_id) ?;
92
+ dlc_protocol:: DlcProtocolType :: Renew { trade_params }
93
+ }
94
+ DlcProtocolType :: Settle => {
95
+ let trade_params = db:: trade_params:: get ( conn, protocol_id) ?;
96
+ dlc_protocol:: DlcProtocolType :: Settle { trade_params }
97
+ }
98
+ DlcProtocolType :: Close => dlc_protocol:: DlcProtocolType :: Close {
99
+ trader : PublicKey :: from_str ( & dlc_protocol. trader_pubkey ) . expect ( "valid public key" ) ,
100
+ } ,
101
+ DlcProtocolType :: ForceClose => dlc_protocol:: DlcProtocolType :: ForceClose {
102
+ trader : PublicKey :: from_str ( & dlc_protocol. trader_pubkey ) . expect ( "valid public key" ) ,
103
+ } ,
104
+ DlcProtocolType :: Rollover => dlc_protocol:: DlcProtocolType :: Rollover {
105
+ trader : PublicKey :: from_str ( & dlc_protocol. trader_pubkey ) . expect ( "valid public key" ) ,
106
+ } ,
107
+ } ;
108
+
109
+ let protocol = dlc_protocol:: DlcProtocol {
110
+ id : dlc_protocol. protocol_id . into ( ) ,
111
+ timestamp : dlc_protocol. timestamp ,
112
+ channel_id : DlcChannelId :: from_hex ( & dlc_protocol. channel_id ) . expect ( "valid dlc channel id" ) ,
113
+ contract_id : ContractId :: from_hex ( & dlc_protocol. contract_id ) . expect ( "valid contract id" ) ,
114
+ trader : PublicKey :: from_str ( & dlc_protocol. trader_pubkey ) . expect ( "valid public key" ) ,
115
+ protocol_state : dlc_protocol. protocol_state . into ( ) ,
116
+ protocol_type,
117
+ } ;
118
+
119
+ Ok ( protocol)
63
120
}
64
121
65
122
pub ( crate ) fn set_dlc_protocol_state_to_failed (
@@ -81,8 +138,8 @@ pub(crate) fn set_dlc_protocol_state_to_failed(
81
138
pub ( crate ) fn set_dlc_protocol_state_to_success (
82
139
conn : & mut PgConnection ,
83
140
protocol_id : ProtocolId ,
84
- contract_id : ContractId ,
85
- channel_id : DlcChannelId ,
141
+ contract_id : & ContractId ,
142
+ channel_id : & DlcChannelId ,
86
143
) -> QueryResult < ( ) > {
87
144
let affected_rows = diesel:: update ( dlc_protocols:: table)
88
145
. filter ( dlc_protocols:: protocol_id. eq ( protocol_id. to_uuid ( ) ) )
@@ -104,8 +161,9 @@ pub(crate) fn create(
104
161
conn : & mut PgConnection ,
105
162
protocol_id : ProtocolId ,
106
163
previous_protocol_id : Option < ProtocolId > ,
107
- contract_id : ContractId ,
108
- channel_id : DlcChannelId ,
164
+ contract_id : & ContractId ,
165
+ channel_id : & DlcChannelId ,
166
+ protocol_type : dlc_protocol:: DlcProtocolType ,
109
167
trader : & PublicKey ,
110
168
) -> QueryResult < ( ) > {
111
169
let affected_rows = diesel:: insert_into ( dlc_protocols:: table)
@@ -117,6 +175,7 @@ pub(crate) fn create(
117
175
dlc_protocols:: protocol_state. eq ( DlcProtocolState :: Pending ) ,
118
176
dlc_protocols:: trader_pubkey. eq ( trader. to_string ( ) ) ,
119
177
dlc_protocols:: timestamp. eq ( OffsetDateTime :: now_utc ( ) ) ,
178
+ dlc_protocols:: protocol_type. eq ( DlcProtocolType :: from ( protocol_type) ) ,
120
179
) )
121
180
. execute ( conn) ?;
122
181
@@ -127,19 +186,6 @@ pub(crate) fn create(
127
186
Ok ( ( ) )
128
187
}
129
188
130
- impl From < DlcProtocol > for dlc_protocol:: DlcProtocol {
131
- fn from ( value : DlcProtocol ) -> Self {
132
- dlc_protocol:: DlcProtocol {
133
- id : value. protocol_id . into ( ) ,
134
- timestamp : value. timestamp ,
135
- channel_id : DlcChannelId :: from_hex ( & value. channel_id ) . expect ( "valid dlc channel id" ) ,
136
- contract_id : ContractId :: from_hex ( & value. contract_id ) . expect ( "valid contract id" ) ,
137
- trader : PublicKey :: from_str ( & value. trader_pubkey ) . expect ( "valid public key" ) ,
138
- protocol_state : value. protocol_state . into ( ) ,
139
- }
140
- }
141
- }
142
-
143
189
impl From < dlc_protocol:: DlcProtocolState > for DlcProtocolState {
144
190
fn from ( value : dlc_protocol:: DlcProtocolState ) -> Self {
145
191
match value {
@@ -159,3 +205,16 @@ impl From<DlcProtocolState> for dlc_protocol::DlcProtocolState {
159
205
}
160
206
}
161
207
}
208
+
209
+ impl From < dlc_protocol:: DlcProtocolType > for DlcProtocolType {
210
+ fn from ( value : dlc_protocol:: DlcProtocolType ) -> Self {
211
+ match value {
212
+ dlc_protocol:: DlcProtocolType :: Open { .. } => DlcProtocolType :: Open ,
213
+ dlc_protocol:: DlcProtocolType :: Renew { .. } => DlcProtocolType :: Renew ,
214
+ dlc_protocol:: DlcProtocolType :: Settle { .. } => DlcProtocolType :: Settle ,
215
+ dlc_protocol:: DlcProtocolType :: Close { .. } => DlcProtocolType :: Close ,
216
+ dlc_protocol:: DlcProtocolType :: ForceClose { .. } => DlcProtocolType :: ForceClose ,
217
+ dlc_protocol:: DlcProtocolType :: Rollover { .. } => DlcProtocolType :: Rollover ,
218
+ }
219
+ }
220
+ }
0 commit comments