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

cross-chain-transaction-fee #1216

Merged
merged 4 commits into from
Mar 29, 2024
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 Cargo.lock

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

2 changes: 1 addition & 1 deletion integration-tests/bifrost-kusama/src/slp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ fn transfer_to() {
Kusama::execute_with(|| {
assert_eq!(
kusama_runtime::Balances::free_balance(&AccountId::from(KSM_DELEGATOR_0_ACCOUNT)),
10049999921672574
10050009921672574
);
});
}
Expand Down
5 changes: 4 additions & 1 deletion pallets/fee-share/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use crate as bifrost_fee_share;

pub type BlockNumber = u64;
pub type Amount = i128;
pub type Balance = u64;
pub type Balance = u128;

pub type AccountId = AccountId32;
pub const ALICE: AccountId = AccountId32::new([0u8; 32]);
Expand Down Expand Up @@ -301,6 +301,9 @@ impl bifrost_slp::Config for Runtime {
type MaxLengthLimit = MaxLengthLimit;
type XcmWeightAndFeeHandler = ();
type ChannelCommission = ();
type StablePoolHandler = ();
type AssetIdMaps = AssetIdMaps<Runtime>;
type TreasuryAccount = TreasuryAccount;
}

parameter_type_with_key! {
Expand Down
2 changes: 2 additions & 0 deletions pallets/slp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ bifrost-xcm-interface = { workspace = true }
bifrost-vtoken-minting = { workspace = true }
bifrost-asset-registry = { workspace = true }
bifrost-parachain-staking = { workspace = true }
bifrost-stable-pool = { workspace = true }

[dev-dependencies]
hex = "0.4.3"
Expand Down Expand Up @@ -72,6 +73,7 @@ std = [
"bifrost-asset-registry/std",
"bifrost-parachain-staking/std",
"orml-xtokens/std",
"bifrost-stable-pool/std",
]

runtime-benchmarks = [
Expand Down
44 changes: 38 additions & 6 deletions pallets/slp/src/agents/astar_agent/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl<T: Config>

// Wrap the xcm message as it is sent from a subaccount of the parachain account, and
// send it out.
let (query_id, timeout, _fee, xcm_message) =
let (query_id, timeout, fee, xcm_message) =
Pallet::<T>::construct_xcm_as_subaccount_with_query_id(
XcmOperationType::Bond,
call.encode(),
Expand All @@ -138,6 +138,10 @@ impl<T: Config>
weight_and_fee,
)?;

// withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the
// process.
Pallet::<T>::burn_fee_from_source_account(fee, currency_id)?;

// Insert a delegator ledger update record into DelegatorLedgerXcmUpdateQueue<T>.
Self::insert_delegator_ledger_update_entry(
who,
Expand Down Expand Up @@ -171,14 +175,18 @@ impl<T: Config>

// Wrap the xcm message as it is sent from a subaccount of the parachain account, and
// send it out.
Pallet::<T>::construct_xcm_and_send_as_subaccount_without_query_id(
let fee = Pallet::<T>::construct_xcm_and_send_as_subaccount_without_query_id(
XcmOperationType::Payout,
call,
who,
currency_id,
weight_and_fee,
)?;

// withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the
// process.
Pallet::<T>::burn_fee_from_source_account(fee, currency_id)?;

Ok(Zero::zero())
}

Expand Down Expand Up @@ -220,7 +228,7 @@ impl<T: Config>

// Wrap the xcm message as it is sent from a subaccount of the parachain account, and
// send it out.
let (query_id, timeout, _, xcm_message) =
let (query_id, timeout, fee, xcm_message) =
Pallet::<T>::construct_xcm_as_subaccount_with_query_id(
XcmOperationType::Unbond,
call.encode(),
Expand All @@ -229,6 +237,10 @@ impl<T: Config>
weight_and_fee,
)?;

// withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the
// process.
Pallet::<T>::burn_fee_from_source_account(fee, currency_id)?;

// Insert a delegator ledger update record into DelegatorLedgerXcmUpdateQueue<T>.
Self::insert_delegator_ledger_update_entry(
who,
Expand Down Expand Up @@ -323,14 +335,18 @@ impl<T: Config>

// Wrap the xcm message as it is sent from a subaccount of the parachain account, and
// send it out.
Pallet::<T>::construct_xcm_and_send_as_subaccount_without_query_id(
let fee = Pallet::<T>::construct_xcm_and_send_as_subaccount_without_query_id(
XcmOperationType::Payout,
call,
who,
currency_id,
weight_and_fee,
)?;

// withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the
// process.
Pallet::<T>::burn_fee_from_source_account(fee, currency_id)?;

Ok(Zero::zero())
}

Expand All @@ -355,7 +371,7 @@ impl<T: Config>

// Wrap the xcm message as it is sent from a subaccount of the parachain account, and
// send it out.
let (query_id, timeout, _fee, xcm_message) =
let (query_id, timeout, fee, xcm_message) =
Pallet::<T>::construct_xcm_as_subaccount_with_query_id(
XcmOperationType::Liquidize,
call.encode(),
Expand All @@ -364,6 +380,10 @@ impl<T: Config>
weight_and_fee,
)?;

// withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the
// process.
Pallet::<T>::burn_fee_from_source_account(fee, currency_id)?;

// Insert a delegator ledger update record into DelegatorLedgerXcmUpdateQueue<T>.
Self::insert_delegator_ledger_update_entry(
who,
Expand Down Expand Up @@ -450,14 +470,18 @@ impl<T: Config>

// Wrap the xcm message as it is sent from a subaccount of the parachain account, and
// send it out.
Pallet::<T>::construct_xcm_and_send_as_subaccount_without_query_id(
let fee = Pallet::<T>::construct_xcm_and_send_as_subaccount_without_query_id(
XcmOperationType::TransferBack,
call.encode(),
from,
currency_id,
weight_and_fee,
)?;

// withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the
// process.
Pallet::<T>::burn_fee_from_source_account(fee, currency_id)?;

Ok(())
}

Expand All @@ -481,6 +505,14 @@ impl<T: Config>
let (entrance_account, _) = T::VtokenMinting::get_entrance_and_exit_accounts();
ensure!(from_account_id == entrance_account, Error::<T>::InvalidAccount);

// transfer supplementary fee from treasury to the "from" account. Return the added up
// amount
let amount = Pallet::<T>::get_transfer_to_added_amount_and_supplement(
from_account_id,
amount,
currency_id,
)?;

Pallet::<T>::do_transfer_to(from, to, amount, currency_id)?;

Ok(())
Expand Down
35 changes: 31 additions & 4 deletions pallets/slp/src/agents/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ use crate::{
ParachainStakingLedgerUpdateEntry, ParachainStakingLedgerUpdateOperation, TIMEOUT_BLOCKS,
},
traits::QueryResponseManager,
vec, BalanceOf, BlockNumberFor, BoundedVec, Box, Config, CurrencyDelays, DelegationsOccupied,
DelegatorLatestTuneRecord, DelegatorLedgerXcmUpdateQueue, DelegatorLedgers, DelegatorNextIndex,
DelegatorsIndex2Multilocation, DelegatorsMultilocation2Index, Encode, Event, FeeSources,
vec, AccountIdOf, BalanceOf, BlockNumberFor, BoundedVec, Box, Config, CurrencyDelays,
DelegationsOccupied, DelegatorLatestTuneRecord, DelegatorLedgerXcmUpdateQueue,
DelegatorLedgers, DelegatorNextIndex, DelegatorsIndex2Multilocation,
DelegatorsMultilocation2Index, Encode, Event, FeeSources,
Junction::{AccountId32, Parachain},
Junctions::X1,
Ledger, LedgerUpdateEntry, MinimumsAndMaximums, MultiLocation, Pallet, TimeUnit, Validators,
Expand All @@ -35,7 +36,7 @@ use orml_traits::{MultiCurrency, XcmTransfer};
use polkadot_parachain_primitives::primitives::Sibling;
use sp_core::{Get, U256};
use sp_runtime::{
traits::{AccountIdConversion, UniqueSaturatedFrom, UniqueSaturatedInto},
traits::{AccountIdConversion, CheckedAdd, UniqueSaturatedFrom, UniqueSaturatedInto},
DispatchResult, Saturating,
};
use xcm::{opaque::v3::Instruction, v3::prelude::*, VersionedMultiLocation};
Expand Down Expand Up @@ -629,4 +630,30 @@ impl<T: Config> Pallet<T> {

Ok(Some(unlock_time_unit))
}

pub(crate) fn get_transfer_to_added_amount_and_supplement(
from: AccountIdOf<T>,
amount: BalanceOf<T>,
currency_id: CurrencyId,
) -> Result<BalanceOf<T>, Error<T>> {
// get transfer_to extra fee
let (_weight, supplementary_fee) = T::XcmWeightAndFeeHandler::get_operation_weight_and_fee(
currency_id,
XcmOperationType::TransferTo,
)
.ok_or(Error::<T>::WeightAndFeeNotExists)?;

// transfer supplementary_fee from treasury to "from" account
// get fee source first
let (source_location, _reserved_fee) =
FeeSources::<T>::get(currency_id).ok_or(Error::<T>::FeeSourceNotExist)?;
let source_account = Self::native_multilocation_to_account(&source_location)?;

// transfer supplementary_fee from treasury to "from" account
T::MultiCurrency::transfer(currency_id, &source_account, &from, supplementary_fee)
.map_err(|_| Error::<T>::Unexpected)?;
let added_amount = amount.checked_add(&supplementary_fee).ok_or(Error::<T>::OverFlow)?;

Ok(added_amount)
}
}
8 changes: 8 additions & 0 deletions pallets/slp/src/agents/parachain_staking_agent/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,14 @@ impl<T: Config>
T::MultiCurrency::transfer(currency_id, &from_account_id, &to_account, amount)
.map_err(|_| Error::<T>::Unexpected)?;
} else {
// transfer supplementary fee from treasury to the "from" account. Return the added up
// amount
let amount = Pallet::<T>::get_transfer_to_added_amount_and_supplement(
from_account_id,
amount,
currency_id,
)?;

Pallet::<T>::do_transfer_to(from, to, amount, currency_id)?;
}

Expand Down
38 changes: 33 additions & 5 deletions pallets/slp/src/agents/phala_agent/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl<T: Config>

// Wrap the xcm message as it is sent from a subaccount of the parachain account, and
// send it out.
let (query_id, timeout, _fee, xcm_message) =
let (query_id, timeout, fee, xcm_message) =
Pallet::<T>::construct_xcm_as_subaccount_with_query_id(
XcmOperationType::Bond,
calls,
Expand All @@ -145,6 +145,10 @@ impl<T: Config>
weight_and_fee,
)?;

// withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the
// process.
Pallet::<T>::burn_fee_from_source_account(fee, currency_id)?;

// Calculate how many shares we can get by the amount at current price
let shares = if let Some(MultiLocation {
parents: _,
Expand Down Expand Up @@ -253,7 +257,7 @@ impl<T: Config>

// Wrap the xcm message as it is sent from a subaccount of the parachain account, and
// send it out.
let (query_id, timeout, _fee, xcm_message) =
let (query_id, timeout, fee, xcm_message) =
Pallet::<T>::construct_xcm_as_subaccount_with_query_id(
XcmOperationType::Unbond,
call,
Expand All @@ -262,6 +266,10 @@ impl<T: Config>
weight_and_fee,
)?;

// withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the
// process.
Pallet::<T>::burn_fee_from_source_account(fee, currency_id)?;

// Insert a delegator ledger update record into DelegatorLedgerXcmUpdateQueue<T>.
Self::insert_delegator_ledger_update_entry(
who,
Expand Down Expand Up @@ -487,7 +495,7 @@ impl<T: Config>

// Wrap the xcm message as it is sent from a subaccount of the parachain account, and
// send it out.
let (query_id, _timeout, _fee, xcm_message) =
let (query_id, _timeout, fee, xcm_message) =
Pallet::<T>::construct_xcm_as_subaccount_with_query_id(
XcmOperationType::Payout,
call,
Expand All @@ -496,6 +504,10 @@ impl<T: Config>
weight_and_fee,
)?;

// withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the
// process.
Pallet::<T>::burn_fee_from_source_account(fee, currency_id)?;

// Send out the xcm message.
let dest = Pallet::<T>::get_para_multilocation_by_currency_id(currency_id)?;
send_xcm::<T::XcmRouter>(dest, xcm_message).map_err(|_e| Error::<T>::XcmFailure)?;
Expand Down Expand Up @@ -588,14 +600,18 @@ impl<T: Config>

// Wrap the xcm message as it is sent from a subaccount of the parachain account, and
// send it out.
Pallet::<T>::construct_xcm_and_send_as_subaccount_without_query_id(
let fee = Pallet::<T>::construct_xcm_and_send_as_subaccount_without_query_id(
XcmOperationType::TransferBack,
call.encode(),
from,
currency_id,
weight_and_fee,
)?;

// withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the
// process.
Pallet::<T>::burn_fee_from_source_account(fee, currency_id)?;

Ok(())
}

Expand All @@ -619,6 +635,14 @@ impl<T: Config>
let (entrance_account, _) = T::VtokenMinting::get_entrance_and_exit_accounts();
ensure!(from_account_id == entrance_account, Error::<T>::InvalidAccount);

// transfer supplementary fee from treasury to the "from" account. Return the added up
// amount
let amount = Pallet::<T>::get_transfer_to_added_amount_and_supplement(
from_account_id,
amount,
currency_id,
)?;

Pallet::<T>::do_transfer_to(from, to, amount, currency_id)?;

Ok(())
Expand Down Expand Up @@ -653,7 +677,7 @@ impl<T: Config>

// Wrap the xcm message as it is sent from a subaccount of the parachain account, and
// send it out.
let (query_id, _timeout, _fee, xcm_message) =
let (query_id, _timeout, fee, xcm_message) =
Pallet::<T>::construct_xcm_as_subaccount_with_query_id(
XcmOperationType::ConvertAsset,
call.encode(),
Expand All @@ -662,6 +686,10 @@ impl<T: Config>
weight_and_fee,
)?;

// withdraw this xcm fee from treasury. If treasury doesn't have this money, stop the
// process.
Pallet::<T>::burn_fee_from_source_account(fee, currency_id)?;

// Send out the xcm message.
let dest = Pallet::<T>::get_para_multilocation_by_currency_id(currency_id)?;
send_xcm::<T::XcmRouter>(dest, xcm_message).map_err(|_e| Error::<T>::XcmFailure)?;
Expand Down
Loading