mirror of
https://github.com/logos-blockchain/lez-programs.git
synced 2026-07-25 08:13:12 +00:00
test(stablecoin): cover invalid withdraw transfer pre-states
This commit is contained in:
parent
eb7f44a98a
commit
a0a1e08dfb
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -3055,6 +3055,7 @@ dependencies = [
|
|||||||
"nssa_core",
|
"nssa_core",
|
||||||
"stablecoin_core",
|
"stablecoin_core",
|
||||||
"token_core",
|
"token_core",
|
||||||
|
"token_program",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@ -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"] }
|
nssa_core = { git = "https://github.com/logos-blockchain/logos-execution-zone.git", tag = "v0.2.0-rc3", features = ["host"] }
|
||||||
stablecoin_core = { path = "core" }
|
stablecoin_core = { path = "core" }
|
||||||
token_core = { path = "../token/core" }
|
token_core = { path = "../token/core" }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
token_program.workspace = true
|
||||||
|
|||||||
@ -30,6 +30,26 @@ fn user_holding_id() -> AccountId {
|
|||||||
AccountId::new([0x30u8; 32])
|
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 {
|
fn position_id() -> AccountId {
|
||||||
compute_position_pda(
|
compute_position_pda(
|
||||||
STABLECOIN_PROGRAM_ID,
|
STABLECOIN_PROGRAM_ID,
|
||||||
@ -68,19 +88,9 @@ fn collateral_definition_account() -> AccountWithMetadata {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn user_holding_account(balance: u128) -> AccountWithMetadata {
|
fn user_holding_account(balance: u128) -> AccountWithMetadata {
|
||||||
AccountWithMetadata {
|
let mut account = token_holding_account(user_holding_id(), collateral_definition_id(), balance);
|
||||||
account: Account {
|
account.is_authorized = true;
|
||||||
program_owner: TOKEN_PROGRAM_ID,
|
account
|
||||||
balance: 0,
|
|
||||||
data: Data::from(&TokenHolding::Fungible {
|
|
||||||
definition_id: collateral_definition_id(),
|
|
||||||
balance,
|
|
||||||
}),
|
|
||||||
nonce: Nonce(0),
|
|
||||||
},
|
|
||||||
is_authorized: true,
|
|
||||||
account_id: user_holding_id(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn uninit_position_account() -> AccountWithMetadata {
|
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 {
|
fn init_vault_account() -> AccountWithMetadata {
|
||||||
AccountWithMetadata {
|
token_holding_account(vault_id(), collateral_definition_id(), 0)
|
||||||
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(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn destination_holding_account() -> AccountWithMetadata {
|
fn destination_holding_account() -> AccountWithMetadata {
|
||||||
AccountWithMetadata {
|
token_holding_account(destination_holding_id(), collateral_definition_id(), 0)
|
||||||
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(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -499,6 +485,30 @@ fn withdraw_collateral_updates_position_and_emits_transfer() {
|
|||||||
assert_eq!(chained_calls[0], expected_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]
|
#[test]
|
||||||
fn withdraw_collateral_allows_full_drain() {
|
fn withdraw_collateral_allows_full_drain() {
|
||||||
let amount: u128 = 500;
|
let amount: u128 = 500;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user