Skip to content

Commit

Permalink
Add transfer related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrylavrenov committed Jan 4, 2024
1 parent 293b45c commit 028f051
Showing 1 changed file with 132 additions and 1 deletion.
133 changes: 132 additions & 1 deletion frame/evm-balances/src/tests/fungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use frame_support::{
traits::{
fungible::{Inspect, Mutate, Unbalanced},
tokens::Precision,
RankedMembers,
},
};
use sp_core::H160;
Expand Down Expand Up @@ -649,3 +648,135 @@ fn restore_fails_overflow() {
);
});
}

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

let transfered_amount = 100;

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

// Invoke the function under test.
assert_ok!(EvmBalances::transfer(
&alice(),
&bob(),
transfered_amount,
Preservation::Preserve
));

// Assert state changes.
assert_eq!(
EvmBalances::total_balance(&alice()),
INIT_BALANCE - transfered_amount
);
assert_eq!(
EvmBalances::total_balance(&bob()),
INIT_BALANCE + transfered_amount
);
System::assert_has_event(RuntimeEvent::EvmBalances(Event::Transfer {
from: alice(),
to: bob(),
amount: transfered_amount,
}));
});
}

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

let transfered_amount = INIT_BALANCE;

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

// Invoke the function under test.
assert_ok!(EvmBalances::transfer(
&alice(),
&bob(),
transfered_amount,
Preservation::Expendable
));

// Assert state changes.
assert_eq!(
EvmBalances::total_balance(&alice()),
INIT_BALANCE - transfered_amount
);
assert_eq!(
EvmBalances::total_balance(&bob()),
INIT_BALANCE + transfered_amount
);
System::assert_has_event(RuntimeEvent::EvmBalances(Event::Transfer {
from: alice(),
to: bob(),
amount: transfered_amount,
}));
assert!(!EvmSystem::account_exists(&alice()));
System::assert_has_event(RuntimeEvent::EvmSystem(
pallet_evm_system::Event::KilledAccount { account: alice() },
));
});
}

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

let transfered_amount = INIT_BALANCE + 1;

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

// Invoke the function under test.
assert_noop!(
EvmBalances::transfer(&alice(), &bob(), transfered_amount, Preservation::Preserve),
TokenError::FundsUnavailable
);
});
}

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

let transfered_amount = INIT_BALANCE;

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

// Invoke the function under test.
assert_noop!(
EvmBalances::transfer(&alice(), &bob(), transfered_amount, Preservation::Preserve),
TokenError::NotExpendable
);
});
}

#[test]
fn transfer_fails_underflow() {
new_test_ext().execute_with(|| {
// Prepare test preconditions.
let charlie = H160::from_str("1000000000000000000000000000000000000003").unwrap();
let eve = H160::from_str("1000000000000000000000000000000000000004").unwrap();
EvmBalances::set_balance(&charlie, u64::MAX);
EvmBalances::set_balance(&eve, 1);

// Invoke the function under test.
assert_noop!(
EvmBalances::transfer(&charlie, &eve, u64::MAX, Preservation::Expendable),
// Withdraw consequence is checked first by reducing total issuance.
ArithmeticError::Underflow,
);
});
}

0 comments on commit 028f051

Please sign in to comment.