Skip to content

Commit

Permalink
fix: ekubo calldata (#314)
Browse files Browse the repository at this point in the history
* fix: ekubo calldata

* fix comment

* reverse the logic

* fixing vesu parsing

* updating vesu api

* fixing ekubo
  • Loading branch information
Marchand-Nicolas authored Nov 10, 2024
1 parent e265c92 commit 917904f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
54 changes: 41 additions & 13 deletions src/endpoints/defi/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
AppState, CommonReward, ContractCall, DefiReward, EkuboRewards, NimboraRewards,
NostraPeriodsResponse, NostraResponse, RewardSource, VesuRewards, ZkLendReward,
},
utils::{check_if_claimed, read_contract, to_hex, to_hex_trimmed},
utils::{check_if_unclaimed, read_contract, to_hex, to_hex_trimmed},
};
use axum::{
extract::{Query, State},
Expand Down Expand Up @@ -107,6 +107,8 @@ async fn fetch_zklend_rewards(
token_symbol: reward.token.symbol,
reward_source: RewardSource::ZkLend,
claimed: reward.claimed,
start_date: None,
end_date: None,
})
.collect();
Ok(rewards)
Expand Down Expand Up @@ -185,7 +187,7 @@ async fn fetch_nostra_rewards(
if let Some(distributor) =
matching_period.and_then(|period| period.defi_spring_rewards_distributor)
{
if check_if_claimed(
if check_if_unclaimed(
state,
distributor,
selector!("amount_already_claimed"),
Expand All @@ -203,6 +205,8 @@ async fn fetch_nostra_rewards(
token_symbol,
reward_source: RewardSource::Nostra,
claimed: false,
start_date: None,
end_date: None,
})
} else {
None
Expand Down Expand Up @@ -259,6 +263,8 @@ async fn fetch_nimbora_rewards(
claim_contract: config.rewards.nimbora.contract,
reward_source: RewardSource::Nimbora,
claimed: false,
start_date: None,
end_date: None,
};
Ok(vec![reward])
}
Expand Down Expand Up @@ -290,14 +296,14 @@ async fn fetch_ekubo_rewards(
return Err(Error::Reqwest(err));
}
};

let last_reward_id = rewards.last().map(|reward| reward.claim.id);
let tasks: FuturesOrdered<_> = rewards
.into_iter()
.rev()
.map(|reward| {
let strk_token = strk_token.clone();
async move {
if check_if_claimed(
if check_if_unclaimed(
state,
reward.contract_address,
selector!("is_claimed"),
Expand All @@ -315,37 +321,57 @@ async fn fetch_ekubo_rewards(
token_symbol: strk_token.symbol,
reward_source: RewardSource::Ekubo,
claimed: false,
start_date: Some(reward.start_date),
end_date: Some(reward.end_date),
})
} else {
None
}
}
})
.collect();
let active_rewards = tasks.filter_map(|res| async move { res }).collect().await;
Ok(active_rewards)
let active_rewards: Vec<CommonReward> =
tasks.filter_map(|res| async move { res }).collect().await;
if active_rewards.len() >= 1 && active_rewards[0].reward_id.unwrap() != last_reward_id.unwrap()
{
return Ok(vec![]);
}
// If several tasks have both the same start and end date, only the last one should returned
let filtered_tasks =
active_rewards
.into_iter()
.fold(Vec::<CommonReward>::new(), |mut acc, reward| {
if !acc
.iter()
.any(|r| r.start_date == reward.start_date && r.end_date == reward.end_date)
{
acc.push(reward);
}
acc
});

Ok(filtered_tasks)
}

async fn fetch_vesu_rewards(
client: &ClientWithMiddleware,
addr: &str,
state: &AppState,
) -> Result<Vec<CommonReward>, Error> {
let vesu_url = format!("https://staging.api.vesu.xyz/users/{}/strk-rewards", addr);
let vesu_url = format!("https://api.vesu.xyz/users/{}/strk-rewards", addr);
let response = client.get(&vesu_url).headers(get_headers()).send().await?;

match response.json::<VesuRewards>().await {
Ok(result) => {
let strk_token = state.conf.tokens.strk.clone();
let config = &state.conf;

let disctributed_amount: u64 = result
let disctributed_amount: FieldElement = result
.data
.distributor_data
.distributed_amount
.parse()
.expect("Failed to parse string to integer");
let claimed_amount: u64 = result
let claimed_amount: FieldElement = result
.data
.distributor_data
.claimed_amount
Expand All @@ -354,19 +380,21 @@ async fn fetch_vesu_rewards(
let amount = disctributed_amount - claimed_amount;

// If amount is 0, return empty vector
if amount == 0 {
if amount == FieldElement::ZERO {
return Ok(vec![]);
}

let reward = CommonReward {
amount: FieldElement::from(disctributed_amount),
displayed_amount: amount.into(),
amount: disctributed_amount,
displayed_amount: amount,
proof: result.data.distributor_data.call_data.proof,
reward_id: None,
claim_contract: config.rewards.vesu.contract,
token_symbol: strk_token.symbol,
reward_source: RewardSource::Vesu,
claimed: false,
start_date: None,
end_date: None,
};
Ok(vec![reward])
}
Expand Down
2 changes: 2 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,8 @@ pub struct CommonReward {
pub token_symbol: String,
pub reward_source: RewardSource,
pub claimed: bool,
pub start_date: Option<String>,
pub end_date: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down
7 changes: 4 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::logger::Logger;
use crate::models::{
AchievementDocument, AppState, BoostTable, CompletedTasks, LeaderboardTable, QuestDocument, QuestTaskDocument, QuizQuestionDocument, RewardSource, UserExperience
AchievementDocument, AppState, BoostTable, CompletedTasks, LeaderboardTable, QuestDocument,
QuestTaskDocument, QuizQuestionDocument, RewardSource, UserExperience,
};
use async_trait::async_trait;
use axum::{
Expand Down Expand Up @@ -924,12 +925,12 @@ pub async fn read_contract(
.await
}

pub async fn check_if_claimed(
pub async fn check_if_unclaimed(
state: &AppState,
contract: FieldElement,
selector: FieldElement,
calldata: Vec<FieldElement>,
source: RewardSource
source: RewardSource,
) -> bool {
match read_contract(state, contract, selector, calldata).await {
Ok(result) => result.get(0) == Some(&FieldElement::ZERO),
Expand Down

0 comments on commit 917904f

Please sign in to comment.