diff options
author | alpaylan | 2021-04-14 00:55:24 +0300 |
---|---|---|
committer | alpaylan | 2021-04-14 00:55:24 +0300 |
commit | 2354e90ff0aff42b420eefd2afb2166169ec5857 (patch) | |
tree | b53eba358a7ab53ba9bee1489a18119e315243c1 /src | |
parent | fc9aa0c69b11f6ec93255b984839e0c2418a63cf (diff) | |
download | gradecoin-2354e90ff0aff42b420eefd2afb2166169ec5857.tar.gz gradecoin-2354e90ff0aff42b420eefd2afb2166169ec5857.tar.bz2 gradecoin-2354e90ff0aff42b420eefd2afb2166169ec5857.zip |
add balance check to transactions.
Diffstat (limited to 'src')
-rw-r--r-- | src/handlers.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/handlers.rs b/src/handlers.rs index 362a341..be2a020 100644 --- a/src/handlers.rs +++ b/src/handlers.rs | |||
@@ -291,6 +291,40 @@ pub async fn authorized_propose_transaction( | |||
291 | 291 | ||
292 | // `user` is an authenticated student, can propose | 292 | // `user` is an authenticated student, can propose |
293 | 293 | ||
294 | // check if user can afford the transaction | ||
295 | if new_transaction.by == new_transaction.source { // Propose to transact with another user | ||
296 | if internal_user.balance < new_transaction.amount { | ||
297 | return Ok(warp::reply::with_status( | ||
298 | warp::reply::json(&GradeCoinResponse { | ||
299 | res: ResponseType::Error, | ||
300 | message: "User does not have enough balance in their account" | ||
301 | .to_owned(), | ||
302 | }), | ||
303 | StatusCode::BAD_REQUEST, | ||
304 | )); | ||
305 | } | ||
306 | } else if new_transaction.by == new_transaction.target { | ||
307 | if internal_user.balance < new_transaction.amount { | ||
308 | return Ok(warp::reply::with_status( | ||
309 | warp::reply::json(&GradeCoinResponse { | ||
310 | res: ResponseType::Error, | ||
311 | message: "Bank does not have enough balance" | ||
312 | .to_owned(), | ||
313 | }), | ||
314 | StatusCode::BAD_REQUEST, | ||
315 | )); | ||
316 | } | ||
317 | } else { | ||
318 | return Ok(warp::reply::with_status( | ||
319 | warp::reply::json(&GradeCoinResponse { | ||
320 | res: ResponseType::Error, | ||
321 | message: "Invalid by field for the proposed transaction" | ||
322 | .to_owned(), | ||
323 | }), | ||
324 | StatusCode::BAD_REQUEST, | ||
325 | )); | ||
326 | } | ||
327 | |||
294 | // This public key was already written to the database, we can panic if it's not valid at | 328 | // This public key was already written to the database, we can panic if it's not valid at |
295 | // *this* point | 329 | // *this* point |
296 | let proposer_public_key = &internal_user.public_key; | 330 | let proposer_public_key = &internal_user.public_key; |
@@ -331,6 +365,7 @@ pub async fn authorized_propose_transaction( | |||
331 | 365 | ||
332 | debug!("clear for transaction proposal"); | 366 | debug!("clear for transaction proposal"); |
333 | 367 | ||
368 | |||
334 | let mut transactions = db.pending_transactions.write(); | 369 | let mut transactions = db.pending_transactions.write(); |
335 | transactions.insert(new_transaction.source.to_owned(), new_transaction); | 370 | transactions.insert(new_transaction.source.to_owned(), new_transaction); |
336 | Ok(warp::reply::with_status( | 371 | Ok(warp::reply::with_status( |