-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add expensive scenario for asset exchange #7952
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
Merged
Merged
Changes from 89 commits
Commits
Show all changes
90 commits
Select commit
Hold shift + click to select a range
5ac5af0
Add expensive scenario for asset exchange
raymondkfcheung 5d2049d
Update from github-actions[bot] running command 'prdoc --audience run…
github-actions[bot] c6ff6ec
Use MAX_ITEMS_IN_ASSETS
raymondkfcheung f53a889
Update comment
raymondkfcheung bb505c1
Merge branch 'master' into ray-fix-worst-case
raymondkfcheung 5dfe33a
Add TODO
raymondkfcheung 6df104a
Merge branch 'master' into ray-fix-worst-case
raymondkfcheung 6a8d95b
Create pool
raymondkfcheung 4b205ba
Update comment
raymondkfcheung b5c98a6
Update code style
raymondkfcheung 1904c4e
Update cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs
raymondkfcheung 2e6338b
Update lib.rs
raymondkfcheung 59cc394
Update lib.rs
raymondkfcheung 0e3c3ff
Update cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs
raymondkfcheung 6ff1374
Merge branch 'master' into ray-fix-worst-case
raymondkfcheung 827725c
Remove comments
raymondkfcheung b7c2992
Update from github-actions[bot] running command 'bench --pallet palle…
github-actions[bot] 1461d11
Add placeholder for weight
raymondkfcheung 6562e6b
Merge remote-tracking branch 'origin/ray-fix-worst-case' into ray-fix…
raymondkfcheung d35cc0a
Merge branch 'master' into ray-fix-worst-case
raymondkfcheung 9780a89
Update cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs
raymondkfcheung 5486afd
Update cumulus/parachains/runtimes/assets/asset-hub-westend/src/lib.rs
raymondkfcheung 1f0b7ad
Update 1:1
raymondkfcheung 39a1a76
Merge remote-tracking branch 'origin/ray-fix-worst-case' into ray-fix…
raymondkfcheung 3778d76
Update format
raymondkfcheung f0a235e
Remove clone
raymondkfcheung 7651c63
Revert change
raymondkfcheung 7603169
Update lib.rs
raymondkfcheung 0e8612f
Update lib.rs
raymondkfcheung 1a228a6
Add debug logs
raymondkfcheung d73c4d4
Update from github-actions[bot] running command 'bench --pallet palle…
github-actions[bot] 4cc5a61
Merge remote-tracking branch 'origin/ray-fix-worst-case' into ray-fix…
raymondkfcheung b9ed27f
Try to fix
raymondkfcheung b9d03b9
Try to fix #2
raymondkfcheung 7ccf2a7
Try to fix #3
raymondkfcheung 0918335
Try to fix #4
raymondkfcheung 816318f
Update from github-actions[bot] running command 'bench --pallet palle…
github-actions[bot] 9f014d5
Nits
bkontur 259eb64
Merge branch 'master' into ray-fix-worst-case
raymondkfcheung 3ab0af6
Use assert
raymondkfcheung 7d8b94c
Merge branch 'master' into ray-fix-worst-case
raymondkfcheung ed6e2b9
Fix fmt
raymondkfcheung 0973019
Add more logs
raymondkfcheung b617e1a
Make it pass
raymondkfcheung 1062900
Make it pass v2
raymondkfcheung 5da20d2
Merge branch 'master' into ray-fix-worst-case
raymondkfcheung 27755de
Add tracing
raymondkfcheung e31db53
Remove tracing
raymondkfcheung 04a0dfe
Comment exchange_asset
raymondkfcheung d938368
Update from github-actions[bot] running command 'bench --pallet palle…
github-actions[bot] 8e0bc75
Comment assert_eq only
raymondkfcheung d00fcf5
Merge branch 'master' into ray-fix-worst-case
raymondkfcheung e589021
Uncomment
raymondkfcheung 409e5e8
Fix ratio
raymondkfcheung 77bb4fc
Uncomment assert_eq
raymondkfcheung 3164a77
Use contains
raymondkfcheung 4ba3627
Remove logs
raymondkfcheung 6c7f1c3
Use BenchmarkError
raymondkfcheung ba6d264
Update from github-actions[bot] running command 'bench --pallet palle…
github-actions[bot] e13dca2
Merge branch 'master' into ray-fix-worst-case
raymondkfcheung e891567
Update from github-actions[bot] running command 'bench --pallet palle…
github-actions[bot] 04cb06a
Use fixed weight
raymondkfcheung bca15f5
Update from github-actions[bot] running command 'bench --pallet palle…
github-actions[bot] 89a33bc
Add test placeholder
raymondkfcheung 1d1c384
Revert "Use fixed weight"
raymondkfcheung afd9fc1
Update from github-actions[bot] running command 'bench --pallet palle…
github-actions[bot] bbbc8f1
Use max(give, receive)
raymondkfcheung 17aa6d6
Remove unused import
raymondkfcheung 22f73ce
Simplify the code
raymondkfcheung 9eba60e
Merge branch 'master' into ray-fix-worst-case
raymondkfcheung f60ce0b
Use BenchmarkError
raymondkfcheung 6b846d1
Remove comments
raymondkfcheung 791a851
Add tests
raymondkfcheung ed7607b
Fix tests
raymondkfcheung c8173e4
Fix tests v2
raymondkfcheung 5b50223
Use assert_ok
raymondkfcheung dfd0975
Merge branch 'master' into ray-fix-worst-case
raymondkfcheung 443c2e0
Update PRDoc
raymondkfcheung aec4409
Fix test
raymondkfcheung dbaf905
Fix test
raymondkfcheung 7da94cd
Fix test
raymondkfcheung 80c410d
Merge branch 'master' into ray-fix-worst-case
raymondkfcheung 11c91b5
Fix fmt
raymondkfcheung 7c0e1be
Fix test
raymondkfcheung c13e62b
Merge branch 'master' into ray-fix-worst-case
raymondkfcheung ab2bc50
Fix test
raymondkfcheung aac1a42
Fix test
raymondkfcheung 861c026
Merge branch 'master' into ray-fix-worst-case
raymondkfcheung 318c692
Merge branch 'master' into ray-fix-worst-case
raymondkfcheung 58b3365
Merge branch 'master' into ray-fix-worst-case
raymondkfcheung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
...ins/integration-tests/emulated/tests/assets/asset-hub-westend/src/tests/exchange_asset.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| // Copyright (C) Parity Technologies (UK) Ltd. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use crate::{ | ||
| create_pool_with_wnd_on, | ||
| imports::{ | ||
| asset_hub_westend_runtime::{ExistentialDeposit, Runtime}, | ||
| *, | ||
| }, | ||
| }; | ||
| use asset_hub_westend_runtime::{ | ||
| xcm_config::WestendLocation, Balances, ForeignAssets, PolkadotXcm, RuntimeOrigin, | ||
| }; | ||
| use emulated_integration_tests_common::{accounts::ALICE, xcm_emulator::TestExt}; | ||
| use frame_support::{ | ||
| assert_err_ignore_postinfo, assert_ok, | ||
| traits::fungible::{Inspect, Mutate}, | ||
| }; | ||
| use parachains_common::{AccountId, Balance}; | ||
| use sp_tracing::capture_test_logs; | ||
| use std::convert::Into; | ||
| use xcm::latest::{Assets, Location, Xcm}; | ||
|
|
||
| const UNITS: Balance = 1_000_000_000; | ||
|
|
||
| #[test] | ||
| fn exchange_asset_success() { | ||
| test_exchange_asset(true, 500 * UNITS, 665 * UNITS, true); | ||
| } | ||
|
|
||
| #[test] | ||
| fn exchange_asset_insufficient_liquidity() { | ||
| let log_capture = capture_test_logs!({ | ||
| test_exchange_asset(true, 1_000 * UNITS, 2_000 * UNITS, false); | ||
| }); | ||
| assert!(log_capture.contains("NoDeal")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn exchange_asset_insufficient_balance() { | ||
| let log_capture = capture_test_logs!({ | ||
| test_exchange_asset(true, 5_000 * UNITS, 1_665 * UNITS, false); | ||
| }); | ||
| assert!(log_capture.contains("Funds are unavailable")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn exchange_asset_pool_not_created() { | ||
| test_exchange_asset(false, 500 * UNITS, 665 * UNITS, false); | ||
| } | ||
|
|
||
| fn test_exchange_asset( | ||
| create_pool: bool, | ||
| give_amount: Balance, | ||
| want_amount: Balance, | ||
| should_succeed: bool, | ||
| ) { | ||
| let alice: AccountId = Westend::account_id_of(ALICE); | ||
| let native_asset_location = WestendLocation::get(); | ||
| let native_asset_id = AssetId(native_asset_location.clone()); | ||
| let origin = RuntimeOrigin::signed(alice.clone()); | ||
| let asset_location = Location::new(1, [Parachain(2001)]); | ||
| let asset_id = AssetId(asset_location.clone()); | ||
|
|
||
| // Setup initial state | ||
| AssetHubWestend::execute_with(|| { | ||
| assert_ok!(<Balances as Mutate<_>>::mint_into( | ||
| &alice, | ||
| ExistentialDeposit::get() + (1_000 * UNITS) | ||
| )); | ||
|
|
||
| assert_ok!(ForeignAssets::force_create( | ||
| RuntimeOrigin::root(), | ||
| asset_location.clone().into(), | ||
| alice.clone().into(), | ||
| true, | ||
| 1 | ||
| )); | ||
| }); | ||
|
|
||
| if create_pool { | ||
| create_pool_with_wnd_on!(AssetHubWestend, asset_location.clone(), true, alice.clone()); | ||
| } | ||
|
|
||
| // Execute and verify swap | ||
| AssetHubWestend::execute_with(|| { | ||
| let foreign_balance_before = ForeignAssets::balance(asset_location.clone(), &alice); | ||
| let wnd_balance_before = Balances::total_balance(&alice); | ||
|
|
||
| let give: Assets = (native_asset_id, give_amount).into(); | ||
| let want: Assets = (asset_id, want_amount).into(); | ||
| let xcm = Xcm(vec![ | ||
| WithdrawAsset(give.clone().into()), | ||
| ExchangeAsset { give: give.into(), want: want.into(), maximal: true }, | ||
| DepositAsset { assets: Wild(All), beneficiary: alice.clone().into() }, | ||
| ]); | ||
|
|
||
| let result = PolkadotXcm::execute(origin, bx!(xcm::VersionedXcm::from(xcm)), Weight::MAX); | ||
|
|
||
| let foreign_balance_after = ForeignAssets::balance(asset_location, &alice); | ||
| let wnd_balance_after = Balances::total_balance(&alice); | ||
|
|
||
| if should_succeed { | ||
| assert_ok!(result); | ||
| assert!( | ||
| foreign_balance_after >= foreign_balance_before + want_amount, | ||
| "Expected foreign balance to increase by at least {want_amount} units, got {foreign_balance_after} from {foreign_balance_before}" | ||
| ); | ||
| assert_eq!( | ||
| wnd_balance_after, wnd_balance_before - give_amount, | ||
| "Expected WND balance to decrease by {give_amount} units, got {wnd_balance_after} from {wnd_balance_before}" | ||
| ); | ||
| } else { | ||
| assert_err_ignore_postinfo!( | ||
| result, | ||
| pallet_xcm::Error::<Runtime>::LocalExecutionIncomplete | ||
| ); | ||
| assert_eq!( | ||
| foreign_balance_after, foreign_balance_before, | ||
| "Foreign balance changed unexpectedly: got {foreign_balance_after}, expected {foreign_balance_before}" | ||
| ); | ||
| assert_eq!( | ||
| wnd_balance_after, wnd_balance_before, | ||
| "WND balance changed unexpectedly: got {wnd_balance_after}, expected {wnd_balance_before}" | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this tests interact just with AssetHubWestend runtimes, so they could be added as unit-tests directly to the AssetHubWestend runtime module - for that we have also
asset-test-utilswith predefined test-cases, co we don't need to copy those tests.For emulated, I would expect here some cross-chain/runtimes interaction - e.g. Penpal wants to exchange assets on AssetHub and pay fees there or something like that.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I'll do it on the follow-up PR #8074.