-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add coins pallet tests #606
Merged
kayabaNerve
merged 4 commits into
serai-dex:develop
from
akildemir:add-coins-pallet-tests
Oct 30, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//! Test environment for Coins pallet. | ||
|
||
use super::*; | ||
|
||
use frame_support::{ | ||
construct_runtime, | ||
traits::{ConstU32, ConstU64}, | ||
}; | ||
|
||
use sp_core::{H256, sr25519::Public}; | ||
use sp_runtime::{ | ||
traits::{BlakeTwo256, IdentityLookup}, | ||
BuildStorage, | ||
}; | ||
|
||
use crate as coins; | ||
|
||
type Block = frame_system::mocking::MockBlock<Test>; | ||
|
||
construct_runtime!( | ||
pub enum Test | ||
{ | ||
System: frame_system, | ||
Coins: coins, | ||
} | ||
); | ||
|
||
impl frame_system::Config for Test { | ||
type BaseCallFilter = frame_support::traits::Everything; | ||
type BlockWeights = (); | ||
type BlockLength = (); | ||
type RuntimeOrigin = RuntimeOrigin; | ||
type RuntimeCall = RuntimeCall; | ||
type Nonce = u64; | ||
type Hash = H256; | ||
type Hashing = BlakeTwo256; | ||
type AccountId = Public; | ||
type Lookup = IdentityLookup<Self::AccountId>; | ||
type Block = Block; | ||
type RuntimeEvent = RuntimeEvent; | ||
type BlockHashCount = ConstU64<250>; | ||
type DbWeight = (); | ||
type Version = (); | ||
type PalletInfo = PalletInfo; | ||
type AccountData = (); | ||
type OnNewAccount = (); | ||
type OnKilledAccount = (); | ||
type SystemWeightInfo = (); | ||
type SS58Prefix = (); | ||
type OnSetCode = (); | ||
type MaxConsumers = ConstU32<16>; | ||
} | ||
|
||
impl Config for Test { | ||
type RuntimeEvent = RuntimeEvent; | ||
|
||
type AllowMint = (); | ||
} | ||
|
||
pub(crate) fn new_test_ext() -> sp_io::TestExternalities { | ||
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap(); | ||
|
||
crate::GenesisConfig::<Test> { accounts: vec![], _ignore: Default::default() } | ||
.assimilate_storage(&mut t) | ||
.unwrap(); | ||
|
||
let mut ext = sp_io::TestExternalities::new(t); | ||
ext.execute_with(|| System::set_block_number(0)); | ||
ext | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
use crate::{mock::*, primitives::*}; | ||
|
||
use frame_system::RawOrigin; | ||
use sp_core::Pair; | ||
|
||
use serai_primitives::*; | ||
|
||
pub type CoinsEvent = crate::Event<Test, ()>; | ||
|
||
#[test] | ||
fn mint() { | ||
new_test_ext().execute_with(|| { | ||
// minting u64::MAX should work | ||
let coin = Coin::Serai; | ||
let to = insecure_pair_from_name("random1").public(); | ||
let balance = Balance { coin, amount: Amount(u64::MAX) }; | ||
|
||
Coins::mint(to, balance).unwrap(); | ||
assert_eq!(Coins::balance(to, coin), balance.amount); | ||
|
||
// minting more should fail | ||
assert!(Coins::mint(to, Balance { coin, amount: Amount(1) }).is_err()); | ||
|
||
// supply now should be equal to sum of the accounts balance sum | ||
assert_eq!(Coins::supply(coin), balance.amount.0); | ||
|
||
// test events | ||
let mint_events = System::events() | ||
.iter() | ||
.filter_map(|event| { | ||
if let RuntimeEvent::Coins(e) = &event.event { | ||
if matches!(e, CoinsEvent::Mint { .. }) { | ||
Some(e.clone()) | ||
} else { | ||
None | ||
} | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
assert_eq!(mint_events, vec![CoinsEvent::Mint { to, balance }]); | ||
}) | ||
} | ||
|
||
#[test] | ||
fn burn_with_instruction() { | ||
new_test_ext().execute_with(|| { | ||
// mint some coin | ||
let coin = Coin::External(ExternalCoin::Bitcoin); | ||
let to = insecure_pair_from_name("random1").public(); | ||
let balance = Balance { coin, amount: Amount(10 * 10u64.pow(coin.decimals())) }; | ||
|
||
Coins::mint(to, balance).unwrap(); | ||
assert_eq!(Coins::balance(to, coin), balance.amount); | ||
assert_eq!(Coins::supply(coin), balance.amount.0); | ||
|
||
// we shouldn't be able to burn more than what we have | ||
let mut instruction = OutInstructionWithBalance { | ||
instruction: OutInstruction { address: ExternalAddress::new(vec![]).unwrap(), data: None }, | ||
balance: ExternalBalance { | ||
coin: coin.try_into().unwrap(), | ||
amount: Amount(balance.amount.0 + 1), | ||
}, | ||
}; | ||
assert!( | ||
Coins::burn_with_instruction(RawOrigin::Signed(to).into(), instruction.clone()).is_err() | ||
); | ||
|
||
// it should now work | ||
instruction.balance.amount = balance.amount; | ||
Coins::burn_with_instruction(RawOrigin::Signed(to).into(), instruction.clone()).unwrap(); | ||
|
||
// balance & supply now should be back to 0 | ||
assert_eq!(Coins::balance(to, coin), Amount(0)); | ||
assert_eq!(Coins::supply(coin), 0); | ||
|
||
let burn_events = System::events() | ||
.iter() | ||
.filter_map(|event| { | ||
if let RuntimeEvent::Coins(e) = &event.event { | ||
if matches!(e, CoinsEvent::BurnWithInstruction { .. }) { | ||
Some(e.clone()) | ||
} else { | ||
None | ||
} | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
assert_eq!(burn_events, vec![CoinsEvent::BurnWithInstruction { from: to, instruction }]); | ||
}) | ||
} | ||
|
||
#[test] | ||
fn transfer() { | ||
new_test_ext().execute_with(|| { | ||
// mint some coin | ||
let coin = Coin::External(ExternalCoin::Bitcoin); | ||
let from = insecure_pair_from_name("random1").public(); | ||
let balance = Balance { coin, amount: Amount(10 * 10u64.pow(coin.decimals())) }; | ||
|
||
Coins::mint(from, balance).unwrap(); | ||
assert_eq!(Coins::balance(from, coin), balance.amount); | ||
assert_eq!(Coins::supply(coin), balance.amount.0); | ||
|
||
// we can't send more than what we have | ||
let to = insecure_pair_from_name("random2").public(); | ||
assert!(Coins::transfer( | ||
RawOrigin::Signed(from).into(), | ||
to, | ||
Balance { coin, amount: Amount(balance.amount.0 + 1) } | ||
) | ||
.is_err()); | ||
|
||
// we can send it all | ||
Coins::transfer(RawOrigin::Signed(from).into(), to, balance).unwrap(); | ||
|
||
// check the balances | ||
assert_eq!(Coins::balance(from, coin), Amount(0)); | ||
assert_eq!(Coins::balance(to, coin), balance.amount); | ||
|
||
// supply shouldn't change | ||
assert_eq!(Coins::supply(coin), balance.amount.0); | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't set this to
ValidatorSets
pallet here mainly because it isn't even a dependency for theCoins
pallet. Also we test the basic functionality for the coins pallet here so I doubt we needAllowMint
check to be present that extra complicates the tests. It is present for other necessary pallets. Still not sure whether it should exist here or not.