Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Update errors #230

Merged
merged 7 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions integration-tests/src/tests/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ use frame_support::{assert_err, assert_ok, dispatch::GetDispatchInfo, traits::to
use macros::generate_accounts;
use polimec_common::credentials::InvestorType;
use polimec_common_test_utils::{get_fake_jwt, get_test_jwt};
use sp_runtime::{generic::Era, traits::SignedExtension, AccountId32, DispatchError};
use sp_runtime::transaction_validity::InvalidTransaction::Payment;
use sp_runtime::transaction_validity::TransactionValidityError;
use sp_runtime::{
generic::Era,
traits::SignedExtension,
transaction_validity::{InvalidTransaction::Payment, TransactionValidityError},
AccountId32, DispatchError,
};
use tests::defaults::*;

#[test]
Expand All @@ -33,7 +36,7 @@ fn test_jwt_for_create() {
let retail_jwt = get_test_jwt(PolitestAccountId::from(ISSUER), InvestorType::Retail);
assert_noop!(
PolitestFundingPallet::create_project(PolitestOrigin::signed(ISSUER.into()), retail_jwt, project.clone()),
pallet_funding::Error::<PolitestRuntime>::NotAllowed
pallet_funding::Error::<PolitestRuntime>::WrongInvestorType
);
let inst_jwt = get_test_jwt(PolitestAccountId::from(ISSUER), InvestorType::Institutional);
assert_ok!(PolitestFundingPallet::create_project(
Expand Down Expand Up @@ -69,7 +72,7 @@ fn dispenser_signed_extensions_pass_for_new_account() {

let jwt = get_test_jwt(who.clone(), InvestorType::Retail);
let free_call = PolitestCall::Dispenser(pallet_dispenser::Call::dispense { jwt: jwt.clone() });
let paid_call = PolitestCall::System(frame_system::Call::remark{remark: vec![69, 69]});
let paid_call = PolitestCall::System(frame_system::Call::remark { remark: vec![69, 69] });
let extra: politest_runtime::SignedExtra = (
frame_system::CheckNonZeroSender::<PolitestRuntime>::new(),
frame_system::CheckSpecVersion::<PolitestRuntime>::new(),
Expand All @@ -80,8 +83,14 @@ fn dispenser_signed_extensions_pass_for_new_account() {
frame_system::CheckWeight::<PolitestRuntime>::new(),
pallet_transaction_payment::ChargeTransactionPayment::<PolitestRuntime>::from(0u64.into()).into(),
);
assert_err!(extra.validate(&who, &paid_call, &paid_call.get_dispatch_info(), 0), TransactionValidityError::Invalid(Payment));
assert_err!(extra.clone().pre_dispatch(&who, &paid_call, &paid_call.get_dispatch_info(), 0), TransactionValidityError::Invalid(Payment));
assert_err!(
extra.validate(&who, &paid_call, &paid_call.get_dispatch_info(), 0),
TransactionValidityError::Invalid(Payment)
);
assert_err!(
extra.clone().pre_dispatch(&who, &paid_call, &paid_call.get_dispatch_info(), 0),
TransactionValidityError::Invalid(Payment)
);

assert_ok!(extra.validate(&who, &free_call, &free_call.get_dispatch_info(), 0));
assert_ok!(extra.pre_dispatch(&who, &free_call, &free_call.get_dispatch_info(), 0));
Expand Down
66 changes: 0 additions & 66 deletions pallets/funding/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,72 +108,6 @@ directly. This is useful if you need to make use of this pallet's
functionalities in a pallet of your own, and you don't want to pay the
transaction fees twice.

### Example: A retail user buying tokens for a project in the community round

```rust
pub use pallet::*;

#[cfg(test)]
mod tests;

#[cfg(test)]
mod mock;

use pallet_funding::{self as funding};

#[frame_support::pallet(dev_mode)]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

#[pallet::pallet]
pub struct Pallet<T>(_);

#[pallet::config]
pub trait Config: frame_system::Config + funding::Config {}

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Buy tokens for a project in the community round if it achieved at least 500k USDT funding
#[pallet::weight(0)]
pub fn buy_if_popular(
origin: OriginFor<T>,
project_id: <T as funding::Config>::ProjectIdParameter,
amount: <T as funding::Config>::CurrencyBalance
) -> DispatchResult {
let retail_user = ensure_signed(origin)?;
let project_id: <T as funding::Config>::ProjectIdentifier = project_id.into();
// Check project is in the community round
let project_info = funding::Pallet::<T>::project_info(project_id).ok_or(Error::<T>::ProjectNotFound)?;
ensure!(project_info.project_status == funding::ProjectStatus::CommunityRound, "Project is not in the community round");

// Calculate how much funding was done already
let project_contributions: <T as funding::Config>::CurrencyBalance = funding::Contributions::<T>::iter_prefix_values(project_id)
.flatten()
.fold(
0u64.into(),
|total_tokens_bought, contribution| {
total_tokens_bought + contribution.contribution_amount
}
);

ensure!(project_contributions >= 500_000_0_000_000_000u64.into(), "Project did not achieve at least 500k USDT funding");

// Buy tokens with the default multiplier
<funding::Pallet<T>>::do_contribute(retail_user, project_id, amount, None)?;

Ok(())
}
}

#[pallet::error]
pub enum Error<T> {
ProjectNotFound,
}
}
```

## Credentials

The pallet will only allow users with certain credential types, to execute
Expand Down
Loading