Skip to content

Commit

Permalink
Governance controlled burn extrinsic (#893)
Browse files Browse the repository at this point in the history
## Describe your changes

1. Added governance extrinsic to burn native tokens
2. Remove Thea finality check for outgoing messages ( Temp. Fix )
  • Loading branch information
Gauthamastro authored Dec 29, 2023
2 parents 0360280 + a101488 commit 5650e81
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 32 deletions.
34 changes: 33 additions & 1 deletion pallets/thea-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ pub mod pallet {
use frame_support::{
pallet_prelude::*,
sp_runtime::SaturatedConversion,
traits::{fungible::Mutate, fungibles::Inspect, tokens::Preservation},
traits::{
fungible::Mutate,
fungibles::Inspect,
tokens::{Fortitude, Precision, Preservation},
},
transactional,
};
use frame_system::pallet_prelude::*;
Expand Down Expand Up @@ -119,6 +123,8 @@ pub mod pallet {
type ExistentialDeposit: Get<u128>;
/// Para Id
type ParaId: Get<u32>;
/// Governance Origin
type GovernanceOrigin: EnsureOrigin<<Self as frame_system::Config>::RuntimeOrigin>;
/// Type representing the weight of this pallet
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -181,6 +187,8 @@ pub mod pallet {
TheaKeyUpdated(Network, u32),
/// Withdrawal Fee Set (NetworkId, Amount)
WithdrawalFeeSet(u8, u128),
/// Native Token Burn event
NativeTokenBurned(T::AccountId, u128),
}

// Errors inform users that something went wrong.
Expand Down Expand Up @@ -370,6 +378,30 @@ pub mod pallet {
Self::deposit_event(Event::<T>::AssetMetadataSet(metadata));
Ok(())
}

/// Burn Native tokens of an account
///
/// # Parameters
///
/// * `who`: AccountId
/// * `amount`: Amount of native tokens to burn.
#[pallet::call_index(5)]
#[pallet::weight(<T as Config>::WeightInfo::parachain_withdraw(1))] // TODO: Update the weights
pub fn burn_native_tokens(
origin: OriginFor<T>,
who: T::AccountId,
amount: u128,
) -> DispatchResult {
T::GovernanceOrigin::ensure_origin(origin)?;
let burned_amt = <T as Config>::Currency::burn_from(
&who,
amount.saturated_into(),
Precision::BestEffort,
Fortitude::Force,
)?;
Self::deposit_event(Event::<T>::NativeTokenBurned(who, burned_amt.saturated_into()));
Ok(())
}
}

impl<T: Config> Pallet<T> {
Expand Down
1 change: 1 addition & 0 deletions pallets/thea-executor/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ impl thea_executor::Config for Test {
type MultiAssetIdAdapter = AssetId;
type AssetBalanceAdapter = u128;
type ExistentialDeposit = ExistentialDeposit;
type GovernanceOrigin = EnsureRoot<Self::AccountId>;
}

impl<C> frame_system::offchain::SendTransactionTypes<C> for Test
Expand Down
1 change: 1 addition & 0 deletions pallets/thea-message-handler/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ impl thea_executor::Config for Test {
type WeightInfo = thea_executor::weights::WeightInfo<Test>;
type MultiAssetIdAdapter = AssetId;
type AssetBalanceAdapter = u128;
type GovernanceOrigin = EnsureRoot<u64>;
type ExistentialDeposit = ExistentialDeposit;
}

Expand Down
32 changes: 2 additions & 30 deletions pallets/thea/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{resolver::Resolver, Config};
use crate::{resolver::Resolver, Config, OutgoingMessages};
use parity_scale_codec::{alloc::string::ToString, Decode, Encode};
use scale_info::prelude::string::String;
use sp_std::{marker::PhantomData, prelude::ToOwned, vec, vec::Vec};
Expand Down Expand Up @@ -55,18 +55,7 @@ impl<S: Decode, T: Config> AggregatorClient<S, T> {
match destination {
Destination::Solochain => {
// Get the outgoing message with nonce: `nonce` for network: `network`
let key = Self::create_solo_outgoing_message_key(nonce, network);
match Self::get_storage_at_latest_finalized_head::<Message>(
"solo_outgoing_message",
destination,
key,
) {
Ok(message) => message,
Err(err) => {
log::error!(target:"thea","Unable to get finalized solo head: {:?}",err);
None
},
}
<OutgoingMessages<T>>::get(network, nonce)
},
Destination::Parachain => {
// Get the outgoing message with nonce: `nonce` from network
Expand All @@ -90,23 +79,6 @@ impl<S: Decode, T: Config> AggregatorClient<S, T> {
}
}

/// Returns the encoded key for outgoing message for given nonce
/// # Parameters
/// * `nonce`: Nonce of the outgoing message
/// * `network`: Network of the outgoing message
/// # Returns
/// * `Vec<u8>`: Encoded key for outgoing message for given nonce
fn create_solo_outgoing_message_key(nonce: u64, network: Network) -> Vec<u8> {
let module_name = sp_io::hashing::twox_128(b"Thea");
let storage_prefix = sp_io::hashing::twox_128(b"OutgoingMessages");
let mut key = Vec::new();
key.append(&mut module_name.to_vec());
key.append(&mut storage_prefix.to_vec());
key.append(&mut network.encode());
key.append(&mut nonce.encode());
key
}

/// Returns the encoded key for outgoing message for given nonce for parachain
/// # Parameters
/// * `nonce`: Nonce of the outgoing message
Expand Down
1 change: 1 addition & 0 deletions pallets/thea/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ impl thea_executor::Config for Test {
type Swap = AssetConversion;
type MultiAssetIdAdapter = AssetId;
type AssetBalanceAdapter = u128;
type GovernanceOrigin = EnsureRoot<Self::AccountId>;
type ExistentialDeposit = ExistentialDeposit;
}

Expand Down
3 changes: 2 additions & 1 deletion runtimes/mainnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 316,
spec_version: 317,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
Expand Down Expand Up @@ -1367,6 +1367,7 @@ impl thea_executor::Config for Runtime {
type Swap = AssetConversion;
type MultiAssetIdAdapter = AssetId;
type AssetBalanceAdapter = u128;
type GovernanceOrigin = EnsureRootOrHalfCouncil;
type ExistentialDeposit = ExistentialDeposit;
}

Expand Down

0 comments on commit 5650e81

Please sign in to comment.