Skip to content

Commit

Permalink
Fix weight annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed Feb 26, 2025
1 parent 50ead17 commit 0c9ef9e
Show file tree
Hide file tree
Showing 31 changed files with 126 additions and 170 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

6 changes: 3 additions & 3 deletions integration-tests/chopsticks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ bun run test

> [!IMPORTANT]
> TODO: Add:
> - [ ] Polimec SA on AH: Add WETH balance to it in the Chopstick ovveride
> - [ ] Polimec to Asset Hub: WETH transfer. This is a "normal" transfer_asset call.
> - [ ] Polimec to Ethereum: WETH transfer. This is a bit more complex, example extrinsic: https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fhydration.ibp.network#/extrinsics/decode/0x6b0d04010100a10f040801000007464a69c7e002020907040300c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200130000e8890423c78a0204010002040816040d01000001010088ca48e3e1d0f1c50bd6b504e1312d21f5bd45ed147e3c30c77eb5e4d63bdc6310010102020907040300c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000201090704081300010300c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20004000d010204000103001501c1413e4178c38567ada8945a80351f7b849600
> - [ ] Polimec SA on AH: Add ETH balance to it in the Chopstick ovveride
> - [ ] Polimec to Asset Hub: ETH transfer. This is a "normal" transfer_asset call.
> - [ ] Polimec to Ethereum: ETH transfer. This is a bit more complex, example extrinsic: https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fhydration.ibp.network#/extrinsics/decode/0x6b0d04010100a10f040801000007464a69c7e002020907040300c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200130000e8890423c78a0204010002040816040d01000001010088ca48e3e1d0f1c50bd6b504e1312d21f5bd45ed147e3c30c77eb5e4d63bdc6310010102020907040300c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000201090704081300010300c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20004000d010204000103001501c1413e4178c38567ada8945a80351f7b849600
4 changes: 2 additions & 2 deletions integration-tests/chopsticks/overrides/polimec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { INITIAL_BALANCES, WETH_ADDRESS } from '@/constants';
import { INITIAL_BALANCES, ETH_ADDRESS } from '@/constants';
import { Accounts } from '@/types';

export const POLIMEC_WASM =
Expand Down Expand Up @@ -82,7 +82,7 @@ export const polimec_storage = {
issuer: Accounts.ALICE,
admin: Accounts.ALICE,
freezer: Accounts.ALICE,
supply: 100n * INITIAL_BALANCES.WETH,
supply: 100n * INITIAL_BALANCES.ETH,
deposit: 0n,
min_balance: 15000000000000n,
is_sufficient: true,
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/chopsticks/overrides/polkadot-hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const polkadot_hub_storage = {
[
[eth_location, Accounts.POLIMEC],
{
balance: INITIAL_BALANCES.WETH,
balance: INITIAL_BALANCES.ETH,
},
],
],
Expand Down
6 changes: 3 additions & 3 deletions integration-tests/chopsticks/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ export const INITIAL_BALANCES = {
USDC: 66000n * 10n ** 6n,
DOT: 1000000n * 10n ** 10n,
PLMC: 1000000n * 10n ** 10n,
WETH: 2n * 10n ** 18n,
ETH: 2n * 10n ** 18n,
} as const;

export const TRANSFER_AMOUNTS = {
TOKENS: 2n * 10n ** 6n, // e.g. 2 USDC
NATIVE: 2n * 10n ** 10n, // e.g. 2 DOT
BRIDGED: 1n * 10n ** 17n, // e.g. 0.1 WETH
BRIDGED: 1n * 10n ** 17n, // e.g. 0.1 ETH
} as const;

export const DERIVE_PATHS = {
[Accounts.ALICE]: '//Alice',
[Accounts.BOB]: '//Bob',
} as const;

export const WETH_ADDRESS = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2';
export const ETH_ADDRESS = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2';
export const DEFAULT_TOPIC = FixedSizeBinary.fromArray(Array(32).fill(1));
export const FEE_AMOUNT = 40_000_000_000n;
export const ETH_AMOUNT = 15_000_000_000_000n;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from 'bun:test';
import { DEFAULT_TOPIC, ETH_AMOUNT, FEE_AMOUNT, WETH_ADDRESS } from '@/constants';
import { DEFAULT_TOPIC, ETH_AMOUNT, FEE_AMOUNT, ETH_ADDRESS } from '@/constants';
import type { BridgerHubManagaer } from '@/managers/BridgeHubManager';
import type { PolimecManager } from '@/managers/PolimecManager';
import type { PolkadotHubManager } from '@/managers/PolkadotHubManager';
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/chopsticks/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
type polkadot,
} from '@polkadot-api/descriptors';
import { FixedSizeBinary, type PolkadotClient, type TypedApi } from 'polkadot-api';
import { WETH_ADDRESS } from './constants';
import { ETH_ADDRESS } from './constants';

