From b92e35ee40c9c9effdae43c5ffbc90b2016dc8f9 Mon Sep 17 00:00:00 2001 From: BrunoAmbricca Date: Wed, 30 Oct 2024 09:58:00 -0300 Subject: [PATCH 1/3] Vesu rewards impl --- src/endpoints/defi/rewards.rs | 47 ++++++++++++++++++++++++++++++++--- src/models.rs | 29 +++++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/src/endpoints/defi/rewards.rs b/src/endpoints/defi/rewards.rs index 97d0fc9..c8df5ba 100644 --- a/src/endpoints/defi/rewards.rs +++ b/src/endpoints/defi/rewards.rs @@ -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}, }; @@ -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 = all_rewards @@ -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 }); @@ -322,6 +326,41 @@ async fn fetch_ekubo_rewards( Ok(active_rewards) } +async fn fetch_vesu_rewards( + client: &ClientWithMiddleware, + addr: &str, + state: &AppState, +) -> Result, 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::().await { + Ok(result) => { + let strk_token = state.conf.tokens.strk.clone(); + let reward = CommonReward { + amount: result.data.amount, + displayed_amount: result.data.amount, + proof: result.data.distributor_data.call_data.proof, + reward_id: None, + claim_contract: result.data.wallet_address, + 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 { rewards .iter() @@ -338,7 +377,7 @@ fn create_calls(rewards: &[CommonReward], addr: &str) -> Vec { 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())), diff --git a/src/models.rs b/src/models.rs index 0015105..3498230 100644 --- a/src/models.rs +++ b/src/models.rs @@ -452,6 +452,34 @@ pub struct EkuboRewards { pub proof: Vec, } +#[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, +} + #[derive(Serialize, Deserialize, Debug)] pub struct Claim { pub id: u64, @@ -465,6 +493,7 @@ pub enum RewardSource { Nostra, Nimbora, Ekubo, + Vesu, } #[derive(Serialize, Deserialize, Debug)] From 19a645e425a3ad949c7acff6732029d74522497d Mon Sep 17 00:00:00 2001 From: BrunoAmbricca Date: Wed, 30 Oct 2024 16:44:58 -0300 Subject: [PATCH 2/3] Fixes + contract address --- src/config.rs | 1 + src/endpoints/defi/rewards.rs | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/config.rs b/src/config.rs index 5e87b38..a62316c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -196,6 +196,7 @@ pub_struct!(Clone, Deserialize; ProtocolStats { pub_struct!(Clone, Deserialize; Rewards { nimbora: Contract, + vesu: Contract, }); pub_struct!(Clone, Deserialize; Token { diff --git a/src/endpoints/defi/rewards.rs b/src/endpoints/defi/rewards.rs index c8df5ba..75639cd 100644 --- a/src/endpoints/defi/rewards.rs +++ b/src/endpoints/defi/rewards.rs @@ -338,16 +338,23 @@ async fn fetch_vesu_rewards( .send() .await?; + match response.json::().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: result.data.amount, - displayed_amount: result.data.amount, + amount: amount.into(), + displayed_amount: amount.into(), proof: result.data.distributor_data.call_data.proof, reward_id: None, - claim_contract: result.data.wallet_address, + claim_contract: config.rewards.vesu.contract, token_symbol: strk_token.symbol, reward_source: RewardSource::Vesu, claimed: false, From a524eb0f64481e3e846abf74593c2a2a3129fc92 Mon Sep 17 00:00:00 2001 From: BrunoAmbricca Date: Wed, 30 Oct 2024 18:45:44 -0300 Subject: [PATCH 3/3] config variables --- config.template.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config.template.toml b/config.template.toml index ddf8f2f..b580acc 100644 --- a/config.template.toml +++ b/config.template.toml @@ -86,6 +86,8 @@ api_endpoint = "xxxxxxx" [rewards] [rewards.nimbora] contract = "0x07ed46700bd12bb1ee8a33a8594791003f9710a1ab18edd958aed86a8f82d3d1" +[rewards.vesu] +contract = "0x0387f3eb1d98632fbe3440a9f1385aec9d87b6172491d3dd81f1c35a7c61048f" [tokens] [tokens.strk]