Skip to main content

topsoil_core/traits/tokens/
transfer.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: Apache-2.0 OR GPL-3.0-or-later WITH Classpath-exception-2.0
6
7//! The transfer trait and associated types
8
9use crate::pallet_prelude::{Decode, Encode};
10use core::fmt::Debug;
11use scale_info::TypeInfo;
12use subsoil::runtime::codec::{FullCodec, MaxEncodedLen};
13use topsoil_core::traits::tokens::PaymentStatus;
14
15/// Is intended to be implemented using a `fungible` impl, but can also be implemented with
16/// XCM/Asset and made generic over assets.
17///
18/// It is similar to the `topsoil_core::traits::tokens::Pay`, but it offers a variable source
19/// account for the payment.
20pub trait Transfer {
21	/// The type by which we measure units of the currency in which we make payments.
22	type Balance;
23	/// The type by which identify the payer involved in the transfer.
24	///
25	/// This is usually and AccountId or a Location.
26	type Sender;
27
28	/// The type by which we identify the beneficiary involved in the transfer.
29	///
30	/// This is usually and AccountId or a Location.
31	type Beneficiary;
32
33	/// The type for the kinds of asset that are going to be paid.
34	///
35	/// The unit type can be used here to indicate there's only one kind of asset to do payments
36	/// with. When implementing, it should be clear from the context what that asset is.
37	type AssetKind;
38
39	/// Asset that is used to pay the xcm execution fees on the remote chain.
40	type RemoteFeeAsset;
41	/// An identifier given to an individual payment.
42	type Id: FullCodec + MaxEncodedLen + TypeInfo + Clone + Eq + PartialEq + Debug + Copy;
43	/// An error which could be returned by the Pay type
44	type Error: Debug;
45	/// Make a payment and return an identifier for later evaluation of success in some off-chain
46	/// mechanism (likely an event, but possibly not on this chain).
47	fn transfer(
48		from: &Self::Sender,
49		to: &Self::Beneficiary,
50		asset_kind: Self::AssetKind,
51		amount: Self::Balance,
52		remote_fee: Option<Self::RemoteFeeAsset>,
53	) -> Result<Self::Id, Self::Error>;
54
55	/// Check how a payment has proceeded. `id` must have been previously returned by `pay` for
56	/// the result of this call to be meaningful.
57	fn check_transfer(id: Self::Id) -> PaymentStatus;
58	/// Ensure that a call to pay with the given parameters will be successful if done immediately
59	/// after this call. Used in benchmarking code.
60	#[cfg(feature = "runtime-benchmarks")]
61	fn ensure_successful(
62		to: &Self::Beneficiary,
63		asset_kind: Self::AssetKind,
64		amount: Self::Balance,
65	);
66	/// Ensure that a call to `check_payment` with the given parameters will return either `Success`
67	/// or `Failure`.
68	#[cfg(feature = "runtime-benchmarks")]
69	fn ensure_concluded(id: Self::Id);
70}
71
72/// Status for making a payment via the `Pay::pay` trait function.
73#[derive(Encode, Decode, Eq, PartialEq, Clone, TypeInfo, MaxEncodedLen, Debug)]
74pub enum TransferStatus {
75	/// Payment is in progress. Nothing to report yet.
76	InProgress,
77	/// Payment status is unknowable. It may already have reported the result, or if not then
78	/// it will never be reported successful or failed.
79	Unknown,
80	/// Payment happened successfully.
81	Success,
82	/// Payment failed. It may safely be retried.
83	Failure,
84}