type Polimec = typeof polimec;
type PolkadotHub = typeof pah;
Expand Down
24 changes: 12 additions & 12 deletions integration-tests/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub struct Prices {
pub usdc: FixedU128,
pub usdt: FixedU128,
pub plmc: FixedU128,
pub weth: FixedU128,
pub eth: FixedU128,
}

// PricesBuilder for optional fields before building Prices
Expand All @@ -80,13 +80,13 @@ pub struct PricesBuilder {
usdc: Option<FixedU128>,
usdt: Option<FixedU128>,
plmc: Option<FixedU128>,
weth: Option<FixedU128>,
eth: Option<FixedU128>,
}

impl PricesBuilder {
// Initialize a new builder with None for each field
pub fn new() -> Self {
Self { dot: None, usdc: None, usdt: None, plmc: None, weth: None }
Self { dot: None, usdc: None, usdt: None, plmc: None, eth: None }
}

pub fn default_prices() -> Prices {
Expand All @@ -95,7 +95,7 @@ impl PricesBuilder {
usdc: FixedU128::from_rational(1, 1),
usdt: FixedU128::from_rational(1, 1),
plmc: FixedU128::from_rational(840, 100),
weth: FixedU128::from_rational(3620, 1),
eth: FixedU128::from_rational(3620, 1),
}
}

Expand All @@ -120,8 +120,8 @@ impl PricesBuilder {
self
}

pub fn weth(&mut self, price: FixedU128) -> &mut Self {
self.weth = Some(price);
pub fn eth(&mut self, price: FixedU128) -> &mut Self {
self.eth = Some(price);
self
}

Expand All @@ -132,7 +132,7 @@ impl PricesBuilder {
usdc: self.usdc.unwrap_or(FixedU128::from_rational(1, 1)), // Default USDC price
usdt: self.usdt.unwrap_or(FixedU128::from_rational(1, 1)), // Default USDT price
plmc: self.plmc.unwrap_or(FixedU128::from_rational(840, 100)), // Default PLMC price
weth: self.weth.unwrap_or(FixedU128::from_rational(3620, 1)), // Default WETH price
eth: self.eth.unwrap_or(FixedU128::from_rational(3620, 1)), // Default ETH price
}
}
}
Expand Down Expand Up @@ -318,10 +318,10 @@ pub mod polimec {
let usdc = (AcceptedFundingAsset::USDC.id(), prices.usdc);
let usdt = (AcceptedFundingAsset::USDT.id(), prices.usdt);
let plmc = (Location::here(), prices.plmc);
let weth = (AcceptedFundingAsset::ETH.id(), prices.weth);
let eth = (AcceptedFundingAsset::ETH.id(), prices.eth);

let values: BoundedVec<(Location, FixedU128), <PolimecRuntime as orml_oracle::Config>::MaxFeedValues> =
vec![dot, usdc, usdt, plmc, weth].try_into().expect("benchmarks can panic");
vec![dot, usdc, usdt, plmc, eth].try_into().expect("benchmarks can panic");
let alice: [u8; 32] = [
212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205,
227, 154, 86, 132, 231, 165, 109, 162, 125,
Expand Down Expand Up @@ -356,7 +356,7 @@ pub mod polimec {
let dot_asset_id = AcceptedFundingAsset::DOT.id();
let usdt_asset_id = AcceptedFundingAsset::USDT.id();
let usdc_asset_id = AcceptedFundingAsset::USDC.id();
let weth_asset_id = AcceptedFundingAsset::ETH.id();
let eth_asset_id = AcceptedFundingAsset::ETH.id();

let mut funded_accounts = vec![(
PolimecNet::sovereign_account_id_of((Parent, xcm::prelude::Parachain(1000)).into()),
Expand Down Expand Up @@ -384,13 +384,13 @@ pub mod polimec {
(dot_asset_id.clone(), alice_account.clone(), true, 100_000_000),
(usdt_asset_id.clone(), alice_account.clone(), true, 70_000),
(usdc_asset_id.clone(), alice_account.clone(), true, 70_000),
(weth_asset_id.clone(), alice_account.clone(), true, 0_000_041_000_000_000_000),
(eth_asset_id.clone(), alice_account.clone(), true, 0_000_041_000_000_000_000),
],
metadata: vec![
(dot_asset_id, "Local DOT".as_bytes().to_vec(), "DOT".as_bytes().to_vec(), 10),
(usdt_asset_id, "Local USDT".as_bytes().to_vec(), "USDT".as_bytes().to_vec(), 6),
(usdc_asset_id.clone(), "Local USDC".as_bytes().to_vec(), "USDC".as_bytes().to_vec(), 6),
(weth_asset_id, "Local WETH".as_bytes().to_vec(), "WETH".as_bytes().to_vec(), 18),
(eth_asset_id, "Local ETH".as_bytes().to_vec(), "ETH".as_bytes().to_vec(), 18),
],
accounts: vec![],
},
Expand Down
12 changes: 10 additions & 2 deletions integration-tests/src/tests/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use pallet_funding::{

use macros::generate_accounts;
use polimec_common::{
assets::AcceptedFundingAsset::{DOT, USDC, USDT, ETH},
ProvideAssetPrice, USD_DECIMALS, USD_UNIT,
assets::AcceptedFundingAsset::{DOT, ETH, USDC, USDT},
ProvideAssetPrice, USD_DECIMALS, USD_UNIT,
};
use polimec_runtime::AccountId;
use sp_runtime::traits::ConstU32;
Expand Down Expand Up @@ -77,3 +77,11 @@ pub fn default_project_metadata(issuer: AccountId) -> ProjectMetadataOf<polimec_
participants_account_type: ParticipantsAccountType::Polkadot,
}
}

#[test]
fn sandbox() {
use pallet_funding::WeightInfo;

let bid_weight = polimec_runtime::weights::pallet_funding::WeightInfo::<PolimecRuntime>::bid(0);
dbg!(bid_weight);
}
6 changes: 3 additions & 3 deletions integration-tests/src/tests/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ use sp_runtime::{bounded_vec, BoundedVec, FixedU128};
use std::collections::BTreeMap;
use tests::defaults::*;

use AcceptedFundingAsset::{DOT, USDC, USDT, ETH};
use AcceptedFundingAsset::{DOT, ETH, USDC, USDT};

fn values(
values: [f64; 5],
) -> BoundedVec<(Location, FixedU128), <polimec_runtime::Runtime as orml_oracle::Config<()>>::MaxFeedValues> {
let [dot, usdc, usdt, weth, plmc] = values;
let [dot, usdc, usdt, eth, plmc] = values;
bounded_vec![
(DOT.id(), FixedU128::from_float(dot)),
(USDC.id(), FixedU128::from_float(usdc)),
(USDT.id(), FixedU128::from_float(usdt)),
(ETH.id(), FixedU128::from_float(weth)),
(ETH.id(), FixedU128::from_float(eth)),
(Location::here(), FixedU128::from_float(plmc))
]
}
Expand Down
16 changes: 6 additions & 10 deletions integration-tests/src/tests/runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,17 @@ mod fungibles_api {
PolimecForeignAssets::set_balance(AcceptedFundingAsset::DOT.id(), &alice_account, 100_0_000_000_000_u128);
PolimecForeignAssets::set_balance(AcceptedFundingAsset::USDT.id(), &alice_account, 100_000_u128);
PolimecForeignAssets::set_balance(AcceptedFundingAsset::USDC.id(), &alice_account, 100_000_u128);
PolimecForeignAssets::set_balance(
AcceptedFundingAsset::ETH.id(),
&alice_account,
100_000_000_000_000_u128,
);
PolimecForeignAssets::set_balance(AcceptedFundingAsset::ETH.id(), &alice_account, 100_000_000_000_000_u128);

let alice_assets = PolimecRuntime::query_account_balances(alice_account).unwrap();

let expected_assets = VersionedAssets::V4(
vec![
Asset::from((Location::here(), 150_0_000_000_000_u128)),
Asset::from((AcceptedFundingAsset::DOT.id(), 100_0_000_000_000_u128)),
Asset::from((AcceptedFundingAsset::USDC.id(), 100_000_u128)),
Asset::from((AcceptedFundingAsset::USDT.id(), 100_000_u128)),
Asset::from((AcceptedFundingAsset::ETH.id(), 100_000_000_000_000_u128)),
Asset::from((Location::here(), 150_0_000_000_000_u128)),
Asset::from((AcceptedFundingAsset::DOT.id(), 100_0_000_000_000_u128)),
Asset::from((AcceptedFundingAsset::USDC.id(), 100_000_u128)),
Asset::from((AcceptedFundingAsset::USDT.id(), 100_000_u128)),
Asset::from((AcceptedFundingAsset::ETH.id(), 100_000_000_000_000_u128)),
]
.into(),
);
Expand Down
4 changes: 0 additions & 4 deletions pallets/funding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ sp-core.workspace = true
pallet-balances.workspace = true
xcm.workspace = true
xcm-executor.workspace = true
pallet-xcm.workspace = true
polkadot-parachain-primitives.workspace = true
sp-api.workspace = true
polimec-common-test-utils = { workspace = true, optional = true }
Expand Down Expand Up @@ -77,7 +76,6 @@ std = [
"pallet-linear-release/std",
"pallet-proxy-bonding/std",
"pallet-timestamp/std",
"pallet-xcm/std",
"parachains-common/std",
"parity-scale-codec/std",
"polimec-common-test-utils",
Expand Down Expand Up @@ -108,7 +106,6 @@ runtime-benchmarks = [
"pallet-linear-release/runtime-benchmarks",
"pallet-proxy-bonding/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"parachains-common/runtime-benchmarks",
"polimec-common-test-utils",
"polimec-common-test-utils?/runtime-benchmarks",
Expand All @@ -126,7 +123,6 @@ try-runtime = [
"pallet-linear-release/try-runtime",
"pallet-proxy-bonding/try-runtime",
"pallet-timestamp/try-runtime",
"pallet-xcm/try-runtime",
"polimec-common-test-utils?/try-runtime",
"polimec-common/try-runtime",
"sp-runtime/try-runtime",
Expand Down
10 changes: 5 additions & 5 deletions pallets/funding/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ use frame_support::{
use itertools::Itertools;
use parity_scale_codec::{Decode, Encode};
use polimec_common::{
assets::AcceptedFundingAsset::{DOT, USDC, USDT, ETH},
credentials::InvestorType,
ProvideAssetPrice, USD_DECIMALS, USD_UNIT,
assets::AcceptedFundingAsset::{DOT, ETH, USDC, USDT},
credentials::InvestorType,
ProvideAssetPrice, USD_DECIMALS, USD_UNIT,
};
use polimec_common_test_utils::{generate_did_from_account, get_mock_jwt_with_cid};
use sp_arithmetic::Percent;
Expand Down Expand Up @@ -622,8 +622,8 @@ mod benchmarks {
let mut inst = BenchInstantiator::<T>::new(None);
<T as Config>::SetPrices::set_prices();

let weth_price = PriceProviderOf::<T>::get_price(ETH.id()).unwrap();
log::info!("weth_price: {:?}", weth_price);
let eth_price = PriceProviderOf::<T>::get_price(ETH.id()).unwrap();
log::info!("eth_price: {:?}", eth_price);

// We can't see events at block 0
inst.advance_time(1u32.into());
Expand Down
13 changes: 9 additions & 4 deletions pallets/funding/src/functions/3_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::*;
impl<T: Config> Pallet<T> {
/// Place a bid on a project in the auction round
#[transactional]
pub fn do_bid(params: DoBidParams<T>) -> DispatchResult {
pub fn do_bid(params: DoBidParams<T>) -> DispatchResultWithPostInfo {
// * Get variables *
let DoBidParams {
bidder,
Expand All @@ -29,8 +29,6 @@ impl<T: Config> Pallet<T> {
// User will spend at least this amount of USD for his bid(s). More if the bid gets split into different buckets
let min_total_ticket_size =
current_bucket.current_price.checked_mul_int(ct_amount).ok_or(Error::<T>::BadMath)?;
// weight return variables
let mut perform_bid_calls = 0;

let metadata_ticket_size_bounds = match investor_type {
InvestorType::Institutional => project_metadata.bidding_ticket_sizes.institutional,
Expand Down Expand Up @@ -70,8 +68,12 @@ impl<T: Config> Pallet<T> {
Error::<T>::UnsupportedReceiverAccountJunction
);

let mut perform_bid_calls = 0u8;

// While there's a remaining amount to bid for
while !amount_to_bid.is_zero() {
perform_bid_calls.saturating_accrue(1);

let ct_amount = if amount_to_bid <= current_bucket.amount_left {
// Simple case, the bucket has enough to cover the bid
amount_to_bid
Expand Down Expand Up @@ -117,7 +119,10 @@ impl<T: Config> Pallet<T> {
// Note: If the bucket has been exhausted, the 'update' function has already made the 'current_bucket' point to the next one.
Buckets::<T>::insert(project_id, current_bucket);

Ok(())
Ok(PostDispatchInfo {
actual_weight: Some(<T as Config>::WeightInfo::bid(perform_bid_calls as u32)),
pays_fee: Pays::No,
})
}

/// Inner function to perform bids within a bucket. do_bid makes sure to split the bid into buckets and call this as
Expand Down
2 changes: 1 addition & 1 deletion pallets/funding/src/instantiator/calculations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use itertools::{izip, GroupBy};
#[allow(clippy::wildcard_imports)]
use polimec_common::assets::AcceptedFundingAsset;
use polimec_common::{
assets::AcceptedFundingAsset::{DOT, USDC, USDT, ETH},
assets::AcceptedFundingAsset::{DOT, ETH, USDC, USDT},
ProvideAssetPrice, USD_DECIMALS,
};
use sp_core::{blake2_256, ecdsa, hexdisplay::AsBytesRef, keccak_256, sr25519, Pair};
Expand Down
8 changes: 4 additions & 4 deletions pallets/funding/src/instantiator/chain_interactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,14 +474,14 @@ impl<
let mut total_expected_dot: Balance = Zero::zero();
let mut total_expected_usdt: Balance = Zero::zero();
let mut total_expected_usdc: Balance = Zero::zero();
let mut total_expected_weth: Balance = Zero::zero();
let mut total_expected_eth: Balance = Zero::zero();

for bid in bids {
match bid.funding_asset {
AcceptedFundingAsset::DOT => total_expected_dot += bid.funding_asset_amount_locked,
AcceptedFundingAsset::USDT => total_expected_usdt += bid.funding_asset_amount_locked,
AcceptedFundingAsset::USDC => total_expected_usdc += bid.funding_asset_amount_locked,
AcceptedFundingAsset::ETH => total_expected_weth += bid.funding_asset_amount_locked,
AcceptedFundingAsset::ETH => total_expected_eth += bid.funding_asset_amount_locked,
}
}

Expand All @@ -497,15 +497,15 @@ impl<
AcceptedFundingAsset::USDC.id(),
project_metadata.funding_destination_account.clone(),
);
let total_stored_weth = self.get_free_funding_asset_balance_for(
let total_stored_eth = self.get_free_funding_asset_balance_for(
AcceptedFundingAsset::ETH.id(),
project_metadata.funding_destination_account,
);

assert_eq!(total_expected_dot, total_stored_dot, "DOT amount is incorrect");
assert_eq!(total_expected_usdt, total_stored_usdt, "USDT amount is incorrect");
assert_eq!(total_expected_usdc, total_stored_usdc, "USDC amount is incorrect");
assert_eq!(total_expected_weth, total_stored_weth, "WETH amount is incorrect");
assert_eq!(total_expected_eth, total_stored_eth, "ETH amount is incorrect");
}

// Used to check if all evaluations are settled correctly. We cannot check amount of
Expand Down
Loading

0 comments on commit 0c9ef9e

Please sign in to comment.