diff --git a/core/payment/src/api/accounts.rs b/core/payment/src/api/accounts.rs index 67abc1cb4..3ea1e2b74 100644 --- a/core/payment/src/api/accounts.rs +++ b/core/payment/src/api/accounts.rs @@ -1,22 +1,27 @@ +use std::time::UNIX_EPOCH; // Extrnal crates use actix_web::{HttpResponse, Scope}; // Workspace uses use ya_client_model::payment::*; -use ya_core_model::payment::local::{GetAccounts, BUS_ID as LOCAL_SERVICE}; +use ya_core_model::payment::local::{GetAccounts, GetStatus, BUS_ID as LOCAL_SERVICE}; use ya_service_api_web::middleware::Identity; use ya_service_bus::{typed as bus, RpcEndpoint}; // Local uses use crate::utils::*; -use actix_web::web::Data; +use crate::api::allocations::DEFAULT_PAYMENT_DRIVER; +use actix_web::web::{Data, Query}; +use chrono::{DateTime, Utc}; +use serde::{Deserialize, Serialize}; use ya_persistence::executor::DbExecutor; pub fn register_endpoints(scope: Scope) -> Scope { scope .service(get_provider_accounts) .service(get_requestor_accounts) + .service(get_wallet_status) } #[actix_web::get("/providerAccounts")] @@ -49,3 +54,46 @@ async fn get_requestor_accounts(db: Data, id: Identity) -> HttpRespo .collect(); response::ok(recv_accounts) } + +/// TODO: Should be in `ya-client-model`, but for faster development it's here. +/// It's should be decided if this endpoint should be merged to master. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct AccountQuery { + #[serde(default)] + pub address: Option, + #[serde(default)] + pub network: Option, + #[serde(default)] + pub driver: Option, +} + +#[actix_web::get("/account/status")] +async fn get_wallet_status( + db: Data, + id: Identity, + query: Query, +) -> HttpResponse { + let query = query.into_inner(); + let address = query.address.unwrap_or(id.identity.to_string()); + + if address != id.identity.to_string() { + return response::bad_request(&"Attempting to get wallet status using wrong identity"); + } + + let status = match bus::service(LOCAL_SERVICE) + .call(GetStatus { + address, + driver: query.driver.unwrap_or(DEFAULT_PAYMENT_DRIVER.to_string()), + network: query.network, + token: None, + after_timestamp: DateTime::::from(UNIX_EPOCH).timestamp(), + }) + .await + { + Ok(Ok(status)) => status, + Ok(Err(e)) => return response::server_error(&e), + Err(e) => return response::server_error(&e), + }; + response::ok(status) +} diff --git a/core/payment/src/api/allocations/mod.rs b/core/payment/src/api/allocations/mod.rs index a6bddbba0..8b41adf84 100644 --- a/core/payment/src/api/allocations/mod.rs +++ b/core/payment/src/api/allocations/mod.rs @@ -28,7 +28,7 @@ use crate::utils::response; const DEFAULT_TESTNET_NETWORK: NetworkName = NetworkName::Holesky; const DEFAULT_MAINNET_NETWORK: NetworkName = NetworkName::Polygon; -const DEFAULT_PAYMENT_DRIVER: DriverName = DriverName::Erc20; +pub(crate) const DEFAULT_PAYMENT_DRIVER: DriverName = DriverName::Erc20; mod api_error; mod platform_triple;