|
| 1 | +// This file is part of HydraDX-node |
| 2 | + |
| 3 | +// Copyright (C) 2020-2025 Intergalactic, Limited (GIB). |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +#![cfg(feature = "runtime-benchmarks")] |
| 19 | + |
| 20 | +use crate::{AccountId, AssetId, Balance, Currencies, FeeProcessor, Omnipool, Runtime}; |
| 21 | + |
| 22 | +use crate::benchmarking::set_period; |
| 23 | +use frame_benchmarking::account; |
| 24 | +use frame_system::RawOrigin; |
| 25 | +use orml_benchmarking::runtime_benchmarks; |
| 26 | +use orml_traits::MultiCurrency; |
| 27 | +use sp_runtime::DispatchResult; |
| 28 | +use sp_std::vec; |
| 29 | + |
| 30 | +const HDX: AssetId = 0; |
| 31 | +const DAI: AssetId = 2; |
| 32 | +const ONE: Balance = 1_000_000_000_000; |
| 33 | + |
| 34 | +fn fund(who: AccountId, asset: AssetId, amount: Balance) -> DispatchResult { |
| 35 | + Currencies::update_balance(RawOrigin::Root.into(), who, asset, amount as i128) |
| 36 | +} |
| 37 | + |
| 38 | +runtime_benchmarks! { |
| 39 | + { Runtime, pallet_fee_processor } |
| 40 | + |
| 41 | + // Worst case: a non-HDX pot balance is sold via Omnipool and distributed to all four |
| 42 | + // receivers (the slice routed to the referrals receiver is the heaviest leg). |
| 43 | + convert { |
| 44 | + crate::benchmarking::omnipool_liquidity_mining::initialize_omnipool(None)?; |
| 45 | + |
| 46 | + // Pre-create every account the conversion touches (receiver pots, treasury, referrals |
| 47 | + // pot) so that distributing to a not-yet-existing account — including the tiny fee-on-fee |
| 48 | + // slices from the Omnipool sell itself — never trips ED. On the live chain these are all |
| 49 | + // genesis-funded. |
| 50 | + let seed = 1_000 * ONE; |
| 51 | + for p in [ |
| 52 | + FeeProcessor::pot_account_id(), |
| 53 | + pallet_staking::Pallet::<Runtime>::pot_account_id(), |
| 54 | + pallet_gigahdx::Pallet::<Runtime>::gigapot_account_id(), |
| 55 | + pallet_gigahdx_rewards::Pallet::<Runtime>::reward_accumulator_pot(), |
| 56 | + pallet_referrals::Pallet::<Runtime>::pot_account_id(), |
| 57 | + crate::Treasury::account_id(), |
| 58 | + ] { |
| 59 | + fund(p, HDX, seed)?; |
| 60 | + } |
| 61 | + |
| 62 | + // Warm the EMA oracle with HDX/DAI trades across a period — the conversion's Omnipool |
| 63 | + // sell and the dynamic fee both read the oracle. Each HDX->DAI sell also accrues a DAI |
| 64 | + // fee into the fee-processor pot (the non-HDX path), which is what `convert` consumes. |
| 65 | + let trader: AccountId = account("trader", 0, 0); |
| 66 | + fund(trader.clone(), HDX, 100_000 * ONE)?; |
| 67 | + Omnipool::sell(RawOrigin::Signed(trader.clone()).into(), HDX, DAI, 100 * ONE, 0)?; |
| 68 | + set_period(24); |
| 69 | + Omnipool::sell(RawOrigin::Signed(trader.clone()).into(), HDX, DAI, 100 * ONE, 0)?; |
| 70 | + |
| 71 | + let pot = FeeProcessor::pot_account_id(); |
| 72 | + fund(pot.clone(), DAI, 100 * ONE)?; |
| 73 | + |
| 74 | + let caller: AccountId = account("caller", 1, 0); |
| 75 | + fund(caller.clone(), HDX, seed)?; |
| 76 | + }: _(RawOrigin::Signed(caller), DAI) |
| 77 | + verify { |
| 78 | + assert_eq!( |
| 79 | + <Currencies as MultiCurrency<AccountId>>::free_balance(DAI, &FeeProcessor::pot_account_id()), |
| 80 | + 0, |
| 81 | + "the entire pot balance must be converted" |
| 82 | + ); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// NOTE: no `impl_benchmark_test_suite!` here. `convert` drives a real Omnipool sell whose |
| 87 | +// downstream fee distribution touches accounts that only exist in the full chain genesis, so it |
| 88 | +// is generated/validated via the benchmarking CLI against the runtime genesis rather than a |
| 89 | +// minimal stand-alone externalities. |
0 commit comments