Skip to content

Commit

Permalink
Fix / adapt existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolacy committed Jun 22, 2023
1 parent 319963f commit 639873f
Showing 1 changed file with 125 additions and 16 deletions.
141 changes: 125 additions & 16 deletions contracts/provider/external-staking/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,20 @@ fn staking() {
)
.call(users[0])
.unwrap();

let last_tx = get_last_pending_tx_id(&vault).unwrap();
// Hardcoded commit_tx call (lack of IBC support yet)
// TODO: Hardcoded vault's commit_tx call (lack of IBC support yet)
let last_tx = get_last_vault_pending_tx_id(&vault).unwrap();
vault
.vault_api_proxy()
.commit_tx(last_tx)
.call(contract.contract_addr.as_str())
.unwrap();
// TODO: Hardcoded external-staking's commit_stake call (lack of IBC support yet).
// This should be through `IbcPacketAckMsg`
let last_external_staking_tx = get_last_external_staking_pending_tx_id(&contract).unwrap();
contract
.commit_stake(last_external_staking_tx)
.call("test")
.unwrap();

vault
.stake_remote(
Expand All @@ -155,10 +161,15 @@ fn staking() {

vault
.vault_api_proxy()
.commit_tx(get_last_pending_tx_id(&vault).unwrap())
.commit_tx(get_last_vault_pending_tx_id(&vault).unwrap())
.call(contract.contract_addr.as_str())
.unwrap();

contract
.commit_stake(get_last_external_staking_pending_tx_id(&contract).unwrap())
.call("test")
.unwrap();

vault
.stake_remote(
contract.contract_addr.to_string(),
Expand All @@ -173,10 +184,15 @@ fn staking() {

vault
.vault_api_proxy()
.commit_tx(get_last_pending_tx_id(&vault).unwrap())
.commit_tx(get_last_vault_pending_tx_id(&vault).unwrap())
.call(contract.contract_addr.as_str())
.unwrap();

contract
.commit_stake(get_last_external_staking_pending_tx_id(&contract).unwrap())
.call("test")
.unwrap();

vault
.stake_remote(
contract.contract_addr.to_string(),
Expand All @@ -191,10 +207,15 @@ fn staking() {

vault
.vault_api_proxy()
.commit_tx(get_last_pending_tx_id(&vault).unwrap())
.commit_tx(get_last_vault_pending_tx_id(&vault).unwrap())
.call(contract.contract_addr.as_str())
.unwrap();

contract
.commit_stake(get_last_external_staking_pending_tx_id(&contract).unwrap())
.call("test")
.unwrap();

vault
.stake_remote(
contract.contract_addr.to_string(),
Expand All @@ -208,10 +229,15 @@ fn staking() {
.unwrap();
vault
.vault_api_proxy()
.commit_tx(get_last_pending_tx_id(&vault).unwrap())
.commit_tx(get_last_vault_pending_tx_id(&vault).unwrap())
.call(contract.contract_addr.as_str())
.unwrap();

contract
.commit_stake(get_last_external_staking_pending_tx_id(&contract).unwrap())
.call("test")
.unwrap();

// All tokens should be only on the vault contract
assert_eq!(app.app().wrap().query_all_balances(users[0]).unwrap(), []);
assert_eq!(app.app().wrap().query_all_balances(users[1]).unwrap(), []);
Expand Down Expand Up @@ -288,7 +314,7 @@ fn staking() {
}

#[track_caller]
fn get_last_pending_tx_id(vault: &VaultContractProxy) -> Option<u64> {
fn get_last_vault_pending_tx_id(vault: &VaultContractProxy) -> Option<u64> {
let txs = vault.all_pending_txs_desc(None, None).unwrap().txs;
txs.first().map(|tx| match tx {
Tx::InFlightStaking { id, .. } => *id,
Expand All @@ -297,6 +323,16 @@ fn get_last_pending_tx_id(vault: &VaultContractProxy) -> Option<u64> {
})
}

#[track_caller]
fn get_last_external_staking_pending_tx_id(contract: &ExternalStakingContractProxy) -> Option<u64> {
let txs = contract.all_pending_txs_desc(None, None).unwrap().txs;
txs.first().map(|tx| match tx {
Tx::InFlightStaking { id, .. } => *id,
Tx::InFlightRemoteStaking { id, .. } => *id,
Tx::InFlightRemoteUnstaking { id, .. } => *id,
})
}

#[test]
fn unstaking() {
let users = ["user1", "user2"];
Expand Down Expand Up @@ -347,13 +383,20 @@ fn unstaking() {
)
.call(users[0])
.unwrap();
let last_tx = get_last_pending_tx_id(&vault).unwrap();
// Hardcoded commit_tx call (lack of IBC support yet)
// TODO: Hardcoded vault's commit_tx call (lack of IBC support yet)
let last_tx = get_last_vault_pending_tx_id(&vault).unwrap();
vault
.vault_api_proxy()
.commit_tx(last_tx)
.call(contract.contract_addr.as_str())
.unwrap();
// TODO: Hardcoded external-staking's commit_stake call (lack of IBC support yet).
// This should be through `IbcPacketAckMsg`
let last_external_staking_tx = get_last_external_staking_pending_tx_id(&contract).unwrap();
contract
.commit_stake(last_external_staking_tx)
.call("test")
.unwrap();

vault
.stake_remote(
Expand All @@ -368,9 +411,14 @@ fn unstaking() {
.unwrap();
vault
.vault_api_proxy()
.commit_tx(get_last_pending_tx_id(&vault).unwrap())
.commit_tx(get_last_vault_pending_tx_id(&vault).unwrap())
.call(contract.contract_addr.as_str())
.unwrap();
let last_external_staking_tx = get_last_external_staking_pending_tx_id(&contract).unwrap();
contract
.commit_stake(last_external_staking_tx)
.call("test")
.unwrap();

vault
.stake_remote(
Expand All @@ -385,9 +433,14 @@ fn unstaking() {
.unwrap();
vault
.vault_api_proxy()
.commit_tx(get_last_pending_tx_id(&vault).unwrap())
.commit_tx(get_last_vault_pending_tx_id(&vault).unwrap())
.call(contract.contract_addr.as_str())
.unwrap();
let last_external_staking_tx = get_last_external_staking_pending_tx_id(&contract).unwrap();
contract
.commit_stake(last_external_staking_tx)
.call("test")
.unwrap();

// Properly unstake some tokens
// users[0] unstakes 50 from validators[0] - 150 left staken in 2 batches
Expand All @@ -396,16 +449,28 @@ fn unstaking() {
.unstake(validators[0].to_string(), coin(20, OSMO))
.call(users[0])
.unwrap();
contract
.commit_unstake(get_last_external_staking_pending_tx_id(&contract).unwrap())
.call("test")
.unwrap();

contract
.unstake(validators[0].to_string(), coin(30, OSMO))
.call(users[0])
.unwrap();
contract
.commit_unstake(get_last_external_staking_pending_tx_id(&contract).unwrap())
.call("test")
.unwrap();

contract
.unstake(validators[0].to_string(), coin(60, OSMO))
.call(users[1])
.unwrap();
contract
.commit_unstake(get_last_external_staking_pending_tx_id(&contract).unwrap())
.call("test")
.unwrap();

// Trying some unstakes over what is staken fails
let err = contract
Expand Down Expand Up @@ -501,11 +566,19 @@ fn unstaking() {
.unstake(validators[0].to_owned(), coin(70, OSMO))
.call(users[0])
.unwrap();
contract
.commit_unstake(get_last_external_staking_pending_tx_id(&contract).unwrap())
.call("test")
.unwrap();

contract
.unstake(validators[1].to_owned(), coin(90, OSMO))
.call(users[0])
.unwrap();
contract
.commit_unstake(get_last_external_staking_pending_tx_id(&contract).unwrap())
.call("test")
.unwrap();

// Verify proper stake values
let stake = contract
Expand Down Expand Up @@ -647,12 +720,19 @@ fn distribution() {
)
.call(users[0])
.unwrap();
// Hardcoded commit_tx call (lack of IBC support yet)
// TODO: Hardcoded vault's commit_tx call (lack of IBC support yet)
vault
.vault_api_proxy()
.commit_tx(get_last_pending_tx_id(&vault).unwrap())
.commit_tx(get_last_vault_pending_tx_id(&vault).unwrap())
.call(contract.contract_addr.as_str())
.unwrap();
// TODO: Hardcoded external-staking's commit_stake call (lack of IBC support yet).
// This should be through `IbcPacketAckMsg`
let last_external_staking_tx = get_last_external_staking_pending_tx_id(&contract).unwrap();
contract
.commit_stake(last_external_staking_tx)
.call("test")
.unwrap();

vault
.stake_remote(
Expand All @@ -667,9 +747,13 @@ fn distribution() {
.unwrap();
vault
.vault_api_proxy()
.commit_tx(get_last_pending_tx_id(&vault).unwrap())
.commit_tx(get_last_vault_pending_tx_id(&vault).unwrap())
.call(contract.contract_addr.as_str())
.unwrap();
contract
.commit_stake(get_last_external_staking_pending_tx_id(&contract).unwrap())
.call("test")
.unwrap();

vault
.stake_remote(
Expand All @@ -684,9 +768,13 @@ fn distribution() {
.unwrap();
vault
.vault_api_proxy()
.commit_tx(get_last_pending_tx_id(&vault).unwrap())
.commit_tx(get_last_vault_pending_tx_id(&vault).unwrap())
.call(contract.contract_addr.as_str())
.unwrap();
contract
.commit_stake(get_last_external_staking_pending_tx_id(&contract).unwrap())
.call("test")
.unwrap();

// Start with equal distribution:
// 20 tokens for users[0]
Expand Down Expand Up @@ -886,6 +974,10 @@ fn distribution() {
.unstake(validators[0].to_owned(), coin(100, OSMO))
.call(users[1])
.unwrap();
contract
.commit_unstake(get_last_external_staking_pending_tx_id(&contract).unwrap())
.call("test")
.unwrap();

// Staking also changes weights - now validators[1] also splits rewards:
// 1/4 for users[0]
Expand All @@ -904,6 +996,15 @@ fn distribution() {
)
.call(users[1])
.unwrap();
vault
.vault_api_proxy()
.commit_tx(get_last_vault_pending_tx_id(&vault).unwrap())
.call(contract.contract_addr.as_str())
.unwrap();
contract
.commit_stake(get_last_external_staking_pending_tx_id(&contract).unwrap())
.call("test")
.unwrap();

// Check if messing up with weights didn't affect withdrawable
let rewards = contract
Expand Down Expand Up @@ -1001,11 +1102,19 @@ fn distribution() {
.unstake(validators[0].to_owned(), coin(50, OSMO))
.call(users[0])
.unwrap();
contract
.commit_unstake(get_last_external_staking_pending_tx_id(&contract).unwrap())
.call("test")
.unwrap();

contract
.unstake(validators[0].to_owned(), coin(100, OSMO))
.call(users[1])
.unwrap();
contract
.commit_unstake(get_last_external_staking_pending_tx_id(&contract).unwrap())
.call("test")
.unwrap();

// Distribute 12 tokens to validator[0]:
//
Expand Down

0 comments on commit 639873f

Please sign in to comment.