Skip to content
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

Fixed Mint Asset Extrinsic and amount precision #473

Merged
merged 2 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pallets/asset-handler/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ benchmarks! {
let encoded_recipient = recipient.encode();
let recipient: [u8;32] = encoded_recipient.as_slice().try_into().unwrap();
let destination_acc = T::AccountId::decode(&mut &recipient[..]).unwrap();
let amount = (b as u128).saturated_into::<BalanceOf<T>>();
}: _(RawOrigin::Signed(relayer), recipient.clone(), amount.clone(), rid)
let amount = b as u128;
}: _(RawOrigin::Signed(relayer), recipient.clone().to_vec(), amount.clone(), rid)
verify {
assert_last_event::<T>(Event::AssetDeposited(destination_acc, rid, amount).into());
}
Expand Down
15 changes: 12 additions & 3 deletions pallets/asset-handler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub mod pallet {
traits::{One, UniqueSaturatedInto},
SaturatedConversion,
};
use sp_std::vec::Vec;

pub type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
Expand Down Expand Up @@ -90,7 +91,7 @@ pub mod pallet {
/// Asset Registered
AssetRegistered(ResourceId),
/// Asset Deposited (Recipient, ResourceId, Amount)
AssetDeposited(T::AccountId, ResourceId, BalanceOf<T>),
AssetDeposited(T::AccountId, ResourceId, u128),
/// Asset Withdrawn (Recipient, ResourceId, Amount)
AssetWithdrawn(H160, ResourceId, BalanceOf<T>),
FeeUpdated(BridgeChainId, BalanceOf<T>),
Expand All @@ -109,6 +110,8 @@ pub mod pallet {
NotEnoughBalance,
/// DestinationAddressNotValid
DestinationAddressNotValid,
/// DivisionUnderflow
DivisionUnderflow
}

#[pallet::hooks]
Expand Down Expand Up @@ -153,8 +156,8 @@ pub mod pallet {
#[pallet::weight(T::WeightInfo::mint_asset(1))]
pub fn mint_asset(
origin: OriginFor<T>,
destination_add: [u8; 32],
amount: BalanceOf<T>,
destination_add: Vec<u8>,
amount: u128,
rid: ResourceId,
) -> DispatchResult {
let sender = ensure_signed(origin)?;
Expand All @@ -164,6 +167,7 @@ pub mod pallet {
chainbridge::Pallet::<T>::account_id() == sender,
Error::<T>::MinterMustBeRelayer
);
let amount = Self::convert_18dec_to_12dec(amount).ok_or_else(|| Error::<T>::DivisionUnderflow)?;
T::AssetManager::mint_into(
Self::convert_asset_id(rid),
&destination_acc,
Expand Down Expand Up @@ -261,6 +265,11 @@ pub mod pallet {
}
}

fn convert_18dec_to_12dec(balance: u128) -> Option<u128> {
balance
.checked_div(1000000u128)
}

pub fn convert_asset_id(token: ResourceId) -> u128 {
let mut temp = [0u8; 16];
temp.copy_from_slice(&token[0..16]);
Expand Down
10 changes: 5 additions & 5 deletions pallets/asset-handler/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ pub fn test_mint_asset_with_not_registered_asset_will_return_unknown_asset_error
assert_noop!(
AssetHandler::mint_asset(
Origin::signed(ChainBridge::account_id()),
recipient,
100,
recipient.to_vec(),
100000000000,
rid
),
TokenError::UnknownAsset
Expand All @@ -101,8 +101,8 @@ pub fn test_mint_asset_with_existed_asset_will_successfully_increase_balance() {
// Mint Asset using Relayer account and verify storage
assert_ok!(AssetHandler::mint_asset(
Origin::signed(ChainBridge::account_id()),
recipient,
100,
recipient.to_vec(),
100000000,
rid
));
assert_eq!(Assets::balance(asset_id, recipient_account), 100);
Expand All @@ -120,7 +120,7 @@ pub fn test_mint_asset_called_by_not_relayer_will_return_minter_must_be_relayer_
let asset_id = AssetHandler::convert_asset_id(rid);

assert_noop!(
AssetHandler::mint_asset(Origin::signed(account), recipient, 100, rid),
AssetHandler::mint_asset(Origin::signed(account), recipient.to_vec(), 100, rid),
Error::<Test>::MinterMustBeRelayer
);
assert_eq!(Assets::balance(asset_id, recipient_account), 0);
Expand Down