From a0a1e08dfbddd91320f0ea433969d925ab6a3a50 Mon Sep 17 00:00:00 2001 From: Ricardo Guilherme Schmidt <3esmit@gmail.com> Date: Wed, 20 May 2026 10:02:12 -0300 Subject: [PATCH] test(stablecoin): cover invalid withdraw transfer pre-states --- Cargo.lock | 1 + stablecoin/Cargo.toml | 3 ++ stablecoin/src/tests.rs | 88 +++++++++++++++++++++++------------------ 3 files changed, 53 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eda78aa..8fa1d33 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3055,6 +3055,7 @@ dependencies = [ "nssa_core", "stablecoin_core", "token_core", + "token_program", ] [[package]] diff --git a/stablecoin/Cargo.toml b/stablecoin/Cargo.toml index 83a0155..af342fb 100644 --- a/stablecoin/Cargo.toml +++ b/stablecoin/Cargo.toml @@ -7,3 +7,6 @@ edition = "2021" nssa_core = { git = "https://github.com/logos-blockchain/logos-execution-zone.git", tag = "v0.2.0-rc3", features = ["host"] } stablecoin_core = { path = "core" } token_core = { path = "../token/core" } + +[dev-dependencies] +token_program.workspace = true diff --git a/stablecoin/src/tests.rs b/stablecoin/src/tests.rs index df73cd8..42ad660 100644 --- a/stablecoin/src/tests.rs +++ b/stablecoin/src/tests.rs @@ -30,6 +30,26 @@ fn user_holding_id() -> AccountId { AccountId::new([0x30u8; 32]) } +fn token_holding_account( + account_id: AccountId, + definition_id: AccountId, + balance: u128, +) -> AccountWithMetadata { + AccountWithMetadata { + account: Account { + program_owner: TOKEN_PROGRAM_ID, + balance: 0, + data: Data::from(&TokenHolding::Fungible { + definition_id, + balance, + }), + nonce: Nonce(0), + }, + is_authorized: false, + account_id, + } +} + fn position_id() -> AccountId { compute_position_pda( STABLECOIN_PROGRAM_ID, @@ -68,19 +88,9 @@ fn collateral_definition_account() -> AccountWithMetadata { } fn user_holding_account(balance: u128) -> AccountWithMetadata { - AccountWithMetadata { - account: Account { - program_owner: TOKEN_PROGRAM_ID, - balance: 0, - data: Data::from(&TokenHolding::Fungible { - definition_id: collateral_definition_id(), - balance, - }), - nonce: Nonce(0), - }, - is_authorized: true, - account_id: user_holding_id(), - } + let mut account = token_holding_account(user_holding_id(), collateral_definition_id(), balance); + account.is_authorized = true; + account } fn uninit_position_account() -> AccountWithMetadata { @@ -122,35 +132,11 @@ fn init_position_account(collateral_amount: u128, debt_amount: u128) -> AccountW } fn init_vault_account() -> AccountWithMetadata { - AccountWithMetadata { - account: Account { - program_owner: TOKEN_PROGRAM_ID, - balance: 0, - data: Data::from(&TokenHolding::Fungible { - definition_id: collateral_definition_id(), - balance: 0, - }), - nonce: Nonce(0), - }, - is_authorized: false, - account_id: vault_id(), - } + token_holding_account(vault_id(), collateral_definition_id(), 0) } fn destination_holding_account() -> AccountWithMetadata { - AccountWithMetadata { - account: Account { - program_owner: TOKEN_PROGRAM_ID, - balance: 0, - data: Data::from(&TokenHolding::Fungible { - definition_id: collateral_definition_id(), - balance: 0, - }), - nonce: Nonce(0), - }, - is_authorized: false, - account_id: destination_holding_id(), - } + token_holding_account(destination_holding_id(), collateral_definition_id(), 0) } #[test] @@ -499,6 +485,30 @@ fn withdraw_collateral_updates_position_and_emits_transfer() { assert_eq!(chained_calls[0], expected_transfer); } +#[test] +#[should_panic(expected = "Insufficient balance")] +fn withdraw_collateral_transfer_pre_states_should_not_be_executable() { + let initial_collateral: u128 = 500; + let amount: u128 = 200; + let (_post_states, chained_calls) = crate::withdraw_collateral::withdraw_collateral( + owner_account(), + init_position_account(initial_collateral, 0), + init_vault_account(), + destination_holding_account(), + STABLECOIN_PROGRAM_ID, + amount, + ); + + let transfer_call = chained_calls + .into_iter() + .next() + .expect("withdraw emits transfer"); + let [sender, recipient] = + <[_; 2]>::try_from(transfer_call.pre_states).expect("token transfer accounts"); + + token_program::transfer::transfer(sender, recipient, amount); +} + #[test] fn withdraw_collateral_allows_full_drain() { let amount: u128 = 500;