Skip to content

Commit

Permalink
Basic contract is ready
Browse files Browse the repository at this point in the history
  • Loading branch information
faust403 committed Nov 28, 2024
1 parent 8177fbd commit feb2453
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 46 deletions.
20 changes: 14 additions & 6 deletions contracts/lazy-staking/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,23 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
}

fn query_rewards(deps: Deps, env: Env) -> StdResult<Uint128> {
let exchange_rate = query_exchange_rate(deps, env.clone())?;
let config = CONFIG.load(deps.storage)?;
let base_denom = config.base_denom;
let base_denom = CONFIG.load(deps.storage)?.base_denom;
let dasset_contract_balance = deps
.querier
.query_balance(env.contract.address, base_denom.clone())?;
let rewards = dasset_contract_balance.amount
- Decimal::one() / exchange_rate * dasset_contract_balance.amount;
Ok(rewards)
let core_exchange_rate = query_core_exchange_rate(deps)?;
let asset_contract_balance = core_exchange_rate.checked_mul(Decimal::from_ratio(
dasset_contract_balance.amount,
Uint128::one(),
))?;
let lazy_denom: String = DENOM.load(deps.storage)?;
let lazy_total_supply = deps.querier.query_supply(lazy_denom)?;
let backing_excess_asset = lazy_total_supply
.amount
.abs_diff(asset_contract_balance.to_uint_floor());
let backing_excess_dasset =
Decimal::from_ratio(backing_excess_asset, Uint128::one()) / core_exchange_rate;
Ok(backing_excess_dasset.to_uint_floor())
}

