Skip to content

Commit 3377a14

Browse files
committed
chore(lst): staker tests
1 parent b552ce5 commit 3377a14

File tree

4 files changed

+108
-12
lines changed

4 files changed

+108
-12
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ ibc-union-msg = { path = "cosmwasm/ibc-union/core/msg", default-feature
391391

392392
frissitheto = { path = "lib/frissitheto", default-features = false }
393393

394-
access-manager = { path = "cosmwasm/access-manager", default-features = false }
394+
access-manager = { path = "cosmwasm/access-manager", default-features = false }
395395

396396
lst = { path = "cosmwasm/lst", default-features = false }
397397

cosmwasm/lst-staker/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ fn withdraw_all_rewards(
178178

179179
// NOTE: Sum the individual rewards, *NOT* the total rewards
180180
// if there are rewards of 1.5 and 0.5, the total will be 2.0 (even after flooring it), but the *actual* claimable amount is floor(1.5) + floor(0.5), which is 1
181-
// TODO: Add a test for this
182181
let total_pending_rewards = delegation_total_rewards
183182
.rewards
184183
.iter()

cosmwasm/lst-staker/src/tests.rs

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
use std::collections::BTreeMap;
22

33
use cosmwasm_std::{
4+
from_json,
45
testing::{message_info, mock_dependencies, mock_env, MockApi, MockQuerier, MockStorage},
5-
Addr, Event, Order, OwnedDeps, Response, Uint128,
6+
to_json_binary, Addr, ContractResult, DecCoin, Decimal, DistributionMsg, Event, Order,
7+
OwnedDeps, QuerierResult, Response, Uint128, WasmQuery,
68
};
79
use cw_account::{
810
state::{Admins, Zkgm},
911
types::{Admin, LocalAdmin},
1012
};
1113
use depolama::StorageExt;
14+
use lst::{msg::ConfigResponse, types::ProtocolFeeConfig};
1215

13-
use crate::{execute, msg::ExecuteMsg, ContractError};
16+
use crate::{execute, msg::ExecuteMsg, withdraw_all_rewards, ContractError};
1417

1518
const ADMIN: &str = "admin";
19+
const LST_HUB: &str = "lst-hub";
1620

1721
fn setup_local() -> OwnedDeps<MockStorage, MockApi, MockQuerier> {
1822
let mut deps = mock_dependencies();
@@ -41,6 +45,14 @@ fn setup_local() -> OwnedDeps<MockStorage, MockApi, MockQuerier> {
4145
// zkgm does not exist after a local admin init
4246
assert!(deps.storage.maybe_read_item::<Zkgm>().unwrap().is_none());
4347

48+
execute(
49+
deps.as_mut(),
50+
mock_env(),
51+
message_info(&Addr::unchecked(ADMIN), &[]),
52+
ExecuteMsg::SetLstHubAddress(Addr::unchecked(LST_HUB)),
53+
)
54+
.unwrap();
55+
4456
deps
4557
}
4658

@@ -108,7 +120,7 @@ fn admin_ops_require_admin() {
108120
fn lst_ops_require_lst() {
109121
let mut deps = setup_local();
110122

111-
let lst_hub = Addr::unchecked("lst-hub");
123+
let lst_hub = Addr::unchecked(LST_HUB);
112124
let non_admin = Addr::unchecked("non-admin");
113125

114126
assert_eq!(
@@ -149,3 +161,84 @@ fn lst_ops_require_lst() {
149161
},
150162
);
151163
}
164+
165+
#[test]
166+
fn withdraw_all_rewards_floors_correctly() {
167+
let mut deps = setup_local();
168+
let env = mock_env();
169+
170+
deps.querier.update_wasm(|w| match w {
171+
WasmQuery::Smart { contract_addr, msg } => match &**contract_addr {
172+
LST_HUB => match from_json::<lst::msg::QueryMsg>(msg).unwrap() {
173+
lst::msg::QueryMsg::Config {} => QuerierResult::Ok(ContractResult::Ok(
174+
to_json_binary(&ConfigResponse {
175+
native_token_denom: "au".to_owned(),
176+
minimum_liquid_stake_amount: Default::default(),
177+
protocol_fee_config: ProtocolFeeConfig {
178+
fee_rate: Default::default(),
179+
fee_recipient: "".to_owned(),
180+
},
181+
monitors: Default::default(),
182+
lst_address: Addr::unchecked(""),
183+
staker_address: Addr::unchecked(""),
184+
batch_period_seconds: Default::default(),
185+
unbonding_period_seconds: Default::default(),
186+
stopped: Default::default(),
187+
})
188+
.unwrap(),
189+
)),
190+
_ => todo!(),
191+
},
192+
_ => todo!(),
193+
},
194+
_ => todo!(),
195+
});
196+
197+
let (native_token_denom, total_pending_rewards, withdraw_msgs) =
198+
withdraw_all_rewards(deps.as_ref(), &env).unwrap();
199+
200+
assert_eq!(native_token_denom, "au");
201+
assert_eq!(total_pending_rewards, Uint128::zero());
202+
assert_eq!(withdraw_msgs.collect::<Vec<_>>(), vec![]);
203+
204+
deps.querier.distribution.set_rewards(
205+
"val-1",
206+
&env.contract.address,
207+
vec![DecCoin::new("1.5".parse::<Decimal>().unwrap(), "au")],
208+
);
209+
210+
let (native_token_denom, total_pending_rewards, withdraw_msgs) =
211+
withdraw_all_rewards(deps.as_ref(), &env).unwrap();
212+
213+
assert_eq!(native_token_denom, "au");
214+
assert_eq!(total_pending_rewards, Uint128::new(1));
215+
assert_eq!(
216+
withdraw_msgs.collect::<Vec<_>>(),
217+
vec![DistributionMsg::WithdrawDelegatorReward {
218+
validator: "val-1".to_owned()
219+
}]
220+
);
221+
222+
deps.querier.distribution.set_rewards(
223+
"val-2",
224+
&env.contract.address,
225+
vec![DecCoin::new("0.5".parse::<Decimal>().unwrap(), "au")],
226+
);
227+
228+
let (native_token_denom, total_pending_rewards, withdraw_msgs) =
229+
withdraw_all_rewards(deps.as_ref(), &env).unwrap();
230+
231+
assert_eq!(native_token_denom, "au");
232+
assert_eq!(total_pending_rewards, Uint128::new(1));
233+
assert_eq!(
234+
withdraw_msgs.collect::<Vec<_>>(),
235+
vec![
236+
DistributionMsg::WithdrawDelegatorReward {
237+
validator: "val-1".to_owned()
238+
},
239+
DistributionMsg::WithdrawDelegatorReward {
240+
validator: "val-2".to_owned()
241+
}
242+
]
243+
);
244+
}

cosmwasm/lst/src/tests/bond_tests.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@
6161
use cosmwasm_std::{
6262
coins,
6363
testing::{message_info, mock_env},
64-
to_json_binary, Addr, BankMsg, CosmosMsg, Decimal, Event, StdError, WasmMsg,
64+
to_json_binary, wasm_execute, Addr, CosmosMsg, Decimal, Event, StdError, WasmMsg,
6565
};
6666
use cw20::Cw20ExecuteMsg;
6767
use depolama::StorageExt;
6868

6969
use crate::{
7070
contract::execute,
7171
error::ContractError,
72-
msg::ExecuteMsg,
72+
msg::{ExecuteMsg, StakerExecuteMsg},
7373
query::query_state,
7474
state::AccountingStateStore,
7575
tests::test_helper::{
@@ -92,13 +92,17 @@ fn bond_without_pending_rewards() {
9292

9393
let res = execute(deps.as_mut(), mock_env(), info.clone(), msg.clone()).unwrap();
9494

95-
// the native funds are sent to the staker
95+
// the native funds are staked
9696
assert_eq!(
9797
res.messages[0].msg,
98-
CosmosMsg::Bank(BankMsg::Send {
99-
to_address: STAKER_ADDRESS.into(),
100-
amount: info.funds.clone()
101-
})
98+
CosmosMsg::Wasm(
99+
wasm_execute(
100+
STAKER_ADDRESS.to_owned(),
101+
&StakerExecuteMsg::Stake {},
102+
coins(1000, NATIVE_TOKEN)
103+
)
104+
.unwrap()
105+
),
102106
);
103107

104108
// 1000 LST token is minted to the `mint_to` address.

0 commit comments

Comments
 (0)