Skip to content

Commit

Permalink
Merge pull request #309 from BrunoAmbricca/feat/add-vesu-rewards
Browse files Browse the repository at this point in the history
Feat: Add Vesu for DeFiSpring rewards
  • Loading branch information
Marchand-Nicolas authored Oct 30, 2024
2 parents 0d2432d + a524eb0 commit 04488f9
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
2 changes: 2 additions & 0 deletions config.template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ api_endpoint = "xxxxxxx"
[rewards]
[rewards.nimbora]
contract = "0x07ed46700bd12bb1ee8a33a8594791003f9710a1ab18edd958aed86a8f82d3d1"
[rewards.vesu]
contract = "0x0387f3eb1d98632fbe3440a9f1385aec9d87b6172491d3dd81f1c35a7c61048f"

[tokens]
[tokens.strk]
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ pub_struct!(Clone, Deserialize; ProtocolStats {

pub_struct!(Clone, Deserialize; Rewards {
nimbora: Contract,
vesu: Contract,
});

pub_struct!(Clone, Deserialize; Token {
Expand Down
54 changes: 50 additions & 4 deletions src/endpoints/defi/rewards.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
models::{
AppState, CommonReward, ContractCall, DefiReward, EkuboRewards, NimboraRewards,
NostraPeriodsResponse, NostraResponse, RewardSource, ZkLendReward,
NostraPeriodsResponse, NostraResponse, RewardSource, ZkLendReward, VesuRewards
},
utils::{check_if_claimed, read_contract, to_hex, to_hex_trimmed},
};
Expand Down Expand Up @@ -41,23 +41,26 @@ pub async fn get_defi_rewards(
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
.build();

let (zklend_rewards, nostra_rewards, nimbora_rewards, ekubo_rewards) = tokio::join!(
let (zklend_rewards, nostra_rewards, nimbora_rewards, ekubo_rewards, vesu_rewards) = tokio::join!(
fetch_zklend_rewards(&client, &addr),
fetch_nostra_rewards(&client, &addr, &state),
fetch_nimbora_rewards(&client, &addr, &state),
fetch_ekubo_rewards(&client, &addr, &state),
fetch_vesu_rewards(&client, &addr, &state),
);

let zklend_rewards = zklend_rewards.unwrap_or_default();
let nostra_rewards = nostra_rewards.unwrap_or_default();
let nimbora_rewards = nimbora_rewards.unwrap_or_default();
let ekubo_rewards = ekubo_rewards.unwrap_or_default();
let vesu_rewards = vesu_rewards.unwrap_or_default();

let all_rewards = [
&zklend_rewards,
&nostra_rewards,
&nimbora_rewards,
&ekubo_rewards,
&vesu_rewards,
];

let all_calls: Vec<ContractCall> = all_rewards
Expand All @@ -70,7 +73,8 @@ pub async fn get_defi_rewards(
"zklend": extract_rewards(&zklend_rewards),
"nostra": extract_rewards(&nostra_rewards),
"nimbora": extract_rewards(&nimbora_rewards),
"ekubo": extract_rewards(&ekubo_rewards)
"ekubo": extract_rewards(&ekubo_rewards),
"vesu": extract_rewards(&vesu_rewards)
},
"calls": all_calls
});
Expand Down Expand Up @@ -322,6 +326,48 @@ async fn fetch_ekubo_rewards(
Ok(active_rewards)
}

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 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.data.distributor_data.distributed_amount.parse().expect("Failed to parse string to integer");
let claimed_amount : u64 = result.data.distributor_data.claimed_amount.parse().expect("Failed to parse string to integer");
let amount = disctributed_amount - claimed_amount;

let reward = CommonReward {
amount: amount.into(),
displayed_amount: amount.into(),
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,
};
Ok(vec![reward])
}
Err(err) => {
eprintln!("Failed to deserialize vesu response: {:?}", err);
Err(Error::Reqwest(err))
}
}
}

fn create_calls(rewards: &[CommonReward], addr: &str) -> Vec<ContractCall> {
rewards
.iter()
Expand All @@ -338,7 +384,7 @@ fn create_calls(rewards: &[CommonReward], addr: &str) -> Vec<ContractCall> {
data.extend(reward.proof.clone());
data
}
RewardSource::Nimbora | RewardSource::Nostra => {
RewardSource::Nimbora | RewardSource::Nostra | RewardSource::Vesu => {
let mut data = vec![
to_hex_trimmed(reward.amount),
to_hex_trimmed(FieldElement::from(reward.proof.len())),
Expand Down
29 changes: 29 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,34 @@ pub struct EkuboRewards {
pub proof: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct VesuRewards {
pub data: VesuData,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct VesuData {
pub wallet_address: FieldElement,
pub amount: FieldElement,
pub decimals: u8,
pub distributor_data: VesuDistributorData,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct VesuDistributorData {
pub distributed_amount: String,
pub claimed_amount: String,
pub call_data: VesuCallData,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct VesuCallData {
pub amount: String,
pub proof: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Claim {
pub id: u64,
Expand All @@ -465,6 +493,7 @@ pub enum RewardSource {
Nostra,
Nimbora,
Ekubo,
Vesu,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down

0 comments on commit 04488f9

Please sign in to comment.