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

feat(multipayment)!: use fallback in set_currency #93

Merged
merged 7 commits into from Jul 7, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 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 pallets/transaction-multi-payment/Cargo.toml
Expand Up @@ -6,7 +6,7 @@ homepage = 'https://github.com/galacticcouncil/basilisk-node'
license = 'Apache 2.0'
name = 'pallet-transaction-multi-payment'
repository = 'https://github.com/galacticcouncil/basilisk-node'
version = '4.0.0'
version = '4.1.0'

[package.metadata.docs.rs]
targets = ['x86_64-unknown-linux-gnu']
Expand Down
14 changes: 7 additions & 7 deletions pallets/transaction-multi-payment/src/lib.rs
Expand Up @@ -231,7 +231,7 @@ pub mod pallet {
<AccountCurrencyMap<T>>::insert(who.clone(), currency);

if T::WithdrawFeeForSetCurrency::get() == Pays::Yes {
Self::withdraw_set_fee(&who, currency)?;
Self::withdraw_set_fee(&who)?;
}

Self::deposit_event(Event::CurrencySet(who, currency));
Expand Down Expand Up @@ -371,15 +371,16 @@ impl<T: Config> Pallet<T> {
Authorities::<T>::mutate(|x| x.push(who.clone()));
}

pub fn withdraw_set_fee(who: &T::AccountId, currency: AssetId) -> DispatchResult {
pub fn withdraw_set_fee(who: &T::AccountId) -> DispatchResult {
let base_fee = Self::weight_to_fee(T::BlockWeights::get().get(DispatchClass::Normal).base_extrinsic);
let adjusted_weight_fee = Self::weight_to_fee(T::WeightInfo::set_currency());
let fee = base_fee.saturating_add(adjusted_weight_fee);

Self::swap_currency(who, fee)?;
T::MultiCurrency::withdraw(currency, who, fee)?;

Ok(())
let result = Self::swap(who, fee)?;
match result {
PaymentSwapResult::Transferred => Ok(()),
PaymentSwapResult::Native | PaymentSwapResult::Swapped => T::MultiCurrency::withdraw(CORE_ASSET_ID, who, fee),
enthusiastmartin marked this conversation as resolved.
Show resolved Hide resolved
}
}

fn weight_to_fee(weight: Weight) -> Balance {
Expand Down Expand Up @@ -463,7 +464,6 @@ where
if let Ok(detail) = SW::swap(&who, fee.into()) {
match detail {
PaymentSwapResult::Transferred => Ok(None),
PaymentSwapResult::Error => Err(InvalidTransaction::Payment.into()),
PaymentSwapResult::Native | PaymentSwapResult::Swapped => {
match C::withdraw(who, fee, withdraw_reason, ExistenceRequirement::KeepAlive) {
Ok(imbalance) => Ok(Some(imbalance)),
Expand Down
4 changes: 2 additions & 2 deletions pallets/transaction-multi-payment/src/mock.rs
Expand Up @@ -41,7 +41,7 @@ use primitives::fee;

pub type AccountId = u64;

pub const INITIAL_BALANCE: Balance = 1000_000_000_000_000u128;
pub const INITIAL_BALANCE: Balance = 1_000_000_000_000_000u128;

pub const ALICE: AccountId = 1;
pub const BOB: AccountId = 2;
Expand Down Expand Up @@ -113,7 +113,7 @@ parameter_types! {
.build_or_panic();

pub ExchangeFeeRate: fee::Fee = fee::Fee::default();
pub PayForSetCurrency : Pays = Pays::No;
pub PayForSetCurrency : Pays = Pays::Yes;
}

impl system::Config for Test {
Expand Down
105 changes: 85 additions & 20 deletions pallets/transaction-multi-payment/src/tests.rs
Expand Up @@ -40,8 +40,8 @@ fn set_unsupported_currency() {
}

#[test]
fn set_supported_currency() {
ExtBuilder::default().build().execute_with(|| {
fn set_supported_currency_without_pool() {
ExtBuilder::default().base_weight(5).build().execute_with(|| {
assert_ok!(PaymentPallet::set_currency(
Origin::signed(ALICE),
SUPPORTED_CURRENCY_WITH_BALANCE
Expand All @@ -51,6 +51,43 @@ fn set_supported_currency() {
PaymentPallet::get_currency(ALICE),
Some(SUPPORTED_CURRENCY_WITH_BALANCE)
);

assert_eq!(
Currencies::free_balance(SUPPORTED_CURRENCY_WITH_BALANCE, &ALICE),
999_999_999_998_457
);
assert_eq!(
Currencies::free_balance(SUPPORTED_CURRENCY_WITH_BALANCE, &FALLBACK_ACCOUNT),
1_543
);
});
}

#[test]
fn set_supported_currency_with_pool() {
ExtBuilder::default().base_weight(5).build().execute_with(|| {
assert_ok!(XYKPallet::create_pool(
Origin::signed(ALICE),
HDX,
SUPPORTED_CURRENCY_WITH_BALANCE,
100_000,
Price::from(1)
));

assert_ok!(PaymentPallet::set_currency(
Origin::signed(ALICE),
SUPPORTED_CURRENCY_WITH_BALANCE
),);

assert_eq!(
PaymentPallet::get_currency(ALICE),
Some(SUPPORTED_CURRENCY_WITH_BALANCE)
);

assert_eq!(
Currencies::free_balance(SUPPORTED_CURRENCY_WITH_BALANCE, &ALICE),
999_999_999_898_958
);
});
}

Expand Down Expand Up @@ -85,6 +122,31 @@ fn set_native_currency_with_no_balance() {
});
}

#[test]
fn set_currency_with_insufficient_balance() {
const CHARLIE: AccountId = 5;

ExtBuilder::default()
.base_weight(5)
.account_native_balance(CHARLIE, 10)
.account_tokens(CHARLIE, SUPPORTED_CURRENCY_WITH_BALANCE, 10)
.build()
.execute_with(|| {
assert_noop!(
PaymentPallet::set_currency(Origin::signed(CHARLIE), SUPPORTED_CURRENCY_WITH_BALANCE),
orml_tokens::Error::<Test>::BalanceTooLow
);

assert_noop!(
PaymentPallet::set_currency(Origin::signed(CHARLIE), HDX),
pallet_balances::Error::<Test>::InsufficientBalance
);

assert_eq!(Currencies::free_balance(SUPPORTED_CURRENCY_WITH_BALANCE, &CHARLIE), 10);
assert_eq!(Currencies::free_balance(HDX, &CHARLIE), 10);
});
}

#[test]
fn fee_payment_in_native_currency() {
const CHARLIE: AccountId = 5;
Expand Down Expand Up @@ -136,7 +198,7 @@ fn fee_payment_in_non_native_currency() {
ExtBuilder::default()
.base_weight(5)
.account_native_balance(CHARLIE, 0)
.account_tokens(CHARLIE, SUPPORTED_CURRENCY_WITH_BALANCE, 10000)
.account_tokens(CHARLIE, SUPPORTED_CURRENCY_WITH_BALANCE, 10_000)
.build()
.execute_with(|| {
// Make sure Charlie ain't got a penny!
Expand All @@ -146,9 +208,10 @@ fn fee_payment_in_non_native_currency() {
Origin::signed(ALICE),
HDX,
SUPPORTED_CURRENCY_WITH_BALANCE,
100000,
100_000,
Price::from(1)
));

assert_ok!(PaymentPallet::set_currency(
Origin::signed(CHARLIE),
SUPPORTED_CURRENCY_WITH_BALANCE
Expand All @@ -160,18 +223,16 @@ fn fee_payment_in_non_native_currency() {
..Default::default()
};

assert_eq!(Tokens::free_balance(SUPPORTED_CURRENCY_WITH_BALANCE, &CHARLIE), 8_958);

assert!(ChargeTransactionPayment::<Test>::from(0)
.pre_dispatch(&CHARLIE, CALL, &info, len)
.is_ok());

//Native balance check - Charlie should be still broke!
assert_eq!(Balances::free_balance(CHARLIE), 0);

// token check should be less by the fee amount and -1 as fee in amm swap
assert_eq!(
Tokens::free_balance(SUPPORTED_CURRENCY_WITH_BALANCE, &CHARLIE),
10000 - 1000 - 23
);
assert_eq!(Tokens::free_balance(SUPPORTED_CURRENCY_WITH_BALANCE, &CHARLIE), 7_914);
});
}

Expand All @@ -182,14 +243,14 @@ fn fee_payment_non_native_insufficient_balance() {
ExtBuilder::default()
.base_weight(5)
.account_native_balance(CHARLIE, 0)
.account_tokens(CHARLIE, SUPPORTED_CURRENCY_WITH_BALANCE, 10)
.account_tokens(CHARLIE, SUPPORTED_CURRENCY_WITH_BALANCE, 2_000)
.build()
.execute_with(|| {
assert_ok!(pallet_xyk::Pallet::<Test>::create_pool(
Origin::signed(ALICE),
HDX,
SUPPORTED_CURRENCY_WITH_BALANCE,
100000,
100_000,
Price::from(1)
));

Expand All @@ -198,7 +259,7 @@ fn fee_payment_non_native_insufficient_balance() {
SUPPORTED_CURRENCY_WITH_BALANCE
));

let len = 10;
let len = 1000;
let info = DispatchInfo {
weight: 5,
..Default::default()
Expand All @@ -208,7 +269,7 @@ fn fee_payment_non_native_insufficient_balance() {
.pre_dispatch(&CHARLIE, CALL, &info, len)
.is_err());

assert_eq!(Tokens::free_balance(SUPPORTED_CURRENCY_WITH_BALANCE, &CHARLIE), 10);
assert_eq!(Tokens::free_balance(SUPPORTED_CURRENCY_WITH_BALANCE, &CHARLIE), 958);
});
}

Expand Down Expand Up @@ -306,7 +367,7 @@ fn fee_payment_in_non_native_currency_with_no_pool() {
ExtBuilder::default()
.base_weight(5)
.account_native_balance(CHARLIE, 0)
.account_tokens(CHARLIE, SUPPORTED_CURRENCY_WITH_BALANCE, 1000)
.account_tokens(CHARLIE, SUPPORTED_CURRENCY_WITH_BALANCE, 10_000)
.build()
.execute_with(|| {
// Make sure Charlie ain't got a penny!
Expand All @@ -323,18 +384,22 @@ fn fee_payment_in_non_native_currency_with_no_pool() {
..Default::default()
};

assert_eq!(
Tokens::free_balance(SUPPORTED_CURRENCY_WITH_BALANCE, &FALLBACK_ACCOUNT),
1_543
);

assert!(ChargeTransactionPayment::<Test>::from(0)
.pre_dispatch(&CHARLIE, CALL, &info, len)
.is_ok());

//Native balance check - Charlie should be still broke!
assert_eq!(Balances::free_balance(CHARLIE), 0);

// token check should be less by the fee amount and -1 as fee in amm swap
assert_eq!(Tokens::free_balance(SUPPORTED_CURRENCY_WITH_BALANCE, &CHARLIE), 970);
assert_eq!(Tokens::free_balance(SUPPORTED_CURRENCY_WITH_BALANCE, &CHARLIE), 8_427);
assert_eq!(
Tokens::free_balance(SUPPORTED_CURRENCY_WITH_BALANCE, &FALLBACK_ACCOUNT),
30
1_573
);
});
}
Expand All @@ -346,15 +411,15 @@ fn fee_payment_non_native_insufficient_balance_with_no_pool() {
ExtBuilder::default()
.base_weight(5)
.account_native_balance(CHARLIE, 0)
.account_tokens(CHARLIE, SUPPORTED_CURRENCY_WITH_BALANCE, 10)
.account_tokens(CHARLIE, SUPPORTED_CURRENCY_WITH_BALANCE, 2_000)
.build()
.execute_with(|| {
assert_ok!(PaymentPallet::set_currency(
Origin::signed(CHARLIE),
SUPPORTED_CURRENCY_WITH_BALANCE
));

let len = 10;
let len = 1000;
let info = DispatchInfo {
weight: 5,
..Default::default()
Expand All @@ -364,6 +429,6 @@ fn fee_payment_non_native_insufficient_balance_with_no_pool() {
.pre_dispatch(&CHARLIE, CALL, &info, len)
.is_err());

assert_eq!(Tokens::free_balance(SUPPORTED_CURRENCY_WITH_BALANCE, &CHARLIE), 10);
assert_eq!(Tokens::free_balance(SUPPORTED_CURRENCY_WITH_BALANCE, &CHARLIE), 457);
});
}
1 change: 0 additions & 1 deletion pallets/transaction-multi-payment/src/traits.rs
@@ -1,7 +1,6 @@
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PaymentSwapResult {
Native,
Error,
Swapped,
Transferred,
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/Cargo.toml
Expand Up @@ -5,7 +5,7 @@ homepage = 'https://github.com/galacticcouncil/Basilisk-node'
license = 'Apache 2.0'
name = 'basilisk-runtime'
repository = 'https://github.com/galacticcouncil/Basilisk-node'
version = '9.0.0'
version = '10.0.0'

[package.metadata.docs.rs]
targets = ['x86_64-unknown-linux-gnu']
Expand Down
4 changes: 2 additions & 2 deletions runtime/src/lib.rs
Expand Up @@ -117,7 +117,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("basilisk"),
impl_name: create_runtime_str!("basilisk"),
authoring_version: 1,
spec_version: 9,
spec_version: 10,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -344,7 +344,7 @@ parameter_types! {
/// Minimum amount of the multiplier. This value cannot be too low. A test case should ensure
/// that combined with `AdjustmentVariable`, we can recover from the minimum.
pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 1_000_000u128);
pub const MultiPaymentCurrencySetFee: Pays = Pays::No;
pub const MultiPaymentCurrencySetFee: Pays = Pays::Yes;
}

/// Parameterized slow adjusting fee updated based on
Expand Down