Skip to content

Commit

Permalink
Ethereum compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed Oct 22, 2024
1 parent 38af93a commit 06d2a5f
Show file tree
Hide file tree
Showing 26 changed files with 433 additions and 17 deletions.
9 changes: 7 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ scale-info = { version = "2.11.3", default-features = false, features = [
] }
jsonrpsee = { version = "0.22.5", features = ["server"] }
hex-literal = "0.4.1"
hex = "0.4.3"
serde = { version = "1.0.204", default-features = false }
serde_json = "1.0.120"
smallvec = "1.13.2"
Expand Down
2 changes: 2 additions & 0 deletions integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ pallet-session.workspace = true
pallet-proxy-bonding.workspace = true
pallet-skip-feeless-payment.workspace = true
xcm-fee-payment-runtime-api.workspace = true
hex-literal.workspace = true
hex.workspace = true

# Runtimes
polkadot-runtime.workspace = true
Expand Down
1 change: 1 addition & 0 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod tests;

pub use constants::{accounts::*, asset_hub, penpal, polimec, polkadot};
pub use frame_support::{assert_noop, assert_ok, pallet_prelude::Weight, parameter_types, traits::Hooks};
use macros::generate_accounts;
pub use parachains_common::{AccountId, AssetHubPolkadotAuraId, AuraId, Balance, BlockNumber};
pub use sp_core::{sr25519, storage::Storage, Encode, Get};
pub use xcm::prelude::*;
Expand Down
5 changes: 3 additions & 2 deletions integration-tests/src/tests/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use crate::PolimecRuntime;
use frame_support::BoundedVec;
pub use pallet_funding::instantiator::{BidParams, ContributionParams, UserToUSDBalance};
use pallet_funding::{
AcceptedFundingAsset, BiddingTicketSizes, ContributingTicketSizes, CurrencyMetadata, ParticipationMode,
PriceProviderOf, ProjectMetadata, ProjectMetadataOf, TicketSize,
AcceptedFundingAsset, BiddingTicketSizes, ContributingTicketSizes, CurrencyMetadata, ParticipantsAccountType,
ParticipationMode, PriceProviderOf, ProjectMetadata, ProjectMetadataOf, TicketSize,
};
use sp_arithmetic::{FixedPointNumber, Percent};

Expand Down Expand Up @@ -88,6 +88,7 @@ pub fn default_project_metadata(issuer: AccountId) -> ProjectMetadataOf<polimec_
participation_currencies: vec![AcceptedFundingAsset::USDT].try_into().unwrap(),
funding_destination_account: issuer,
policy_ipfs_cid: Some(ipfs_hash()),
participants_account_type: ParticipantsAccountType::Polkadot,
}
}
pub fn default_evaluations() -> Vec<UserToUSDBalance<PolimecRuntime>> {
Expand Down
1 change: 1 addition & 0 deletions integration-tests/src/tests/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub fn project_metadata() -> ProjectMetadataOf<PolimecRuntime> {
.unwrap(),
funding_destination_account: ISSUER.into(),
policy_ipfs_cid: Some(metadata_hash),
participants_account_type: ParticipantsAccountType::Polkadot,
}
}

Expand Down
28 changes: 28 additions & 0 deletions integration-tests/src/tests/ethereum_support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// use macros::generate_accounts;
// use crate::PolimecNet;
// use crate::tests::defaults::IntegrationInstantiator;
use crate::*;
use hex_literal::hex;
use sp_core::{crypto::Ss58Codec, ecdsa, ByteArray, Pair};
use sp_io::hashing::keccak_256;

generate_accounts!(ETH_BUYER);

#[test]
fn test_hardcoded_signatures() {
let polimec_account: PolimecAccountId = ETH_BUYER.into();
let project_id = 0;

// Values generated with `https://github.com/lrazovic/ethsigner`
let ethereum_account: [u8; 20] = hex!("FCAd0B19bB29D4674531d6f115237E16AfCE377c");
let signature: [u8; 65] = hex!("76889adb946708eb797edc559326c2053732b307874edb0a021fdab454db85e317169c709c905635223d135a150d54285c67f495ea6162bcbbf05d8d6c7d036b1b");

PolimecNet::execute_with(|| {
assert_ok!(PolimecFunding::verify_receiving_account_signature(
&polimec_account,
project_id,
&Junction::AccountKey20 { network: Some(NetworkId::Ethereum { chain_id: 1 }), key: ethereum_account },
signature,
));
});
}
1 change: 1 addition & 0 deletions integration-tests/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod credentials;
mod ct_migration;
mod defaults;
mod e2e;
mod ethereum_support;
mod evaluator_slash_sideffects;
mod governance;
mod oracle;
Expand Down
3 changes: 3 additions & 0 deletions pallets/funding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ polkadot-parachain-primitives.workspace = true
sp-api.workspace = true
polimec-common-test-utils = { workspace = true, optional = true }
frame-benchmarking = { workspace = true, optional = true }
hex-literal.workspace = true
k256 = "0.13.4"

