use frame_support::{
hypothetically,
traits::{fungible::Mutate, InspectLockableCurrency, LockableCurrency, WithdrawReasons},
};
use sp_runtime::AccountId32;
use std::str::FromStr;
use crate::mock::Runtime as AssetHub;
#[test]
fn sovereign_account_translation() {
let bifrost_cases = [
(
"13YMK2eeopZtUNpeHnJ1Ws2HqMQG6Ts9PGCZYGyFbSYoZfcm",
"13cKp89TtYknbyYnqnF6dWN75q5ZosvFSuqzoEVkUAaNR47A",
None,
),
(
"14vtfeKAVKh1Jzb3s7e43SqZ3zB5MLsdCxZPoKDxeoCFKLu5",
"5ETehspFKFNpBbe5DsfuziN6BWq5Qwp1J8qcTQQoAxwa7BsS",
Some(("13YMK2eeopZtUNpeHnJ1Ws2HqMQG6Ts9PGCZYGyFbSYoZfcm", 0u16)),
),
(
"14QkQ7wVVDRrhbC1UqHsFwKFUns1SRud94CXMWGHWB8Jhtro",
"5DNWZkkAxLhqF8tevcbRGyARAVM7abukftmqvoDFUN5dDDDz",
Some(("13YMK2eeopZtUNpeHnJ1Ws2HqMQG6Ts9PGCZYGyFbSYoZfcm", 1u16)),
),
(
"13hLwqcVHqjiJMbZhR9LtfdhoxmTdssi7Kp8EJaW2yfk3knK",
"5EmiwjDYiackJma1GW3aBbQ74rLfWh756UKDb7Cm83XDkUUZ",
Some(("13YMK2eeopZtUNpeHnJ1Ws2HqMQG6Ts9PGCZYGyFbSYoZfcm", 2u16)),
),
(
"5Ec4AhPV91i9yNuiWuNunPf6AQCYDhFTTA4G5QCbtqYApH9E",
"5Eg2fntJDju46yds4uKzu2zuQssqw7JZWohhLMj6mZZjg2pK",
None,
),
(
"5E78xTBiaN3nAGYtcNnqTJQJqYAkSDGggKqaDfpNsKyPpbcb",
"5CzXNqgBZT5yMpMETdfH55saYNKQoJBXsSfnu4d2s1ejYFir",
Some(("5Ec4AhPV91i9yNuiWuNunPf6AQCYDhFTTA4G5QCbtqYApH9E", 0u16)),
),
(
"5HXi9pzWnTQzk7VKzY6VQn92KfWCcA5NbSm53uKHrYU1VsjP",
"5GcexD4YNqcKTbW1YWDRczQzpxic61byeNeLaHgqQHk8pxQJ",
Some(("5Ec4AhPV91i9yNuiWuNunPf6AQCYDhFTTA4G5QCbtqYApH9E", 1u16)),
),
(
"5CkKS3YMx64TguUYrMERc5Bn6Mn2aKMUkcozUFREQDgHS3Tv",
"5FoYMVucmT552GDMWfYNxcF2XnuuvLbJHt7mU6DfDCpUAS2Y",
Some(("5Ec4AhPV91i9yNuiWuNunPf6AQCYDhFTTA4G5QCbtqYApH9E", 2u16)),
),
(
"5Crxhmiw5CQq3Mnfcu3dR3yJ3YpjbxjqaeDFtNNtqgmcnN4S",
"5FP39fgPYhJw3vcLwSMqMnwBuEVGexUMG6JQLPR9yPVhq6Wy",
Some(("5Ec4AhPV91i9yNuiWuNunPf6AQCYDhFTTA4G5QCbtqYApH9E", 3u16)),
),
(
"5DAZP4gZKZafGv42uoWNTMau4tYuDd2XteJLGL4upermhQpn",
"5ExtLdYnjHLJbngU1QpumjPieCGaCXwwkH1JrFBQ9GATuNGv",
Some(("5Ec4AhPV91i9yNuiWuNunPf6AQCYDhFTTA4G5QCbtqYApH9E", 4u16)),
),
];
for (from, to, derivation) in bifrost_cases {
let from = AccountId32::from_str(from).unwrap();
let to = AccountId32::from_str(to).unwrap();
println!("Translating {from}/{derivation:?} -> {to}");
if let Some((parent, index)) = derivation {
let parent = AccountId32::from_str(parent).unwrap();
let (got_to, _) =
crate::Pallet::<AssetHub>::try_rc_sovereign_derived_to_ah(&from, &parent, index)
.unwrap();
assert_eq!(got_to, to);
} else {
let (got_to, _) =
crate::Pallet::<AssetHub>::try_translate_rc_sovereign_to_ah(&from).unwrap();
assert_eq!(got_to, to);
}
}
}
#[test]
fn translate_sovereign_acc_good() {
sp_io::TestExternalities::new(Default::default()).execute_with(move || {
let balance = 1000000000000000000;
let lock = balance / 20;
const LID: [u8; 8] = *b"lockID00";
let from =
AccountId32::from_str("14vtfeKAVKh1Jzb3s7e43SqZ3zB5MLsdCxZPoKDxeoCFKLu5").unwrap();
let to = AccountId32::from_str("5ETehspFKFNpBbe5DsfuziN6BWq5Qwp1J8qcTQQoAxwa7BsS").unwrap();
let parent =
AccountId32::from_str("13YMK2eeopZtUNpeHnJ1Ws2HqMQG6Ts9PGCZYGyFbSYoZfcm").unwrap();
let derivation_proof = Some((parent, 0u16));
hypothetically!({
crate::Pallet::<AssetHub>::do_migrate_parachain_sovereign_derived_acc(
&from,
&to,
derivation_proof.clone(),
)
.unwrap();
crate::Pallet::<AssetHub>::do_migrate_parachain_sovereign_derived_acc(
&from,
&to,
derivation_proof.clone(),
)
.unwrap();
});
<AssetHub as crate::Config>::Currency::mint_into(&from, balance).unwrap();
hypothetically!({
crate::Pallet::<AssetHub>::do_migrate_parachain_sovereign_derived_acc(
&from,
&to,
derivation_proof.clone(),
)
.unwrap();
crate::Pallet::<AssetHub>::do_migrate_parachain_sovereign_derived_acc(
&from,
&to,
derivation_proof.clone(),
)
.unwrap();
assert_eq!(<AssetHub as crate::Config>::Currency::free_balance(&from), 0);
assert_eq!(<AssetHub as crate::Config>::Currency::free_balance(&to), balance);
});
<AssetHub as crate::Config>::Currency::set_lock(LID, &from, lock, WithdrawReasons::FEE);
hypothetically!({
crate::Pallet::<AssetHub>::do_migrate_parachain_sovereign_derived_acc(
&from,
&to,
derivation_proof.clone(),
)
.unwrap();
assert_eq!(<AssetHub as crate::Config>::Currency::free_balance(&from), 0);
assert_eq!(<AssetHub as crate::Config>::Currency::free_balance(&to), balance);
assert_eq!(<AssetHub as crate::Config>::Currency::balance_locked(LID, &from), 0);
assert_eq!(<AssetHub as crate::Config>::Currency::balance_locked(LID, &to), lock);
});
});
}