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 and NativeDenom to pallet-cosmwasm
Browse files Browse the repository at this point in the history
  • Loading branch information
code0xff committed Sep 13, 2024
1 parent ef69394 commit 3df9893
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
8 changes: 7 additions & 1 deletion frame/cosmos/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use super::*;
use alloc::{boxed::Box, string::String, vec::Vec};
use bech32::{Bech32, Hrp};
use config_preludes::{NativeAssetId, NativeDenom};
use core::marker::PhantomData;
use cosmos_sdk_proto::{
cosmos::bank::v1beta1::MsgSend,
Expand Down Expand Up @@ -114,7 +115,7 @@ impl<T, Context> pallet_cosmos_types::msgservice::MsgServiceRouter<Context> for
where
T: frame_system::Config + pallet_cosmos::Config + pallet_cosmwasm::Config,
T::AccountId: EcdsaExt,
Context: context::Context,
Context: context::traits::Context,
{
fn route(msg: &Any) -> Option<Box<dyn MsgHandler<Context>>> {
any_match!(
Expand Down Expand Up @@ -282,6 +283,10 @@ impl pallet_cosmwasm::Config for Test {
type UploadWasmOrigin = frame_system::EnsureSigned<Self::AccountId>;

type ExecuteWasmOrigin = frame_system::EnsureSigned<Self::AccountId>;

type NativeAssetId = NativeAssetId;

type NativeDenom = NativeDenom;
}

impl pallet_cosmos_accounts::Config for Test {
Expand Down Expand Up @@ -328,6 +333,7 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall {

fn pre_dispatch_self_contained(
&self,
_info: &Self::SignedInfo,
dispatch_info: &DispatchInfoOf<RuntimeCall>,
len: usize,
) -> Option<Result<(), TransactionValidityError>> {
Expand Down
37 changes: 28 additions & 9 deletions frame/cosmwasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ use frame_support::{
traits::{
fungibles::{Inspect as FungiblesInspect, Mutate as FungiblesMutate},
tokens::Preservation,
Get, ReservableCurrency, UnixTime,
Currency, ExistenceRequirement, Get, ReservableCurrency, UnixTime,
},
ReversibleStorageHasher, StorageHasher,
};
Expand Down Expand Up @@ -126,7 +126,7 @@ pub mod pallet {
traits::{
fungibles::{Inspect as FungiblesInspect, Mutate as FungiblesMutate},
tokens::{AssetId, Balance},
BuildGenesisConfig, Get, ReservableCurrency, UnixTime,
BuildGenesisConfig, Currency, Get, ReservableCurrency, UnixTime,
},
transactional, PalletId, Twox64Concat,
};
Expand Down Expand Up @@ -308,7 +308,8 @@ pub mod pallet {
+ Convert<String, Result<AssetIdOf<Self>, ()>>;

/// Interface used to pay when uploading code.
type NativeAsset: ReservableCurrency<AccountIdOf<Self>, Balance = BalanceOf<Self>>;
type NativeAsset: ReservableCurrency<AccountIdOf<Self>, Balance = BalanceOf<Self>>
+ Currency<AccountIdOf<Self>>;

/// Interface from which we are going to execute assets operations.
type Assets: FungiblesInspect<
Expand All @@ -331,6 +332,10 @@ pub mod pallet {
type UploadWasmOrigin: EnsureOrigin<Self::RuntimeOrigin>;

type ExecuteWasmOrigin: EnsureOrigin<Self::RuntimeOrigin>;

type NativeDenom: Get<&'static str>;

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

#[pallet::pallet]
Expand Down Expand Up @@ -1274,12 +1279,20 @@ impl<T: Config> Pallet<T> {
/// Retrieve an account balance.
pub(crate) fn do_balance(account: &AccountIdOf<T>, denom: String) -> Result<u128, Error<T>> {
let asset = Self::cosmwasm_asset_to_native_asset(denom)?;
Ok(T::Assets::balance(asset, account).into())
if asset == T::NativeAssetId::get() {
Ok(T::NativeAsset::free_balance(account).into())
} else {
Ok(T::Assets::balance(asset, account).into())
}
}

pub(crate) fn do_supply(denom: String) -> Result<u128, Error<T>> {
let asset = Self::cosmwasm_asset_to_native_asset(denom)?;
Ok(T::Assets::total_issuance(asset).into())
if denom == T::NativeDenom::get() {
Ok(T::NativeAsset::total_issuance().into())
} else {
let asset = Self::cosmwasm_asset_to_native_asset(denom)?;
Ok(T::Assets::total_issuance(asset).into())
}
}

/// Execute a transfer of funds between two accounts.
Expand All @@ -1290,10 +1303,16 @@ impl<T: Config> Pallet<T> {
preservation: Preservation,
) -> Result<(), Error<T>> {
for Coin { denom, amount } in funds {
let asset = Self::cosmwasm_asset_to_native_asset(denom.clone())?;
let amount = amount.u128().saturated_into();
T::Assets::transfer(asset, from, to, amount, preservation)
.map_err(|_| Error::<T>::TransferFailed)?;

if denom == T::NativeDenom::get() {
T::NativeAsset::transfer(from, to, amount, ExistenceRequirement::KeepAlive)
.map_err(|_| Error::<T>::TransferFailed)?;
} else {
let asset = Self::cosmwasm_asset_to_native_asset(denom.clone())?;
T::Assets::transfer(asset, from, to, amount, preservation)
.map_err(|_| Error::<T>::TransferFailed)?;
}
}
Ok(())
}
Expand Down
8 changes: 3 additions & 5 deletions template/runtime/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ where
{
fn created(id: &T::AssetId, _owner: &T::AccountId) -> Result<(), ()> {
let symbol = <T as pallet_cosmos::Config>::Assets::symbol(id.clone());
if symbol.is_empty() {
return Err(());
}
ensure!(!symbol.is_empty(), ());

let denom = BoundedVec::<u8, T::MaxDenomLimit>::try_from(symbol).map_err(|_| ())?;

Expand Down Expand Up @@ -61,8 +59,8 @@ where
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(|_| ())?;
let denom: BoundedVec<u8, T::MaxDenomLimit> =
denom.as_bytes().to_vec().try_into().map_err(|_| ())?;
pallet_cosmos::DenomAssetRouter::<T>::get(denom).ok_or(())
}
}
Expand Down
4 changes: 4 additions & 0 deletions template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,10 @@ impl pallet_cosmwasm::Config for Runtime {
type UploadWasmOrigin = frame_system::EnsureSigned<Self::AccountId>;

type ExecuteWasmOrigin = frame_system::EnsureSigned<Self::AccountId>;

type NativeDenom = NativeDenom;

type NativeAssetId = NativeAssetId;
}

impl pallet_sudo::Config for Runtime {
Expand Down

0 comments on commit 3df9893

Please sign in to comment.