From 659fce0e3377d74c78d6ff1be04bf6e2e9a164bc Mon Sep 17 00:00:00 2001 From: David Date: Wed, 5 Jul 2023 22:26:03 -0400 Subject: [PATCH] Deposit history for stripe and admin (endpoints) (#160) * added endpoints for stripe and admin deposits * modified comments --- web3_proxy/src/frontend/mod.rs | 13 +++- web3_proxy/src/frontend/users/payment.rs | 91 +++++++++++++++++++++++- 2 files changed, 100 insertions(+), 4 deletions(-) diff --git a/web3_proxy/src/frontend/mod.rs b/web3_proxy/src/frontend/mod.rs index 84e019f4..bdd22610 100644 --- a/web3_proxy/src/frontend/mod.rs +++ b/web3_proxy/src/frontend/mod.rs @@ -166,7 +166,18 @@ pub async fn serve( ) .route("/user", get(users::user_get).post(users::user_post)) .route("/user/balance", get(users::payment::user_balance_get)) - .route("/user/deposits", get(users::payment::user_deposits_get)) + .route( + "/user/deposits/chain", + get(users::payment::user_chain_deposits_get), + ) + .route( + "/user/deposits/stripe", + get(users::payment::user_stripe_deposits_get), + ) + .route( + "/user/deposits/admin", + get(users::payment::user_admin_deposits_get), + ) .route( "/user/balance/stripe", get(users::payment_stripe::user_stripe_deposits_get) diff --git a/web3_proxy/src/frontend/users/payment.rs b/web3_proxy/src/frontend/users/payment.rs index c060b152..8663ef30 100644 --- a/web3_proxy/src/frontend/users/payment.rs +++ b/web3_proxy/src/frontend/users/payment.rs @@ -13,7 +13,10 @@ use axum::{ }; use axum_client_ip::InsecureClientIp; use axum_macros::debug_handler; -use entities::{balance, increase_on_chain_balance_receipt, rpc_key, user}; +use entities::{ + admin_increase_balance_receipt, balance, increase_on_chain_balance_receipt, rpc_key, + stripe_increase_balance_receipt, user, +}; use ethers::abi::AbiEncode; use ethers::types::{Address, Block, TransactionReceipt, TxHash, H256}; use hashbrown::{HashMap, HashSet}; @@ -67,11 +70,11 @@ pub async fn user_balance_get( Ok(Json(response).into_response()) } -/// `GET /user/deposits` -- Use a bearer token to get the user's balance and spend. +/// `GET /user/deposits/chain` -- Use a bearer token to get the user's balance and spend. /// /// - shows a list of all deposits, including their chain-id, amount and tx-hash #[debug_handler] -pub async fn user_deposits_get( +pub async fn user_chain_deposits_get( Extension(app): Extension>, TypedHeader(Authorization(bearer)): TypedHeader>, ) -> Web3ProxyResponse { @@ -105,6 +108,88 @@ pub async fn user_deposits_get( Ok(Json(response).into_response()) } +/// `GET /user/deposits/stripe` -- Use a bearer token to get the user's balance and spend. +/// +/// - shows a list of all deposits done through stripe +#[debug_handler] +pub async fn user_stripe_deposits_get( + Extension(app): Extension>, + TypedHeader(Authorization(bearer)): TypedHeader>, +) -> Web3ProxyResponse { + let user = app.bearer_is_authorized(bearer).await?; + + let db_replica = app.db_replica()?; + + // Filter by user ... + let receipts = stripe_increase_balance_receipt::Entity::find() + .filter(increase_on_chain_balance_receipt::Column::DepositToUserId.eq(Some(user.id))) + .all(db_replica.as_ref()) + .await?; + + // Return the response, all except the user ... + let receipts = receipts + .into_iter() + .map(|x| { + json!({ + "id": x.id, + "stripe_payment_intend_id": x.stripe_payment_intend_id, + "deposit_to_user_id": x.deposit_to_user_id, + "amount": x.amount, + "currency": x.currency, + "status": x.status, + "description": x.description, + "date_created": x.date_created + }) + }) + .collect::>(); + + let response = json!({ + "user": Address::from_slice(&user.address), + "deposits": receipts, + }); + + Ok(Json(response).into_response()) +} + +/// `GET /user/deposits/admin` -- Use a bearer token to get the user's balance and spend. +/// +/// - shows a list of all deposits done by admins +#[debug_handler] +pub async fn user_admin_deposits_get( + Extension(app): Extension>, + TypedHeader(Authorization(bearer)): TypedHeader>, +) -> Web3ProxyResponse { + let user = app.bearer_is_authorized(bearer).await?; + + let db_replica = app.db_replica()?; + + // Filter by user ... + let receipts = admin_increase_balance_receipt::Entity::find() + .filter(increase_on_chain_balance_receipt::Column::DepositToUserId.eq(user.id)) + .all(db_replica.as_ref()) + .await?; + + // Return the response, all except the user ... + let receipts = receipts + .into_iter() + .map(|x| { + json!({ + "id": x.id, + "amount": x.amount, + "deposit_to_user_id": x.deposit_to_user_id, + "note": x.note, + }) + }) + .collect::>(); + + let response = json!({ + "user": Address::from_slice(&user.address), + "deposits": receipts, + }); + + Ok(Json(response).into_response()) +} + /// `POST /user/balance/:tx_hash` -- Process a confirmed txid to update a user's balance. #[debug_handler] pub async fn user_balance_post(