-
Notifications
You must be signed in to change notification settings - Fork 743
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Treasury spends various asset kinds (#1333)
### Summary This PR introduces new dispatchables to the treasury pallet, allowing spends of various asset types. The enhanced features of the treasury pallet, in conjunction with the asset-rate pallet, are set up and enabled for Westend and Rococo. ### Westend and Rococo runtimes. Polkadot/Kusams/Rococo Treasury can accept proposals for `spends` of various asset kinds by specifying the asset's location and ID. #### Treasury Instance New Dispatchables: - `spend(AssetKind, AssetBalance, Beneficiary, Option<ValidFrom>)` - propose and approve a spend; - `payout(SpendIndex)` - payout an approved spend or retry a failed payout - `check_payment(SpendIndex)` - check the status of a payout; - `void_spend(SpendIndex)` - void previously approved spend; > existing spend dispatchable renamed to spend_local in this context, the `AssetKind` parameter contains the asset's location and it's corresponding `asset_id`, for example: `USDT` on `AssetHub`, ``` rust location = MultiLocation(0, X1(Parachain(1000))) asset_id = MultiLocation(0, X2(PalletInstance(50), GeneralIndex(1984))) ``` the `Beneficiary` parameter is a `MultiLocation` in the context of the asset's location, for example ``` rust // the Fellowship salary pallet's location / account FellowshipSalaryPallet = MultiLocation(1, X2(Parachain(1001), PalletInstance(64))) // or custom `AccountId` Alice = MultiLocation(0, AccountId32(network: None, id: [1,...])) ``` the `AssetBalance` represents the amount of the `AssetKind` to be transferred to the `Beneficiary`. For permission checks, the asset amount is converted to the native amount and compared against the maximum spendable amount determined by the commanding spend origin. the `spend` dispatchable allows for batching spends with different `ValidFrom` arguments, enabling milestone-based spending. If the expectations tied to an approved spend are not met, it is possible to void the spend later using the `void_spend` dispatchable. Asset Rate Pallet provides the conversion rate from the `AssetKind` to the native balance. #### Asset Rate Instance Dispatchables: - `create(AssetKind, Rate)` - initialize a conversion rate to the native balance for the given asset - `update(AssetKind, Rate)` - update the conversion rate to the native balance for the given asset - `remove(AssetKind)` - remove an existing conversion rate to the native balance for the given asset the pallet's dispatchables can be executed by the Root or Treasurer origins. ### Treasury Pallet Treasury Pallet can accept proposals for `spends` of various asset kinds and pay them out through the implementation of the `Pay` trait. New Dispatchables: - `spend(Config::AssetKind, AssetBalance, Config::Beneficiary, Option<ValidFrom>)` - propose and approve a spend; - `payout(SpendIndex)` - payout an approved spend or retry a failed payout; - `check_payment(SpendIndex)` - check the status of a payout; - `void_spend(SpendIndex)` - void previously approved spend; > existing spend dispatchable renamed to spend_local The parameters' types of the `spend` dispatchable exposed via the pallet's `Config` and allows to propose and accept a spend of a certain amount. An approved spend can be claimed via the `payout` within the `Config::SpendPeriod`. Clients provide an implementation of the `Pay` trait which can pay an asset of the `AssetKind` to the `Beneficiary` in `AssetBalance` units. The implementation of the Pay trait might not have an immediate final payment status, for example if implemented over `XCM` and the actual transfer happens on a remote chain. The `check_status` dispatchable can be executed to update the spend's payment state and retry the `payout` if the payment has failed. --------- Co-authored-by: joe petrowski <[email protected]> Co-authored-by: command-bot <>
- Loading branch information
1 parent
5a69126
commit cb944dc
Showing
32 changed files
with
2,212 additions
and
285 deletions.
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 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 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 |
---|---|---|
|
@@ -18,3 +18,4 @@ mod send; | |
mod set_xcm_versions; | ||
mod swap; | ||
mod teleport; | ||
mod treasury; |
126 changes: 126 additions & 0 deletions
126
cumulus/parachains/integration-tests/emulated/assets/asset-hub-westend/src/tests/treasury.rs
This file contains 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,126 @@ | ||
// 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::*; | ||
use frame_support::traits::fungibles::{Create, Inspect, Mutate}; | ||
use integration_tests_common::constants::accounts::{ALICE, BOB}; | ||
use polkadot_runtime_common::impls::VersionedLocatableAsset; | ||
use xcm_executor::traits::ConvertLocation; | ||
|
||
#[test] | ||
fn create_and_claim_treasury_spend() { | ||
const ASSET_ID: u32 = 1984; | ||
const SPEND_AMOUNT: u128 = 1_000_000; | ||
// treasury location from a sibling parachain. | ||
let treasury_location: MultiLocation = MultiLocation::new(1, PalletInstance(37)); | ||
// treasury account on a sibling parachain. | ||
let treasury_account = | ||
asset_hub_westend_runtime::xcm_config::LocationToAccountId::convert_location( | ||
&treasury_location, | ||
) | ||
.unwrap(); | ||
let asset_hub_location = MultiLocation::new(0, Parachain(AssetHubWestend::para_id().into())); | ||
let root = <Westend as Chain>::RuntimeOrigin::root(); | ||
// asset kind to be spend from the treasury. | ||
let asset_kind = VersionedLocatableAsset::V3 { | ||
location: asset_hub_location, | ||
asset_id: AssetId::Concrete((PalletInstance(50), GeneralIndex(ASSET_ID.into())).into()), | ||
}; | ||
// treasury spend beneficiary. | ||
let alice: AccountId = Westend::account_id_of(ALICE); | ||
let bob: AccountId = Westend::account_id_of(BOB); | ||
let bob_signed = <Westend as Chain>::RuntimeOrigin::signed(bob.clone()); | ||
|
||
AssetHubWestend::execute_with(|| { | ||
type Assets = <AssetHubWestend as AssetHubWestendPallet>::Assets; | ||
|
||
// create an asset class and mint some assets to the treasury account. | ||
assert_ok!(<Assets as Create<_>>::create( | ||
ASSET_ID, | ||
treasury_account.clone(), | ||
true, | ||
SPEND_AMOUNT / 2 | ||
)); | ||
assert_ok!(<Assets as Mutate<_>>::mint_into(ASSET_ID, &treasury_account, SPEND_AMOUNT * 4)); | ||
// beneficiary has zero balance. | ||
assert_eq!(<Assets as Inspect<_>>::balance(ASSET_ID, &alice,), 0u128,); | ||
}); | ||
|
||
Westend::execute_with(|| { | ||
type RuntimeEvent = <Westend as Chain>::RuntimeEvent; | ||
type Treasury = <Westend as WestendPallet>::Treasury; | ||
type AssetRate = <Westend as WestendPallet>::AssetRate; | ||
|
||
// create a conversion rate from `asset_kind` to the native currency. | ||
assert_ok!(AssetRate::create(root.clone(), Box::new(asset_kind.clone()), 2.into())); | ||
|
||
// create and approve a treasury spend. | ||
assert_ok!(Treasury::spend( | ||
root, | ||
Box::new(asset_kind), | ||
SPEND_AMOUNT, | ||
Box::new(MultiLocation::new(0, Into::<[u8; 32]>::into(alice.clone())).into()), | ||
None, | ||
)); | ||
// claim the spend. | ||
assert_ok!(Treasury::payout(bob_signed.clone(), 0)); | ||
|
||
assert_expected_events!( | ||
Westend, | ||
vec![ | ||
RuntimeEvent::Treasury(pallet_treasury::Event::Paid { .. }) => {}, | ||
] | ||
); | ||
}); | ||
|
||
AssetHubWestend::execute_with(|| { | ||
type RuntimeEvent = <AssetHubWestend as Chain>::RuntimeEvent; | ||
type Assets = <AssetHubWestend as AssetHubWestendPallet>::Assets; | ||
|
||
// assert events triggered by xcm pay program | ||
// 1. treasury asset transferred to spend beneficiary | ||
// 2. response to Relay Chain treasury pallet instance sent back | ||
// 3. XCM program completed | ||
assert_expected_events!( | ||
AssetHubWestend, | ||
vec![ | ||
RuntimeEvent::Assets(pallet_assets::Event::Transferred { asset_id: id, from, to, amount }) => { | ||
id: id == &ASSET_ID, | ||
from: from == &treasury_account, | ||
to: to == &alice, | ||
amount: amount == &SPEND_AMOUNT, | ||
}, | ||
RuntimeEvent::ParachainSystem(cumulus_pallet_parachain_system::Event::UpwardMessageSent { .. }) => {}, | ||
RuntimeEvent::DmpQueue(cumulus_pallet_dmp_queue::Event::ExecutedDownward { outcome: Outcome::Complete(..) ,.. }) => {}, | ||
] | ||
); | ||
// beneficiary received the assets from the treasury. | ||
assert_eq!(<Assets as Inspect<_>>::balance(ASSET_ID, &alice,), SPEND_AMOUNT,); | ||
}); | ||
|
||
Westend::execute_with(|| { | ||
type RuntimeEvent = <Westend as Chain>::RuntimeEvent; | ||
type Treasury = <Westend as WestendPallet>::Treasury; | ||
|
||
// check the payment status to ensure the response from the AssetHub was received. | ||
assert_ok!(Treasury::check_status(bob_signed, 0)); | ||
assert_expected_events!( | ||
Westend, | ||
vec![ | ||
RuntimeEvent::Treasury(pallet_treasury::Event::SpendProcessed { .. }) => {}, | ||
] | ||
); | ||
}); | ||
} |
This file contains 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 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 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.