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

pallet-asset-conversion: Swap Credit #1677

Merged
merged 50 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
39935e9
move swap trait and impl into separate module
muharem Sep 15, 2023
d8ab7c1
restructure imports
muharem Sep 15, 2023
6d6cc18
transfer swap
muharem Sep 20, 2023
a355241
swap credit
muharem Sep 22, 2023
a78ac71
with transaction
muharem Sep 22, 2023
888c9fa
preservation
muharem Sep 22, 2023
793b612
add amounts to the event
muharem Sep 22, 2023
cdaf500
not expendable constraint
muharem Sep 25, 2023
2a19a41
Merge remote-tracking branch 'origin/master' into muharem-asset-conve…
muharem Sep 25, 2023
e237dfc
vec import
muharem Sep 25, 2023
0c13c85
fix tests, preserve account
muharem Sep 25, 2023
24654b8
tests
muharem Sep 26, 2023
e0bd8b7
max lenght of the swap path in swap trait
muharem Sep 26, 2023
80dbcab
fix clippy
muharem Sep 26, 2023
847207b
debug impls for imbalances
muharem Sep 26, 2023
53c699f
eq and partial_eq impls for imbalance
muharem Sep 28, 2023
827ce1b
eq, partial_eq, runtime debug for credit
muharem Sep 28, 2023
d533acf
tests
muharem Sep 28, 2023
69b17f8
Apply suggestions from code review
muharem Sep 28, 2023
28edff5
correct docs
muharem Sep 28, 2023
697afa6
dev comments
muharem Sep 28, 2023
1c50220
use eq/partial_eq/runtime_debug derives with no bound version
muharem Sep 29, 2023
fe9e4f8
Merge remote-tracking branch 'origin/master' into muharem-asset-conve…
muharem Oct 5, 2023
e7ffbb4
swap trait update
muharem Oct 5, 2023
ee49f27
box multi asset id
muharem Oct 7, 2023
ee9d2eb
use vec instead bounded vec
muharem Oct 7, 2023
ced5e99
update to new api
muharem Oct 7, 2023
7fee81e
drop clone
muharem Oct 7, 2023
b63cd73
box arguments and drop bounded vec from benches
muharem Oct 8, 2023
01bd4da
no box for benchmarks impls
muharem Oct 8, 2023
e2b99f9
single balance type for Currency and Assets
muharem Oct 8, 2023
37b2e34
Balance trait bound on Balance generic type
muharem Oct 8, 2023
d9bc836
try into for credit
muharem Oct 8, 2023
0193864
drop clone
muharem Oct 8, 2023
ae19bb8
fix
muharem Oct 8, 2023
2372277
clippy fixes
muharem Oct 9, 2023
d1f37c7
review fixes
muharem Oct 9, 2023
3a4935e
remove unused trait
muharem Oct 9, 2023
75a7b8a
note in docs for swap traits
muharem Oct 10, 2023
ec2cf1d
impl on_nonzero_unbalanced instead on_unbalanced
muharem Oct 18, 2023
a8fadda
Merge remote-tracking branch 'origin/master' into muharem-asset-conve…
muharem Oct 23, 2023
168141d
Merge remote-tracking branch 'origin/master' into muharem-asset-conve…
muharem Oct 23, 2023
2b13f27
adjustments for rococo setup
muharem Oct 24, 2023
bbca52d
adjustments for rococo tests
muharem Oct 24, 2023
251d3f8
Merge branch 'master' into muharem-asset-conversion-credit-swap
muharem Oct 24, 2023
66a6611
rococo benchmarks
muharem Oct 24, 2023
01eab06
Merge remote-tracking branch 'origin/master' into muharem-asset-conve…
muharem Oct 25, 2023
82ba19b
Merge remote-tracking branch 'origin/master' into muharem-asset-conve…
muharem Dec 19, 2023
f7b7a2c
prdoc
muharem Dec 19, 2023
f24b49e
fix
muharem Dec 19, 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@
// limitations under the License.

use crate::*;
use frame_support::BoundedVec;
use parachains_common::rococo::currency::EXISTENTIAL_DEPOSIT;
use rococo_system_emulated_network::penpal_emulated_chain::LocalTeleportableToAssetHub as PenpalLocalTeleportableToAssetHub;
use sp_runtime::ModuleError;

