Skip to content

Commit

Permalink
Verify if the client had allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-neti committed Nov 7, 2024
1 parent 0eda5a6 commit 17fc070
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions fplus-lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub fn default_env_vars() -> &'static HashMap<&'static str, &'static str> {
m.insert("KYC_URL", "https://kyc.allocator.tech");
m.insert("RPC_URL", "https://mainnet.optimism.io");
m.insert("DMOB_API_URL", "https://api.datacapstats.io/public/api");
m.insert("DATACAPSTATS_API_URL", "https://api.datacapstats.io/api");
m.insert("DMOB_API_KEY", "5c993a17-7b18-4ead-a8a8-89dad981d87e");
m.insert("DAYS_TO_NEXT_AUTOALLOCATION", "14");
m.insert(
Expand Down
28 changes: 28 additions & 0 deletions fplus-lib/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use reqwest::Response;
use serde::{Deserialize, Serialize};
use serde_json::from_str;

use crate::external_services::filecoin::get_client_allocation;
use crate::{
base64,
config::get_env_var_or_default,
Expand Down Expand Up @@ -752,6 +753,33 @@ impl LDNApplication {
}
}
}

match get_client_allocation(&application_id).await {
Ok(response) => {
if let Some(_) = response.count {

Check failure on line 759 in fplus-lib/src/core/mod.rs

View workflow job for this annotation

GitHub Actions / code-check / format_and_lint

redundant pattern matching, consider using `is_some()`
log::info!("Allocation found for client {}", application_id);
Self::issue_pathway_mismatch_comment(
issue_number,
info.owner,
info.repo,
None,
)
.await?;

return Err(LDNError::New(
"Pathway mismatch: Client has already allocation".to_string(),
));
} else {
log::info!("Client allocation not found");
}
}
Err(e) => {
return Err(LDNError::New(format!(
"Getting client allocation failed /// {}",
e
)));
}
}
}

let file_content = match serde_json::to_string_pretty(&application_file) {
Expand Down
18 changes: 18 additions & 0 deletions fplus-lib/src/external_services/filecoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
config::get_env_var_or_default,
models::filecoin::{
StateReadStateResponse, StateVerifiedClientStatusResponse, StateVerifierStatusResponse,
VerifiedClientResponse,
},
};

Expand Down Expand Up @@ -86,3 +87,20 @@ pub async fn get_allowance_for_client(address: &str) -> Result<String, reqwest::
.await?;
Ok(response.result)
}

pub async fn get_client_allocation(
address: &str,
) -> Result<VerifiedClientResponse, reqwest::Error> {
let api_url = get_env_var_or_default("DATACAPSTATS_API_URL");
let url = format!("{}/getVerifiedClients?filter={}", api_url, address);

let client = reqwest::Client::new();

let response = client
.get(&url)
.send()
.await?
.json::<VerifiedClientResponse>()
.await?;
Ok(response)
}
23 changes: 23 additions & 0 deletions fplus-lib/src/models/filecoin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;

pub type StateReadStateResponse = JSONRPCResponse<StateReadStateResult>;
pub type StateVerifierStatusResponse = JSONRPCResponse<StateVerifierStatusResult>;
Expand Down Expand Up @@ -47,3 +48,25 @@ pub struct MultisigState {
#[serde(rename = "PendingTxns")]
pub pending_txns: Code,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct VerifiedClientResponse {
#[serde(deserialize_with = "number_to_string")]
pub count: Option<String>,
}

fn number_to_string<'de, D>(de: D) -> Result<Option<String>, D::Error>
where
D: serde::Deserializer<'de>,
{
let helper: Value = Deserialize::deserialize(de)?;

match helper {
Value::Number(n) => Ok(n
.as_u64()
.filter(|&number| number != 0)
.map(|_| n.to_string())),
Value::String(s) => Ok(Some(s)),
_ => Ok(None),
}
}

0 comments on commit 17fc070

Please sign in to comment.