mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-15 02:11:07 +00:00
Balance to Asset Balance Conversion (#9076)
* add BalanceConversion trait and implementation * derive some useful traits on Imbalance * Update frame/support/src/traits/tokens/fungibles/imbalance.rs Co-authored-by: Xiliang Chen <xlchen1291@gmail.com> * make BalanceConversion error type configurable * add RuntimeDebug import and derive traits on other Imbalance * formatting * move BalanceConversion trait to frame-support * add necessary trait import Co-authored-by: Xiliang Chen <xlchen1291@gmail.com>
This commit is contained in:
@@ -19,7 +19,7 @@
|
||||
|
||||
use super::*;
|
||||
use crate::{Error, mock::*};
|
||||
use sp_runtime::TokenError;
|
||||
use sp_runtime::{TokenError, traits::ConvertInto};
|
||||
use frame_support::{assert_ok, assert_noop, traits::Currency};
|
||||
use pallet_balances::Error as BalancesError;
|
||||
|
||||
@@ -699,3 +699,29 @@ fn force_asset_status_should_work(){
|
||||
assert_eq!(Assets::total_supply(0), 200);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn balance_conversion_should_work() {
|
||||
new_test_ext().execute_with(|| {
|
||||
use frame_support::traits::tokens::BalanceConversion;
|
||||
|
||||
let id = 42;
|
||||
assert_ok!(Assets::force_create(Origin::root(), id, 1, true, 10));
|
||||
let not_sufficient = 23;
|
||||
assert_ok!(Assets::force_create(Origin::root(), not_sufficient, 1, false, 10));
|
||||
|
||||
assert_eq!(
|
||||
BalanceToAssetBalance::<Balances, Test, ConvertInto>::to_asset_balance(100, 1234),
|
||||
Err(ConversionError::AssetMissing)
|
||||
);
|
||||
assert_eq!(
|
||||
BalanceToAssetBalance::<Balances, Test, ConvertInto>::to_asset_balance(100, not_sufficient),
|
||||
Err(ConversionError::AssetNotSufficient)
|
||||
);
|
||||
// 10 / 1 == 10 -> the conversion should 10x the value
|
||||
assert_eq!(
|
||||
BalanceToAssetBalance::<Balances, Test, ConvertInto>::to_asset_balance(100, id),
|
||||
Ok(100 * 10)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user