#[test]
fn swap_locally_on_chain_using_local_assets() {
let asset_native = Box::new(asset_hub_rococo_runtime::xcm_config::TokenLocation::get());
let asset_one = Box::new(MultiLocation {
let asset_native = asset_hub_rococo_runtime::xcm_config::TokenLocation::get();
let asset_one = MultiLocation {
parents: 0,
interior: X2(PalletInstance(ASSETS_PALLET_ID), GeneralIndex(ASSET_ID.into())),
});
};

AssetHubRococo::execute_with(|| {
type RuntimeEvent = <AssetHubRococo as Chain>::RuntimeEvent;
Expand All @@ -47,8 +46,8 @@ fn swap_locally_on_chain_using_local_assets() {

assert_ok!(<AssetHubRococo as AssetHubRococoPallet>::AssetConversion::create_pool(
<AssetHubRococo as Chain>::RuntimeOrigin::signed(AssetHubRococoSender::get()),
asset_native.clone(),
asset_one.clone(),
Box::new(asset_native),
Box::new(asset_one),
));

assert_expected_events!(
Expand All @@ -60,8 +59,8 @@ fn swap_locally_on_chain_using_local_assets() {

assert_ok!(<AssetHubRococo as AssetHubRococoPallet>::AssetConversion::add_liquidity(
<AssetHubRococo as Chain>::RuntimeOrigin::signed(AssetHubRococoSender::get()),
asset_native.clone(),
asset_one.clone(),
Box::new(asset_native),
Box::new(asset_one),
1_000_000_000_000,
2_000_000_000_000,
0,
Expand All @@ -76,7 +75,7 @@ fn swap_locally_on_chain_using_local_assets() {
]
);

let path = BoundedVec::<_, _>::truncate_from(vec![asset_native.clone(), asset_one.clone()]);
let path = vec![Box::new(asset_native), Box::new(asset_one)];

assert_ok!(
<AssetHubRococo as AssetHubRococoPallet>::AssetConversion::swap_exact_tokens_for_tokens(
Expand All @@ -101,8 +100,8 @@ fn swap_locally_on_chain_using_local_assets() {

assert_ok!(<AssetHubRococo as AssetHubRococoPallet>::AssetConversion::remove_liquidity(
<AssetHubRococo as Chain>::RuntimeOrigin::signed(AssetHubRococoSender::get()),
asset_native,
asset_one,
Box::new(asset_native),
Box::new(asset_one),
1414213562273 - EXISTENTIAL_DEPOSIT * 2, // all but the 2 EDs can't be retrieved.
0,
0,
Expand All @@ -113,7 +112,7 @@ fn swap_locally_on_chain_using_local_assets() {

#[test]
fn swap_locally_on_chain_using_foreign_assets() {
let asset_native = Box::new(asset_hub_rococo_runtime::xcm_config::TokenLocation::get());
let asset_native = asset_hub_rococo_runtime::xcm_config::TokenLocation::get();
let ah_as_seen_by_penpal = PenpalA::sibling_location_of(AssetHubRococo::para_id());
let asset_location_on_penpal = PenpalLocalTeleportableToAssetHub::get();
let asset_id_on_penpal = match asset_location_on_penpal.last() {
Expand Down Expand Up @@ -165,12 +164,11 @@ fn swap_locally_on_chain_using_foreign_assets() {
]
);

let foreign_asset_at_asset_hub_rococo = Box::new(foreign_asset_at_asset_hub_rococo);
// 4. Create pool:
assert_ok!(<AssetHubRococo as AssetHubRococoPallet>::AssetConversion::create_pool(
<AssetHubRococo as Chain>::RuntimeOrigin::signed(AssetHubRococoSender::get()),
asset_native.clone(),
foreign_asset_at_asset_hub_rococo.clone(),
Box::new(asset_native),
Box::new(foreign_asset_at_asset_hub_rococo),
));

assert_expected_events!(
Expand All @@ -183,8 +181,8 @@ fn swap_locally_on_chain_using_foreign_assets() {
// 5. Add liquidity:
assert_ok!(<AssetHubRococo as AssetHubRococoPallet>::AssetConversion::add_liquidity(
<AssetHubRococo as Chain>::RuntimeOrigin::signed(sov_penpal_on_ahr.clone()),
asset_native.clone(),
foreign_asset_at_asset_hub_rococo.clone(),
Box::new(asset_native),
Box::new(foreign_asset_at_asset_hub_rococo),
1_000_000_000_000,
2_000_000_000_000,
0,
Expand All @@ -202,10 +200,7 @@ fn swap_locally_on_chain_using_foreign_assets() {
);

// 6. Swap!
let path = BoundedVec::<_, _>::truncate_from(vec![
asset_native.clone(),
foreign_asset_at_asset_hub_rococo.clone(),
]);
let path = vec![Box::new(asset_native), Box::new(foreign_asset_at_asset_hub_rococo)];

assert_ok!(
<AssetHubRococo as AssetHubRococoPallet>::AssetConversion::swap_exact_tokens_for_tokens(
Expand All @@ -231,8 +226,8 @@ fn swap_locally_on_chain_using_foreign_assets() {
// 7. Remove liquidity
assert_ok!(<AssetHubRococo as AssetHubRococoPallet>::AssetConversion::remove_liquidity(
<AssetHubRococo as Chain>::RuntimeOrigin::signed(sov_penpal_on_ahr.clone()),
asset_native,
foreign_asset_at_asset_hub_rococo,
Box::new(asset_native),
Box::new(foreign_asset_at_asset_hub_rococo),
1414213562273 - 2_000_000_000, // all but the 2 EDs can't be retrieved.
0,
0,
Expand All @@ -243,7 +238,7 @@ fn swap_locally_on_chain_using_foreign_assets() {

#[test]
fn cannot_create_pool_from_pool_assets() {
let asset_native = Box::new(asset_hub_rococo_runtime::xcm_config::TokenLocation::get());
let asset_native = asset_hub_rococo_runtime::xcm_config::TokenLocation::get();
let mut asset_one = asset_hub_rococo_runtime::xcm_config::PoolAssetsPalletLocation::get();
asset_one.append_with(GeneralIndex(ASSET_ID.into())).expect("pool assets");

Expand All @@ -268,7 +263,7 @@ fn cannot_create_pool_from_pool_assets() {
assert_matches::assert_matches!(
<AssetHubRococo as AssetHubRococoPallet>::AssetConversion::create_pool(
<AssetHubRococo as Chain>::RuntimeOrigin::signed(AssetHubRococoSender::get()),
asset_native.clone(),
Box::new(asset_native),
Box::new(asset_one),
),
Err(DispatchError::Module(ModuleError{index: _, error: _, message})) => assert_eq!(message, Some("UnsupportedAsset"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ use westend_system_emulated_network::penpal_emulated_chain::LocalTeleportableToA

#[test]
fn swap_locally_on_chain_using_local_assets() {
let asset_native = Box::new(asset_hub_westend_runtime::xcm_config::WestendLocation::get());
let asset_one = Box::new(MultiLocation {
let asset_native = asset_hub_westend_runtime::xcm_config::WestendLocation::get();
let asset_one = MultiLocation {
parents: 0,
interior: X2(PalletInstance(ASSETS_PALLET_ID), GeneralIndex(ASSET_ID.into())),
});
};

AssetHubWestend::execute_with(|| {
type RuntimeEvent = <AssetHubWestend as Chain>::RuntimeEvent;
Expand All @@ -44,8 +44,8 @@ fn swap_locally_on_chain_using_local_assets() {

assert_ok!(<AssetHubWestend as AssetHubWestendPallet>::AssetConversion::create_pool(
<AssetHubWestend as Chain>::RuntimeOrigin::signed(AssetHubWestendSender::get()),
asset_native.clone(),
asset_one.clone(),
Box::new(asset_native),
Box::new(asset_one),
));

assert_expected_events!(
Expand All @@ -57,8 +57,8 @@ fn swap_locally_on_chain_using_local_assets() {

assert_ok!(<AssetHubWestend as AssetHubWestendPallet>::AssetConversion::add_liquidity(
<AssetHubWestend as Chain>::RuntimeOrigin::signed(AssetHubWestendSender::get()),
asset_native.clone(),
asset_one.clone(),
Box::new(asset_native),
Box::new(asset_one),
1_000_000_000_000,
2_000_000_000_000,
0,
Expand All @@ -73,7 +73,7 @@ fn swap_locally_on_chain_using_local_assets() {
]
);

let path = BoundedVec::<_, _>::truncate_from(vec![asset_native.clone(), asset_one.clone()]);
let path = vec![Box::new(asset_native), Box::new(asset_one)];

assert_ok!(<AssetHubWestend as AssetHubWestendPallet>::AssetConversion::swap_exact_tokens_for_tokens(
<AssetHubWestend as Chain>::RuntimeOrigin::signed(AssetHubWestendSender::get()),
Expand All @@ -96,8 +96,8 @@ fn swap_locally_on_chain_using_local_assets() {

assert_ok!(<AssetHubWestend as AssetHubWestendPallet>::AssetConversion::remove_liquidity(
<AssetHubWestend as Chain>::RuntimeOrigin::signed(AssetHubWestendSender::get()),
asset_native,
asset_one,
Box::new(asset_native),
Box::new(asset_one),
1414213562273 - 2_000_000_000, // all but the 2 EDs can't be retrieved.
0,
0,
Expand All @@ -108,7 +108,7 @@ fn swap_locally_on_chain_using_local_assets() {

#[test]
fn swap_locally_on_chain_using_foreign_assets() {
let asset_native = Box::new(asset_hub_westend_runtime::xcm_config::WestendLocation::get());
let asset_native = asset_hub_westend_runtime::xcm_config::WestendLocation::get();
let ah_as_seen_by_penpal = PenpalB::sibling_location_of(AssetHubWestend::para_id());
let asset_location_on_penpal = PenpalLocalTeleportableToAssetHub::get();
let asset_id_on_penpal = match asset_location_on_penpal.last() {
Expand Down Expand Up @@ -160,12 +160,11 @@ fn swap_locally_on_chain_using_foreign_assets() {
]
);

let foreign_asset_at_asset_hub_westend = Box::new(foreign_asset_at_asset_hub_westend);
// 4. Create pool:
assert_ok!(<AssetHubWestend as AssetHubWestendPallet>::AssetConversion::create_pool(
<AssetHubWestend as Chain>::RuntimeOrigin::signed(AssetHubWestendSender::get()),
asset_native.clone(),
foreign_asset_at_asset_hub_westend.clone(),
Box::new(asset_native),
Box::new(foreign_asset_at_asset_hub_westend),
));

assert_expected_events!(
Expand All @@ -178,8 +177,8 @@ fn swap_locally_on_chain_using_foreign_assets() {
// 5. Add liquidity:
assert_ok!(<AssetHubWestend as AssetHubWestendPallet>::AssetConversion::add_liquidity(
<AssetHubWestend as Chain>::RuntimeOrigin::signed(sov_penpal_on_ahw.clone()),
asset_native.clone(),
foreign_asset_at_asset_hub_westend.clone(),
Box::new(asset_native),
Box::new(foreign_asset_at_asset_hub_westend),
1_000_000_000_000,
2_000_000_000_000,
0,
Expand All @@ -197,10 +196,7 @@ fn swap_locally_on_chain_using_foreign_assets() {
);

// 6. Swap!
let path = BoundedVec::<_, _>::truncate_from(vec![
asset_native.clone(),
foreign_asset_at_asset_hub_westend.clone(),
]);
let path = vec![Box::new(asset_native), Box::new(foreign_asset_at_asset_hub_westend)];

assert_ok!(<AssetHubWestend as AssetHubWestendPallet>::AssetConversion::swap_exact_tokens_for_tokens(
<AssetHubWestend as Chain>::RuntimeOrigin::signed(AssetHubWestendSender::get()),
Expand All @@ -224,19 +220,19 @@ fn swap_locally_on_chain_using_foreign_assets() {
// 7. Remove liquidity
assert_ok!(<AssetHubWestend as AssetHubWestendPallet>::AssetConversion::remove_liquidity(
<AssetHubWestend as Chain>::RuntimeOrigin::signed(sov_penpal_on_ahw.clone()),
asset_native,
foreign_asset_at_asset_hub_westend,
Box::new(asset_native),
Box::new(foreign_asset_at_asset_hub_westend),
1414213562273 - 2_000_000_000, // all but the 2 EDs can't be retrieved.
0,
0,
sov_penpal_on_ahw.clone().into(),
sov_penpal_on_ahw.into(),
));
});
}

#[test]
fn cannot_create_pool_from_pool_assets() {
let asset_native = Box::new(asset_hub_westend_runtime::xcm_config::WestendLocation::get());
let asset_native = asset_hub_westend_runtime::xcm_config::WestendLocation::get();
let mut asset_one = asset_hub_westend_runtime::xcm_config::PoolAssetsPalletLocation::get();
asset_one.append_with(GeneralIndex(ASSET_ID.into())).expect("pool assets");

Expand All @@ -261,7 +257,7 @@ fn cannot_create_pool_from_pool_assets() {
assert_matches::assert_matches!(
<AssetHubWestend as AssetHubWestendPallet>::AssetConversion::create_pool(
<AssetHubWestend as Chain>::RuntimeOrigin::signed(AssetHubWestendSender::get()),
asset_native.clone(),
Box::new(asset_native),
Box::new(asset_one),
),
Err(DispatchError::Module(ModuleError{index: _, error: _, message})) => assert_eq!(message, Some("UnsupportedAsset"))
Expand Down
Loading