Skip to content

Commit

Permalink
Eoa field (#287)
Browse files Browse the repository at this point in the history
* new types

* ensure supported currency method

* brief migration, fixes

* fixes

* fmt

* fix up benchmarks

* fix brief test
  • Loading branch information
f-gate committed Dec 7, 2023
1 parent b232698 commit bd2562d
Show file tree
Hide file tree
Showing 18 changed files with 275 additions and 51 deletions.
3 changes: 3 additions & 0 deletions libs/common-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ std = [
'sp-std/std',
'xcm/std',
]

runtime-benchmarks = []

try-runtime = [
"common-traits/try-runtime",
"frame-support/try-runtime",
Expand Down
80 changes: 70 additions & 10 deletions libs/common-types/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,76 @@ pub enum CurrencyId {
ForeignAsset(ForeignAssetId),
}

#[derive(
Clone,
Copy,
PartialOrd,
Ord,
PartialEq,
Eq,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
Serialize,
Deserialize,
)]
pub enum ForeignAssetId {
ETH,
USDT,
}

#[derive(
Clone,
Copy,
PartialOrd,
Ord,
PartialEq,
Eq,
Debug,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
Serialize,
Deserialize,
)]
/// The foreign owned account describes the chain
pub enum ForeignOwnedAccount {
TRON([u8; 22]),
ETH([u8; 20]),
}

impl ForeignOwnedAccount {
/// Here we can define which currencies per network we support
/// For example when given a TRON account we can use this to see if the account
/// and the currency are compatible.
pub fn ensure_supported_currency(&self, currency: CurrencyId) -> bool {
match currency {
CurrencyId::Native => false,
CurrencyId::KSM => false,
CurrencyId::AUSD => false,
CurrencyId::KAR => false,
CurrencyId::MGX => false,
CurrencyId::ForeignAsset(asset) => match &self {
ForeignOwnedAccount::TRON(_) => match asset {
ForeignAssetId::ETH => false,
ForeignAssetId::USDT => true,
},
ForeignOwnedAccount::ETH(_) => match asset {
ForeignAssetId::ETH => true,
ForeignAssetId::USDT => true,
},
},
}
}
#[cfg(feature = "runtime-benchmarks")]
pub fn get_supported_currency_eoa_combo() -> (ForeignOwnedAccount, CurrencyId) {
(ForeignOwnedAccount::ETH(Default::default()), CurrencyId::ForeignAsset(ForeignAssetId::ETH))
}
}

