-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
closes CHAIN-18
- Loading branch information
Showing
14 changed files
with
980 additions
and
130 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,114 @@ | ||
use crate::frame::traits::ExistenceRequirement; | ||
use crate::{whitelist, AccountIdOf, BalanceOf, Block}; | ||
use codec::{Decode, Encode, MaxEncodedLen}; | ||
use polkadot_sdk::frame_election_provider_support::Get; | ||
use polkadot_sdk::frame_support::dispatch::DispatchResult; | ||
use polkadot_sdk::frame_support::traits::Currency; | ||
use polkadot_sdk::frame_support::traits::WithdrawReasons; | ||
use polkadot_sdk::frame_support::DebugNoBound; | ||
use polkadot_sdk::polkadot_sdk_frame::prelude::OriginFor; | ||
use polkadot_sdk::sp_core::ConstU32; | ||
use polkadot_sdk::sp_runtime::BoundedVec; | ||
use polkadot_sdk::sp_std::vec::Vec; | ||
use scale_info::TypeInfo; | ||
|
||
use crate::{AccountIdOf, BalanceOf, Block}; | ||
|
||
#[derive(DebugNoBound, TypeInfo, Decode, Encode, MaxEncodedLen)] | ||
#[scale_info(skip_type_params(T))] | ||
pub struct AgentApplication<T: crate::Config> { | ||
pub id: u32, | ||
pub payer_key: AccountIdOf<T>, | ||
pub agent_key: AccountIdOf<T>, | ||
pub data: BoundedVec<u8, ConstU32<256>>, | ||
pub data: BoundedVec<u8, T::MaxApplicationDataLength>, | ||
pub cost: BalanceOf<T>, | ||
pub expires_at: Block, | ||
} | ||
|
||
pub fn submit_application<T: crate::Config>( | ||
_origin: OriginFor<T>, | ||
_agent_key: AccountIdOf<T>, | ||
_data: Vec<u8>, | ||
payer: AccountIdOf<T>, | ||
agent_key: AccountIdOf<T>, | ||
data: Vec<u8>, | ||
) -> DispatchResult { | ||
todo!() | ||
if whitelist::is_whitelisted::<T>(&agent_key) { | ||
return Err(crate::Error::<T>::AlreadyWhitelisted.into()); | ||
} | ||
|
||
let config = crate::GlobalGovernanceConfig::<T>::get(); | ||
let cost = config.agent_application_cost; | ||
|
||
let _ = <T as crate::Config>::Currency::withdraw( | ||
&payer, | ||
cost, | ||
WithdrawReasons::except(WithdrawReasons::TIP), | ||
ExistenceRequirement::KeepAlive, | ||
) | ||
.map_err(|_| crate::Error::<T>::NotEnoughBalanceToApply)?; | ||
|
||
let data_len: u32 = data | ||
.len() | ||
.try_into() | ||
.map_err(|_| crate::Error::<T>::InvalidApplicationDataLength)?; | ||
|
||
let data_range = T::MinApplicationDataLength::get()..T::MaxApplicationDataLength::get(); | ||
if !data_range.contains(&data_len) { | ||
return Err(crate::Error::<T>::InvalidApplicationDataLength.into()); | ||
} | ||
|
||
let current_block: u64 = | ||
TryInto::try_into(<polkadot_sdk::frame_system::Pallet<T>>::block_number()) | ||
.ok() | ||
.expect("blockchain will not exceed 2^64 blocks; QED."); | ||
|
||
let expires_at = current_block + config.agent_application_expiration; | ||
|
||
let application_id: u32 = crate::AgentApplicationId::<T>::mutate(|id| { | ||
let last_id = *id; | ||
*id = id.saturating_add(1); | ||
last_id | ||
}); | ||
|
||
let application = AgentApplication::<T> { | ||
id: application_id, | ||
payer_key: payer, | ||
agent_key, | ||
data: BoundedVec::truncate_from(data), | ||
cost, | ||
expires_at, | ||
}; | ||
|
||
crate::AgentApplications::<T>::insert(application_id, application); | ||
crate::Pallet::<T>::deposit_event(crate::Event::<T>::ApplicationCreated(application_id)); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn accept_application<T: crate::Config>( | ||
_origin: OriginFor<T>, | ||
_application_id: u32, | ||
) -> DispatchResult { | ||
todo!() | ||
pub fn accept_application<T: crate::Config>(application_id: u32) -> DispatchResult { | ||
let application = crate::AgentApplications::<T>::get(application_id) | ||
.ok_or(crate::Error::<T>::ApplicationNotFound)?; | ||
|
||
crate::AgentApplications::<T>::remove(application.id); | ||
|
||
whitelist::add_to_whitelist::<T>(application.agent_key.clone())?; | ||
crate::Pallet::<T>::deposit_event(crate::Event::<T>::ApplicationAccepted(application.id)); | ||
crate::Pallet::<T>::deposit_event(crate::Event::<T>::WhitelistAdded(application.agent_key)); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn deny_application<T: crate::Config>( | ||
_origin: OriginFor<T>, | ||
_application_id: u32, | ||
) -> DispatchResult { | ||
todo!() | ||
pub fn deny_application<T: crate::Config>(application_id: u32) -> DispatchResult { | ||
let application = crate::AgentApplications::<T>::get(application_id) | ||
.ok_or(crate::Error::<T>::ApplicationNotFound)?; | ||
|
||
crate::AgentApplications::<T>::remove(application.id); | ||
crate::Pallet::<T>::deposit_event(crate::Event::<T>::ApplicationDenied(application.id)); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub(crate) fn remove_expired_applications<T: crate::Config>(current_block: Block) { | ||
for application in crate::AgentApplications::<T>::iter_values() { | ||
if current_block < application.expires_at { | ||
continue; | ||
} | ||
|
||
crate::AgentApplications::<T>::remove(application.id); | ||
crate::Pallet::<T>::deposit_event(crate::Event::<T>::ApplicationExpired(application.id)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,55 @@ | ||
use crate::AccountIdOf; | ||
use polkadot_sdk::frame_election_provider_support::Get; | ||
use polkadot_sdk::sp_runtime::{DispatchError, Percent}; | ||
use polkadot_sdk::{ | ||
frame_support::dispatch::DispatchResult, polkadot_sdk_frame::prelude::OriginFor, | ||
frame_support::dispatch::DispatchResult, frame_system::ensure_signed, | ||
polkadot_sdk_frame::prelude::OriginFor, | ||
}; | ||
|
||
use crate::AccountIdOf; | ||
pub fn add_curator<T: crate::Config>(key: AccountIdOf<T>) -> DispatchResult { | ||
if crate::Curators::<T>::contains_key(&key) { | ||
return Err(crate::Error::<T>::AlreadyCurator.into()); | ||
} | ||
|
||
pub fn add_curator<T: crate::Config>( | ||
_origin: OriginFor<T>, | ||
_key: AccountIdOf<T>, | ||
) -> DispatchResult { | ||
todo!() | ||
crate::Curators::<T>::insert(key, ()); | ||
Ok(()) | ||
} | ||
|
||
pub fn remove_curator<T: crate::Config>( | ||
_origin: OriginFor<T>, | ||
_key: AccountIdOf<T>, | ||
pub fn remove_curator<T: crate::Config>(key: AccountIdOf<T>) -> DispatchResult { | ||
if !crate::Curators::<T>::contains_key(&key) { | ||
return Err(crate::Error::<T>::NotCurator.into()); | ||
} | ||
|
||
crate::Curators::<T>::remove(&key); | ||
Ok(()) | ||
} | ||
|
||
pub fn penalize_agent<T: crate::Config>( | ||
agent_key: AccountIdOf<T>, | ||
percentage: u8, | ||
) -> DispatchResult { | ||
todo!() | ||
if percentage > T::MaxPenaltyPercentage::get() { | ||
return Err(crate::Error::<T>::InvalidPenaltyPercentage.into()); | ||
} | ||
|
||
pallet_torus0::Agents::<T>::try_mutate(&agent_key, |agent| { | ||
let Some(agent) = agent else { | ||
return Err(crate::Error::<T>::AgentNotFound.into()); | ||
}; | ||
|
||
agent.weight_factor = Percent::from_percent(100u8.saturating_sub(percentage)); | ||
|
||
Ok::<(), DispatchError>(()) | ||
})?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn ensure_curator<T: crate::Config>(origin: OriginFor<T>) -> DispatchResult { | ||
let key: AccountIdOf<T> = ensure_signed(origin)?; | ||
if !crate::Curators::<T>::contains_key(key) { | ||
return Err(crate::Error::<T>::NotCurator.into()); | ||
} | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.