Skip to content

Commit

Permalink
brief migration, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
f-gate committed Dec 6, 2023
1 parent c4e26ba commit 85f3e4a
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 24 deletions.
74 changes: 54 additions & 20 deletions libs/common-types/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,45 @@ pub enum CurrencyId {
ForeignAsset(ForeignAssetId),
}

enum 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
enum ForeignOwnedAccount {
pub enum ForeignOwnedAccount {
TRON([u8; 22]),
ETH([u8; 20]),
}
Expand All @@ -46,28 +78,30 @@ 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.
fn ensure_supported_currency(&self, currency: CurrencyId) -> bool {
pub fn ensure_supported_currency(&self, currency: CurrencyId) -> bool {
match currency {
Native => false,
KSM => false,
AUSD => false,
KAR => false,
MGX => false,
ForeignAsset(asset) => {
match self {
ForeignOwnedAccount::TRON(_) => {
match asset => {
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
}
}
}
},
ForeignOwnedAccount::ETH(_) => {
match asset {
ForeignAssetId::ETH => true,
ForeignAssetId::USDT => true
}
}
}

},
}
}
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
17 changes: 17 additions & 0 deletions pallets/briefs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,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 +174,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 +183,13 @@ 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 +242,7 @@ pub mod pallet {
applicant,
milestones,
deposit_id,
external_owned_address,
);

Briefs::<T>::insert(brief_id, brief);
Expand Down Expand Up @@ -325,6 +338,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 +385,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 +412,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 +422,7 @@ pub mod pallet {
applicant,
milestones,
deposit_id,
eoa,
}
}
}
Expand Down
58 changes: 58 additions & 0 deletions pallets/briefs/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,64 @@ pub mod v2 {
}
}

pub mod v3 {
use super::*;

pub struct MigrateToV3<T: Config>(T);
impl<T: Config> OnRuntimeUpgrade for MigrateToV2<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
let onchain = StorageVersion::<T>::on_chain_storage_version();
ensure!(onchain == 2, "onchain must be version 2 to run the migration.")
Ok(<Vec<u8> as Default>::default())
}

fn on_runtime_upgrade() -> Weight {
let current = Pallet::<T>::current_storage_version();
let onchain = StorageVersion::<T>::on_chain_storage_version();
let mut weight: Weight = Default::default();
if current == 3 && onchain == 2 {

Briefs::<T>::drain().for_each(|(key, brief)| {
let migrated_brief = BriefData {
created_at: brief.created_at,
brief_owners: brief.brief_owners,
budget: brief.budget,
currency_id: brief.currency_id,
applicant: brief.applicant,
milestones: brief.milestones,
deposit_id: brief.deposit_id,
eoa: None,
};

T::DbWeight::get().reads_writes(2, 2)
Briefs::<T>::insert(key, migrated_brief);
})

current.put::<Pallet<T>>();

log::warn!("v3 has been successfully applied");
weight = weight + T::DbWeight::get().reads_writes(2, 1);
} else {
log::warn!("Skipping v3, should be removed");
weight = weight + T::DbWeight::get().reads(1);
}
weight
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), TryRuntimeError> {
frame_support::ensure!(
Pallet::<T>::on_chain_storage_version() == 3,
"v3 has not been applied"
);

Ok(())
}
}
}


#[cfg(test)]
mod test {
use super::*;
Expand Down
12 changes: 12 additions & 0 deletions pallets/briefs/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn create_brief_brief_owner_overflow() {
gen_hash(1),
CurrencyId::Native,
get_milestones(10),
None,
),
Error::<Test>::TooManyBriefOwners
);
Expand All @@ -50,6 +51,7 @@ fn create_brief_with_no_contribution_ok() {
gen_hash(1),
CurrencyId::Native,
get_milestones(10),
None,
));
});
}
Expand All @@ -70,6 +72,7 @@ fn create_brief_no_contribution_and_contribute() {
brief_id,
CurrencyId::Native,
get_milestones(10),
None,
));

(0..5).for_each(|_| {
Expand Down Expand Up @@ -111,6 +114,7 @@ fn contribute_to_brief_not_brief_owner() {
brief_id,
CurrencyId::Native,
get_milestones(10),
None,
));

assert_noop!(
Expand Down Expand Up @@ -139,6 +143,7 @@ fn contribute_to_brief_more_than_total_ok() {
brief_id,
CurrencyId::Native,
get_milestones(10),
None,
));
assert_ok!(BriefsMod::contribute_to_brief(
RuntimeOrigin::signed(BOB),
Expand All @@ -163,6 +168,7 @@ fn create_brief_already_exists() {
brief_id,
CurrencyId::Native,
get_milestones(10),
None,
));

assert_noop!(
Expand All @@ -175,6 +181,7 @@ fn create_brief_already_exists() {
brief_id,
CurrencyId::Native,
get_milestones(10),
None,
),
Error::<Test>::BriefAlreadyExists
);
Expand All @@ -196,6 +203,7 @@ fn only_applicant_can_start_work() {
brief_id,
CurrencyId::Native,
get_milestones(10),
None,
));

assert_noop!(
Expand Down Expand Up @@ -225,6 +233,7 @@ fn initial_contribution_and_extra_contribution_aggregates() {
brief_id,
CurrencyId::Native,
get_milestones(10),
None,
));

assert_ok!(BriefsMod::contribute_to_brief(
Expand Down Expand Up @@ -261,6 +270,7 @@ fn reserved_funds_are_transferred_to_project_kitty() {
brief_id,
CurrencyId::Native,
get_milestones(10),
None,
);

assert_ok!(BriefsMod::commence_work(
Expand Down Expand Up @@ -293,6 +303,7 @@ fn cancel_brief_works() {
brief_id,
CurrencyId::Native,
get_milestones(10),
None,
));

assert_ok!(BriefsMod::contribute_to_brief(
Expand Down Expand Up @@ -359,6 +370,7 @@ fn cancel_brief_not_brief_owner() {
brief_id,
CurrencyId::Native,
get_milestones(10),
None,
));

assert_noop!(
Expand Down
1 change: 1 addition & 0 deletions pallets/grants/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn create_proposal_from_grant() {
contribution_value,
TreasuryOrigin::Imbue,
grant_id,
None,
));
assert!(Projects::<Test>::get(1).is_some());
});
Expand Down
13 changes: 13 additions & 0 deletions pallets/grants/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ pub mod pallet {
InvalidTreasuryOrigin,
/// Too many approvers
TooManyApprovers,
/// 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 @@ -116,9 +120,17 @@ pub mod pallet {
amount_requested: BalanceOf<T>,
treasury_origin: TreasuryOrigin,
grant_id: GrantId,
external_owned_address: Option<common_types::ForeignOwnedAccount>
) -> DispatchResultWithPostInfo {
let submitter = ensure_signed(origin)?;

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 percentage_sum = proposed_milestones
.iter()
.fold(Default::default(), |acc: Percent, x| {
Expand Down Expand Up @@ -168,6 +180,7 @@ pub mod pallet {
.try_into()
.map_err(|_| Error::<T>::TooManyApprovers)?,
pallet_proposals::FundingPath::WaitForFunding,
external_owned_address,
)?;

GrantsSubmittedBy::<T>::insert(&submitter, grant_id, ());
Expand Down
Loading

0 comments on commit 85f3e4a

Please sign in to comment.