Skip to content

Commit

Permalink
Merge pull request #184 from Reecepbcups/reece/active-external-contracts
Browse files Browse the repository at this point in the history
feat(vault): `active_external_staking` query
  • Loading branch information
JakeHartnell authored Jan 8, 2024
2 parents 25774f0 + e92ab55 commit c170596
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
27 changes: 25 additions & 2 deletions contracts/provider/vault/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use sylvia::{contract, schemars};
use crate::error::ContractError;
use crate::msg::{
AccountClaimsResponse, AccountDetailsResponse, AccountResponse, AllAccountsResponse,
AllAccountsResponseItem, AllTxsResponse, AllTxsResponseItem, ConfigResponse, LienResponse,
StakingInitInfo, TxResponse,
AllAccountsResponseItem, AllActiveExternalStakingResponse, AllTxsResponse, AllTxsResponseItem,
ConfigResponse, LienResponse, StakingInitInfo, TxResponse,
};
use crate::state::{Config, Lien, LocalStaking, UserInfo};
use crate::txs::Txs;
Expand Down Expand Up @@ -55,6 +55,8 @@ pub struct VaultContract<'a> {
pub liens: Map<'a, (&'a Addr, &'a Addr), Lien>,
/// Per-user information
pub users: Map<'a, &'a Addr, UserInfo>,
/// All active external staking contracts in use by this vault
pub active_external: Map<'a, &'a Addr, ()>,
/// Pending txs information
pub tx_count: Item<'a, u64>,
pub pending: Txs<'a>,
Expand All @@ -73,6 +75,7 @@ impl VaultContract<'_> {
users: Map::new("users"),
pending: Txs::new("pending_txs", "users"),
tx_count: Item::new("tx_count"),
active_external: Map::new("active_external"),
}
}

Expand Down Expand Up @@ -206,6 +209,9 @@ impl VaultContract<'_> {
vec![],
)?;

self.active_external
.save(ctx.deps.storage, &contract.0, &())?;

let resp = Response::new()
.add_message(stake_msg)
.add_attribute("action", "stake_remote")
Expand Down Expand Up @@ -308,6 +314,23 @@ impl VaultContract<'_> {
Ok(resp)
}

#[msg(query)]
fn active_external_staking(
&self,
ctx: QueryCtx,
) -> Result<AllActiveExternalStakingResponse, ContractError> {
let active = self
.active_external
.keys(ctx.deps.storage, None, None, Order::Ascending)
.collect::<StdResult<Vec<_>>>()?;

let resp = AllActiveExternalStakingResponse {
contracts: active.into_iter().map(|addr| addr.to_string()).collect(),
};

Ok(resp)
}

/// Returns a single claim between the user and lienholder
#[msg(query)]
fn claim(
Expand Down
5 changes: 5 additions & 0 deletions contracts/provider/vault/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ pub struct ConfigResponse {
pub local_staking: Option<String>,
}

#[cw_serde]
pub struct AllActiveExternalStakingResponse {
pub contracts: Vec<String>,
}

pub type TxResponse = Tx;
pub type AllTxsResponseItem = TxResponse;

Expand Down
16 changes: 15 additions & 1 deletion contracts/provider/vault/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ use crate::contract;
use crate::contract::multitest_utils::VaultContractProxy;
use crate::contract::test_utils::VaultApi;
use crate::error::ContractError;
use crate::msg::{AccountResponse, AllAccountsResponseItem, LienResponse, StakingInitInfo};
use crate::msg::{
AccountResponse, AllAccountsResponseItem, AllActiveExternalStakingResponse, LienResponse,
StakingInitInfo,
};

const OSMO: &str = "OSMO";
const STAR: &str = "star";
Expand Down Expand Up @@ -712,6 +715,9 @@ fn stake_cross() {
coin(0, OSMO)
);

let res = vault.active_external_staking().unwrap();
assert_eq!(res, AllActiveExternalStakingResponse { contracts: vec![] });

// Staking remotely
vault
.stake_remote(
Expand All @@ -725,6 +731,14 @@ fn stake_cross() {
.call(user)
.unwrap();

let res = vault.active_external_staking().unwrap();
assert_eq!(
res,
AllActiveExternalStakingResponse {
contracts: vec![cross_staking.contract_addr.to_string()],
}
);

let acc = vault.account(user.to_owned()).unwrap();
assert_eq!(
acc,
Expand Down

0 comments on commit c170596

Please sign in to comment.