From 042b3f57dc3ad672bce94282a277ca41f3e6e3f9 Mon Sep 17 00:00:00 2001 From: nahem Date: Tue, 20 Aug 2024 16:24:08 +0200 Subject: [PATCH] fix: math now accounts for osmosis_fee. Not all tests are passing --- .../bonding-manager/src/tests/claim.rs | 34 +++++ .../bonding-manager/src/tests/queries.rs | 17 +++ .../bonding-manager/src/tests/rewards.rs | 17 +++ .../src/tests/unbond_withdraw.rs | 17 +++ .../proptest-regressions/helpers.txt | 1 + .../src/tests/integration_tests.rs | 116 +++++++++++++++--- .../pool-network/incentive/src/claim.rs | 7 +- .../incentive/src/queries/get_rewards.rs | 7 +- .../src/execute/migrate_incentive.rs | 2 +- .../terraswap_pair/src/tests/swap.rs | 3 + 10 files changed, 195 insertions(+), 26 deletions(-) diff --git a/contracts/liquidity_hub/bonding-manager/src/tests/claim.rs b/contracts/liquidity_hub/bonding-manager/src/tests/claim.rs index 9d6d93dd..e0870f94 100644 --- a/contracts/liquidity_hub/bonding-manager/src/tests/claim.rs +++ b/contracts/liquidity_hub/bonding-manager/src/tests/claim.rs @@ -18,6 +18,23 @@ fn test_claim_successfully() { let asset_denoms = vec!["uwhale".to_string(), "uusdc".to_string()]; + #[cfg(feature = "osmosis")] + let pool_fees = PoolFee { + protocol_fee: Fee { + share: Decimal::from_ratio(1u128, 100u128), + }, + swap_fee: Fee { + share: Decimal::from_ratio(1u128, 100u128), + }, + burn_fee: Fee { + share: Decimal::zero(), + }, + extra_fees: vec![], + osmosis_fee: Fee { + share: Decimal::permille(1), + }, + }; + #[cfg(not(feature = "osmosis"))] let pool_fees = PoolFee { protocol_fee: Fee { @@ -895,6 +912,23 @@ fn test_rewards_forwarding() { extra_fees: vec![], }; + #[cfg(feature = "osmosis")] + let pool_fees = PoolFee { + protocol_fee: Fee { + share: Decimal::from_ratio(1u128, 100u128), + }, + swap_fee: Fee { + share: Decimal::from_ratio(1u128, 100u128), + }, + burn_fee: Fee { + share: Decimal::zero(), + }, + extra_fees: vec![], + osmosis_fee: Fee { + share: Decimal::permille(1), + }, + }; + suite .instantiate_default() .create_pair( diff --git a/contracts/liquidity_hub/bonding-manager/src/tests/queries.rs b/contracts/liquidity_hub/bonding-manager/src/tests/queries.rs index 7bc0e843..11e2659b 100644 --- a/contracts/liquidity_hub/bonding-manager/src/tests/queries.rs +++ b/contracts/liquidity_hub/bonding-manager/src/tests/queries.rs @@ -28,6 +28,23 @@ fn test_queries() { extra_fees: vec![], }; + #[cfg(feature = "osmosis")] + let pool_fees = PoolFee { + protocol_fee: Fee { + share: Decimal::from_ratio(1u128, 100u128), + }, + swap_fee: Fee { + share: Decimal::from_ratio(1u128, 100u128), + }, + burn_fee: Fee { + share: Decimal::zero(), + }, + extra_fees: vec![], + osmosis_fee: Fee { + share: Decimal::permille(1), + }, + }; + suite .instantiate_default() .bond(creator.clone(), &coins(1_000u128, "ampWHALE"), |result| { diff --git a/contracts/liquidity_hub/bonding-manager/src/tests/rewards.rs b/contracts/liquidity_hub/bonding-manager/src/tests/rewards.rs index 40d41b85..7b61c3dd 100644 --- a/contracts/liquidity_hub/bonding-manager/src/tests/rewards.rs +++ b/contracts/liquidity_hub/bonding-manager/src/tests/rewards.rs @@ -30,6 +30,23 @@ fn test_fill_rewards_from_pool_manager() { extra_fees: vec![], }; + #[cfg(feature = "osmosis")] + let pool_fees = PoolFee { + protocol_fee: Fee { + share: Decimal::from_ratio(1u128, 100u128), + }, + swap_fee: Fee { + share: Decimal::from_ratio(1u128, 100u128), + }, + burn_fee: Fee { + share: Decimal::zero(), + }, + extra_fees: vec![], + osmosis_fee: Fee { + share: Decimal::permille(1), + }, + }; + suite .instantiate_default() .fast_forward(90_000) diff --git a/contracts/liquidity_hub/bonding-manager/src/tests/unbond_withdraw.rs b/contracts/liquidity_hub/bonding-manager/src/tests/unbond_withdraw.rs index 19d8f65c..cee80ca8 100644 --- a/contracts/liquidity_hub/bonding-manager/src/tests/unbond_withdraw.rs +++ b/contracts/liquidity_hub/bonding-manager/src/tests/unbond_withdraw.rs @@ -34,6 +34,23 @@ fn test_unbonding_withdraw() { extra_fees: vec![], }; + #[cfg(feature = "osmosis")] + let pool_fees = PoolFee { + protocol_fee: Fee { + share: Decimal::from_ratio(1u128, 100u128), + }, + swap_fee: Fee { + share: Decimal::from_ratio(1u128, 100u128), + }, + burn_fee: Fee { + share: Decimal::zero(), + }, + extra_fees: vec![], + osmosis_fee: Fee { + share: Decimal::permille(1), + }, + }; + suite .instantiate_default() .bond(creator.clone(), &coins(1_000u128, "ampWHALE"), |result| { diff --git a/contracts/liquidity_hub/pool-manager/proptest-regressions/helpers.txt b/contracts/liquidity_hub/pool-manager/proptest-regressions/helpers.txt index 5ef4eb07..8c252455 100644 --- a/contracts/liquidity_hub/pool-manager/proptest-regressions/helpers.txt +++ b/contracts/liquidity_hub/pool-manager/proptest-regressions/helpers.txt @@ -7,3 +7,4 @@ cc 55857276de2241e3d09d36aba47854e0017db66f6c5a61e306b38ad0d3b8aeeb # shrinks to amp_factor = 1, initial_user_token_a_amount = 10000000, initial_user_token_b_amount = 10000000 cc 33456e9a9f11bed69ac5171155ce7a64f73f912fcbfede19046989302d1b2da9 # shrinks to amp_factor = 10, deposit_amount_a = 0, deposit_amount_b = 0, deposit_amount_c = 0, swap_token_a_amount = 0, swap_token_b_amount = 0, swap_token_c_amount = 1, pool_token_supply = 0 cc 75c3b0922c450b034b92dc8c2ea87edff47c90bbede702d84c9fd9c672e2f31f # shrinks to amp_factor = 141, deposit_amount_a = 308442737939502983046195411808336, deposit_amount_b = 0, deposit_amount_c = 0, swap_token_a_amount = 870112623450347049437652954298478, swap_token_b_amount = 501497230776538877048085549853566, swap_token_c_amount = 24063806364666791266594852039507, pool_token_supply = 2 +cc 0e8058d82bcea9791825f64915edb7336ff8f46a9e5400f9a035f2602838fb96 # shrinks to amp_factor = 139040, source_token_amount = 2201422791601550300438305308643575, swap_source_amount = 0, swap_destination_amount = 0, unswapped_amount = 0 diff --git a/contracts/liquidity_hub/pool-manager/src/tests/integration_tests.rs b/contracts/liquidity_hub/pool-manager/src/tests/integration_tests.rs index 1ada98dd..cae135ab 100644 --- a/contracts/liquidity_hub/pool-manager/src/tests/integration_tests.rs +++ b/contracts/liquidity_hub/pool-manager/src/tests/integration_tests.rs @@ -57,7 +57,7 @@ fn deposit_and_withdraw_sanity_check() { share: Decimal::zero(), }, osmosis_fee: Fee { - share: Decimal::zero(), + share: Decimal::permille(1), }, extra_fees: vec![], }; @@ -250,8 +250,9 @@ mod pool_creation_failures { ); } + // Wrong osmosis fee on pool creation; 0.01 instead of 0.001 #[test] - fn cant_recreate_existing_poo() { + fn wrong_osmosis_fee_on_pool_creation() { let mut suite = TestingSuite::default_with_balances(vec![ coin(1_000_000_001u128, "uwhale".to_string()), coin(1_000_000_000u128, "uluna".to_string()), @@ -290,11 +291,88 @@ mod pool_creation_failures { share: Decimal::zero(), }, osmosis_fee: Fee { + share: Decimal::permille(1), + }, + extra_fees: vec![], + }; + // Create a pool + suite + .instantiate_default() + .add_one_day() + .create_new_epoch(|result| { + result.unwrap(); + }) + .create_pool( + creator.clone(), + asset_infos, + vec![6u8, 6u8], + pool_fees, + PoolType::ConstantProduct, + None, + vec![coin(1000, "uusd")], + |result| { + #[cfg(feature = "osmosis")] + { + let err = result.unwrap_err().downcast::().unwrap(); + match err { + ContractError::InvalidPoolCreationFee { .. } => {} + _ => panic!( + "Wrong error type, should return ContractError::InvalidPoolCreationFee" + ), + } + } + #[cfg(not(feature = "osmosis"))] + result.unwrap(); + + }, + ); + } + + #[test] + fn cant_recreate_existing_poo() { + let mut suite = TestingSuite::default_with_balances(vec![ + coin(1_000_000_001u128, "uwhale".to_string()), + coin(1_000_000_000u128, "uluna".to_string()), + coin(1_000_000_001u128, "uusd".to_string()), + ]); + let creator = suite.creator(); + let _other = suite.senders[1].clone(); + let _unauthorized = suite.senders[2].clone(); + // Asset infos with uwhale and cw20 + + let asset_infos = vec!["uwhale".to_string()]; + + #[cfg(not(feature = "osmosis"))] + let pool_fees = PoolFee { + protocol_fee: Fee { + share: Decimal::zero(), + }, + swap_fee: Fee { + share: Decimal::zero(), + }, + burn_fee: Fee { share: Decimal::zero(), }, extra_fees: vec![], }; + #[cfg(feature = "osmosis")] + let pool_fees = PoolFee { + protocol_fee: Fee { + share: Decimal::zero(), + }, + swap_fee: Fee { + share: Decimal::zero(), + }, + burn_fee: Fee { + share: Decimal::zero(), + }, + osmosis_fee: Fee { + share: Decimal::permille(1), + }, + extra_fees: vec![], + }; + // Create a poo suite .instantiate_default() @@ -334,7 +412,7 @@ mod pool_creation_failures { } mod router { - use cosmwasm_std::{Event, StdError}; + use cosmwasm_std::{assert_approx_eq, Event, StdError}; use white_whale_std::pool_manager::{SwapRoute, SwapRouteCreatorResponse}; @@ -380,7 +458,7 @@ mod router { share: Decimal::bps(50), }, osmosis_fee: Fee { - share: Decimal::bps(50), + share: Decimal::permille(1), }, extra_fees: vec![], }; @@ -578,7 +656,7 @@ mod router { share: Decimal::zero(), }, osmosis_fee: Fee { - share: Decimal::from_ratio(1u128, 100_000u128), + share: Decimal::permille(1), }, extra_fees: vec![], }; @@ -726,7 +804,7 @@ mod router { share: Decimal::zero(), }, osmosis_fee: Fee { - share: Decimal::from_ratio(1u128, 100_000u128), + share: Decimal::permille(1), }, extra_fees: vec![], }; @@ -890,7 +968,7 @@ mod router { share: Decimal::zero(), }, osmosis_fee: Fee { - share: Decimal::from_ratio(1u128, 100_000u128), + share: Decimal::permille(1), }, extra_fees: vec![], }; @@ -1123,7 +1201,7 @@ mod router { share: Decimal::bps(50), }, osmosis_fee: Fee { - share: Decimal::bps(50), + share: Decimal::permille(1), }, extra_fees: vec![], }; @@ -1294,7 +1372,7 @@ mod router { share: Decimal::bps(50), }, osmosis_fee: Fee { - share: Decimal::bps(50), + share: Decimal::permille(1), }, extra_fees: vec![], }; @@ -1464,7 +1542,7 @@ mod router { share: Decimal::bps(50), }, osmosis_fee: Fee { - share: Decimal::bps(50), + share: Decimal::permille(1), }, extra_fees: vec![], }; @@ -1666,7 +1744,7 @@ mod router { share: Decimal::bps(50), }, osmosis_fee: Fee { - share: Decimal::bps(50), + share: Decimal::permille(1), }, extra_fees: vec![], }; @@ -1826,7 +1904,7 @@ mod router { swap_operations.clone(), |result| { let result = result.unwrap(); - assert_eq!(result.amount.u128(), 1_007); + assert_approx_eq!(result.amount.u128(), 1_007, "0.1"); }, ); @@ -1892,7 +1970,7 @@ mod swapping { share: Decimal::zero(), }, osmosis_fee: Fee { - share: Decimal::from_ratio(1u128, 100_000u128), + share: Decimal::permille(1), }, extra_fees: vec![], }; @@ -2319,7 +2397,7 @@ mod swapping { share: Decimal::zero(), }, osmosis_fee: Fee { - share: Decimal::zero(), + share: Decimal::permille(1), }, extra_fees: vec![], }; @@ -2415,7 +2493,7 @@ mod swapping { assert_approx_eq!( offer_amount.parse::().unwrap(), return_amount.parse::().unwrap(), - "0.01" + "0.011" ); }, ); @@ -2615,7 +2693,7 @@ mod locking_lp { share: Decimal::zero(), }, osmosis_fee: Fee { - share: Decimal::zero(), + share: Decimal::permille(1), }, extra_fees: vec![], }; @@ -2811,7 +2889,7 @@ mod locking_lp { share: Decimal::zero(), }, osmosis_fee: Fee { - share: Decimal::zero(), + share: Decimal::permille(1), }, extra_fees: vec![], }; @@ -3014,7 +3092,7 @@ mod provide_liquidity { share: Decimal::zero(), }, osmosis_fee: Fee { - share: Decimal::zero(), + share: Decimal::permille(1), }, extra_fees: vec![], }; @@ -3469,7 +3547,7 @@ mod provide_liquidity { share: Decimal::zero(), }, osmosis_fee: Fee { - share: Decimal::percent(10), + share: Decimal::permille(1), }, extra_fees: vec![], }; diff --git a/contracts/liquidity_hub/pool-network/incentive/src/claim.rs b/contracts/liquidity_hub/pool-network/incentive/src/claim.rs index f4cbf452..e3b6a2a6 100644 --- a/contracts/liquidity_hub/pool-network/incentive/src/claim.rs +++ b/contracts/liquidity_hub/pool-network/incentive/src/claim.rs @@ -129,9 +129,10 @@ pub fn claim(deps: &mut DepsMut, info: &MessageInfo) -> Result, C // record the emitted tokens for this epoch if it hasn't been recorded before. // emitted tokens for this epoch is the total emitted tokens in previous epoch + the ones // that where emitted in this epoch - if !flow.emitted_tokens.contains_key(&epoch_id) { - flow.emitted_tokens - .insert(epoch_id, emission_per_epoch.checked_add(emitted_tokens)?); + if let std::collections::hash_map::Entry::Vacant(e) = + flow.emitted_tokens.entry(epoch_id) + { + e.insert(emission_per_epoch.checked_add(emitted_tokens)?); } // get user weight for this epoch diff --git a/contracts/liquidity_hub/pool-network/incentive/src/queries/get_rewards.rs b/contracts/liquidity_hub/pool-network/incentive/src/queries/get_rewards.rs index 89272633..a245d847 100644 --- a/contracts/liquidity_hub/pool-network/incentive/src/queries/get_rewards.rs +++ b/contracts/liquidity_hub/pool-network/incentive/src/queries/get_rewards.rs @@ -113,9 +113,10 @@ pub fn get_rewards(deps: Deps, address: String) -> Result