Skip to content

Commit

Permalink
WETH from assethub support
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed Jan 16, 2025
1 parent c44cce6 commit d33ed15
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 31 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

18 changes: 16 additions & 2 deletions integration-tests/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub struct Prices {
pub usdc: FixedU128,
pub usdt: FixedU128,
pub plmc: FixedU128,
pub weth: FixedU128,
}

// PricesBuilder for optional fields before building Prices
Expand All @@ -78,12 +79,13 @@ pub struct PricesBuilder {
usdc: Option<FixedU128>,
usdt: Option<FixedU128>,
plmc: Option<FixedU128>,
weth: 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 }
Self { dot: None, usdc: None, usdt: None, plmc: None, weth: None }
}

pub fn default() -> Prices {
Expand All @@ -92,6 +94,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),
}
}

Expand All @@ -116,13 +119,19 @@ impl PricesBuilder {
self
}

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

// Build Prices using provided values or default values
pub fn build(self) -> Prices {
Prices {
dot: self.dot.unwrap_or(FixedU128::from_rational(69, 1)), // Default DOT price
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
}
}
}
Expand Down Expand Up @@ -446,9 +455,10 @@ pub mod polimec {
let usdc = (AcceptedFundingAsset::USDC.id(), prices.usdc);
let usdt = (AcceptedFundingAsset::USDT.id(), prices.usdt);
let plmc = (polimec_common::PLMC_FOREIGN_ID, prices.plmc);
let weth = (AcceptedFundingAsset::WETH.id(), prices.weth);

let values: BoundedVec<(u32, FixedU128), <PolimecRuntime as orml_oracle::Config>::MaxFeedValues> =
vec![dot, usdc, usdt, plmc].try_into().expect("benchmarks can panic");
vec![dot, usdc, usdt, plmc, weth].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 @@ -483,6 +493,8 @@ 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::WETH.id();

let mut funded_accounts = vec![
(
PolimecNet::sovereign_account_id_of((Parent, xcm::prelude::Parachain(penpal::PARA_ID)).into()),
Expand Down Expand Up @@ -515,11 +527,13 @@ pub mod polimec {
(dot_asset_id, alice_account.clone(), true, 100_000_000),
(usdt_asset_id, alice_account.clone(), true, 70_000),
(usdc_asset_id, alice_account.clone(), true, 70_000),
(weth_asset_id, 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, "Local USDC".as_bytes().to_vec(), "USDC".as_bytes().to_vec(), 6),
(usdc_asset_id, "Local WETH".as_bytes().to_vec(), "WETH".as_bytes().to_vec(), 18),
],
accounts: vec![],
},
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/src/tests/ethereum_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn test_hardcoded_signatures() {
let project_id = 1;

// Values generated with `/~https://github.com/lrazovic/ethsigner`
let polimec_account_ss58 = polimec_runtime::SS58Converter::convert(polimec_account.clone());
let _polimec_account_ss58 = polimec_runtime::SS58Converter::convert(polimec_account.clone());
let ethereum_account: [u8; 20] = hex!("796afe7b8933ee8cf337f17887e5c19b657f9ab8");
let signature: [u8; 65] = hex!("952e312ac892fefc7c69051521e78a3bc2727fbb495585bdb5fb77e662b8a3de2b1254058d824e85034710e338c2590e2f92d74ce3c60292ed25c7537d94ed621b");

Expand Down
8 changes: 8 additions & 0 deletions pallets/funding/src/instantiator/chain_interactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,12 +545,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();

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::WETH => total_expected_weth += bid.funding_asset_amount_locked,
}
}

Expand All @@ -559,6 +561,7 @@ impl<
AcceptedFundingAsset::DOT => total_expected_dot += contribution.funding_asset_amount,
AcceptedFundingAsset::USDT => total_expected_usdt += contribution.funding_asset_amount,
AcceptedFundingAsset::USDC => total_expected_usdc += contribution.funding_asset_amount,
AcceptedFundingAsset::WETH => total_expected_weth += contribution.funding_asset_amount,
}
}

Expand All @@ -572,12 +575,17 @@ impl<
);
let total_stored_usdc = self.get_free_funding_asset_balance_for(
AcceptedFundingAsset::USDC.id(),
project_metadata.funding_destination_account.clone(),
);
let total_stored_weth = self.get_free_funding_asset_balance_for(
AcceptedFundingAsset::WETH.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");
}

