Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Endpoint for returning wallet balance (and status) #3345

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions core/payment/src/api/accounts.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down Expand Up @@ -49,3 +54,46 @@ async fn get_requestor_accounts(db: Data<DbExecutor>, 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<String>,
#[serde(default)]
pub network: Option<String>,
#[serde(default)]
pub driver: Option<String>,
}

#[actix_web::get("/account/status")]
async fn get_wallet_status(
db: Data<DbExecutor>,
id: Identity,
query: Query<AccountQuery>,
) -> 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::<Utc>::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)
}
2 changes: 1 addition & 1 deletion core/payment/src/api/allocations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading