Skip to content

Commit

Permalink
Improve transaction payment and DEX swap (#2845)
Browse files Browse the repository at this point in the history
* fix swap_from_pool_or_dex

* fix tests

* add tests

* update orml

* fix tests

* fix tests

* fix

* fix
  • Loading branch information
zjb0807 authored Jan 13, 2025
1 parent 8f06516 commit c61d548
Show file tree
Hide file tree
Showing 35 changed files with 715 additions and 135 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ concurrency:
cancel-in-progress: true

env:
TARPAULIN_VERSION: 0.31.2
TARPAULIN_VERSION: 0.31.4
CARGO_INCREMENTAL: 0
jobs:
test:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ jobs:
with:
node-version: 18.x
- name: Install deps
run: cargo install staging-chain-spec-builder
run: cargo install staging-chain-spec-builder --force
- name: Run ts tests
run: |
npm install -g yarn
Expand Down
3 changes: 3 additions & 0 deletions modules/aggregated-dex/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ mod aggregated_dex {

pub const ALICE: AccountId = AccountId32::new([1u8; 32]);
pub const BOB: AccountId = AccountId32::new([2u8; 32]);
pub const ACA: CurrencyId = CurrencyId::Token(TokenSymbol::ACA);
pub const AUSD: CurrencyId = CurrencyId::Token(TokenSymbol::AUSD);
pub const DOT: CurrencyId = CurrencyId::Token(TokenSymbol::DOT);
pub const LDOT: CurrencyId = CurrencyId::Token(TokenSymbol::LDOT);
Expand Down Expand Up @@ -81,6 +82,7 @@ ord_parameter_types! {
parameter_types! {
pub const DEXPalletId: PalletId = PalletId(*b"aca/dexm");
pub const GetExchangeFee: (u32, u32) = (0, 100);
pub const GetNativeCurrencyId: CurrencyId = ACA;
pub EnabledTradingPairs: Vec<TradingPair> = vec![];
}

Expand All @@ -90,6 +92,7 @@ impl module_dex::Config for Runtime {
type GetExchangeFee = GetExchangeFee;
type TradingPathLimit = ConstU32<4>;
type PalletId = DEXPalletId;
type GetNativeCurrencyId = GetNativeCurrencyId;
type Erc20InfoMapping = ();
type DEXIncentives = ();
type WeightInfo = ();
Expand Down
10 changes: 8 additions & 2 deletions modules/auction-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#![allow(clippy::upper_case_acronyms)]
#![allow(clippy::unnecessary_unwrap)]

use frame_support::{pallet_prelude::*, transactional};
use frame_support::{pallet_prelude::*, traits::ExistenceRequirement, transactional};
use frame_system::{
offchain::{SendTransactionTypes, SubmitTransaction},
pallet_prelude::*,
Expand Down Expand Up @@ -554,7 +554,13 @@ impl<T: Config> Pallet<T> {
// if there's bid before, return stablecoin from new bidder to last bidder
if let Some(last_bidder) = last_bidder {
let refund = collateral_auction.payment_amount(last_bid_price);
T::Currency::transfer(T::GetStableCurrencyId::get(), &new_bidder, last_bidder, refund)?;
T::Currency::transfer(
T::GetStableCurrencyId::get(),
&new_bidder,
last_bidder,
refund,
ExistenceRequirement::AllowDeath,
)?;

payment = payment
.checked_sub(refund)
Expand Down
3 changes: 3 additions & 0 deletions modules/auction-manager/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub type Amount = i64;
pub const ALICE: AccountId = 1;
pub const BOB: AccountId = 2;
pub const CAROL: AccountId = 3;
pub const ACA: CurrencyId = CurrencyId::Token(TokenSymbol::ACA);
pub const AUSD: CurrencyId = CurrencyId::Token(TokenSymbol::AUSD);
pub const BTC: CurrencyId = CurrencyId::ForeignAsset(255);
pub const DOT: CurrencyId = CurrencyId::Token(TokenSymbol::DOT);
Expand Down Expand Up @@ -141,6 +142,7 @@ impl PriceProvider<CurrencyId> for MockPriceSource {
parameter_types! {
pub const DEXPalletId: PalletId = PalletId(*b"aca/dexm");
pub const GetExchangeFee: (u32, u32) = (0, 100);
pub const GetNativeCurrencyId: CurrencyId = ACA;
pub EnabledTradingPairs: Vec<TradingPair> = vec![
TradingPair::from_currency_ids(AUSD, BTC).unwrap(),
TradingPair::from_currency_ids(DOT, BTC).unwrap(),
Expand All @@ -154,6 +156,7 @@ impl module_dex::Config for Runtime {
type GetExchangeFee = GetExchangeFee;
type TradingPathLimit = ConstU32<4>;
type PalletId = DEXPalletId;
type GetNativeCurrencyId = GetNativeCurrencyId;
type Erc20InfoMapping = ();
type DEXIncentives = ();
type WeightInfo = ();
Expand Down
29 changes: 25 additions & 4 deletions modules/cdp-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
#![allow(clippy::unused_unit)]
#![allow(clippy::upper_case_acronyms)]

use frame_support::{pallet_prelude::*, traits::UnixTime, transactional, BoundedVec, PalletId};
use frame_support::{
pallet_prelude::*, traits::ExistenceRequirement, traits::UnixTime, transactional, BoundedVec, PalletId,
};
use frame_system::{
offchain::{SendTransactionTypes, SubmitTransaction},
pallet_prelude::*,
Expand Down Expand Up @@ -1014,10 +1016,22 @@ impl<T: Config> Pallet<T> {

// refund unused lp component tokens
if let Some(remainer) = available_0.checked_sub(consumption_0) {
<T as Config>::Currency::transfer(token_0, &loans_module_account, who, remainer)?;
<T as Config>::Currency::transfer(
token_0,
&loans_module_account,
who,
remainer,
ExistenceRequirement::AllowDeath,
)?;
}
if let Some(remainer) = available_1.checked_sub(consumption_1) {
<T as Config>::Currency::transfer(token_1, &loans_module_account, who, remainer)?;
<T as Config>::Currency::transfer(
token_1,
&loans_module_account,
who,
remainer,
ExistenceRequirement::AllowDeath,
)?;
}

actual_increase_lp
Expand Down Expand Up @@ -1119,6 +1133,7 @@ impl<T: Config> Pallet<T> {
&loans_module_account,
who,
actual_stable_amount.saturating_sub(previous_debit_value),
ExistenceRequirement::AllowDeath,
)?;

(previous_debit_value, debit)
Expand Down Expand Up @@ -1484,7 +1499,13 @@ impl<T: Config> LiquidateCollateral<T::AccountId> for LiquidateViaContracts<T> {
return Ok(());
} else if repayment > 0 {
// insufficient repayment, refund
CurrencyOf::<T>::transfer(stable_coin, &repay_dest_account_id, &contract_account_id, repayment)?;
CurrencyOf::<T>::transfer(
stable_coin,
&repay_dest_account_id,
&contract_account_id,
repayment,
ExistenceRequirement::AllowDeath,
)?;
// notify liquidation failed
T::LiquidationEvmBridge::on_repayment_refund(
InvokeContext {
Expand Down
1 change: 1 addition & 0 deletions modules/cdp-engine/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ impl module_dex::Config for Runtime {
type GetExchangeFee = GetExchangeFee;
type TradingPathLimit = ConstU32<4>;
type PalletId = DEXPalletId;
type GetNativeCurrencyId = GetNativeCurrencyId;
type Erc20InfoMapping = ();
type DEXIncentives = ();
type WeightInfo = ();
Expand Down
42 changes: 36 additions & 6 deletions modules/cdp-treasury/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#![allow(clippy::unused_unit)]
#![allow(clippy::needless_range_loop)]

use frame_support::{pallet_prelude::*, transactional, PalletId};
use frame_support::{pallet_prelude::*, traits::ExistenceRequirement, transactional, PalletId};
use frame_system::pallet_prelude::*;
use module_support::{AuctionManager, CDPTreasury, CDPTreasuryExtended, DEXManager, Ratio, Swap, SwapLimit};
use nutsfinance_stable_asset::traits::StableAsset;
Expand Down Expand Up @@ -194,6 +194,7 @@ pub mod module {
&Self::account_id(),
&T::TreasuryAccount::get(),
amount,
ExistenceRequirement::AllowDeath,
)?;
Ok(())
}
Expand Down Expand Up @@ -391,23 +392,52 @@ impl<T: Config> CDPTreasury<T::AccountId> for Pallet<T> {

/// This should be the only function in the system that burns stable coin
fn burn_debit(who: &T::AccountId, debit: Self::Balance) -> DispatchResult {
T::Currency::withdraw(T::GetStableCurrencyId::get(), who, debit)
T::Currency::withdraw(
T::GetStableCurrencyId::get(),
who,
debit,
ExistenceRequirement::AllowDeath,
)
}

fn deposit_surplus(from: &T::AccountId, surplus: Self::Balance) -> DispatchResult {
T::Currency::transfer(T::GetStableCurrencyId::get(), from, &Self::account_id(), surplus)
T::Currency::transfer(
T::GetStableCurrencyId::get(),
from,
&Self::account_id(),
surplus,
ExistenceRequirement::AllowDeath,
)
}

fn withdraw_surplus(to: &T::AccountId, surplus: Self::Balance) -> DispatchResult {
T::Currency::transfer(T::GetStableCurrencyId::get(), &Self::account_id(), to, surplus)
T::Currency::transfer(
T::GetStableCurrencyId::get(),
&Self::account_id(),
to,
surplus,
ExistenceRequirement::AllowDeath,
)
}

fn deposit_collateral(from: &T::AccountId, currency_id: Self::CurrencyId, amount: Self::Balance) -> DispatchResult {
T::Currency::transfer(currency_id, from, &Self::account_id(), amount)
T::Currency::transfer(
currency_id,
from,
&Self::account_id(),
amount,
ExistenceRequirement::AllowDeath,
)
}

fn withdraw_collateral(to: &T::AccountId, currency_id: Self::CurrencyId, amount: Self::Balance) -> DispatchResult {
T::Currency::transfer(currency_id, &Self::account_id(), to, amount)
T::Currency::transfer(
currency_id,
&Self::account_id(),
to,
amount,
ExistenceRequirement::AllowDeath,
)
}
}

Expand Down
1 change: 1 addition & 0 deletions modules/cdp-treasury/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl module_dex::Config for Runtime {
type GetExchangeFee = GetExchangeFee;
type TradingPathLimit = ConstU32<4>;
type PalletId = DEXPalletId;
type GetNativeCurrencyId = GetNativeCurrencyId;
type Erc20InfoMapping = ();
type DEXIncentives = ();
type WeightInfo = ();
Expand Down
Loading

0 comments on commit c61d548

Please sign in to comment.