// Used to check if all evaluations are settled correctly. We cannot check amount of
Expand Down
9 changes: 9 additions & 0 deletions pallets/funding/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ thread_local! {
(AcceptedFundingAsset::DOT.id(), FixedU128::from_float(69f64)), // DOT
(AcceptedFundingAsset::USDC.id(), FixedU128::from_float(0.97f64)), // USDC
(AcceptedFundingAsset::USDT.id(), FixedU128::from_float(1.0f64)), // USDT
(AcceptedFundingAsset::WETH.id(), FixedU128::from_float(3619.451f64)), // WETH
(PLMC_FOREIGN_ID, FixedU128::from_float(8.4f64)), // PLMC
]));
}
Expand Down Expand Up @@ -511,11 +512,19 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
true,
100_000_000,
),
(
AcceptedFundingAsset::WETH.id(),
<TestRuntime as Config>::PalletId::get().into_account_truncating(),
true,
// 0.07 USD = .000041WETH at this moment
0_000_041_000_000_000_000,
),
],
metadata: vec![
(AcceptedFundingAsset::USDT.id(), "USDT".as_bytes().to_vec(), "USDT".as_bytes().to_vec(), 6),
(AcceptedFundingAsset::USDC.id(), "USDC".as_bytes().to_vec(), "USDC".as_bytes().to_vec(), 6),
(AcceptedFundingAsset::DOT.id(), "DOT".as_bytes().to_vec(), "DOT".as_bytes().to_vec(), 10),
(AcceptedFundingAsset::WETH.id(), "WETH".as_bytes().to_vec(), "WETH".as_bytes().to_vec(), 18),
],
accounts: vec![],
},
Expand Down
26 changes: 18 additions & 8 deletions pallets/funding/src/tests/3_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ mod round_flow {
AcceptedFundingAsset::USDT => usdt_price,
AcceptedFundingAsset::USDC => usdc_price,
AcceptedFundingAsset::DOT => dot_price,
AcceptedFundingAsset::WETH => todo!(),
};

