From 5644d5d028235f535237291edffd0705141740a6 Mon Sep 17 00:00:00 2001 From: herryho Date: Wed, 14 Jul 2021 10:18:11 +0800 Subject: [PATCH 1/2] - change the name of charge-transaction-fee module to flexible fee - add a add tokens function for sudo in Bancor module --- Cargo.lock | 18 +++---- Cargo.toml | 2 +- node/rpc/Cargo.toml | 4 +- node/rpc/src/lib.rs | 6 +-- node/service/Cargo.toml | 2 +- node/service/src/collator.rs | 2 +- pallets/bancor/src/lib.rs | 27 +++++++--- pallets/bancor/src/weights.rs | 9 ++++ .../Cargo.toml | 2 +- .../rpc/Cargo.toml | 6 +-- .../rpc/runtime-api/Cargo.toml | 2 +- .../rpc/runtime-api/src/lib.rs | 2 +- .../rpc/src/lib.rs | 12 ++--- .../src/default_weight.rs | 0 .../src/lib.rs | 0 .../src/mock.rs | 6 +-- .../src/tests.rs | 52 ++++++++----------- runtime/asgard/Cargo.toml | 8 +-- runtime/asgard/src/lib.rs | 18 +++---- 19 files changed, 96 insertions(+), 82 deletions(-) rename pallets/{charge-transaction-fee => flexible-fee}/Cargo.toml (97%) rename pallets/{charge-transaction-fee => flexible-fee}/rpc/Cargo.toml (79%) rename pallets/{charge-transaction-fee => flexible-fee}/rpc/runtime-api/Cargo.toml (88%) rename pallets/{charge-transaction-fee => flexible-fee}/rpc/runtime-api/src/lib.rs (95%) rename pallets/{charge-transaction-fee => flexible-fee}/rpc/src/lib.rs (87%) rename pallets/{charge-transaction-fee => flexible-fee}/src/default_weight.rs (100%) rename pallets/{charge-transaction-fee => flexible-fee}/src/lib.rs (100%) rename pallets/{charge-transaction-fee => flexible-fee}/src/mock.rs (97%) rename pallets/{charge-transaction-fee => flexible-fee}/src/tests.rs (87%) diff --git a/Cargo.lock b/Cargo.lock index 2eb4d31d31..014bf2d2ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -179,8 +179,8 @@ version = "0.8.0" dependencies = [ "bifrost-bancor", "bifrost-bancor-runtime-api", - "bifrost-charge-transaction-fee", - "bifrost-charge-transaction-fee-rpc-runtime-api", + "bifrost-flexible-fee", + "bifrost-flexible-fee-rpc-runtime-api", "bifrost-minter-reward", "bifrost-runtime-common", "bifrost-salp", @@ -670,7 +670,7 @@ dependencies = [ ] [[package]] -name = "bifrost-charge-transaction-fee" +name = "bifrost-flexible-fee" version = "0.8.0" dependencies = [ "cumulus-primitives-core", @@ -693,10 +693,10 @@ dependencies = [ ] [[package]] -name = "bifrost-charge-transaction-fee-rpc" +name = "bifrost-flexible-fee-rpc" version = "0.8.0" dependencies = [ - "bifrost-charge-transaction-fee-rpc-runtime-api", + "bifrost-flexible-fee-rpc-runtime-api", "jsonrpc-core", "jsonrpc-core-client", "jsonrpc-derive", @@ -711,7 +711,7 @@ dependencies = [ ] [[package]] -name = "bifrost-charge-transaction-fee-rpc-runtime-api" +name = "bifrost-flexible-fee-rpc-runtime-api" version = "0.8.0" dependencies = [ "node-primitives", @@ -4918,8 +4918,8 @@ dependencies = [ name = "node-rpc" version = "0.8.0" dependencies = [ - "bifrost-charge-transaction-fee-rpc", - "bifrost-charge-transaction-fee-rpc-runtime-api", + "bifrost-flexible-fee-rpc", + "bifrost-flexible-fee-rpc-runtime-api", "jsonrpc-core", "node-primitives", "pallet-transaction-payment-rpc", @@ -4937,7 +4937,7 @@ name = "node-service" version = "0.8.0" dependencies = [ "asgard-runtime", - "bifrost-charge-transaction-fee-rpc-runtime-api", + "bifrost-flexible-fee-rpc-runtime-api", "bifrost-runtime", "bs58 0.3.1", "cumulus-client-consensus-aura", diff --git a/Cargo.toml b/Cargo.toml index ded7b3d973..07118a1dfc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ members = [ "pallets/assets", "pallets/bancor", "pallets/bid", - "pallets/charge-transaction-fee", + "pallets/flexible-fee", "pallets/minter-reward", "pallets/vesting", "pallets/vsbond-auction", diff --git a/node/rpc/Cargo.toml b/node/rpc/Cargo.toml index 36938dbbbf..8265a13ecf 100644 --- a/node/rpc/Cargo.toml +++ b/node/rpc/Cargo.toml @@ -16,7 +16,7 @@ sp-api = { version = "3.0.0" } sp-blockchain = { version = "3.0.0" } sp-transaction-pool = { version = "3.0.0" } pallet-transaction-payment-rpc = { version = "3.0.0" } -bifrost-charge-transaction-fee-rpc = { path = "../../pallets/charge-transaction-fee/rpc" } -bifrost-charge-transaction-fee-rpc-runtime-api = { path = "../../pallets/charge-transaction-fee/rpc/runtime-api" } +bifrost-flexible-fee-rpc = { path = "../../pallets/flexible-fee/rpc" } +bifrost-flexible-fee-rpc-runtime-api = { path = "../../pallets/flexible-fee/rpc/runtime-api" } zenlink-protocol-rpc = "*" zenlink-protocol-runtime-api = "*" \ No newline at end of file diff --git a/node/rpc/src/lib.rs b/node/rpc/src/lib.rs index 99a3d1fcd8..8a13992375 100644 --- a/node/rpc/src/lib.rs +++ b/node/rpc/src/lib.rs @@ -33,7 +33,7 @@ use std::sync::Arc; -use bifrost_charge_transaction_fee_rpc_runtime_api::ChargeTransactionFeeRuntimeApi as FeeRuntimeApi; +use bifrost_flexible_fee_rpc_runtime_api::FlexibleFeeRuntimeApi as FeeRuntimeApi; use node_primitives::{AccountId, Balance, Block}; pub use sc_rpc_api::DenyUnsafe; use sp_api::ProvideRuntimeApi; @@ -76,10 +76,10 @@ where io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(client.clone()))); - use bifrost_charge_transaction_fee_rpc::{ChargeTransactionFeeStruct, FeeRpcApi}; + use bifrost_flexible_fee_rpc::{FeeRpcApi, FlexibleFeeStruct}; use zenlink_protocol_rpc::{ZenlinkProtocol, ZenlinkProtocolApi}; - io.extend_with(FeeRpcApi::to_delegate(ChargeTransactionFeeStruct::new(client.clone()))); + io.extend_with(FeeRpcApi::to_delegate(FlexibleFeeStruct::new(client.clone()))); io.extend_with(ZenlinkProtocolApi::to_delegate(ZenlinkProtocol::new(client.clone()))); diff --git a/node/service/Cargo.toml b/node/service/Cargo.toml index a92210ce7f..caddd2f2c5 100644 --- a/node/service/Cargo.toml +++ b/node/service/Cargo.toml @@ -72,7 +72,7 @@ jsonrpc-core = "15.1.0" zenlink-protocol-runtime-api = "*" # Bifrost rpc -bifrost-charge-transaction-fee-rpc-runtime-api = { path = "../../pallets/charge-transaction-fee/rpc/runtime-api" } +bifrost-flexible-fee-rpc-runtime-api = { path = "../../pallets/flexible-fee/rpc/runtime-api" } [features] default = [ "std" ] diff --git a/node/service/src/collator.rs b/node/service/src/collator.rs index da713695b1..6a36758861 100644 --- a/node/service/src/collator.rs +++ b/node/service/src/collator.rs @@ -18,7 +18,7 @@ use std::sync::Arc; -use bifrost_charge_transaction_fee_rpc_runtime_api::ChargeTransactionFeeRuntimeApi as FeeRuntimeApi; +use bifrost_flexible_fee_rpc_runtime_api::FlexibleFeeRuntimeApi as FeeRuntimeApi; use cumulus_client_consensus_aura::{ build_aura_consensus, BuildAuraConsensusParams, SlotProportion, }; diff --git a/pallets/bancor/src/lib.rs b/pallets/bancor/src/lib.rs index 4bbd485558..6993ec8f78 100644 --- a/pallets/bancor/src/lib.rs +++ b/pallets/bancor/src/lib.rs @@ -20,7 +20,7 @@ use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; -use node_primitives::{traits::BancorHandler, CurrencyId}; +use node_primitives::{traits::BancorHandler, CurrencyId, CurrencyIdExt}; use num_bigint::BigUint; use orml_traits::MultiCurrency; use sp_arithmetic::per_things::{PerThing, Perbill, Percent}; @@ -79,6 +79,7 @@ pub mod pallet { VSTokenSupplyNotEnough, PriceNotQualified, CalculationOverflow, + NotSupportTokenType, } #[pallet::event] @@ -135,6 +136,19 @@ pub mod pallet { #[pallet::call] impl Pallet { + #[pallet::weight(T::WeightInfo::add_token_to_pool())] + pub fn add_token_to_pool( + origin: OriginFor, + currency_id: CurrencyId, + token_amount: BalanceOf, + ) -> DispatchResult { + ensure_root(origin)?; + ensure!(currency_id.is_token(), Error::::NotSupportTokenType); + + Self::add_token(currency_id, token_amount)?; + + Ok(()) + } // exchange vstoken for token #[pallet::weight(T::WeightInfo::exchange_for_token())] pub fn exchange_for_token( @@ -145,6 +159,7 @@ pub mod pallet { ) -> DispatchResult { // Check origin let exchanger = ensure_signed(origin)?; + ensure!(currency_id.is_token(), Error::::NotSupportTokenType); let vstoken_id = currency_id.to_vstoken().map_err(|_| Error::::ConversionError)?; // Get exchanger's vstoken balance @@ -182,6 +197,7 @@ pub mod pallet { ) -> DispatchResult { // Check origin let exchanger = ensure_signed(origin)?; + ensure!(currency_id.is_token(), Error::::NotSupportTokenType); let vstoken_id = currency_id.to_vstoken().map_err(|_| Error::::ConversionError)?; // Get exchanger's token balance @@ -255,9 +271,8 @@ impl Pallet { let nominator_rhs = token_supply_square .checked_mul(&vstoken_amount) .ok_or(Error::::CalculationOverflow)?; - let nominator = nominator_lhs - .checked_add(&nominator_rhs) - .ok_or(Error::::CalculationOverflow)?; + let nominator = + nominator_lhs.checked_add(&nominator_rhs).ok_or(Error::::CalculationOverflow)?; let inside = nominator.checked_div(&vstoken_supply).ok_or(Error::::CalculationOverflow)?; @@ -421,8 +436,8 @@ impl BancorHandler> for Pallet { let amount_kept: BalanceOf; // if vstoken price is lower than 0.75 token - if T::InterventionPercentage::get().saturating_reciprocal_mul_floor(nominator) <= - denominator + if T::InterventionPercentage::get().saturating_reciprocal_mul_floor(nominator) + <= denominator { amount_kept = token_amount / BalanceOf::::saturated_from(2u128); } else { diff --git a/pallets/bancor/src/weights.rs b/pallets/bancor/src/weights.rs index e82e85c2a7..1ecc861260 100644 --- a/pallets/bancor/src/weights.rs +++ b/pallets/bancor/src/weights.rs @@ -27,6 +27,7 @@ use sp_std::marker::PhantomData; /// Weight functions needed for the pallet. pub trait WeightInfo { + fn add_token_to_pool() -> Weight; fn exchange_for_token() -> Weight; fn exchange_for_vstoken() -> Weight; } @@ -34,6 +35,10 @@ pub trait WeightInfo { /// Weights for the pallet using the Bifrost node and recommended hardware. pub struct BifrostWeight(PhantomData); impl WeightInfo for BifrostWeight { + fn add_token_to_pool() -> Weight { + (50_000_000 as Weight) + } + fn exchange_for_token() -> Weight { (50_000_000 as Weight) } @@ -45,6 +50,10 @@ impl WeightInfo for BifrostWeight { // For backwards compatibility and tests impl WeightInfo for () { + fn add_token_to_pool() -> Weight { + (50_000_000 as Weight) + } + fn exchange_for_token() -> Weight { (50_000_000 as Weight) } diff --git a/pallets/charge-transaction-fee/Cargo.toml b/pallets/flexible-fee/Cargo.toml similarity index 97% rename from pallets/charge-transaction-fee/Cargo.toml rename to pallets/flexible-fee/Cargo.toml index 7e91378116..07f598d8c8 100644 --- a/pallets/charge-transaction-fee/Cargo.toml +++ b/pallets/flexible-fee/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "bifrost-charge-transaction-fee" +name = "bifrost-flexible-fee" version = "0.8.0" authors = ["Herry Ho "] edition = "2018" diff --git a/pallets/charge-transaction-fee/rpc/Cargo.toml b/pallets/flexible-fee/rpc/Cargo.toml similarity index 79% rename from pallets/charge-transaction-fee/rpc/Cargo.toml rename to pallets/flexible-fee/rpc/Cargo.toml index a69c8bf62c..c17322b872 100644 --- a/pallets/charge-transaction-fee/rpc/Cargo.toml +++ b/pallets/flexible-fee/rpc/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "bifrost-charge-transaction-fee-rpc" +name = "bifrost-flexible-fee-rpc" version = "0.8.0" authors = ["Herry Ho "] edition = "2018" @@ -15,7 +15,7 @@ sp-blockchain = "3.0.0" sp-core = "3.0.0" sp-rpc = "3.0.0" pallet-transaction-payment-rpc-runtime-api = "3.0.0" -bifrost-charge-transaction-fee-rpc-runtime-api = { path = "./runtime-api" } +bifrost-flexible-fee-rpc-runtime-api = { path = "./runtime-api" } node-primitives = { path = "../../../node/primitives", default-features = false } [features] @@ -26,6 +26,6 @@ std = [ "sp-runtime/std", "sp-core/std", "pallet-transaction-payment-rpc-runtime-api/std", - "bifrost-charge-transaction-fee-rpc-runtime-api/std", + "bifrost-flexible-fee-rpc-runtime-api/std", "node-primitives/std", ] \ No newline at end of file diff --git a/pallets/charge-transaction-fee/rpc/runtime-api/Cargo.toml b/pallets/flexible-fee/rpc/runtime-api/Cargo.toml similarity index 88% rename from pallets/charge-transaction-fee/rpc/runtime-api/Cargo.toml rename to pallets/flexible-fee/rpc/runtime-api/Cargo.toml index 0a86ad0c38..50ae052f88 100644 --- a/pallets/charge-transaction-fee/rpc/runtime-api/Cargo.toml +++ b/pallets/flexible-fee/rpc/runtime-api/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "bifrost-charge-transaction-fee-rpc-runtime-api" +name = "bifrost-flexible-fee-rpc-runtime-api" version = "0.8.0" authors = ["Herry Ho "] edition = "2018" diff --git a/pallets/charge-transaction-fee/rpc/runtime-api/src/lib.rs b/pallets/flexible-fee/rpc/runtime-api/src/lib.rs similarity index 95% rename from pallets/charge-transaction-fee/rpc/runtime-api/src/lib.rs rename to pallets/flexible-fee/rpc/runtime-api/src/lib.rs index c8c31ad832..0edb2b42ca 100644 --- a/pallets/charge-transaction-fee/rpc/runtime-api/src/lib.rs +++ b/pallets/flexible-fee/rpc/runtime-api/src/lib.rs @@ -23,7 +23,7 @@ use node_primitives::{Balance, CurrencyId}; use sp_api::decl_runtime_apis; decl_runtime_apis! { - pub trait ChargeTransactionFeeRuntimeApi where + pub trait FlexibleFeeRuntimeApi where AccountId: Codec, { /// get flexible fee token and amount to be deducted diff --git a/pallets/charge-transaction-fee/rpc/src/lib.rs b/pallets/flexible-fee/rpc/src/lib.rs similarity index 87% rename from pallets/charge-transaction-fee/rpc/src/lib.rs rename to pallets/flexible-fee/rpc/src/lib.rs index 77ab0f2c11..e46a393c3c 100644 --- a/pallets/charge-transaction-fee/rpc/src/lib.rs +++ b/pallets/flexible-fee/rpc/src/lib.rs @@ -18,7 +18,7 @@ use std::{marker::PhantomData, sync::Arc}; -pub use bifrost_charge_transaction_fee_rpc_runtime_api::ChargeTransactionFeeRuntimeApi as FeeRuntimeApi; +pub use bifrost_flexible_fee_rpc_runtime_api::FlexibleFeeRuntimeApi as FeeRuntimeApi; use codec::{Codec, Decode}; use jsonrpc_core::{Error as RpcError, ErrorCode, Result as JsonRpcResult}; use jsonrpc_derive::rpc; @@ -35,12 +35,12 @@ use sp_runtime::{ }; #[derive(Clone, Debug)] -pub struct ChargeTransactionFeeStruct { +pub struct FlexibleFeeStruct { client: Arc, _marker: PhantomData, } -impl ChargeTransactionFeeStruct { +impl FlexibleFeeStruct { pub fn new(client: Arc) -> Self { Self { client, _marker: PhantomData } } @@ -49,8 +49,8 @@ impl ChargeTransactionFeeStruct { #[rpc] pub trait FeeRpcApi { /// rpc method get balances by account id - /// useage: curl http://localhost:9933 -H "Content-Type:application/json;charset=utf-8" -d '{"jsonrpc":"2.0","id":1,"method":"chargeTransactionFee_getFeeTokenAndAmount","params": ["0x0e0626477621754200486f323e3858cd5f28fcbe52c69b2581aecb622e384764", "0xa0040400008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48cef70500"]}’ - #[rpc(name = "chargeTransactionFee_getFeeTokenAndAmount")] + /// useage: curl http://localhost:9933 -H "Content-Type:application/json;charset=utf-8" -d '{"jsonrpc":"2.0","id":1,"method":"flexibleFeeFee_getFeeTokenAndAmount","params": ["0x0e0626477621754200486f323e3858cd5f28fcbe52c69b2581aecb622e384764", "0xa0040400008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48cef70500"]}’ + #[rpc(name = "flexibleFee_getFeeTokenAndAmount")] fn get_fee_token_and_amount( &self, who: AccountId, @@ -77,7 +77,7 @@ impl From for i64 { } impl FeeRpcApi<::Hash, AccountId> - for ChargeTransactionFeeStruct + for FlexibleFeeStruct where Block: BlockT, C: Send + Sync + 'static + ProvideRuntimeApi + HeaderBackend, diff --git a/pallets/charge-transaction-fee/src/default_weight.rs b/pallets/flexible-fee/src/default_weight.rs similarity index 100% rename from pallets/charge-transaction-fee/src/default_weight.rs rename to pallets/flexible-fee/src/default_weight.rs diff --git a/pallets/charge-transaction-fee/src/lib.rs b/pallets/flexible-fee/src/lib.rs similarity index 100% rename from pallets/charge-transaction-fee/src/lib.rs rename to pallets/flexible-fee/src/lib.rs diff --git a/pallets/charge-transaction-fee/src/mock.rs b/pallets/flexible-fee/src/mock.rs similarity index 97% rename from pallets/charge-transaction-fee/src/mock.rs rename to pallets/flexible-fee/src/mock.rs index 42556a383d..442d81f07d 100644 --- a/pallets/charge-transaction-fee/src/mock.rs +++ b/pallets/flexible-fee/src/mock.rs @@ -41,7 +41,7 @@ use sp_std::cell::RefCell; use zenlink_protocol::{LocalAssetHandler, ZenlinkMultiAssets}; use super::*; -use crate as charge_transaction_fee; +use crate as flexible_fee; pub type BlockNumber = u64; pub type Amount = i128; @@ -61,7 +61,7 @@ frame_support::construct_runtime!( Assets: orml_tokens::{Pallet, Storage, Event}, Balances: balances::{Pallet, Call, Storage, Event}, // TransactionPayment: pallet_transaction_payment::{Module, Storage}, - ChargeTransactionFee: charge_transaction_fee::{Pallet, Call, Storage,Event}, + FlexibleFee: flexible_fee::{Pallet, Call, Storage,Event}, ZenlinkProtocol: zenlink_protocol::{Pallet, Call, Storage, Event}, Currencies: orml_currencies::{Pallet, Call, Storage, Event}, } @@ -122,7 +122,7 @@ parameter_types! { impl pallet_transaction_payment::Config for Test { type FeeMultiplierUpdate = (); - type OnChargeTransaction = ChargeTransactionFee; + type OnChargeTransaction = FlexibleFee; type TransactionByteFee = TransactionByteFee; type WeightToFee = IdentityFee; } diff --git a/pallets/charge-transaction-fee/src/tests.rs b/pallets/flexible-fee/src/tests.rs similarity index 87% rename from pallets/charge-transaction-fee/src/tests.rs rename to pallets/flexible-fee/src/tests.rs index 737ea034ae..ed2e5c3f55 100644 --- a/pallets/charge-transaction-fee/src/tests.rs +++ b/pallets/flexible-fee/src/tests.rs @@ -91,8 +91,8 @@ fn basic_setup() { asset_1_currency_id )); - let mut deadline: BlockNumberFor = >::block_number() + - ::BlockNumber::from(100u32); + let mut deadline: BlockNumberFor = >::block_number() + + ::BlockNumber::from(100u32); assert_ok!(ZenlinkProtocol::add_liquidity( Origin::signed(DICK), asset_0_currency_id, @@ -110,14 +110,13 @@ fn basic_setup() { asset_2_currency_id )); // asset 0 and 2 - let pool_0_2_account = ZenlinkProtocol::lp_metadata((asset_0_currency_id, asset_2_currency_id)) - .unwrap() - .0; + let pool_0_2_account = + ZenlinkProtocol::lp_metadata((asset_0_currency_id, asset_2_currency_id)).unwrap().0; println!("pool_0_2_account: {:?}", pool_0_2_account); // pool 0 2 - deadline = >::block_number() + - ::BlockNumber::from(100u32); + deadline = >::block_number() + + ::BlockNumber::from(100u32); assert_ok!(ZenlinkProtocol::add_liquidity( Origin::signed(DICK), asset_0_currency_id, @@ -136,7 +135,7 @@ fn set_user_fee_charge_order_should_work() { let origin_signed_alice = Origin::signed(ALICE); let mut asset_order_list_vec: Vec = vec![CURRENCY_ID_4, CURRENCY_ID_3, CURRENCY_ID_2, CURRENCY_ID_1, CURRENCY_ID_0]; - assert_ok!(ChargeTransactionFee::set_user_fee_charge_order( + assert_ok!(FlexibleFee::set_user_fee_charge_order( origin_signed_alice.clone(), Some(asset_order_list_vec.clone()) )); @@ -144,7 +143,7 @@ fn set_user_fee_charge_order_should_work() { asset_order_list_vec.insert(0, CURRENCY_ID_0); assert_eq!(crate::UserFeeChargeOrderList::::get(ALICE), asset_order_list_vec); - assert_ok!(ChargeTransactionFee::set_user_fee_charge_order(origin_signed_alice, None)); + assert_ok!(FlexibleFee::set_user_fee_charge_order(origin_signed_alice, None)); assert_eq!(crate::UserFeeChargeOrderList::::get(ALICE).is_empty(), true); }); @@ -165,22 +164,16 @@ fn inner_get_user_fee_charge_order_list_should_work() { default_order_list.push(CurrencyId::Token(TokenSymbol::ETH)); default_order_list.push(CurrencyId::VToken(TokenSymbol::ETH)); - assert_eq!( - ChargeTransactionFee::inner_get_user_fee_charge_order_list(&ALICE), - default_order_list - ); + assert_eq!(FlexibleFee::inner_get_user_fee_charge_order_list(&ALICE), default_order_list); - let _ = ChargeTransactionFee::set_user_fee_charge_order( + let _ = FlexibleFee::set_user_fee_charge_order( origin_signed_alice.clone(), Some(asset_order_list_vec.clone()), ); asset_order_list_vec.insert(0, CURRENCY_ID_0); - assert_eq!( - ChargeTransactionFee::inner_get_user_fee_charge_order_list(&ALICE), - asset_order_list_vec - ); + assert_eq!(FlexibleFee::inner_get_user_fee_charge_order_list(&ALICE), asset_order_list_vec); }); } @@ -207,7 +200,7 @@ fn ensure_can_charge_fee_should_work() { default_order_list.push(CurrencyId::VToken(TokenSymbol::ETH)); // Set bob order as [4,3,2,1]. Alice and Charlie will use the default order of [0..11]] - let _ = ChargeTransactionFee::set_user_fee_charge_order( + let _ = FlexibleFee::set_user_fee_charge_order( origin_signed_bob.clone(), Some(asset_order_list_vec.clone()), ); @@ -233,7 +226,7 @@ fn ensure_can_charge_fee_should_work() { Currencies::total_balance(CURRENCY_ID_1, &pool_0_1_account) ); - assert_ok!(ChargeTransactionFee::ensure_can_charge_fee( + assert_ok!(FlexibleFee::ensure_can_charge_fee( &ALICE, 100, WithdrawReasons::TRANSACTION_PAYMENT, @@ -247,7 +240,7 @@ fn ensure_can_charge_fee_should_work() { assert_eq!(::Currency::free_balance(&ALICE), 150); // Bob - assert_ok!(ChargeTransactionFee::ensure_can_charge_fee( + assert_ok!(FlexibleFee::ensure_can_charge_fee( &BOB, 100, WithdrawReasons::TRANSACTION_PAYMENT, @@ -266,9 +259,8 @@ fn withdraw_fee_should_work() { // prepare call variable let asset_order_list_vec: Vec = vec![CURRENCY_ID_0, CURRENCY_ID_1, CURRENCY_ID_2, CURRENCY_ID_3, CURRENCY_ID_4]; - let call = Call::ChargeTransactionFee(crate::Call::set_user_fee_charge_order(Some( - asset_order_list_vec, - ))); + let call = + Call::FlexibleFee(crate::Call::set_user_fee_charge_order(Some(asset_order_list_vec))); // prepare info variable let extra = (); @@ -276,7 +268,7 @@ fn withdraw_fee_should_work() { let info = xt.get_dispatch_info(); // 99 inclusion fee and a tip of 8 - assert_ok!(ChargeTransactionFee::withdraw_fee(&CHARLIE, &call, &info, 107, 8)); + assert_ok!(FlexibleFee::withdraw_fee(&CHARLIE, &call, &info, 107, 8)); assert_eq!(::Currency::free_balance(&CHARLIE), 93); }); @@ -290,9 +282,8 @@ fn correct_and_deposit_fee_should_work() { // prepare call variable let asset_order_list_vec: Vec = vec![CURRENCY_ID_0, CURRENCY_ID_1, CURRENCY_ID_2, CURRENCY_ID_3, CURRENCY_ID_4]; - let call = Call::ChargeTransactionFee(crate::Call::set_user_fee_charge_order(Some( - asset_order_list_vec, - ))); + let call = + Call::FlexibleFee(crate::Call::set_user_fee_charge_order(Some(asset_order_list_vec))); // prepare info variable let extra = (); let xt = TestXt::new(call.clone(), Some((CHARLIE.try_into().unwrap(), extra))); @@ -304,12 +295,11 @@ fn correct_and_deposit_fee_should_work() { let corrected_fee = 80; let tip = 8; - let already_withdrawn = - ChargeTransactionFee::withdraw_fee(&CHARLIE, &call, &info, 107, 8).unwrap(); + let already_withdrawn = FlexibleFee::withdraw_fee(&CHARLIE, &call, &info, 107, 8).unwrap(); assert_eq!(::Currency::free_balance(&CHARLIE), 93); - assert_ok!(ChargeTransactionFee::correct_and_deposit_fee( + assert_ok!(FlexibleFee::correct_and_deposit_fee( &CHARLIE, &info, &post_info, diff --git a/runtime/asgard/Cargo.toml b/runtime/asgard/Cargo.toml index a264c345be..1da5182bf2 100644 --- a/runtime/asgard/Cargo.toml +++ b/runtime/asgard/Cargo.toml @@ -82,8 +82,8 @@ polkadot-runtime-parachains = { git = "https://github.com/paritytech/polkadot", bifrost-runtime-common = { package = "bifrost-runtime-common", path = "../common", default-features = false } bifrost-vtoken-mint = { path = "../../pallets/vtoken-mint", default-features = false } bifrost-minter-reward = { path = "../../pallets/minter-reward", default-features = false } -bifrost-charge-transaction-fee = { path = "../../pallets/charge-transaction-fee", default-features = false } -bifrost-charge-transaction-fee-rpc-runtime-api = { path = "../../pallets/charge-transaction-fee/rpc/runtime-api", default-features = false } +bifrost-flexible-fee = { path = "../../pallets/flexible-fee", default-features = false } +bifrost-flexible-fee-rpc-runtime-api = { path = "../../pallets/flexible-fee/rpc/runtime-api", default-features = false } bifrost-salp= {path = "../../pallets/salp",default-features = false} bifrost-bancor= {path = "../../pallets/bancor",default-features = false} bifrost-vsbond-auction = { path = "../../pallets/vsbond-auction", default-features = false } @@ -161,8 +161,8 @@ std = [ "polkadot-runtime-parachains/std", "bifrost-bancor/std", "bifrost-bancor-runtime-api/std", - "bifrost-charge-transaction-fee/std", - "bifrost-charge-transaction-fee-rpc-runtime-api/std", + "bifrost-flexible-fee/std", + "bifrost-flexible-fee-rpc-runtime-api/std", "bifrost-minter-reward/std", "bifrost-runtime-common/std", "bifrost-salp/std", diff --git a/runtime/asgard/src/lib.rs b/runtime/asgard/src/lib.rs index efa96bd62f..1ec20d3ba8 100644 --- a/runtime/asgard/src/lib.rs +++ b/runtime/asgard/src/lib.rs @@ -306,11 +306,11 @@ impl InstanceFilter for ProxyType { ), ProxyType::Governance => matches!( c, - Call::Democracy(..) | - Call::Council(..) | Call::TechnicalCommittee(..) | - Call::Elections(..) | Call::Treasury(..) | - Call::Bounties(..) | Call::Tips(..) | - Call::Utility(..) + Call::Democracy(..) + | Call::Council(..) | Call::TechnicalCommittee(..) + | Call::Elections(..) + | Call::Treasury(..) | Call::Bounties(..) + | Call::Tips(..) | Call::Utility(..) ), ProxyType::CancelProxy => { matches!(c, Call::Proxy(pallet_proxy::Call::reject_announcement(..))) @@ -900,7 +900,7 @@ parameter_types! { pub const NativeCurrencyId: CurrencyId = CurrencyId::Native(TokenSymbol::ASG); } -impl bifrost_charge_transaction_fee::Config for Runtime { +impl bifrost_flexible_fee::Config for Runtime { type Balance = Balance; type Currency = Balances; type DexOperator = ZenlinkProtocol; @@ -1186,7 +1186,7 @@ construct_runtime! { // Bifrost modules VtokenMint: bifrost_vtoken_mint::{Pallet, Call, Storage, Event, Config} = 101, MinterReward: bifrost_minter_reward::{Pallet, Storage, Event, Config} = 102, - ChargeTransactionFee: bifrost_charge_transaction_fee::{Pallet, Call, Storage, Event} = 104, + FlexibleFee: bifrost_flexible_fee::{Pallet, Call, Storage, Event} = 104, Salp: bifrost_salp::{Pallet, Call, Storage, Event} = 105, Bancor: bifrost_bancor::{Pallet, Call, Storage, Event, Config} = 106, VSBondAuction: bifrost_vsbond_auction::{Pallet, Call, Storage, Event} = 107, @@ -1344,9 +1344,9 @@ impl_runtime_apis! { } } - impl bifrost_charge_transaction_fee_rpc_runtime_api::ChargeTransactionFeeRuntimeApi for Runtime { + impl bifrost_flexible_fee_rpc_runtime_api::FlexibleFeeRuntimeApi for Runtime { fn get_fee_token_and_amount(who: AccountId, fee: Balance) -> (CurrencyId, Balance) { - let rs = ChargeTransactionFee::cal_fee_token_and_amount(&who, fee); + let rs = FlexibleFee::cal_fee_token_and_amount(&who, fee); match rs { Ok(val) => val, _ => (CurrencyId::Native(TokenSymbol::ASG), Zero::zero()), From 5d49537ae74190f1e5b16b6336e7fd8d55eac308 Mon Sep 17 00:00:00 2001 From: herryho Date: Wed, 14 Jul 2021 11:25:39 +0800 Subject: [PATCH 2/2] cargo +nightly fmt --all --- pallets/bancor/src/lib.rs | 10 ++++++---- pallets/flexible-fee/src/tests.rs | 13 +++++++------ runtime/asgard/src/lib.rs | 10 +++++----- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/pallets/bancor/src/lib.rs b/pallets/bancor/src/lib.rs index 6993ec8f78..8f08ba764c 100644 --- a/pallets/bancor/src/lib.rs +++ b/pallets/bancor/src/lib.rs @@ -149,6 +149,7 @@ pub mod pallet { Ok(()) } + // exchange vstoken for token #[pallet::weight(T::WeightInfo::exchange_for_token())] pub fn exchange_for_token( @@ -271,8 +272,9 @@ impl Pallet { let nominator_rhs = token_supply_square .checked_mul(&vstoken_amount) .ok_or(Error::::CalculationOverflow)?; - let nominator = - nominator_lhs.checked_add(&nominator_rhs).ok_or(Error::::CalculationOverflow)?; + let nominator = nominator_lhs + .checked_add(&nominator_rhs) + .ok_or(Error::::CalculationOverflow)?; let inside = nominator.checked_div(&vstoken_supply).ok_or(Error::::CalculationOverflow)?; @@ -436,8 +438,8 @@ impl BancorHandler> for Pallet { let amount_kept: BalanceOf; // if vstoken price is lower than 0.75 token - if T::InterventionPercentage::get().saturating_reciprocal_mul_floor(nominator) - <= denominator + if T::InterventionPercentage::get().saturating_reciprocal_mul_floor(nominator) <= + denominator { amount_kept = token_amount / BalanceOf::::saturated_from(2u128); } else { diff --git a/pallets/flexible-fee/src/tests.rs b/pallets/flexible-fee/src/tests.rs index ed2e5c3f55..678dc7a66e 100644 --- a/pallets/flexible-fee/src/tests.rs +++ b/pallets/flexible-fee/src/tests.rs @@ -91,8 +91,8 @@ fn basic_setup() { asset_1_currency_id )); - let mut deadline: BlockNumberFor = >::block_number() - + ::BlockNumber::from(100u32); + let mut deadline: BlockNumberFor = >::block_number() + + ::BlockNumber::from(100u32); assert_ok!(ZenlinkProtocol::add_liquidity( Origin::signed(DICK), asset_0_currency_id, @@ -110,13 +110,14 @@ fn basic_setup() { asset_2_currency_id )); // asset 0 and 2 - let pool_0_2_account = - ZenlinkProtocol::lp_metadata((asset_0_currency_id, asset_2_currency_id)).unwrap().0; + let pool_0_2_account = ZenlinkProtocol::lp_metadata((asset_0_currency_id, asset_2_currency_id)) + .unwrap() + .0; println!("pool_0_2_account: {:?}", pool_0_2_account); // pool 0 2 - deadline = >::block_number() - + ::BlockNumber::from(100u32); + deadline = >::block_number() + + ::BlockNumber::from(100u32); assert_ok!(ZenlinkProtocol::add_liquidity( Origin::signed(DICK), asset_0_currency_id, diff --git a/runtime/asgard/src/lib.rs b/runtime/asgard/src/lib.rs index 1ec20d3ba8..154fb5c9a4 100644 --- a/runtime/asgard/src/lib.rs +++ b/runtime/asgard/src/lib.rs @@ -306,11 +306,11 @@ impl InstanceFilter for ProxyType { ), ProxyType::Governance => matches!( c, - Call::Democracy(..) - | Call::Council(..) | Call::TechnicalCommittee(..) - | Call::Elections(..) - | Call::Treasury(..) | Call::Bounties(..) - | Call::Tips(..) | Call::Utility(..) + Call::Democracy(..) | + Call::Council(..) | Call::TechnicalCommittee(..) | + Call::Elections(..) | Call::Treasury(..) | + Call::Bounties(..) | Call::Tips(..) | + Call::Utility(..) ), ProxyType::CancelProxy => { matches!(c, Call::Proxy(pallet_proxy::Call::reject_announcement(..)))