pub mod currency_decimals {
pub const NATIVE: u32 = 12;
pub const AUSD: u32 = 12;
Expand All @@ -39,16 +109,6 @@ pub mod currency_decimals {
pub const MGX: u32 = 18;
}

// A way to generate different currencies from a number.
// Can be used in tests/benchmarks to generate different currencies.
impl From<u32> for CurrencyId {
fn from(value: u32) -> Self {
CurrencyId::ForeignAsset(value)
}
}

pub type ForeignAssetId = u32;

#[derive(
Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Debug, Encode, Decode, TypeInfo, MaxEncodedLen,
)]
Expand Down
1 change: 1 addition & 0 deletions pallets/briefs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ std = [

runtime-benchmarks = [
"common-runtime/runtime-benchmarks",
"common-types/runtime-benchmarks",
"frame-benchmarking/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"pallet-deposits/runtime-benchmarks",
Expand Down
56 changes: 33 additions & 23 deletions pallets/briefs/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::*;
use crate::test_utils::gen_hash;
use crate::Pallet as Briefs;
use crate::{BoundedBriefOwners, BoundedProposedMilestones};
use common_types::CurrencyId;
use common_types::{CurrencyId, ForeignOwnedAccount};
use frame_benchmarking::v2::*;
use frame_support::{assert_ok, traits::Get};
use frame_system::{EventRecord, RawOrigin};
Expand All @@ -22,14 +22,15 @@ mod benchmarks {

#[benchmark]
fn create_brief() {
let brief_owners = get_max_brief_owners::<T>();
let (eoa, currency_id) = ForeignOwnedAccount::get_supported_currency_eoa_combo();
let brief_owners = get_max_brief_owners::<T>(currency_id);
let caller: T::AccountId = brief_owners[0].clone();
let applicant = create_account_id::<T>("applicant", 1);
let applicant = create_account_id::<T>("applicant", 1, currency_id);
let budget = 10_000u32.into();
let initial_contribution = 5_000u32.into();
let brief_id = gen_hash(1);
let milestones = get_max_milestones::<T>();
// (origin, brief_owners, applicant, budget, initial_contribution, brief_id, currency_id, milestones)
// (origin, brief_owners, applicant, budget, initial_contribution, brief_id, currency_id, milestones, Option<eoa>)

#[extrinsic_call]
create_brief(
Expand All @@ -39,17 +40,19 @@ mod benchmarks {
budget,
initial_contribution,
brief_id,
CurrencyId::Native,
currency_id,
milestones,
Some(eoa)
);
assert_last_event::<T>(Event::<T>::BriefSubmitted(caller, brief_id).into());
}

#[benchmark]
fn contribute_to_brief() {
let brief_owners = get_max_brief_owners::<T>();
let currency_id = CurrencyId::Native;
let brief_owners = get_max_brief_owners::<T>(currency_id);
let caller: T::AccountId = brief_owners[0].clone();
let applicant: T::AccountId = create_account_id::<T>("applicant", 1);
let applicant: T::AccountId = create_account_id::<T>("applicant", 1, currency_id);
let budget = 10_000_000_000_000u128.saturated_into();
let initial_contribution = 5_000_000_000_000u128.saturated_into();
let contribution = 5_000_000_000_000u128.saturated_into();
Expand All @@ -62,8 +65,9 @@ mod benchmarks {
budget,
initial_contribution,
brief_id,
CurrencyId::Native,
milestones
currency_id,
milestones,
None,
));
let brief_owner: T::AccountId = brief_owners[0].clone();
// (brief_owner, brief_id, contribution)
Expand All @@ -78,22 +82,26 @@ mod benchmarks {

#[benchmark]
fn commence_work() {
let brief_owners = get_max_brief_owners::<T>();
let currency_id = CurrencyId::Native;
let brief_owners = get_max_brief_owners::<T>(currency_id);
let caller: T::AccountId = brief_owners[0].clone();
let applicant: T::AccountId = create_account_id::<T>("applicant", 1);
let applicant: T::AccountId = create_account_id::<T>("applicant", 1, currency_id);
let budget = 10_000_000_000_000u128.saturated_into();
let initial_contribution = 5_000_000_000_000u128.saturated_into();
let brief_id = gen_hash(1);
let milestones = get_max_milestones::<T>();


assert_ok!(Briefs::<T>::create_brief(
RawOrigin::Signed(caller).into(),
brief_owners,
applicant.clone(),
budget,
initial_contribution,
brief_id,
CurrencyId::Native,
milestones
currency_id,
milestones,
None,
));
// (origin, brief_id)
#[extrinsic_call]
Expand All @@ -103,9 +111,10 @@ mod benchmarks {

#[benchmark]
fn cancel_brief() {
let brief_owners = get_max_brief_owners::<T>();
let currency_id = CurrencyId::Native;
let brief_owners = get_max_brief_owners::<T>(currency_id);
let caller: T::AccountId = brief_owners[0].clone();
let applicant: T::AccountId = create_account_id::<T>("applicant", 1);
let applicant: T::AccountId = create_account_id::<T>("applicant", 1, currency_id);
let budget = 10_000_000_000_000u128.saturated_into();
let initial_contribution = 5_000_000_000_000u128.saturated_into();
let brief_id = gen_hash(1);
Expand All @@ -117,8 +126,9 @@ mod benchmarks {
budget,
initial_contribution,
brief_id,
CurrencyId::Native,
milestones
currency_id,
milestones,
None,
));
// (origin, brief_id)
#[extrinsic_call]
Expand All @@ -133,11 +143,11 @@ mod benchmarks {
);
}

fn create_account_id<T: Config>(suri: &'static str, n: u32) -> T::AccountId {
fn create_account_id<T: Config>(suri: &'static str, n: u32, currency_id: CurrencyId) -> T::AccountId {
let user = account(suri, n, SEED);
let initial_balance = 1_000_000_000_000_000u128;
assert_ok!(T::RMultiCurrency::deposit(
CurrencyId::Native,
currency_id,
&user,
initial_balance.saturated_into()
));
Expand All @@ -152,21 +162,21 @@ fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
assert_eq!(event, &system_event);
}

fn get_brief_owners<T: Config>(mut n: u32) -> BoundedBriefOwners<T> {
fn get_brief_owners<T: Config>(mut n: u32, currency: CurrencyId) -> BoundedBriefOwners<T> {
let max = <T as Config>::MaxBriefOwners::get();
if n > max {
n = max;
}
(0..n)
.map(|i| create_account_id::<T>("brief_owner", i))
.map(|i| create_account_id::<T>("brief_owner", i, currency))
.collect::<Vec<T::AccountId>>()
.try_into()
.expect("qed")
}

fn get_max_brief_owners<T: Config>() -> BoundedBriefOwners<T> {
fn get_max_brief_owners<T: Config>(currency_id: CurrencyId) -> BoundedBriefOwners<T> {
let max_brief_owners: u32 = <T as Config>::MaxBriefOwners::get();
get_brief_owners::<T>(max_brief_owners)
get_brief_owners::<T>(max_brief_owners, currency_id)
}

fn get_milestones<T: Config>(mut n: u32) -> BoundedProposedMilestones<T> {
Expand Down
1 change: 1 addition & 0 deletions pallets/briefs/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn create_proposal_from_brief() {
brief_id,
CurrencyId::Native,
get_milestones(10),
None,
);

assert_ok!(BriefsMod::commence_work(
Expand Down
29 changes: 27 additions & 2 deletions pallets/briefs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ pub use pallet::*;
pub mod weights;
pub use weights::*;

pub mod migrations;

#[cfg(test)]
mod mock;

Expand Down Expand Up @@ -57,12 +59,12 @@ pub mod pallet {
BalanceOf<T>,
AccountIdOf<T>,
>>::StorageItem;
type DepositIdOf<T> =
pub(crate) type DepositIdOf<T> =
<<T as Config>::DepositHandler as DepositHandler<BalanceOf<T>, AccountIdOf<T>>>::DepositId;

pub type BriefHash = H256;

const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);
const STORAGE_VERSION: StorageVersion = StorageVersion::new(3);

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
Expand Down Expand Up @@ -152,6 +154,10 @@ pub mod pallet {
MilestonesTotalPercentageMustEqual100,
/// too many milestones here mate fixed with https://github.com/ImbueNetwork/imbue/issues/267
TooManyMilestones,
/// If youre using a foreign currency then you need an external_owned_address.
EoaRequiredForForeignCurrencies,
/// Currency is not supported for this external address.
CurrencyAccountComboNotSupported,
}

#[pallet::call]
Expand All @@ -170,6 +176,7 @@ pub mod pallet {
brief_id: BriefHash,
currency_id: CurrencyId,
milestones: BoundedProposedMilestones<T>,
external_owned_address: Option<common_types::ForeignOwnedAccount>,
) -> DispatchResult {
let who = ensure_signed(origin)?;

Expand All @@ -178,6 +185,19 @@ pub mod pallet {
Error::<T>::BriefAlreadyExists
);

if let CurrencyId::ForeignAsset(_) = currency_id {
ensure!(
external_owned_address.is_some(),
Error::<T>::EoaRequiredForForeignCurrencies
);
}
if let Some(eoa) = external_owned_address {
ensure!(
eoa.ensure_supported_currency(currency_id),
Error::<T>::CurrencyAccountComboNotSupported
);
}

let total_percentage = milestones
.iter()
.fold(Percent::zero(), |acc: Percent, ms: &ProposedMilestone| {
Expand Down Expand Up @@ -230,6 +250,7 @@ pub mod pallet {
applicant,
milestones,
deposit_id,
external_owned_address,
);

Briefs::<T>::insert(brief_id, brief);
Expand Down Expand Up @@ -325,6 +346,7 @@ pub mod pallet {
.try_into()
.map_err(|_| Error::<T>::TooManyMilestones)?,
FundingPath::TakeFromReserved,
brief.eoa,
)?;

BriefContributions::<T>::remove(brief_id);
Expand Down Expand Up @@ -371,6 +393,7 @@ pub mod pallet {
pub applicant: AccountIdOf<T>,
pub milestones: BoundedProposedMilestones<T>,
pub deposit_id: DepositIdOf<T>,
pub eoa: Option<common_types::ForeignOwnedAccount>,
}

impl<T: Config> Pallet<T> {
Expand All @@ -397,6 +420,7 @@ pub mod pallet {
applicant: AccountIdOf<T>,
milestones: BoundedProposedMilestones<T>,
deposit_id: DepositIdOf<T>,
eoa: Option<common_types::ForeignOwnedAccount>,
) -> Self {
Self {
created_at,
Expand All @@ -406,6 +430,7 @@ pub mod pallet {
applicant,
milestones,
deposit_id,
eoa,
}
}
}
Expand Down
Loading

0 comments on commit bd2562d

Please sign in to comment.