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

[MGX-393] MVR - Fix transacation fees #589

Merged
merged 3 commits into from
Aug 31, 2023
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
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Tiltfile
**/*.txt
**/*.md
./docker-cargo
./mvr/docker-cargo
!/target/release/polkadot-collator
!/docker-cargo/release/wbuild/mangata-kusama-runtime/mangata_kusama_runtime.compact.compressed.wasm
!/docker-cargo/release/wbuild/mangata-kusama-runtime/mangata_kusama_runtime.compact.compressed.wasm.md5
Expand Down
2 changes: 2 additions & 0 deletions mvr/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 mvr/runtime/integration-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" }
sp-tracing = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" }
pallet-transaction-payment = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" }
sc-tracing = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42" }

# Polkadot
Expand Down
31 changes: 30 additions & 1 deletion mvr/runtime/integration-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ use cumulus_primitives_core::{
WildMultiAsset::{self, AllCounted},
Xcm,
};
use frame_support::{assert_ok, traits::Currency};
use frame_support::{assert_ok, traits::Currency, weights::WeightToFee};
use orml_traits::currency::MultiCurrency;
use xcm::VersionedXcm;
use xcm_emulator::TestExt;
use orml_tokens::WeightInfo as OrmlWeightInfoT;

use networks::*;

pub type RelayChainPalletXcm = pallet_xcm::Pallet<polkadot_runtime::Runtime>;
pub type ParachainPalletXcm = pallet_xcm::Pallet<mangata_polkadot_runtime::Runtime>;
pub type XParachainPalletXTokens = orml_xtokens::Pallet<xtokens_parachain::Runtime>;
pub type OrmlWeights = <mangata_polkadot_runtime::Runtime as orml_tokens::Config>::WeightInfo;
pub type MangataWeightToFee = <mangata_polkadot_runtime::Runtime as pallet_transaction_payment::Config>::WeightToFee;

pub const TRANSFER_AMOUNT: u128 = 50 * unit(12);

#[test]
Expand Down Expand Up @@ -197,3 +201,28 @@ fn xtokens_transfer_triggers_asset_trap() {
);
});
}

