Skip to content

Commit

Permalink
refactor: Embed maps in frame-babel (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
conr2d authored Dec 19, 2024
1 parent 0cf1f94 commit 23bdfd7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
33 changes: 32 additions & 1 deletion frame/babel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ pub mod pallet {
},
};
use frame_system::{ensure_root, pallet_prelude::*};
use np_multimap::traits::{UniqueMap, UniqueMultimap};
use np_multimap::{
traits::{UniqueMap, UniqueMultimap},
Error as MapError, UniqueMapAdapter, UniqueMultimapAdapter,
};
use pallet_cosmos::{
types::{AssetIdOf, DenomOf},
AddressMapping as _,
Expand Down Expand Up @@ -104,6 +107,34 @@ pub mod pallet {
InvalidTransaction,
}

/// Mapping from addresses to accounts.
pub type AddressMap<T> = UniqueMultimapAdapter<
<T as frame_system::Config>::AccountId,
VarAddress,
AddressMapStorage<T>,
AddressIndex<T>,
ConstU32<{ VarAddress::variant_count() }>,
MapError,
>;
#[pallet::storage]
pub type AddressMapStorage<T: Config> = StorageMap<
_,
Twox64Concat,
T::AccountId,
BoundedBTreeSet<VarAddress, ConstU32<{ VarAddress::variant_count() }>>,
ValueQuery,
>;
#[pallet::storage]
pub type AddressIndex<T: Config> = StorageMap<_, Twox64Concat, VarAddress, T::AccountId>;

/// Mapping from asset IDs to denoms.
pub type AssetMap<T> =
UniqueMapAdapter<AssetIdOf<T>, DenomOf<T>, AssetMapStorage<T>, AssetIndex<T>, MapError>;
#[pallet::storage]
pub type AssetMapStorage<T: Config> = StorageMap<_, Twox64Concat, AssetIdOf<T>, DenomOf<T>>;
#[pallet::storage]
pub type AssetIndex<T: Config> = StorageMap<_, Twox64Concat, DenomOf<T>, AssetIdOf<T>>;

#[pallet::call]
impl<T: Config> Pallet<T>
where
Expand Down
18 changes: 6 additions & 12 deletions frame/babel/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use frame_support::{
};
use frame_system::EnsureRoot;
use np_babel::cosmos::traits::CosmosHub;
use np_multimap::traits::UniqueMap;
use np_runtime::{AccountId32, MultiSigner};
use pallet_cosmos::{
config_preludes::{MaxDenomLimit, NativeAssetId},
Expand All @@ -64,7 +65,6 @@ use pallet_cosmos_x_wasm::msgs::{
MsgStoreCodeHandler, MsgUpdateAdminHandler,
};
use pallet_cosmwasm::instrument::CostRules;
use pallet_multimap::traits::UniqueMap;
use sp_core::{ConstU128, Pair, H256};
use sp_runtime::{
traits::{IdentityLookup, TryConvert},
Expand Down Expand Up @@ -115,10 +115,6 @@ mod runtime {
#[runtime::pallet_index(41)]
pub type Cosmwasm = pallet_cosmwasm;

#[runtime::pallet_index(50)]
pub type AddressMap = pallet_multimap<Instance1>;
#[runtime::pallet_index(51)]
pub type AssetMap = pallet_multimap<Instance2>;
#[runtime::pallet_index(100)]
pub type Babel = frame_babel;
}
Expand Down Expand Up @@ -198,7 +194,7 @@ impl TryConvert<String, AssetId> for AssetToDenom {
} else {
let denom_raw: BoundedVec<u8, MaxDenomLimit> =
denom.as_bytes().to_vec().try_into().map_err(|_| denom.clone())?;
AssetMap::find_key(denom_raw).ok_or(denom.clone())
<Test as frame_babel::Config>::AssetMap::find_key(denom_raw).ok_or(denom.clone())
}
}
}
Expand All @@ -207,9 +203,7 @@ impl TryConvert<AssetId, String> for AssetToDenom {
if asset_id == NativeAssetId::get() {
Ok(NativeDenom::get().to_string())
} else {
let denom =
<AssetMap as UniqueMap<AssetId, BoundedVec<u8, MaxDenomLimit>>>::get(asset_id)
.ok_or(asset_id)?;
let denom = <Test as frame_babel::Config>::AssetMap::get(asset_id).ok_or(asset_id)?;
String::from_utf8(denom.into()).map_err(|_| asset_id)
}
}
Expand Down Expand Up @@ -343,13 +337,13 @@ impl pallet_multimap::Config<Instance2> for Test {
}

impl frame_babel::Config for Test {
type AddressMap = AddressMap;
type AssetMap = AssetMap;
type AddressMap = frame_babel::AddressMap<Self>;
type AssetMap = frame_babel::AssetMap<Self>;
type Balance = Balance;
}

impl unify_account::Config for Test {
type AddressMap = AddressMap;
type AddressMap = frame_babel::AddressMap<Self>;
type DrainBalance = Balances;
}

Expand Down

0 comments on commit 23bdfd7

Please sign in to comment.