let mut project_metadata = default_project_metadata.clone();
Expand Down Expand Up @@ -2269,20 +2270,29 @@ mod end_auction_extrinsic {
let accounts = [ADAM, TOM, SOFIA, FRED, ANNA, DAMIAN];
let mut project_metadata = default_project_metadata(ISSUER_1);
project_metadata.total_allocation_size = 100_000 * CT_UNIT;
project_metadata.participation_currencies =
bounded_vec![AcceptedFundingAsset::USDT, AcceptedFundingAsset::USDC, AcceptedFundingAsset::DOT,];
project_metadata.participation_currencies = bounded_vec![
AcceptedFundingAsset::USDT,
AcceptedFundingAsset::USDC,
AcceptedFundingAsset::DOT,
AcceptedFundingAsset::WETH
];

// overfund with plmc
let plmc_fundings = accounts
.iter()
.map(|acc| UserToPLMCBalance { account: *acc, plmc_amount: PLMC * 1_000_000 })
.collect_vec();

let fundings = [AcceptedFundingAsset::USDT, AcceptedFundingAsset::USDC, AcceptedFundingAsset::DOT];
let fundings = [
AcceptedFundingAsset::USDT,
AcceptedFundingAsset::USDC,
AcceptedFundingAsset::DOT,
AcceptedFundingAsset::WETH,
];
assert_eq!(fundings.len(), AcceptedFundingAsset::VARIANT_COUNT);
let mut fundings = fundings.into_iter().cycle();

let usdt_fundings = accounts
let funding_asset_mints = accounts
.iter()
.map(|acc| {
let accepted_asset = fundings.next().unwrap();
Expand All @@ -2293,17 +2303,17 @@ mod end_auction_extrinsic {
})
.collect_vec();
inst.mint_plmc_to(plmc_fundings);
inst.mint_funding_asset_to(usdt_fundings);
inst.mint_funding_asset_to(funding_asset_mints);

let project_id = inst.create_auctioning_project(project_metadata, ISSUER_1, None, default_evaluations());

let bids = vec![
(ADAM, 10_000 * CT_UNIT, ParticipationMode::Classic(1), AcceptedFundingAsset::USDT).into(),
(TOM, 20_000 * CT_UNIT, ParticipationMode::Classic(1), AcceptedFundingAsset::USDC).into(),
(SOFIA, 20_000 * CT_UNIT, ParticipationMode::Classic(1), AcceptedFundingAsset::DOT).into(),
(FRED, 10_000 * CT_UNIT, ParticipationMode::Classic(1), AcceptedFundingAsset::USDT).into(),
(ANNA, 5_000 * CT_UNIT, ParticipationMode::Classic(1), AcceptedFundingAsset::USDC).into(),
(DAMIAN, 5_000 * CT_UNIT, ParticipationMode::Classic(1), AcceptedFundingAsset::DOT).into(),
(FRED, 10_000 * CT_UNIT, ParticipationMode::Classic(1), AcceptedFundingAsset::WETH).into(),
(ANNA, 5_000 * CT_UNIT, ParticipationMode::Classic(1), AcceptedFundingAsset::USDT).into(),
(DAMIAN, 5_000 * CT_UNIT, ParticipationMode::Classic(1), AcceptedFundingAsset::USDC).into(),
];

inst.bid_for_users(project_id, bids).unwrap();
Expand Down
1 change: 1 addition & 0 deletions pallets/funding/src/tests/4_contribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ mod round_flow {
AcceptedFundingAsset::USDT => usdt_price,
AcceptedFundingAsset::USDC => usdc_price,
AcceptedFundingAsset::DOT => dot_price,
AcceptedFundingAsset::WETH => todo!(),
};

let mut project_metadata = default_project_metadata.clone();
Expand Down
1 change: 0 additions & 1 deletion pallets/funding/src/tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use super::*;
mod helper_functions {
use super::*;
use polimec_common::USD_DECIMALS;
use sp_core::{ecdsa, hexdisplay::AsBytesRef, keccak_256, sr25519, Pair};

#[test]
fn test_usd_price_decimal_aware() {
Expand Down
3 changes: 3 additions & 0 deletions pallets/funding/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,13 +652,16 @@ pub mod inner {
USDC,
#[codec(index = 2)]
DOT,
#[codec(index = 3)]
WETH,
}
impl AcceptedFundingAsset {
pub const fn id(&self) -> u32 {
match self {
AcceptedFundingAsset::USDT => 1984,
AcceptedFundingAsset::DOT => 10,
AcceptedFundingAsset::USDC => 1337,
AcceptedFundingAsset::WETH => 10_000,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions pallets/oracle-ocw/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ impl Convert<(AssetName, FixedU128), (OracleKey, OracleValue)> for AssetPriceCon
AssetName::USDC => (1337, price),
AssetName::USDT => (1984, price),
AssetName::PLMC => (3344, price),
AssetName::WETH => (10_000, price),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions pallets/oracle-ocw/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub enum AssetName {
USDC,
DOT,
PLMC,
WETH,
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -131,6 +132,7 @@ impl FetchPrice for KrakenFetcher {
AssetName::DOT => "https://api.kraken.com/0/public/OHLC?pair=DOTUSD&interval=1",
AssetName::USDC => "https://api.kraken.com/0/public/OHLC?pair=USDCUSD&interval=1",
AssetName::PLMC => "",
AssetName::WETH => "",
}
}
}
Expand Down Expand Up @@ -224,6 +226,7 @@ impl FetchPrice for BitStampFetcher {
AssetName::DOT => "https://www.bitstamp.net/api/v2/ohlc/dotusd/?step=60&limit=15",
AssetName::USDC => "https://www.bitstamp.net/api/v2/ohlc/usdcusd/?step=60&limit=15",
AssetName::PLMC => "",
AssetName::WETH => "",
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions runtimes/polimec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ scale-info = { workspace= true, default-features = false, features = [
"derive",
] }

lazy_static = "1.5.0"

# Uncomment this and the std feature below to see variables instead of <wasm:stripped> in the console output
#sp-debug-derive = { workspace = true, features = ["force-debug"]}

Expand Down
8 changes: 4 additions & 4 deletions runtimes/polimec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ parameter_types! {
pub const ExpiresIn: Moment = 1000 * 60; // 1 mins
pub const MaxHasDispatchedSize: u32 = 20;
pub RootOperatorAccountId: AccountId = AccountId::from([0xffu8; 32]);
pub const MaxFeedValues: u32 = 4; // max 4 values allowd to feed in one call (USDT, USDC, DOT, PLMC).
pub const MaxFeedValues: u32 = AcceptedFundingAsset::VARIANT_COUNT as u32 + 1; // Funding asset prices + PLMC
}

impl orml_oracle::Config for Runtime {
Expand Down Expand Up @@ -1708,9 +1708,9 @@ impl_runtime_apis! {
fn query_acceptable_payment_assets(xcm_version: xcm::Version) -> Result<Vec<VersionedAssetId>, XcmPaymentApiError> {
let acceptable_assets = vec![
xcm_config::HereLocation::get().into(),
xcm_config::DotLocation::get().into(),
xcm_config::UsdtLocation::get().into(),
xcm_config::UsdcLocation::get().into()
xcm_config::DOTLocation::get().into(),
xcm_config::USDTLocation::get().into(),
xcm_config::USDCLocation::get().into()
];

PolkadotXcm::query_acceptable_payment_assets(xcm_version, acceptable_assets)
Expand Down
Loading

0 comments on commit d33ed15

Please sign in to comment.