# Used in the instantiator.
itertools.workspace = true
Expand Down Expand Up @@ -90,6 +92,7 @@ std = [
"xcm-builder/std",
"xcm-executor/std",
"xcm/std",
"k256/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
Expand Down
2 changes: 2 additions & 0 deletions pallets/funding/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ where
participation_currencies: vec![AcceptedFundingAsset::USDT].try_into().unwrap(),
funding_destination_account: issuer,
policy_ipfs_cid: Some(metadata_hash.into()),
participants_account_type: ParticipantsAccountType::Polkadot,
}
}

Expand Down Expand Up @@ -410,6 +411,7 @@ mod benchmarks {
participation_currencies: vec![AcceptedFundingAsset::USDT, AcceptedFundingAsset::USDC].try_into().unwrap(),
funding_destination_account: issuer_funding.clone().clone(),
policy_ipfs_cid: Some(BoundedVec::try_from(IPFS_CID.as_bytes().to_vec()).unwrap()),
participants_account_type: ParticipantsAccountType::Ethereum,
};

let jwt = get_mock_jwt_with_cid(
Expand Down
7 changes: 7 additions & 0 deletions pallets/funding/src/functions/2_evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl<T: Config> Pallet<T> {
usd_amount: Balance,
did: Did,
whitelisted_policy: Cid,
receiving_account: Junction,
) -> DispatchResultWithPostInfo {
// * Get variables *
let project_metadata = ProjectsMetadata::<T>::get(project_id).ok_or(Error::<T>::ProjectMetadataNotFound)?;
Expand All @@ -88,6 +89,7 @@ impl<T: Config> Pallet<T> {
let total_evaluations_count = EvaluationCounts::<T>::get(project_id);
let user_evaluations_count = Evaluations::<T>::iter_prefix((project_id, evaluator)).count() as u32;
let project_policy = project_metadata.policy_ipfs_cid.ok_or(Error::<T>::ImpossibleState)?;
let project_metadata = ProjectsMetadata::<T>::get(project_id).ok_or(Error::<T>::ProjectDetailsNotFound)?;

// * Validity Checks *
ensure!(project_policy == whitelisted_policy, Error::<T>::PolicyMismatch);
Expand All @@ -100,6 +102,10 @@ impl<T: Config> Pallet<T> {
);
ensure!(total_evaluations_count < T::MaxEvaluationsPerProject::get(), Error::<T>::TooManyProjectParticipations);
ensure!(user_evaluations_count < T::MaxEvaluationsPerUser::get(), Error::<T>::TooManyUserParticipations);
ensure!(
project_metadata.participants_account_type.junction_is_supported(&receiving_account),
Error::<T>::UnsupportedReceiverAccountJunction
);

let plmc_bond = plmc_usd_price
.reciprocal()
Expand Down Expand Up @@ -129,6 +135,7 @@ impl<T: Config> Pallet<T> {
early_usd_amount,
late_usd_amount,
when: now,
receiving_account,
};

T::NativeCurrency::hold(&HoldReason::Evaluation.into(), evaluator, plmc_bond)?;
Expand Down
20 changes: 18 additions & 2 deletions pallets/funding/src/functions/3_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,17 @@ impl<T: Config> Pallet<T> {
#[transactional]
pub fn do_bid(params: DoBidParams<T>) -> DispatchResultWithPostInfo {
// * Get variables *
let DoBidParams { bidder, project_id, ct_amount, mode, funding_asset, investor_type, did, whitelisted_policy } =
params;
let DoBidParams {
bidder,
project_id,
ct_amount,
mode,
funding_asset,
investor_type,
did,
whitelisted_policy,
receiving_account,
} = params;
let project_metadata = ProjectsMetadata::<T>::get(project_id).ok_or(Error::<T>::ProjectMetadataNotFound)?;
let project_details = ProjectsDetails::<T>::get(project_id).ok_or(Error::<T>::ProjectDetailsNotFound)?;

Expand Down Expand Up @@ -110,6 +119,10 @@ impl<T: Config> Pallet<T> {
Error::<T>::TooHigh
);
ensure!(existing_bids.len() < T::MaxBidsPerUser::get() as usize, Error::<T>::TooManyUserParticipations);
ensure!(
project_metadata.participants_account_type.junction_is_supported(&receiving_account),
Error::<T>::UnsupportedReceiverAccountJunction
);

// While there's a remaining amount to bid for
while !amount_to_bid.is_zero() {
Expand All @@ -135,6 +148,7 @@ impl<T: Config> Pallet<T> {
metadata_ticket_size_bounds,
total_bids_by_bidder: existing_bids_amount.saturating_add(perform_bid_calls),
total_bids_for_project: total_bids_for_project.saturating_add(perform_bid_calls),
receiving_account,
};
Self::do_perform_bid(perform_params)?;

Expand Down Expand Up @@ -169,6 +183,7 @@ impl<T: Config> Pallet<T> {
metadata_ticket_size_bounds,
total_bids_by_bidder,
total_bids_for_project,
receiving_account,
} = do_perform_bid_params;

let ticket_size = ct_usd_price.checked_mul_int(ct_amount).ok_or(Error::<T>::BadMath)?;
Expand Down Expand Up @@ -200,6 +215,7 @@ impl<T: Config> Pallet<T> {
mode,
plmc_bond,
when: now,
receiving_account,
};

Self::bond_plmc_with_mode(&bidder, project_id, plmc_bond, mode, funding_asset)?;
Expand Down
8 changes: 8 additions & 0 deletions pallets/funding/src/functions/4_contribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ impl<T: Config> Pallet<T> {
investor_type,
did,
whitelisted_policy,
receiving_account,
} = params;
let mut project_details = ProjectsDetails::<T>::get(project_id).ok_or(Error::<T>::ProjectDetailsNotFound)?;
let did_has_winning_bid = DidWithWinningBids::<T>::get(project_id, did.clone());
Expand Down Expand Up @@ -46,6 +47,7 @@ impl<T: Config> Pallet<T> {
investor_type,
did,
whitelisted_policy,
receiving_account,
};

Self::do_perform_contribution(perform_params)
Expand All @@ -63,6 +65,7 @@ impl<T: Config> Pallet<T> {
investor_type,
did,
whitelisted_policy,
receiving_account,
} = params;

let project_metadata = ProjectsMetadata::<T>::get(project_id).ok_or(Error::<T>::ProjectMetadataNotFound)?;
Expand Down Expand Up @@ -107,6 +110,10 @@ impl<T: Config> Pallet<T> {
contributor_ticket_size.usd_ticket_below_maximum_per_did(total_usd_bought_by_did + ticket_size),
Error::<T>::TooHigh
);
ensure!(
project_metadata.participants_account_type.junction_is_supported(&receiving_account),
Error::<T>::UnsupportedReceiverAccountJunction
);

let plmc_bond = Self::calculate_plmc_bond(ticket_size, multiplier)?;
let funding_asset_amount = Self::calculate_funding_asset_amount(ticket_size, funding_asset)?;
Expand All @@ -124,6 +131,7 @@ impl<T: Config> Pallet<T> {
funding_asset_amount,
plmc_bond,
when: now,
receiving_account,
};

// Try adding the new contribution to the system
Expand Down
9 changes: 6 additions & 3 deletions pallets/funding/src/functions/6_settlement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use polimec_common::{
ReleaseSchedule,
};
use sp_runtime::{
traits::{Convert, Zero},
traits::{Zero},
Perquintill,
};

Expand Down Expand Up @@ -140,6 +140,7 @@ impl<T: Config> Pallet<T> {
ParticipationType::Evaluation,
ct_rewarded,
duration,
evaluation.receiving_account,
)?;
}
Evaluations::<T>::remove((project_id, evaluation.evaluator.clone(), evaluation.id));
Expand Down Expand Up @@ -204,6 +205,7 @@ impl<T: Config> Pallet<T> {
ParticipationType::Bid,
final_ct_amount,
ct_vesting_duration,
bid.receiving_account,
)?;

Self::release_funding_asset(
Expand Down Expand Up @@ -310,6 +312,7 @@ impl<T: Config> Pallet<T> {
ParticipationType::Contribution,
contribution.ct_amount,
ct_vesting_duration,
contribution.receiving_account,
)?;

final_ct_amount = contribution.ct_amount;
Expand Down Expand Up @@ -484,10 +487,10 @@ impl<T: Config> Pallet<T> {
participation_type: ParticipationType,
ct_amount: Balance,
vesting_time: BlockNumberFor<T>,
receiving_account: Junction,
) -> DispatchResult {
UserMigrations::<T>::try_mutate((project_id, origin), |maybe_migrations| -> DispatchResult {
let location_user =
Location::new(0, AccountId32 { network: None, id: T::AccountId32Conversion::convert(origin.clone()) });
let location_user = Location::new(0, receiving_account);
let migration_origin = MigrationOrigin { user: location_user, id, participation_type };
let vesting_time: u64 = vesting_time.try_into().map_err(|_| Error::<T>::BadMath)?;
let migration_info: MigrationInfo = (ct_amount, vesting_time).into();
Expand Down
Loading

0 comments on commit 06d2a5f

Please sign in to comment.