Skip to content

Commit

Permalink
Add IBC burn test
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolacy committed Nov 3, 2023
1 parent 40204f7 commit 2fb7b62
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
21 changes: 21 additions & 0 deletions contracts/consumer/converter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,27 @@ impl ConverterContract<'_> {
}
}

/// This is only used for tests.
/// Ideally we want conditional compilation of these whole methods and the enum variants
#[msg(exec)]
fn test_burn(
&self,
ctx: ExecCtx,
validators: Vec<String>,
burn: Coin,
) -> Result<Response, ContractError> {
#[cfg(any(test, feature = "mt"))]
{
// This can only ever be called in tests
self.burn(ctx.deps, &validators, burn)
}
#[cfg(not(any(test, feature = "mt")))]
{
let _ = (ctx, validators, burn);
Err(ContractError::Unauthorized)
}
}

#[msg(query)]
fn config(&self, ctx: QueryCtx) -> Result<ConfigResponse, ContractError> {
let config = self.config.load(ctx.deps.storage)?;
Expand Down
86 changes: 86 additions & 0 deletions contracts/consumer/converter/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,92 @@ fn ibc_stake_and_unstake() {
);
}

#[test]
fn ibc_stake_and_burn() {
let app = App::default();

let owner = "sunny"; // Owner of the staking contract (i. e. the vault contract)
let admin = "theman";
let discount = Decimal::percent(40); // 1 OSMO worth of JUNO should give 0.6 OSMO of stake
let native_per_foreign = Decimal::percent(50); // 1 JUNO is worth 0.5 OSMO

let SetupResponse {
price_feed: _,
converter,
virtual_staking,
} = setup(
&app,
SetupArgs {
owner,
admin,
discount,
native_per_foreign,
},
);

// no one is staked
let val1 = "Val Kilmer";
let val2 = "Valley Girl";
assert!(virtual_staking.all_stake().unwrap().stakes.is_empty());
assert_eq!(
virtual_staking
.stake(val1.to_string())
.unwrap()
.stake
.u128(),
0
);
assert_eq!(
virtual_staking
.stake(val2.to_string())
.unwrap()
.stake
.u128(),
0
);

// let's stake some
converter
.test_stake(val1.to_string(), coin(1000, JUNO))
.call(owner)
.unwrap();
converter
.test_stake(val2.to_string(), coin(4000, JUNO))
.call(owner)
.unwrap();

// and burn some
converter
.test_burn(vec![val2.to_string()], coin(2000, JUNO))
.call(owner)
.unwrap();

// and check the stakes (1000 * 0.6 * 0.5 = 300) (2000 * 0.6 * 0.5 = 600)
assert_eq!(
virtual_staking
.stake(val1.to_string())
.unwrap()
.stake
.u128(),
300
);
assert_eq!(
virtual_staking
.stake(val2.to_string())
.unwrap()
.stake
.u128(),
600
);
assert_eq!(
virtual_staking.all_stake().unwrap().stakes,
vec![
(val1.to_string(), Uint128::new(300)),
(val2.to_string(), Uint128::new(600)),
]
);
}

#[test]
fn valset_update_works() {
let app = App::default();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ impl VirtualStakingApi for VirtualStakingMock<'_> {
) -> Result<Response, Self::Error> {
nonpayable(&ctx.info)?;
let cfg = self.config.load(ctx.deps.storage)?;
ensure_eq!(ctx.info.sender, cfg.converter, ContractError::Unauthorized);
// only the converter can call this
ensure_eq!(ctx.info.sender, cfg.converter, ContractError::Unauthorized);
ensure_eq!(
amount.denom,
cfg.denom,
Expand Down

0 comments on commit 2fb7b62

Please sign in to comment.