Skip to content

Commit

Permalink
fixes the failing tests and some type magic
Browse files Browse the repository at this point in the history
  • Loading branch information
gangov committed Nov 21, 2024
1 parent 4cfafaf commit f0ef161
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
32 changes: 14 additions & 18 deletions contracts/factory/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::{
error::ContractError,
stake_contract,
storage::{
get_config, get_lp_vec, get_stable_wasm_hash, is_initialized, save_config, save_lp_vec,
save_lp_vec_with_tuple_as_key, save_stable_wasm_hash, set_initialized, Asset, Config,
LiquidityPoolInfo, LpPortfolio, PairTupleKey, StakePortfolio, StakedResponse,
UserPortfolio,
LiquidityPoolInfo, LpPortfolio, PairTupleKey, StakePortfolio, UserPortfolio,
},
token_contract,
utils::{deploy_and_initialize_multihop_contract, deploy_lp_contract},
ConvertVec,
};
use phoenix::{
ttl::{INSTANCE_BUMP_AMOUNT, INSTANCE_LIFETIME_THRESHOLD},
Expand Down Expand Up @@ -433,17 +435,13 @@ impl FactoryTrait for Factory {

// get the lp share token balance for the user
// if the user has any liquidity tokens in the pool add to the lp_portfolio
let lp_share_balance: i128 = env.invoke_contract(
&response.pool_response.asset_lp_share.address,
&Symbol::new(&env, "balance"),
vec![&env, sender.into_val(&env)],
);
let lp_share_balance =
token_contract::Client::new(&env, &response.pool_response.asset_lp_share.address)
.balance(&sender);

let lp_share_staked: StakedResponse = env.invoke_contract(
&response.pool_response.stake_address,
&Symbol::new(&env, "query_staked"),
vec![&env, sender.into_val(&env)],
);
let lp_share_staked =
stake_contract::Client::new(&env, &response.pool_response.stake_address)
.query_staked(&sender);

let sum_of_lp_share_staked: i128 =
lp_share_staked.stakes.iter().map(|stake| stake.stake).sum();
Expand All @@ -467,17 +465,15 @@ impl FactoryTrait for Factory {

// make a call towards the stake contract to check the staked amount
if staking {
let stake_response: StakedResponse = env.invoke_contract(
&response.pool_response.stake_address,
&Symbol::new(&env, "query_staked"),
vec![&env, sender.into_val(&env)],
);
let stake_response =
stake_contract::Client::new(&env, &response.pool_response.stake_address)
.query_staked(&sender);

// only stakes that the user has made
if !stake_response.stakes.is_empty() {
stake_portfolio.push_back(StakePortfolio {
staking_contract: response.pool_response.stake_address,
stakes: stake_response.stakes,
stakes: stake_response.stakes.convert_vec(),
})
}
}
Expand Down
27 changes: 27 additions & 0 deletions contracts/factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,30 @@ pub mod token_contract {
file = "../../target/wasm32-unknown-unknown/release/soroban_token_contract.wasm"
);
}

#[allow(clippy::too_many_arguments)]
pub mod stake_contract {
soroban_sdk::contractimport!(
file = "../../target/wasm32-unknown-unknown/release/phoenix_stake.wasm"
);
}

pub trait ConvertVec<T, U> {
fn convert_vec(&self) -> soroban_sdk::Vec<U>;
}

impl ConvertVec<stake_contract::Stake, storage::Stake> for soroban_sdk::Vec<stake_contract::Stake> {
fn convert_vec(&self) -> soroban_sdk::Vec<storage::Stake> {
let env = self.env(); // Get the environment
let mut result = soroban_sdk::Vec::new(env);

for stake in self.iter() {
result.push_back(storage::Stake {
stake: stake.stake,
stake_timestamp: stake.stake_timestamp,
});
}

result
}
}

0 comments on commit f0ef161

Please sign in to comment.