-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathservice.rs
229 lines (200 loc) · 6.64 KB
/
service.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use crate::networks::Network;
use crate::processor::GNTDriverProcessor;
use crate::{DRIVER_DETAILS, DRIVER_NAME};
use bigdecimal::BigDecimal;
use std::convert::TryInto;
use ya_core_model::driver::*;
use ya_core_model::payment::local as payment_srv;
use ya_persistence::executor::DbExecutor;
use ya_service_bus::typed::service;
use ya_service_bus::{typed as bus, RpcEndpoint};
pub fn bind_service(db: &DbExecutor, processor: GNTDriverProcessor) {
log::debug!("Binding payment driver service to service bus");
bus::ServiceBinder::new(&driver_bus_id(DRIVER_NAME), db, processor)
.bind_with_processor(account_event)
.bind_with_processor(fund)
.bind_with_processor(init)
.bind_with_processor(get_account_balance)
.bind_with_processor(get_transaction_balance)
.bind_with_processor(schedule_payment)
.bind_with_processor(verify_payment)
.bind_with_processor(validate_allocation);
log::debug!("Successfully bound payment driver service to service bus");
}
pub async fn subscribe_to_identity_events() -> anyhow::Result<()> {
bus::service(ya_core_model::identity::BUS_ID)
.send(ya_core_model::identity::Subscribe {
endpoint: driver_bus_id(DRIVER_NAME),
})
.await??;
Ok(())
}
pub async fn register_in_payment_service() -> anyhow::Result<()> {
log::debug!("Registering driver in payment service...");
let message = payment_srv::RegisterDriver {
driver_name: DRIVER_NAME.to_string(),
details: DRIVER_DETAILS.clone(),
};
service(payment_srv::BUS_ID).send(message).await?.unwrap(); // Unwrap on purpose because it's NoError
log::debug!("Successfully registered driver in payment service.");
Ok(())
}
async fn fund(
_db: DbExecutor,
processor: GNTDriverProcessor,
_caller: String,
msg: Fund,
) -> Result<String, GenericError> {
log::debug!("Funding account: {:?}", msg);
let address = msg.address();
let network = parse_network(msg.network())?;
processor
.fund(address.as_str(), network)
.await
.map_err(GenericError::new)
}
async fn init(
_db: DbExecutor,
processor: GNTDriverProcessor,
_caller: String,
msg: Init,
) -> Result<Ack, GenericError> {
log::debug!("Initializing account: {:?}", msg);
let address = msg.address();
let mode = msg.mode();
let network = parse_network(msg.network())?;
processor
.init(mode, address.as_str(), network)
.await
.map_or_else(|e| Err(GenericError::new(e)), |()| Ok(Ack {}))
}
async fn get_account_balance(
_db: DbExecutor,
processor: GNTDriverProcessor,
_caller: String,
msg: GetAccountBalance,
) -> Result<BigDecimal, GenericError> {
log::info!("get account balance: {:?}", msg);
let addr = msg.address();
let network = parse_platform(msg.platform())?;
processor
.get_account_balance(addr.as_str(), network)
.await
.map_or_else(
|e| Err(GenericError::new(e)),
|account_balance| Ok(account_balance),
)
}
async fn get_transaction_balance(
_db: DbExecutor,
processor: GNTDriverProcessor,
_caller: String,
msg: GetTransactionBalance,
) -> Result<BigDecimal, GenericError> {
log::info!("get transaction balance: {:?}", msg);
let sender = msg.sender();
let recipient = msg.recipient();
let network = parse_platform(msg.platform())?;
processor
.get_transaction_balance(sender.as_str(), recipient.as_str(), network)
.await
.map_or_else(|e| Err(GenericError::new(e)), |balance| Ok(balance))
}
async fn schedule_payment(
_db: DbExecutor,
processor: GNTDriverProcessor,
_caller: String,
msg: SchedulePayment,
) -> Result<String, GenericError> {
log::info!("schedule payment: {:?}", msg);
let amount = msg.amount();
let sender = msg.sender();
let recipient = msg.recipient();
let due_date = msg.due_date();
let network = parse_platform(msg.platform())?;
processor
.schedule_payment(
amount,
sender.as_str(),
recipient.as_str(),
network,
due_date,
)
.await
.map_or_else(|e| Err(GenericError::new(e)), |r| Ok(r))
}
async fn verify_payment(
_db: DbExecutor,
processor: GNTDriverProcessor,
_caller: String,
msg: VerifyPayment,
) -> Result<PaymentDetails, GenericError> {
log::info!("verify payment: {:?}", msg);
let confirmation = msg.confirmation();
let network = parse_platform(msg.platform())?;
processor
.verify_payment(confirmation, network)
.await
.map_or_else(
|e| Err(GenericError::new(e)),
|payment_details| Ok(payment_details),
)
}
async fn validate_allocation(
_db: DbExecutor,
processor: GNTDriverProcessor,
_caller: String,
msg: ValidateAllocation,
) -> Result<bool, GenericError> {
log::debug!("Validate allocation: {:?}", msg);
let ValidateAllocation {
address,
platform,
amount,
existing_allocations,
} = msg;
let network = parse_platform(platform)?;
processor
.validate_allocation(address, network, amount, existing_allocations)
.await
.map_err(GenericError::new)
}
async fn account_event(
_db: DbExecutor,
processor: GNTDriverProcessor,
_caller: String,
msg: ya_core_model::identity::event::Event,
) -> Result<(), ya_core_model::identity::Error> {
log::debug!("account event: {:?}", msg);
let _ = match msg {
ya_core_model::identity::event::Event::AccountLocked { identity } => {
processor.account_locked(identity).await
}
ya_core_model::identity::event::Event::AccountUnlocked { identity } => {
processor.account_unlocked(identity).await
}
}
.map_err(|e| log::error!("Identity event listener error: {:?}", e));
Ok(())
}
fn parse_network(network: Option<String>) -> Result<Network, GenericError> {
network
.unwrap_or(DRIVER_DETAILS.default_network.clone())
.parse()
.map_err(GenericError::new)
}
fn parse_platform(platform: String) -> Result<Network, GenericError> {
// NOTE: This parsing method is not universally applicable
let parts: Vec<&str> = platform.split("-").collect();
let parts: [&str; 3] = parts
.try_into()
.map_err(|_| GenericError::new(format!("Invalid platform: {}", platform)))?;
if parts[0] != DRIVER_NAME {
return Err(GenericError::new(format!(
"Invalid driver name: {} != {}",
parts[0], DRIVER_NAME
)));
}
let network = parts[1].parse().map_err(GenericError::new)?;
Ok(network)
}