|
1 | 1 | //! Tests for rebalance functionality |
2 | 2 |
|
3 | 3 | use super::utils::*; |
4 | | -use soroban_sdk::{symbol_short, testutils::Address as _, Address, Env}; |
| 4 | +use crate::{BlendWithdrawEvent, RebalanceEvent}; |
| 5 | +use soroban_sdk::{symbol_short, testutils::Address as _, Address, Env, TryFromVal}; |
5 | 6 |
|
6 | 7 | #[test] |
7 | 8 | fn test_agent_can_rebalance_with_custom_protocol() { |
@@ -364,3 +365,102 @@ fn test_blend_supply_and_withdraw_with_events() { |
364 | 365 | "Should have at least 2 BlendWithdrawEvents" |
365 | 366 | ); |
366 | 367 | } |
| 368 | + |
| 369 | +#[test] |
| 370 | +fn test_rebalance_blend_to_none_withdraws_all_and_updates_state_and_events() { |
| 371 | + let env = Env::default(); |
| 372 | + env.mock_all_auths(); |
| 373 | + |
| 374 | + let (contract_id, _agent, owner, usdc_token, blend_pool) = |
| 375 | + setup_vault_with_token_and_blend(&env); |
| 376 | + let client = NeuroWealthVaultClient::new(&env, &contract_id); |
| 377 | + let token_client = TestTokenClient::new(&env, &usdc_token); |
| 378 | + let blend_client = MockBlendPoolClient::new(&env, &blend_pool); |
| 379 | + |
| 380 | + client.set_blend_pool(&owner, &blend_pool); |
| 381 | + |
| 382 | + let user = Address::generate(&env); |
| 383 | + let deposit_amount = 30_000_000_i128; |
| 384 | + mint_and_deposit(&env, &client, &usdc_token, &user, deposit_amount); |
| 385 | + |
| 386 | + client.rebalance(&symbol_short!("blend"), &900_i128); |
| 387 | + |
| 388 | + assert_eq!( |
| 389 | + client.get_current_protocol(), |
| 390 | + symbol_short!("blend"), |
| 391 | + "CurrentProtocol should be 'blend' after rebalance to blend" |
| 392 | + ); |
| 393 | + assert_eq!( |
| 394 | + blend_client.supplied(&usdc_token), |
| 395 | + deposit_amount, |
| 396 | + "All deposited funds should be supplied to Blend" |
| 397 | + ); |
| 398 | + assert_eq!( |
| 399 | + token_client.balance(&contract_id), |
| 400 | + 0, |
| 401 | + "Vault should hold no idle USDC while fully allocated to Blend" |
| 402 | + ); |
| 403 | + |
| 404 | + let none_apy = 0_i128; |
| 405 | + client.rebalance(&symbol_short!("none"), &none_apy); |
| 406 | + |
| 407 | + assert_eq!( |
| 408 | + client.get_current_protocol(), |
| 409 | + symbol_short!("none"), |
| 410 | + "CurrentProtocol should be 'none' after rebalance to none" |
| 411 | + ); |
| 412 | + assert_eq!( |
| 413 | + blend_client.supplied(&usdc_token), |
| 414 | + 0, |
| 415 | + "Blend should be fully withdrawn after rebalance to none" |
| 416 | + ); |
| 417 | + assert_eq!( |
| 418 | + token_client.balance(&contract_id), |
| 419 | + deposit_amount, |
| 420 | + "All funds should be pulled back to vault after rebalance to none" |
| 421 | + ); |
| 422 | + |
| 423 | + let blend_withdraw_events = |
| 424 | + find_events_by_topic(env.events().all(), &env, symbol_short!("blend_wd")); |
| 425 | + assert!( |
| 426 | + !blend_withdraw_events.is_empty(), |
| 427 | + "BlendWithdrawEvent should be emitted when switching blend -> none" |
| 428 | + ); |
| 429 | + |
| 430 | + let (_, _, blend_withdraw_data) = blend_withdraw_events |
| 431 | + .last() |
| 432 | + .expect("Expected at least one blend_wd event"); |
| 433 | + let blend_withdraw_event = BlendWithdrawEvent::try_from_val(&env, blend_withdraw_data) |
| 434 | + .expect("blend_wd data should decode to BlendWithdrawEvent"); |
| 435 | + |
| 436 | + assert_eq!( |
| 437 | + blend_withdraw_event.requested_amount, deposit_amount, |
| 438 | + "blend_wd requested_amount should match full deployed balance" |
| 439 | + ); |
| 440 | + assert_eq!( |
| 441 | + blend_withdraw_event.amount_received, deposit_amount, |
| 442 | + "blend_wd amount_received should match full withdrawal" |
| 443 | + ); |
| 444 | + assert!( |
| 445 | + blend_withdraw_event.success, |
| 446 | + "blend_wd event should mark withdrawal as successful" |
| 447 | + ); |
| 448 | + |
| 449 | + let rebalance_events = |
| 450 | + find_events_by_topic(env.events().all(), &env, symbol_short!("rebalance")); |
| 451 | + let (_, _, rebalance_data) = rebalance_events |
| 452 | + .last() |
| 453 | + .expect("Expected rebalance event for none transition"); |
| 454 | + let rebalance_event = RebalanceEvent::try_from_val(&env, rebalance_data) |
| 455 | + .expect("rebalance data should decode to RebalanceEvent"); |
| 456 | + |
| 457 | + assert_eq!( |
| 458 | + rebalance_event.protocol, |
| 459 | + symbol_short!("none"), |
| 460 | + "rebalance event protocol should reflect target none" |
| 461 | + ); |
| 462 | + assert_eq!( |
| 463 | + rebalance_event.expected_apy, none_apy, |
| 464 | + "rebalance event APY should match provided value" |
| 465 | + ); |
| 466 | +} |
0 commit comments