Skip to content

Commit

Permalink
vault: fix tests after removing mock
Browse files Browse the repository at this point in the history
  • Loading branch information
uint committed Oct 9, 2023
1 parent 1cc618b commit dc2ccf0
Showing 1 changed file with 82 additions and 45 deletions.
127 changes: 82 additions & 45 deletions contracts/provider/vault/src/multitest.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cosmwasm_std::{coin, coins, to_binary, Addr, Binary, Decimal, Uint128};
use cw_multi_test::App as MtApp;
use cosmwasm_std::{coin, coins, to_binary, Addr, Decimal, Uint128, Validator};
use cw_multi_test::{App as MtApp, StakingInfo};
use mesh_apis::ibc::AddValidator;
use mesh_external_staking::contract::multitest_utils::ExternalStakingContractProxy;
use mesh_external_staking::msg::{AuthorizedEndpoint, ReceiveVirtualStake};
Expand Down Expand Up @@ -27,17 +27,52 @@ const SLASHING_PERCENTAGE: u64 = 10;

/// App initialization
fn init_app(users: &[&str], amounts: &[u128]) -> App<MtApp> {
let app = MtApp::new(|router, _api, storage| {
let mut app = MtApp::new(|router, _api, storage| {
for (&user, amount) in std::iter::zip(users, amounts) {
router
.bank
.init_balance(storage, &Addr::unchecked(user), coins(*amount, OSMO))
.unwrap();
}
});

set_chain_native_denom(&mut app, OSMO);

App::new(app)
}

fn add_local_validator(app: &mut App<MtApp>, validator: &str) {
let block_info = app.block_info();
app.app_mut()
.init_modules(|router, api, storage| {
router.staking.add_validator(
api,
storage,
&block_info,
Validator {
address: validator.to_string(),
commission: Decimal::zero(),
max_commission: Decimal::zero(),
max_change_rate: Decimal::zero(),
},
)
})
.unwrap();
}

fn set_chain_native_denom(app: &mut MtApp, denom: &str) {
app.init_modules(|router, _, storage| {
router.staking.setup(
storage,
StakingInfo {
bonded_denom: denom.to_string(),
..Default::default()
},
)
})
.unwrap();
}

/// Contracts setup
fn setup<'app>(
app: &'app App<MtApp>,
Expand Down Expand Up @@ -136,11 +171,19 @@ fn bond(vault: &VaultContractProxy<MtApp>, user: &str, amount: u128) {
.unwrap();
}

fn stake_locally(vault: &VaultContractProxy<MtApp>, user: &str, stake: u128) {
fn stake_locally(
vault: &VaultContractProxy<MtApp>,
user: &str,
stake: u128,
validator: &str,
) -> Result<cw_multi_test::AppResponse, ContractError> {
let msg = mesh_native_staking::msg::StakeMsg {
validator: validator.to_string(),
};

vault
.stake_local(coin(stake, OSMO), Binary::default())
.stake_local(coin(stake, OSMO), to_binary(&msg).unwrap())
.call(user)
.unwrap();
}

fn stake_remotely(
Expand Down Expand Up @@ -352,8 +395,10 @@ fn bonding() {
fn stake_local() {
let owner = "owner";
let user = "user1";
let val = "validator";

let app = init_app(&[user], &[300]);
let mut app = init_app(&[user], &[300]);
add_local_validator(&mut app, val);

let (vault, local_staking, _cross_staking1) = setup(&app, owner, SLASHING_PERCENTAGE, 100);

Expand Down Expand Up @@ -385,7 +430,7 @@ fn stake_local() {
);

// Staking locally
stake_locally(&vault, user, 100);
stake_locally(&vault, user, 100, val).unwrap();

assert_eq!(
vault.account(user.to_owned()).unwrap(),
Expand All @@ -410,18 +455,8 @@ fn stake_local() {
.unwrap(),
coin(200, OSMO)
);
assert_eq!(
app.app()
.wrap()
.query_balance(&local_staking.contract_addr, OSMO)
.unwrap(),
coin(100, OSMO)
);

vault
.stake_local(coin(150, OSMO), Binary::default())
.call(user)
.unwrap();
stake_locally(&vault, user, 150, val).unwrap();

assert_eq!(
vault.account(user.to_owned()).unwrap(),
Expand All @@ -446,20 +481,10 @@ fn stake_local() {
.unwrap(),
coin(50, OSMO)
);
assert_eq!(
app.app()
.wrap()
.query_balance(&local_staking.contract_addr, OSMO)
.unwrap(),
coin(250, OSMO)
);

// Cannot stake over collateral

let err = vault
.stake_local(coin(150, OSMO), Binary::default())
.call(user)
.unwrap_err();
let err = stake_locally(&vault, user, 150, val).unwrap_err();

assert_eq!(err, ContractError::InsufficentBalance);

Expand All @@ -475,8 +500,8 @@ fn stake_local() {

let proxy = proxy_for_user(&local_staking, user, &app);
proxy
.unstake(user.to_owned(), coin(50, OSMO))
.call(owner)
.unstake(val.to_string(), coin(50, OSMO))
.call(user)
.unwrap();

assert_eq!(
Expand Down Expand Up @@ -1259,7 +1284,9 @@ fn multiple_stakes() {
let owner = "owner";
let user = "user1";

let app = init_app(&[user], &[1000]);
let mut app = init_app(&[user], &[1000]);
let local_validator = "local";
add_local_validator(&mut app, local_validator);

let slashing_percentage: u64 = 60;
let (vault, local_staking, cross_staking1) = setup(&app, owner, slashing_percentage, 100);
Expand All @@ -1280,7 +1307,7 @@ fn multiple_stakes() {
// lienholder creation. It is guaranteed to work as MT assigns increasing names to the
// contracts, and liens are queried ascending by the lienholder.

stake_locally(&vault, user, 300);
stake_locally(&vault, user, 300, local_validator).unwrap();

stake_remotely(&vault, &cross_staking1, user, &[validator], &[200]);

Expand Down Expand Up @@ -1569,7 +1596,9 @@ fn cross_slash_scenario_1() {
let validators = vec!["validator1", "validator2"];
let validator1 = validators[0];

let app = init_app(&[user], &[collateral]);
let mut app = init_app(&[user], &[1000]);
let local_validator = "local";
add_local_validator(&mut app, local_validator);

let (vault, local_staking, cross_staking) = setup(&app, owner, slashing_percentage, 100);

Expand All @@ -1581,7 +1610,7 @@ fn cross_slash_scenario_1() {

// Stake some tokens locally
let local_stake = 190;
stake_locally(&vault, user, local_stake);
stake_locally(&vault, user, local_stake, local_validator).unwrap();

// Stake some tokens remotely
stake_remotely(&vault, &cross_staking, user, &validators, &[100, 50]);
Expand Down Expand Up @@ -1672,7 +1701,9 @@ fn cross_slash_scenario_2() {
let validators = vec!["validator1", "validator2"];
let validator1 = validators[0];

let app = init_app(&[user], &[collateral]);
let mut app = init_app(&[user], &[1000]);
let local_validator = "local";
add_local_validator(&mut app, local_validator);

let (vault, local_staking, cross_staking) = setup(&app, owner, slashing_percentage, 100);

Expand All @@ -1684,7 +1715,7 @@ fn cross_slash_scenario_2() {

// Stake some tokens locally
let local_stake = 200;
stake_locally(&vault, user, local_stake);
stake_locally(&vault, user, local_stake, local_validator).unwrap();

// Stake some tokens remotely
stake_remotely(&vault, &cross_staking, user, &[validator1], &[200]);
Expand Down Expand Up @@ -1766,7 +1797,9 @@ fn cross_slash_scenario_3() {
let validators = vec!["validator1", "validator2"];
let validator1 = validators[0];

let app = init_app(&[user], &[collateral]);
let mut app = init_app(&[user], &[1000]);
let local_validator = "local";
add_local_validator(&mut app, local_validator);

let (vault, local_staking, cross_staking) = setup(&app, owner, slashing_percentage, 100);

Expand All @@ -1778,7 +1811,7 @@ fn cross_slash_scenario_3() {

// Stake some tokens locally
let local_stake = 190;
stake_locally(&vault, user, local_stake);
stake_locally(&vault, user, local_stake, local_validator).unwrap();

// Stake some tokens remotely
stake_remotely(&vault, &cross_staking, user, &[validator1], &[150]);
Expand Down Expand Up @@ -1861,7 +1894,9 @@ fn cross_slash_scenario_4() {
let validators_2 = vec!["validator3", "validator4"];
let validator1 = validators_1[0];

let app = init_app(&[user], &[collateral]);
let mut app = init_app(&[user], &[1000]);
let local_validator = "local";
add_local_validator(&mut app, local_validator);

let (vault, local_staking, cross_staking_1) = setup(&app, owner, slashing_percentage, 100);
let cross_staking_2 = setup_cross_stake(&app, owner, &vault, slashing_percentage, 100);
Expand All @@ -1875,7 +1910,7 @@ fn cross_slash_scenario_4() {

// Stake some tokens locally
let local_stake = 190;
stake_locally(&vault, user, local_stake);
stake_locally(&vault, user, local_stake, local_validator).unwrap();

// Stake some tokens remotely
stake_remotely(&vault, &cross_staking_1, user, &validators_1, &[140, 40]);
Expand Down Expand Up @@ -1963,12 +1998,14 @@ fn cross_slash_scenario_5() {
// Remote slashing percentage
let slashing_percentage = 50;
let collateral = 200;
let validators = vec!["validator1", "validator2", "validator3"];
let validators = ["validator1", "validator2", "validator3"];
let validator1 = validators[0];
let validator2 = validators[1];
let validator3 = validators[2];

let app = init_app(&[user], &[collateral]);
let mut app = init_app(&[user], &[1000]);
let local_validator = "local";
add_local_validator(&mut app, local_validator);

let (vault, local_staking, cross_staking_1) = setup(&app, owner, slashing_percentage, 100);
let cross_staking_2 = setup_cross_stake(&app, owner, &vault, slashing_percentage, 100);
Expand All @@ -1984,7 +2021,7 @@ fn cross_slash_scenario_5() {

// Stake some tokens locally
let local_stake = 100;
stake_locally(&vault, user, local_stake);
stake_locally(&vault, user, local_stake, local_validator).unwrap();

// Stake some tokens remotely
stake_remotely(&vault, &cross_staking_1, user, &[validator1], &[180]);
Expand Down

0 comments on commit dc2ccf0

Please sign in to comment.