From 132606f59a88548b15450264edccb869ab321410 Mon Sep 17 00:00:00 2001 From: Mauro Lacy Date: Thu, 22 Jun 2023 12:36:09 +0200 Subject: [PATCH] Fix / adapt existing tests --- .../external-staking/src/multitest.rs | 141 ++++++++++++++++-- 1 file changed, 125 insertions(+), 16 deletions(-) diff --git a/contracts/provider/external-staking/src/multitest.rs b/contracts/provider/external-staking/src/multitest.rs index 140c4805..8a29dfc0 100644 --- a/contracts/provider/external-staking/src/multitest.rs +++ b/contracts/provider/external-staking/src/multitest.rs @@ -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( @@ -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(), @@ -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(), @@ -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(), @@ -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(), []); @@ -288,7 +314,7 @@ fn staking() { } #[track_caller] -fn get_last_pending_tx_id(vault: &VaultContractProxy) -> Option { +fn get_last_vault_pending_tx_id(vault: &VaultContractProxy) -> Option { let txs = vault.all_pending_txs_desc(None, None).unwrap().txs; txs.first().map(|tx| match tx { Tx::InFlightStaking { id, .. } => *id, @@ -297,6 +323,16 @@ fn get_last_pending_tx_id(vault: &VaultContractProxy) -> Option { }) } +#[track_caller] +fn get_last_external_staking_pending_tx_id(contract: &ExternalStakingContractProxy) -> Option { + 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"]; @@ -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( @@ -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( @@ -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 @@ -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 @@ -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 @@ -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( @@ -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( @@ -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] @@ -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] @@ -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 @@ -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]: //