Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
feat: Add NativeAssetId to pallet-cosmos
Browse files Browse the repository at this point in the history
  • Loading branch information
code0xff committed Sep 13, 2024
1 parent 2eba410 commit ef69394
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
13 changes: 12 additions & 1 deletion frame/cosmos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ pub mod pallet {
pub const TxSigLimit: u64 = 7;
pub const MaxDenomLimit: u32 = 128;
pub const AddressPrefix: &'static str = "cosmos";
pub const NativeAssetId: u32 = u32::MAX;
}

#[frame_support::register_default_impl(TestDefaultConfig)]
Expand All @@ -240,6 +241,7 @@ pub mod pallet {
type MaxDenomLimit = MaxDenomLimit;
type AddressPrefix = AddressPrefix;
type Context = pallet_cosmos_types::context::Context;
type NativeAssetId = NativeAssetId;
}
}

Expand Down Expand Up @@ -319,6 +321,8 @@ pub mod pallet {
type AddressPrefix: Get<&'static str>;

type Context: Context;

type NativeAssetId: Get<Self::AssetId>;
}

#[pallet::genesis_config]
Expand All @@ -330,13 +334,20 @@ pub mod pallet {
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
let native_denom: BoundedVec<u8, T::MaxDenomLimit> =
T::NativeDenom::get().as_bytes().to_vec().try_into().unwrap();
DenomAssetRouter::<T>::insert(native_denom.clone(), T::NativeAssetId::get());
AssetDenomRouter::<T>::insert(T::NativeAssetId::get(), native_denom);

for (symbol, asset_id) in &self.assets {
let denom = BoundedVec::<u8, T::MaxDenomLimit>::try_from(symbol.clone())
.expect("Invalid denom");
assert!(DenomAssetRouter::<T>::get(denom.clone()).is_none());
assert!(AssetDenomRouter::<T>::get(asset_id.clone()).is_none());
assert!(*symbol == T::Assets::symbol(asset_id.clone()));

DenomAssetRouter::<T>::insert(denom, asset_id);
DenomAssetRouter::<T>::insert(denom.clone(), asset_id.clone());
AssetDenomRouter::<T>::insert(asset_id, denom);
}
}
}
Expand Down
25 changes: 17 additions & 8 deletions template/runtime/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use alloc::string::String;
use alloc::string::{String, ToString};
use frame_support::{ensure, traits::fungibles::metadata::Inspect};
use sp_core::Get;
use sp_runtime::{traits::Convert, BoundedVec};

pub struct AssetsCallback<T>(core::marker::PhantomData<T>);
Expand All @@ -43,7 +44,7 @@ where
}

fn destroyed(id: &T::AssetId) -> Result<(), ()> {
if let Some(denom) = pallet_cosmos::AssetDenomRouter::<T>::take(id.clone()) {
if let Some(denom) = pallet_cosmos::AssetDenomRouter::<T>::take(id) {
pallet_cosmos::DenomAssetRouter::<T>::remove(denom);
}

Expand All @@ -57,9 +58,13 @@ where
T: pallet_cosmos::Config,
{
fn convert(denom: String) -> Result<T::AssetId, ()> {
let denom = BoundedVec::<u8, T::MaxDenomLimit>::try_from(denom.as_bytes().to_vec())
.map_err(|_| ())?;
pallet_cosmos::DenomAssetRouter::<T>::get(denom).ok_or(())
if denom == T::NativeDenom::get() {
Ok(T::NativeAssetId::get())
} else {
let denom = BoundedVec::<u8, T::MaxDenomLimit>::try_from(denom.as_bytes().to_vec())
.map_err(|_| ())?;
pallet_cosmos::DenomAssetRouter::<T>::get(denom).ok_or(())
}
}
}

Expand All @@ -68,8 +73,12 @@ where
T: pallet_cosmos::Config,
{
fn convert(asset_id: T::AssetId) -> String {
// TODO: Handle option
let denom = pallet_cosmos::AssetDenomRouter::<T>::get(asset_id).unwrap().to_vec();
String::from_utf8(denom).unwrap()
if asset_id == T::NativeAssetId::get() {
T::NativeDenom::get().to_string()
} else {
// TODO: Handle option
let denom = pallet_cosmos::AssetDenomRouter::<T>::get(asset_id).unwrap().to_vec();
String::from_utf8(denom).unwrap()
}
}
}
6 changes: 4 additions & 2 deletions template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ use hp_account::CosmosSigner;
use hp_crypto::EcdsaExt;
use pallet_cosmos::{
config_preludes::{
AddressPrefix, ChainId, MaxDenomLimit, MaxMemoCharacters, MsgFilter, NativeDenom,
TxSigLimit, WeightToGas,
AddressPrefix, ChainId, MaxDenomLimit, MaxMemoCharacters, MsgFilter, NativeAssetId,
NativeDenom, TxSigLimit, WeightToGas,
},
AddressMapping,
};
Expand Down Expand Up @@ -377,6 +377,8 @@ impl pallet_cosmos::Config for Runtime {
type AddressPrefix = AddressPrefix;

type Context = Context;

type NativeAssetId = NativeAssetId;
}

impl pallet_cosmos_accounts::Config for Runtime {
Expand Down

0 comments on commit ef69394

Please sign in to comment.