This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Make Pay trait from salaries pallet more generic #13609
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
41d89e9
Make Pay trait from salaries pallet more generic
franciscoaguirre b0c04d6
Rename and add missing
franciscoaguirre e7e35c0
Update frame/support/src/traits/tokens/pay.rs
gavofyork 9ab2699
Update pay.rs
gavofyork e16422b
Update pay.rs
gavofyork 3662f36
Update pay.rs
gavofyork 37985b4
Add better documentation for the AssetKind associated type
franciscoaguirre 5a1baf4
Merge remote-tracking branch 'origin/master' into cisco-pay-trait-gen…
ebb2a39
Merge remote-tracking branch 'origin/master' into cisco-pay-trait-gen…
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
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
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,97 @@ | ||
| // This file is part of Substrate. | ||
|
|
||
| // 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. | ||
|
|
||
| //! The Pay trait and associated types. | ||
|
|
||
| use codec::{Decode, Encode, FullCodec, MaxEncodedLen}; | ||
| use scale_info::TypeInfo; | ||
| use sp_core::{RuntimeDebug, TypedGet}; | ||
| use sp_std::fmt::Debug; | ||
|
|
||
| use super::{fungible, Balance}; | ||
|
|
||
| /// Can be implemented by `PayFromAccount` using a `fungible` impl, but can also be implemented with | ||
| /// XCM/MultiAsset and made generic over assets. | ||
| pub trait Pay { | ||
| /// The type by which we measure units of the currency in which we make payments. | ||
| type Balance: Balance; | ||
| /// The type by which we identify the beneficiaries to whom a payment may be made. | ||
| type Beneficiary; | ||
| /// The kind of asset that is going to be paid, `()` for native asset | ||
| type AssetKind; | ||
| /// An identifier given to an individual payment. | ||
| type Id: FullCodec + MaxEncodedLen + TypeInfo + Clone + Eq + PartialEq + Debug + Copy; | ||
| /// Make a payment and return an identifier for later evaluation of success in some off-chain | ||
| /// mechanism (likely an event, but possibly not on this chain). | ||
| fn pay( | ||
| who: &Self::Beneficiary, | ||
| asset_kind: Self::AssetKind, | ||
| amount: Self::Balance, | ||
| ) -> Result<Self::Id, ()>; | ||
| /// Check how a payment has proceeded. `id` must have been previously returned by `pay` for | ||
| /// the result of this call to be meaningful. | ||
| fn check_payment(id: Self::Id) -> PaymentStatus; | ||
| /// Ensure that a call to pay with the given parameters will be successful if done immediately | ||
| /// after this call. Used in benchmarking code. | ||
| #[cfg(feature = "runtime-benchmarks")] | ||
| fn ensure_successful(who: &Self::Beneficiary, amount: Self::Balance); | ||
| /// Ensure that a call to `check_payment` with the given parameters will return either `Success` | ||
| /// or `Failure`. | ||
| #[cfg(feature = "runtime-benchmarks")] | ||
| fn ensure_concluded(id: Self::Id); | ||
| } | ||
|
|
||
| /// Status for making a payment via the `Pay::pay` trait function. | ||
| #[derive(Encode, Decode, Eq, PartialEq, Clone, TypeInfo, MaxEncodedLen, RuntimeDebug)] | ||
| pub enum PaymentStatus { | ||
| /// Payment is in progress. Nothing to report yet. | ||
| InProgress, | ||
| /// Payment status is unknowable. It will never be reported successful or failed. | ||
| Unknown, | ||
| /// Payment happened successfully. | ||
| Success, | ||
| /// Payment failed. It may safely be retried. | ||
| Failure, | ||
| } | ||
|
|
||
| /// Simple implementation of `Pay` which makes a payment from a "pot" - i.e. a single account. | ||
| pub struct PayFromAccount<F, A>(sp_std::marker::PhantomData<(F, A)>); | ||
| impl<A: TypedGet, F: fungible::Transfer<A::Type> + fungible::Mutate<A::Type>> Pay | ||
| for PayFromAccount<F, A> | ||
| { | ||
| type Balance = F::Balance; | ||
| type Beneficiary = A::Type; | ||
| type AssetKind = (); | ||
| type Id = (); | ||
| fn pay( | ||
| who: &Self::Beneficiary, | ||
| _: Self::AssetKind, | ||
| amount: Self::Balance, | ||
| ) -> Result<Self::Id, ()> { | ||
| <F as fungible::Transfer<_>>::transfer(&A::get(), who, amount, false).map_err(|_| ())?; | ||
| Ok(()) | ||
| } | ||
| fn check_payment(_: ()) -> PaymentStatus { | ||
| PaymentStatus::Success | ||
| } | ||
| #[cfg(feature = "runtime-benchmarks")] | ||
| fn ensure_successful(_: &Self::Beneficiary, amount: Self::Balance) { | ||
| <F as fungible::Mutate<_>>::mint_into(&A::get(), amount).unwrap(); | ||
| } | ||
| #[cfg(feature = "runtime-benchmarks")] | ||
| fn ensure_concluded(_: Self::Id) {} | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.