Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
328bbae
Pay tx by swapping the assets
Jun 9, 2023
146b611
Change liquidity structure
Jun 12, 2023
55255be
Uncomment the event
Jun 12, 2023
dd329fb
Update frame/transaction-payment/asset-tx-payment/src/payment.rs
jsidorenko Jun 12, 2023
589ccbc
New approach
Jun 14, 2023
b215921
Fix bounds
Jun 14, 2023
874e529
Clearer version
Jun 14, 2023
d4c2cdb
Change IsType with Into and From
Jun 14, 2023
7ceb4aa
Enable event
Jun 14, 2023
0d3de23
Check ED + fix the logic
Jun 14, 2023
e6e4060
Add temp comments
Jun 14, 2023
74b36fd
Rework the refund
Jun 14, 2023
4b699a9
Clean up
Jun 14, 2023
7d08ec4
Improve readability
Jun 15, 2023
8ef5339
Getting closer
Jun 15, 2023
2b08707
fix
Jun 15, 2023
4b1748a
Use fungible instead of Currency
Jun 15, 2023
3172f2c
Test account without ed
Jun 15, 2023
bf807fc
Final push
Jun 16, 2023
5a7aab2
Fixed
Jun 16, 2023
fd053d4
Rename to pallet-asset-conversion-tx-payment
Jun 16, 2023
5dab05d
Bring back the old pallet
Jun 16, 2023
b0cb6b5
Update versions
Jun 16, 2023
6a87a43
Merge branch 'master' into js/pay-by-swap
Jun 16, 2023
10e055a
Update docs
Jun 16, 2023
4904e0a
Update readme
Jun 16, 2023
0e51723
Wrong readme updated
Jun 16, 2023
9c94063
Revert back doc change
Jun 16, 2023
3b0d011
Fix import
Jun 16, 2023
fce87bf
Fix kitchensink
Jun 16, 2023
daa0e25
Fix
Jun 16, 2023
19490ae
One more time..
Jun 16, 2023
ff0fd49
Wait pls
Jun 16, 2023
f6ef059
Update frame/asset-conversion/src/lib.rs
jsidorenko Jun 20, 2023
d23631c
Merge branch 'master' into js/pay-by-swap
gilescope Jun 22, 2023
303c900
Update frame/support/src/traits/tokens/fungibles/regular.rs
jsidorenko Jun 22, 2023
bf7663e
Update docs/comments
Jun 22, 2023
f427ef1
Docs improvement
Jun 22, 2023
c9f7910
Update frame/transaction-payment/asset-conversion-tx-payment/src/lib.rs
jsidorenko Jun 22, 2023
6fb01e3
Update frame/transaction-payment/asset-conversion-tx-payment/src/lib.rs
jsidorenko Jun 22, 2023
59afde3
Update frame/transaction-payment/asset-conversion-tx-payment/src/lib.rs
jsidorenko Jun 22, 2023
d054f45
Update frame/transaction-payment/asset-conversion-tx-payment/src/lib.rs
jsidorenko Jun 22, 2023
22fce9e
Update frame/transaction-payment/asset-conversion-tx-payment/src/lib.rs
jsidorenko Jun 22, 2023
4463c9b
Payed -> paid
Jun 22, 2023
b26fdf4
Docs
Jun 22, 2023
1544c6a
Update frame/transaction-payment/asset-conversion-tx-payment/README.md
jsidorenko Jun 22, 2023
314b326
Rewrite docs
Jun 22, 2023
e1ead96
Try to clean the deps
Jun 22, 2023
74c62f0
Add debug assert
Jun 22, 2023
27b7b24
Return back frame-benchmarking
Jun 22, 2023
439eeb2
Update cargo
Jun 22, 2023
bf76045
Update frame/transaction-payment/asset-conversion-tx-payment/src/mock.rs
jsidorenko Jun 22, 2023
12abebf
Rename
Jun 22, 2023
ee288b3
clearer error message
gilescope Jun 22, 2023
1c3c1e0
Docs for Pay by Swap (#14445)
joepetrowski Jun 23, 2023
1fab01c
Fix compile errors
Jun 23, 2023
f39b02c
Another fix
Jun 23, 2023
ffb3cfe
Refactoring
Jun 23, 2023
7e763a0
Update frame/transaction-payment/asset-conversion-tx-payment/src/paym…
jsidorenko Jun 23, 2023
6beecc3
Emit an error if we fail to swap the refund back
Jun 23, 2023
25b201b
Add integrity_test
Jun 23, 2023
a211058
Update frame/asset-conversion/src/lib.rs
jsidorenko Jun 23, 2023
f7abdbc
Fmt
Jun 23, 2023
63588af
Use defensive_ok_or
Jun 23, 2023
f191fdf
Merge branch 'master' into js/pay-by-swap
gilescope Jun 23, 2023
0df5d89
child PR: Tidy swap event (#14441)
gilescope Jun 23, 2023
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
2 changes: 1 addition & 1 deletion 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 frame/transaction-payment/asset-tx-payment/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ serde_json = "1.0.85"
sp-storage = { version = "13.0.0", default-features = false, path = "../../../primitives/storage" }

pallet-assets = { version = "4.0.0-dev", path = "../../assets" }
pallet-authorship = { version = "4.0.0-dev", path = "../../authorship" }
pallet-balances = { version = "4.0.0-dev", path = "../../balances" }
pallet-asset-conversion = { version = "4.0.0-dev", path = "../../asset-conversion" }

[features]
default = ["std"]
Expand Down
44 changes: 22 additions & 22 deletions frame/transaction-payment/asset-tx-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,7 @@ use codec::{Decode, Encode};
use frame_support::{
dispatch::{DispatchInfo, DispatchResult, PostDispatchInfo},
traits::{
tokens::{
fungibles::{Balanced, Credit, Inspect},
WithdrawConsequence,
},
tokens::fungibles::{Balanced, Inspect},
IsType,
},
DefaultNoBound,
Expand Down Expand Up @@ -104,7 +101,7 @@ pub enum InitialPayment<T: Config> {
/// The initial fee was payed in the native currency.
Native(LiquidityInfoOf<T>),
/// The initial fee was payed in an asset.
Asset(Credit<T::AccountId, T::Fungibles>),
Asset(LiquidityInfoOf<T>),
}

pub use pallet::*;
Expand Down Expand Up @@ -159,7 +156,7 @@ where
AssetBalanceOf<T>: Send + Sync + FixedPointOperand,
BalanceOf<T>: Send + Sync + FixedPointOperand + IsType<ChargeAssetBalanceOf<T>>,
ChargeAssetIdOf<T>: Send + Sync,
Credit<T::AccountId, T::Fungibles>: IsType<ChargeAssetLiquidityOf<T>>,
LiquidityInfoOf<T>: IsType<ChargeAssetLiquidityOf<T>>,
{
/// Utility constructor. Used only in client/factory code.
pub fn from(tip: BalanceOf<T>, asset_id: Option<ChargeAssetIdOf<T>>) -> Self {
Expand Down Expand Up @@ -216,7 +213,7 @@ where
AssetBalanceOf<T>: Send + Sync + FixedPointOperand,
BalanceOf<T>: Send + Sync + From<u64> + FixedPointOperand + IsType<ChargeAssetBalanceOf<T>>,
ChargeAssetIdOf<T>: Send + Sync,
Credit<T::AccountId, T::Fungibles>: IsType<ChargeAssetLiquidityOf<T>>,
LiquidityInfoOf<T>: IsType<ChargeAssetLiquidityOf<T>>,
{
const IDENTIFIER: &'static str = "ChargeAssetTxPayment";
type AccountId = T::AccountId;
Expand Down Expand Up @@ -284,21 +281,24 @@ where
len as u32, info, post_info, tip,
);

let (converted_fee, converted_tip) =
T::OnChargeAssetTransaction::correct_and_deposit_fee(
&who,
info,
post_info,
actual_fee.into(),
tip.into(),
already_withdrawn.into(),
)?;
Pallet::<T>::deposit_event(Event::<T>::AssetTxFeePaid {
who,
actual_fee: converted_fee,
tip: converted_tip,
asset_id,
});
match T::OnChargeAssetTransaction::correct_and_deposit_fee(
&who,
info,
post_info,
actual_fee.into(),
tip.into(),
already_withdrawn.into(),
)? {
(Some(converted_fee), Some(converted_tip)) => {
Pallet::<T>::deposit_event(Event::<T>::AssetTxFeePaid {
who,
actual_fee: converted_fee,
tip: converted_tip,
asset_id,
});
},
_ => {},
}
},
InitialPayment::Nothing => {
// `actual_fee` should be zero here for any signed extrinsic. It would be
Expand Down
120 changes: 88 additions & 32 deletions frame/transaction-payment/asset-tx-payment/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@ use crate as pallet_asset_tx_payment;
use codec;
use frame_support::{
dispatch::DispatchClass,
instances::Instance2,
ord_parameter_types,
pallet_prelude::*,
parameter_types,
traits::{AsEnsureOriginWithArg, ConstU32, ConstU64, ConstU8, FindAuthor},
traits::{AsEnsureOriginWithArg, ConstU32, ConstU64, ConstU8, Imbalance, OnUnbalanced},
weights::{Weight, WeightToFee as WeightToFeeT},
ConsensusEngineId,
PalletId,
};
use frame_system as system;
use frame_system::EnsureRoot;
use frame_system::{EnsureRoot, EnsureSignedBy};
use pallet_asset_conversion::{NativeOrAssetId, NativeOrAssetIdConverter};
use pallet_transaction_payment::CurrencyAdapter;
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, ConvertInto, IdentityLookup, SaturatedConversion},
traits::{AccountIdConversion, BlakeTwo256, IdentityLookup, SaturatedConversion},
Permill,
};

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Runtime>;
Expand All @@ -40,8 +44,7 @@ type Balance = u64;
type AccountId = u64;

frame_support::construct_runtime!(
pub struct Runtime
where
pub enum Runtime where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
Expand All @@ -50,7 +53,8 @@ frame_support::construct_runtime!(
Balances: pallet_balances,
TransactionPayment: pallet_transaction_payment,
Assets: pallet_assets,
Authorship: pallet_authorship,
PoolAssets: pallet_assets::<Instance2>,
AssetConversion: pallet_asset_conversion,
AssetTxPayment: pallet_asset_tx_payment,
}
);
Expand Down Expand Up @@ -144,9 +148,28 @@ impl WeightToFeeT for TransactionByteFee {
}
}

parameter_types! {
pub(crate) static TipUnbalancedAmount: u64 = 0;
pub(crate) static FeeUnbalancedAmount: u64 = 0;
}

pub struct DealWithFees;
impl OnUnbalanced<pallet_balances::NegativeImbalance<Runtime>> for DealWithFees {
fn on_unbalanceds<B>(
mut fees_then_tips: impl Iterator<Item = pallet_balances::NegativeImbalance<Runtime>>,
) {
if let Some(fees) = fees_then_tips.next() {
FeeUnbalancedAmount::mutate(|a| *a += fees.peek());
if let Some(tips) = fees_then_tips.next() {
TipUnbalancedAmount::mutate(|a| *a += tips.peek());
}
}
}
}

impl pallet_transaction_payment::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type OnChargeTransaction = CurrencyAdapter<Balances, ()>;
type OnChargeTransaction = CurrencyAdapter<Balances, DealWithFees>;
type WeightToFee = WeightToFee;
type LengthToFee = TransactionByteFee;
type FeeMultiplierUpdate = ();
Expand Down Expand Up @@ -179,38 +202,71 @@ impl pallet_assets::Config for Runtime {
}
}

pub struct HardcodedAuthor;
pub(crate) const BLOCK_AUTHOR: AccountId = 1234;
impl FindAuthor<AccountId> for HardcodedAuthor {
fn find_author<'a, I>(_: I) -> Option<AccountId>
where
I: 'a + IntoIterator<Item = (ConsensusEngineId, &'a [u8])>,
{
Some(BLOCK_AUTHOR)
impl pallet_assets::Config<Instance2> for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = u64;
type RemoveItemsLimit = ConstU32<1000>;
type AssetId = u32;
type AssetIdParameter = u32;
type Currency = Balances;
type CreateOrigin = AsEnsureOriginWithArg<EnsureSignedBy<AssetConversionOrigin, u64>>;
type ForceOrigin = frame_system::EnsureRoot<u64>;
type AssetDeposit = ConstU64<0>;
type AssetAccountDeposit = ConstU64<0>;
type MetadataDepositBase = ConstU64<0>;
type MetadataDepositPerByte = ConstU64<0>;
type ApprovalDeposit = ConstU64<0>;
type StringLimit = ConstU32<50>;
type Freezer = ();
type Extra = ();
type WeightInfo = ();
type CallbackHandle = ();
pallet_assets::runtime_benchmarks_enabled! {
type BenchmarkHelper = ();
}
}

impl pallet_authorship::Config for Runtime {
type FindAuthor = HardcodedAuthor;
type EventHandler = ();
parameter_types! {
pub const AssetConversionPalletId: PalletId = PalletId(*b"py/ascon");
pub storage AllowMultiAssetPools: bool = false;
pub storage LiquidityWithdrawalFee: Permill = Permill::from_percent(0); // should be non-zero if AllowMultiAssetPools is true, otherwise can be zero
pub const MaxSwapPathLength: u32 = 4;
}

pub struct CreditToBlockAuthor;
impl HandleCredit<AccountId, Assets> for CreditToBlockAuthor {
fn handle_credit(credit: Credit<AccountId, Assets>) {
if let Some(author) = pallet_authorship::Pallet::<Runtime>::author() {
// What to do in case paying the author fails (e.g. because `fee < min_balance`)
// default: drop the result which will trigger the `OnDrop` of the imbalance.
let _ = <Assets as Balanced<AccountId>>::resolve(&author, credit);
}
}
ord_parameter_types! {
pub const AssetConversionOrigin: u64 = AccountIdConversion::<u64>::into_account_truncating(&AssetConversionPalletId::get());
}

impl pallet_asset_conversion::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type AssetBalance = <Self as pallet_balances::Config>::Balance;
type AssetId = u32;
type PoolAssetId = u32;
type Assets = Assets;
type PoolAssets = PoolAssets;
type PalletId = AssetConversionPalletId;
type WeightInfo = ();
type LPFee = ConstU32<3>; // means 0.3%
type PoolSetupFee = ConstU64<100>; // should be more or equal to the existential deposit
type PoolSetupFeeReceiver = AssetConversionOrigin;
type LiquidityWithdrawalFee = LiquidityWithdrawalFee;
type AllowMultiAssetPools = AllowMultiAssetPools;
type MaxSwapPathLength = MaxSwapPathLength;
type MintMinLiquidity = ConstU64<100>; // 100 is good enough when the main currency has 12 decimals.

type Balance = u64;
type HigherPrecisionBalance = u128;

type MultiAssetId = NativeOrAssetId<u32>;
type MultiAssetIdConverter = NativeOrAssetIdConverter<u32>;

#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

impl Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Fungibles = Assets;
type OnChargeAssetTransaction = FungiblesAdapter<
pallet_assets::BalanceToAssetBalance<Balances, Runtime, ConvertInto>,
CreditToBlockAuthor,
>;
type OnChargeAssetTransaction = AssetConversionAdapter<AssetConversion>;
}
Loading