#[test]
fn transfer_fee_is_reasonable() {
TestNet::reset();

// arrange
networks::Mangata::execute_with(|| {
sp_tracing::try_init_simple();

let transfer_weight = <OrmlWeights as OrmlWeightInfoT>::transfer();

let bytes_cost = mangata_polkadot_runtime::TransactionPayment::weight_to_fee(transfer_weight);
let weight_cost = mangata_polkadot_runtime::TransactionPayment::length_to_fee(145);
let cost = bytes_cost + weight_cost;

assert!(
cost > mangata_polkadot_runtime::dot_currency::CENTS / 10
);
assert!(
cost < 10 * mangata_polkadot_runtime::dot_currency::CENTS
);

});

}
1 change: 1 addition & 0 deletions mvr/runtime/mangata-polkadot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pallet-transaction-payment = { git = "https://github.com/paritytech/substrate",
pallet-transaction-payment-rpc-runtime-api = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" }
sp-api = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" }
sp-block-builder = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" }
sp-weights = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.42" }
sp-consensus-aura = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" }
sp-core = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" }
sp-inherents = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.42" }
Expand Down
48 changes: 28 additions & 20 deletions mvr/runtime/mangata-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ pub type Executive = frame_executive::Executive<
AllPalletsWithSystem,
>;

pub mod dot_currency{
use super::Balance;
pub const UNITS: Balance = 10_000_000_000;
pub const DOLLARS: Balance = UNITS; // 10_000_000_000
pub const GRAND: Balance = DOLLARS * 1_000; // 10_000_000_000_000
pub const CENTS: Balance = DOLLARS / 100; // 100_000_000
pub const MILLICENTS: Balance = CENTS / 1_000; // 100_000
}

/// Handles converting a weight scalar to a fee value, based on the scale and granularity of the
/// node's balance type.
///
Expand All @@ -136,10 +145,9 @@ pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
type Balance = Balance;
fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
// in Rococo, extrinsic base weight (smallest non-zero weight) is mapped to 1 MILLIUNIT:
// in our template, we map to 1/10 of that, or 1/10 MILLIUNIT
let p = UNIT;
let q = 100 * Balance::from(ExtrinsicBaseWeight::get().ref_time());
// in MVR, extrinsic base weight (smallest non-zero weight) is mapped to 1/10 DOT CENT:
let p = dot_currency::CENTS / 10;
let q = 10 * Balance::from(ExtrinsicBaseWeight::get().ref_time());
smallvec![WeightToFeeCoefficient {
degree: 1,
negative: false,
Expand All @@ -149,21 +157,21 @@ impl WeightToFeePolynomial for WeightToFee {
}
}

pub const DOT_MGX_SCALE_FACTOR_UNADJUSTED: u128 = 10_000_000_000_u128; // 10_000 as DOT/MGX, with 6 decimals accounted for (12 - DOT, 18 - MGX)

pub fn base_tx_in_mgx() -> Balance {
UNIT
}

pub fn mgx_per_second() -> u128 {
let base_weight = Balance::from(ExtrinsicBaseWeight::get().ref_time());
let base_per_second = (WEIGHT_REF_TIME_PER_SECOND / base_weight as u64) as u128;
base_per_second * base_tx_in_mgx()
}

pub fn dot_per_second() -> u128 {
mgx_per_second() / DOT_MGX_SCALE_FACTOR_UNADJUSTED as u128
}
// pub const DOT_MGX_SCALE_FACTOR_UNADJUSTED: u128 = 10_000_000_000_u128; // 10_000 as DOT/MGX, with 6 decimals accounted for (12 - DOT, 18 - MGX)
//
// pub fn base_tx_in_mgx() -> Balance {
// UNIT
// }
//
// pub fn mgx_per_second() -> u128 {
// let base_weight = Balance::from(ExtrinsicBaseWeight::get().ref_time());
// let base_per_second = (WEIGHT_REF_TIME_PER_SECOND / base_weight as u64) as u128;
// base_per_second * base_tx_in_mgx()
// }
//
// pub fn dot_per_second() -> u128 {
// mgx_per_second() / DOT_MGX_SCALE_FACTOR_UNADJUSTED as u128
// }

/// Opaque types. These are used by the CLI to instantiate machinery that don't need to know
/// the specifics of the runtime. They can then be made to be agnostic over specific formats
Expand Down Expand Up @@ -346,7 +354,7 @@ parameter_types! {

parameter_types! {
/// Relay Chain `TransactionByteFee` / 10
pub const TransactionByteFee: Balance = 10 * MICROUNIT;
pub const TransactionByteFee: Balance = 10 * dot_currency::MILLICENTS;
pub const DOTTokenId: u32 = 4;
}
pub type OrmlCurrencyAdapter = orml_tokens::CurrencyAdapter<Runtime, DOTTokenId>;
Expand Down
128 changes: 78 additions & 50 deletions mvr/runtime/mangata-polkadot/src/weights/block_weights.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,81 @@
// This file is part of Substrate.

// Copyright (C) 2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

pub mod constants {
use frame_support::{
parameter_types,
weights::{constants, Weight},
};

parameter_types! {
/// Importing a block with 0 Extrinsics.
pub const BlockExecutionWeight: Weight =
Weight::from_parts(constants::WEIGHT_REF_TIME_PER_NANOS.saturating_mul(5_000_000), 0);
}
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-04-28 (Y/M/D)
//! HOSTNAME: `bm5`, CPU: `Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz`
//!
//! SHORT-NAME: `block`, LONG-NAME: `BlockExecution`, RUNTIME: `Development`
//! WARMUPS: `10`, REPEAT: `100`
//! WEIGHT-PATH: `runtime/polkadot/constants/src/weights/`
//! WEIGHT-METRIC: `Average`, WEIGHT-MUL: `1.0`, WEIGHT-ADD: `0`

// Executed Command:
// ./target/production/polkadot
// benchmark
// overhead
// --chain=polkadot-dev
// --execution=wasm
// --wasm-execution=compiled
// --weight-path=runtime/polkadot/constants/src/weights/
// --warmup=10
// --repeat=100
// --header=./file_header.txt

use sp_core::parameter_types;
use sp_weights::{constants::WEIGHT_REF_TIME_PER_NANOS, Weight};

parameter_types! {
/// Time to execute an empty block.
/// Calculated by multiplying the *Average* with `1.0` and adding `0`.
///
/// Stats nanoseconds:
/// Min, Max: 3_364_185, 3_544_830
/// Average: 3_427_584
/// Median: 3_414_329
/// Std-Dev: 51775.25
///
/// Percentiles nanoseconds:
/// 99th: 3_541_964
/// 95th: 3_517_661
/// 75th: 3_471_477
pub const BlockExecutionWeight: Weight =
Weight::from_parts(WEIGHT_REF_TIME_PER_NANOS.saturating_mul(3_427_584), 0);
}

#[cfg(test)]
mod test_weights {
use sp_weights::constants;

/// Checks that the weight exists and is sane.
// NOTE: If this test fails but you are sure that the generated values are fine,
// you can delete it.
#[test]
fn sane() {
let w = super::BlockExecutionWeight::get();

#[cfg(test)]
mod test_weights {
use frame_support::weights::constants;

/// Checks that the weight exists and is sane.
// NOTE: If this test fails but you are sure that the generated values are fine,
// you can delete it.
#[test]
fn sane() {
let w = super::constants::BlockExecutionWeight::get();

// At least 100 µs.
assert!(
w.ref_time() >= 100u64 * constants::WEIGHT_REF_TIME_PER_MICROS,
"Weight should be at least 100 µs."
);
// At most 50 ms.
assert!(
w.ref_time() <= 50u64 * constants::WEIGHT_REF_TIME_PER_MILLIS,
"Weight should be at most 50 ms."
);
}
// At least 100 µs.
assert!(
w.ref_time() >= 100u64 * constants::WEIGHT_REF_TIME_PER_MICROS,
"Weight should be at least 100 µs."
);
// At most 50 ms.
assert!(
w.ref_time() <= 50u64 * constants::WEIGHT_REF_TIME_PER_MILLIS,
"Weight should be at most 50 ms."
);
}
}
Loading