Skip to content

Commit

Permalink
Add balanced related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrylavrenov committed Jan 4, 2024
1 parent 028f051 commit 4bbd2b5
Showing 1 changed file with 193 additions and 1 deletion.
194 changes: 193 additions & 1 deletion frame/evm-balances/src/tests/fungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use frame_support::{
assert_noop, assert_ok,
traits::{
fungible::{Inspect, Mutate, Unbalanced},
fungible::{Balanced, Inspect, Mutate, Unbalanced},
tokens::Precision,
},
};
Expand Down Expand Up @@ -780,3 +780,195 @@ fn transfer_fails_underflow() {
);
});
}

#[test]
fn rescind_works() {
new_test_ext().execute_with_ext(|_| {
// Check test preconditions.
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE);

// Set block number to enable events.
System::set_block_number(1);

let rescinded_balance = 100;

// Burn some balance.
let imbalance = EvmBalances::rescind(rescinded_balance);

// Assert state changes.
assert_eq!(
EvmBalances::total_issuance(),
2 * INIT_BALANCE - rescinded_balance
);
drop(imbalance);
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE);
System::assert_has_event(RuntimeEvent::EvmBalances(Event::Rescinded {
amount: rescinded_balance,
}));
});
}

#[test]
fn issue_works() {
new_test_ext().execute_with_ext(|_| {
// Check test preconditions.
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE);

// Set block number to enable events.
System::set_block_number(1);

let issued_balance = 100;

// Burn some balance.
let imbalance = EvmBalances::issue(issued_balance);

// Assert state changes.
assert_eq!(
EvmBalances::total_issuance(),
2 * INIT_BALANCE + issued_balance
);
drop(imbalance);
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE);
System::assert_has_event(RuntimeEvent::EvmBalances(Event::Issued {
amount: issued_balance,
}));
});
}

#[test]
fn deposit_flow_works() {
new_test_ext().execute_with_ext(|_| {
// Check test preconditions.
assert_eq!(EvmBalances::total_balance(&alice()), INIT_BALANCE);

let deposited_amount = 10;

// Set block number to enable events.
System::set_block_number(1);

// Invoke the function under test.
let debt = EvmBalances::deposit(&alice(), deposited_amount, Precision::Exact).unwrap();

// Assert state changes.
assert_eq!(
EvmBalances::total_balance(&alice()),
INIT_BALANCE + deposited_amount
);
System::assert_has_event(RuntimeEvent::EvmBalances(Event::Deposit {
who: alice(),
amount: deposited_amount,
}));
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE);

let _ = EvmBalances::settle(&bob(), debt, Preservation::Expendable);
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE);
});
}

#[test]
fn deposit_works_new_account() {
new_test_ext().execute_with_ext(|_| {
let charlie = H160::from_str("1000000000000000000000000000000000000003").unwrap();

// Check test preconditions.
assert_eq!(EvmBalances::total_balance(&charlie), 0);

let deposited_amount = 10;

// Set block number to enable events.
System::set_block_number(1);

// Invoke the function under test.
let debt = EvmBalances::deposit(&charlie, deposited_amount, Precision::Exact).unwrap();

// Assert state changes.
assert_eq!(EvmBalances::total_balance(&charlie), deposited_amount);
System::assert_has_event(RuntimeEvent::EvmBalances(Event::Deposit {
who: charlie,
amount: deposited_amount,
}));
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE);
let _ = EvmBalances::settle(&bob(), debt, Preservation::Expendable);
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE);
assert!(EvmSystem::account_exists(&charlie));
System::assert_has_event(RuntimeEvent::EvmSystem(
pallet_evm_system::Event::NewAccount { account: charlie },
));
});
}

#[test]
fn withdraw_works() {
new_test_ext().execute_with_ext(|_| {
// Check test preconditions.
assert_eq!(EvmBalances::total_balance(&alice()), INIT_BALANCE);

let withdrawed_amount = 1000;

// Set block number to enable events.
System::set_block_number(1);

// Invoke the function under test.
let credit = EvmBalances::withdraw(
&alice(),
withdrawed_amount,
Precision::Exact,
Preservation::Preserve,
Fortitude::Polite,
)
.unwrap();

// Assert state changes.
assert_eq!(
EvmBalances::total_balance(&alice()),
INIT_BALANCE - withdrawed_amount
);
System::assert_has_event(RuntimeEvent::EvmBalances(Event::Withdraw {
who: alice(),
amount: withdrawed_amount,
}));
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE);
let _ = EvmBalances::resolve(&bob(), credit);
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE);
});
}

#[test]
fn withdraw_works_full_balance() {
new_test_ext().execute_with_ext(|_| {
// Check test preconditions.
assert_eq!(EvmBalances::total_balance(&alice()), INIT_BALANCE);

let withdrawed_amount = INIT_BALANCE;

// Set block number to enable events.
System::set_block_number(1);

// Invoke the function under test.
let credit = EvmBalances::withdraw(
&alice(),
withdrawed_amount,
Precision::Exact,
Preservation::Expendable,
Fortitude::Polite,
)
.unwrap();

// Assert state changes.
assert_eq!(
EvmBalances::total_balance(&alice()),
INIT_BALANCE - withdrawed_amount
);
System::assert_has_event(RuntimeEvent::EvmBalances(crate::Event::Withdraw {
who: alice(),
amount: withdrawed_amount,
}));
assert!(!EvmSystem::account_exists(&alice()));
System::assert_has_event(RuntimeEvent::EvmSystem(
pallet_evm_system::Event::KilledAccount { account: alice() },
));
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE);
let _ = EvmBalances::resolve(&bob(), credit);
assert_eq!(EvmBalances::total_issuance(), 2 * INIT_BALANCE);
});
}

0 comments on commit 4bbd2b5

Please sign in to comment.