fn query_exchange_rate(deps: Deps, env: Env) -> StdResult<Decimal> {
Expand Down
207 changes: 167 additions & 40 deletions contracts/lazy-staking/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,71 +1,197 @@
use crate::contract::query;
use crate::msg::QueryMsg;
use crate::state::{Config, CONFIG, DENOM};
use crate::contract::{execute, query};
use crate::msg::{ExecuteMsg, QueryMsg};
use crate::state::{Config, SplittingTarget, CONFIG, DENOM};
use cosmwasm_std::testing::mock_info;
use cosmwasm_std::BalanceResponse;
use cosmwasm_std::{
from_json, testing::mock_env, to_json_binary, Api, Coin, Decimal, SupplyResponse, Uint128,
};
use drop_helpers::testing::mock_dependencies;
use drop_helpers::testing::mock_dependencies_with_api;
use drop_staking_base::state::factory::State;

#[test]
fn test_query_exchange_rate() {
let mut deps = mock_dependencies(&[]);
let lazy_denom = "lazy_denom".to_string();
let factory_addr = deps.api.addr_validate("factory_addr").unwrap();
DENOM.save(deps.as_mut().storage, &lazy_denom).unwrap();
fn test_execute_lazy_denom() {
let mut deps = mock_dependencies_with_api(&[]);
let splitting_targets = vec![
SplittingTarget {
addr: deps.api.addr_validate("recipient1").unwrap(),
unbonding_weight: Uint128::from(100u128),
},
SplittingTarget {
addr: deps.api.addr_validate("recipient2").unwrap(),
unbonding_weight: Uint128::from(100u128),
},
];
CONFIG
.save(
deps.as_mut().storage,
&Config {
factory_addr: factory_addr.into_string(),
base_denom: "base_denom".to_string(),
splitting_targets: vec![],
factory_addr: "factory_contract".to_string(),
base_denom: "dAsset".to_string(),
splitting_targets,
},
)
.unwrap();
deps.querier.add_wasm_query_response("factory_addr", |_| {
to_json_binary(&State {
token_contract: "token_contract".to_string(),
core_contract: "core_contract".to_string(),
puppeteer_contract: "puppeteer_contract".to_string(),
withdrawal_voucher_contract: "withdrawal_voucher_contract".to_string(),
withdrawal_manager_contract: "withdrawal_manager_contract".to_string(),
strategy_contract: "strategy_contract".to_string(),
validators_set_contract: "validators_set_contract".to_string(),
distribution_contract: "distribution_contract".to_string(),
rewards_manager_contract: "rewards_manager_contract".to_string(),
rewards_pump_contract: "rewards_pump_contract".to_string(),
splitter_contract: "splitter_contract".to_string(),
lsm_share_bond_provider_contract: "lsm_share_bond_provider_contract".to_string(),
native_bond_provider_contract: "native_bond_provider_contract".to_string(),
})
DENOM
.save(deps.as_mut().storage, &"lAsset".to_string())
.unwrap();
deps.querier
.add_wasm_query_response("factory_contract", |_| {
to_json_binary(&State {
token_contract: "token_contract".to_string(),
core_contract: "core_contract".to_string(),
puppeteer_contract: "puppeteer_contract".to_string(),
withdrawal_voucher_contract: "withdrawal_voucher_contract".to_string(),
withdrawal_manager_contract: "withdrawal_manager_contract".to_string(),
strategy_contract: "strategy_contract".to_string(),
validators_set_contract: "validators_set_contract".to_string(),
distribution_contract: "distribution_contract".to_string(),
rewards_manager_contract: "rewards_manager_contract".to_string(),
rewards_pump_contract: "rewards_pump_contract".to_string(),
splitter_contract: "splitter_contract".to_string(),
lsm_share_bond_provider_contract: "lsm_share_bond_provider_contract".to_string(),
native_bond_provider_contract: "native_bond_provider_contract".to_string(),
})
.unwrap()
});
deps.querier.add_wasm_query_response("core_contract", |_| {
to_json_binary(&Decimal::from_ratio(
Uint128::from(200u128),
Uint128::from(100u128),
))
.unwrap()
});
let res = execute(
deps.as_mut().into_empty(),
mock_env(),
mock_info(
"sender",
&[Coin {
denom: "dAsset".to_string(),
amount: Uint128::from(100u128),
}],
),
ExecuteMsg::Bond {},
)
.unwrap();
deps.querier
.add_wasm_query_response("factory_contract", |_| {
to_json_binary(&State {
token_contract: "token_contract".to_string(),
core_contract: "core_contract".to_string(),
puppeteer_contract: "puppeteer_contract".to_string(),
withdrawal_voucher_contract: "withdrawal_voucher_contract".to_string(),
withdrawal_manager_contract: "withdrawal_manager_contract".to_string(),
strategy_contract: "strategy_contract".to_string(),
validators_set_contract: "validators_set_contract".to_string(),
distribution_contract: "distribution_contract".to_string(),
rewards_manager_contract: "rewards_manager_contract".to_string(),
rewards_pump_contract: "rewards_pump_contract".to_string(),
splitter_contract: "splitter_contract".to_string(),
lsm_share_bond_provider_contract: "lsm_share_bond_provider_contract".to_string(),
native_bond_provider_contract: "native_bond_provider_contract".to_string(),
})
.unwrap()
});
deps.querier.add_wasm_query_response("core_contract", |_| {
to_json_binary(&Decimal::from_ratio(
Uint128::from(123u128),
Uint128::from(300u128),
Uint128::from(100u128),
))
.unwrap()
});
let mut supply_response = SupplyResponse::default();
supply_response.amount = Coin {
denom: lazy_denom.clone(),
amount: Uint128::from(100u128),
};
let res = execute(
deps.as_mut().into_empty(),
mock_env(),
mock_info(
"sender",
&[Coin {
denom: "dAsset".to_string(),
amount: Uint128::from(200u128),
}],
),
ExecuteMsg::Bond {},
)
.unwrap();
deps.querier
.add_bank_query_supply_response(lazy_denom.clone(), supply_response);
.add_wasm_query_response("factory_contract", |_| {
to_json_binary(&State {
token_contract: "token_contract".to_string(),
core_contract: "core_contract".to_string(),
puppeteer_contract: "puppeteer_contract".to_string(),
withdrawal_voucher_contract: "withdrawal_voucher_contract".to_string(),
withdrawal_manager_contract: "withdrawal_manager_contract".to_string(),
strategy_contract: "strategy_contract".to_string(),
validators_set_contract: "validators_set_contract".to_string(),
distribution_contract: "distribution_contract".to_string(),
rewards_manager_contract: "rewards_manager_contract".to_string(),
rewards_pump_contract: "rewards_pump_contract".to_string(),
splitter_contract: "splitter_contract".to_string(),
lsm_share_bond_provider_contract: "lsm_share_bond_provider_contract".to_string(),
native_bond_provider_contract: "native_bond_provider_contract".to_string(),
})
.unwrap()
});
deps.querier.add_wasm_query_response("core_contract", |_| {
to_json_binary(&Decimal::from_ratio(
Uint128::from(1000u128),
Uint128::from(100u128),
))
.unwrap()
});
let res = execute(
deps.as_mut().into_empty(),
mock_env(),
mock_info(
"sender",
&[Coin {
denom: "lAsset".to_string(),
amount: Uint128::from(800u128),
}],
),
ExecuteMsg::Unbond {},
)
.unwrap();
deps.querier
.add_wasm_query_response("factory_contract", |_| {
to_json_binary(&State {
token_contract: "token_contract".to_string(),
core_contract: "core_contract".to_string(),
puppeteer_contract: "puppeteer_contract".to_string(),
withdrawal_voucher_contract: "withdrawal_voucher_contract".to_string(),
withdrawal_manager_contract: "withdrawal_manager_contract".to_string(),
strategy_contract: "strategy_contract".to_string(),
validators_set_contract: "validators_set_contract".to_string(),
distribution_contract: "distribution_contract".to_string(),
rewards_manager_contract: "rewards_manager_contract".to_string(),
rewards_pump_contract: "rewards_pump_contract".to_string(),
splitter_contract: "splitter_contract".to_string(),
lsm_share_bond_provider_contract: "lsm_share_bond_provider_contract".to_string(),
native_bond_provider_contract: "native_bond_provider_contract".to_string(),
})
.unwrap()
});
deps.querier.add_wasm_query_response("core_contract", |_| {
to_json_binary(&Decimal::from_ratio(
Uint128::from(1000u128),
Uint128::from(100u128),
))
.unwrap()
});
deps.querier.add_bank_query_response(
mock_env().contract.address.to_string(),
mock_env().contract.address.into_string(),
BalanceResponse {
amount: Coin {
denom: "base_denom".to_string(),
amount: Uint128::from(100u128),
denom: "dAsset".to_string(),
amount: Uint128::from(300u128),
},
},
);
let res: Decimal = from_json(
let mut supply_response = SupplyResponse::default();
supply_response.amount = Coin::new(800u128, "lAsset".to_string());
deps.querier
.add_bank_query_supply_response("lAsset".to_string(), supply_response);
let exchange_rate: Decimal = from_json(
query(
deps.as_ref().into_empty(),
mock_env(),
Expand All @@ -74,5 +200,6 @@ fn test_query_exchange_rate() {
.unwrap(),
)
.unwrap();
println!("{:?}", res)
println!("{:?}", res);
println!("{:?}", exchange_rate);
}

0 comments on commit feb2453

Please sign in to comment.