transact_asset.rs 6.81 KB
Newer Older
Gavin Wood's avatar
Gavin Wood committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.

use sp_std::result::Result;
use xcm::v0::{Error as XcmError, Result as XcmResult, MultiAsset, MultiLocation};
use crate::Assets;

/// Facility for asset transacting.
///
Alexander Popiak's avatar
Alexander Popiak committed
23
24
25
26
27
/// This should work with as many asset/location combinations as possible. Locations to support may include non-account
/// locations such as a `MultiLocation::X1(Junction::Parachain)`. Different chains may handle them in different ways.
///
/// Can be amalgamated as a tuple of items that implement this trait. In such executions, if any of the transactors
/// returns `Ok(())`, then it will short circuit. Else, execution is passed to the next transactor.
Gavin Wood's avatar
Gavin Wood committed
28
pub trait TransactAsset {
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
	/// Ensure that `check_in` will result in `Ok`.
	///
	/// When composed as a tuple, all type-items are called and at least one must result in `Ok`.
	fn can_check_in(_origin: &MultiLocation, _what: &MultiAsset) -> XcmResult {
		Err(XcmError::Unimplemented)
	}

	/// An asset has been teleported in from the given origin. This should do whatever housekeeping is needed.
	///
	/// NOTE: This will make only a best-effort at bookkeeping. The caller should ensure that `can_check_in` has
	/// returned with `Ok` in order to guarantee that this operation proceeds properly.
	///
	/// Implementation note: In general this will do one of two things: On chains where the asset is native,
	/// it will reduce the assets from a special "teleported" account so that a) total-issuance is preserved;
	/// and b) to ensure that no more assets can be teleported in than were teleported out overall (this should
	/// not be needed if the teleporting chains are to be trusted, but better to be safe than sorry). On chains
	/// where the asset is not native then it will generally just be a no-op.
	///
Denis_P's avatar
Denis_P committed
47
	/// When composed as a tuple, all type-items are called. It is up to the implementer that there exists no
48
49
50
51
52
53
54
55
56
57
58
	/// value for `_what` which can cause side-effects for more than one of the type-items.
	fn check_in(_origin: &MultiLocation, _what: &MultiAsset) {}

	/// An asset has been teleported out to the given destination. This should do whatever housekeeping is needed.
	///
	/// Implementation note: In general this will do one of two things: On chains where the asset is native,
	/// it will increase the assets in a special "teleported" account so that a) total-issuance is preserved; and
	/// b) to ensure that no more assets can be teleported in than were teleported out overall (this should not
	/// be needed if the teleporting chains are to be trusted, but better to be safe than sorry). On chains where
	/// the asset is not native then it will generally just be a no-op.
	///
Denis_P's avatar
Denis_P committed
59
	/// When composed as a tuple, all type-items are called. It is up to the implementer that there exists no
60
61
62
	/// value for `_what` which can cause side-effects for more than one of the type-items.
	fn check_out(_origin: &MultiLocation, _what: &MultiAsset) {}

Gavin Wood's avatar
Gavin Wood committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
	/// Deposit the `what` asset into the account of `who`.
	///
	/// Implementations should return `XcmError::FailedToTransactAsset` if deposit failed.
	fn deposit_asset(_what: &MultiAsset, _who: &MultiLocation) -> XcmResult {
		Err(XcmError::Unimplemented)
	}

	/// Withdraw the given asset from the consensus system. Return the actual asset(s) withdrawn. In
	/// the case of `what` being a wildcard, this may be something more specific.
	///
	/// Implementations should return `XcmError::FailedToTransactAsset` if withdraw failed.
	fn withdraw_asset(_what: &MultiAsset, _who: &MultiLocation) -> Result<Assets, XcmError> {
		Err(XcmError::Unimplemented)
	}

	/// Move an `asset` `from` one location in `to` another location.
	///
	/// Returns `XcmError::FailedToTransactAsset` if transfer failed.
	fn transfer_asset(_asset: &MultiAsset, _from: &MultiLocation, _to: &MultiLocation) -> Result<Assets, XcmError> {
		Err(XcmError::Unimplemented)
	}

	/// Move an `asset` `from` one location in `to` another location.
	///
	/// Attempts to use `transfer_asset` and if not available then falls back to using a two-part withdraw/deposit.
	fn teleport_asset(asset: &MultiAsset, from: &MultiLocation, to: &MultiLocation) -> Result<Assets, XcmError> {
		match Self::transfer_asset(asset, from, to) {
			Err(XcmError::Unimplemented) => {
				let assets = Self::withdraw_asset(asset, from)?;
				// Not a very forgiving attitude; once we implement roll-backs then it'll be nicer.
				Self::deposit_asset(asset, to)?;
				Ok(assets)
			}
			result => result
		}
	}
}

#[impl_trait_for_tuples::impl_for_tuples(30)]
impl TransactAsset for Tuple {
103
104
105
106
107
108
109
	fn can_check_in(origin: &MultiLocation, what: &MultiAsset) -> XcmResult {
		for_tuples!( #(
			match Tuple::can_check_in(origin, what) {
				Err(XcmError::AssetNotFound) => (),
				r => return r,
			}
		)* );
Alexander Popiak's avatar
Alexander Popiak committed
110
111
112
113
114
115
		log::trace!(
			target: "xcm::TransactAsset::can_check_in",
			"asset not found: what: {:?}, origin: {:?}",
			what,
			origin,
		);
116
117
		Err(XcmError::AssetNotFound)
	}
Alexander Popiak's avatar
Alexander Popiak committed
118

119
120
121
122
123
	fn check_in(origin: &MultiLocation, what: &MultiAsset) {
		for_tuples!( #(
			Tuple::check_in(origin, what);
		)* );
	}
Alexander Popiak's avatar
Alexander Popiak committed
124

125
126
127
128
129
	fn check_out(dest: &MultiLocation, what: &MultiAsset) {
		for_tuples!( #(
			Tuple::check_out(dest, what);
		)* );
	}
Alexander Popiak's avatar
Alexander Popiak committed
130

Gavin Wood's avatar
Gavin Wood committed
131
132
133
134
	fn deposit_asset(what: &MultiAsset, who: &MultiLocation) -> XcmResult {
		for_tuples!( #(
			match Tuple::deposit_asset(what, who) { o @ Ok(_) => return o, _ => () }
		)* );
Alexander Popiak's avatar
Alexander Popiak committed
135
136
137
138
139
140
		log::trace!(
			target: "xcm::TransactAsset::deposit_asset",
			"did not deposit asset: what: {:?}, who: {:?}",
			what,
			who,
		);
Gavin Wood's avatar
Gavin Wood committed
141
142
		Err(XcmError::Unimplemented)
	}
Alexander Popiak's avatar
Alexander Popiak committed
143

Gavin Wood's avatar
Gavin Wood committed
144
145
146
147
	fn withdraw_asset(what: &MultiAsset, who: &MultiLocation) -> Result<Assets, XcmError> {
		for_tuples!( #(
			match Tuple::withdraw_asset(what, who) { o @ Ok(_) => return o, _ => () }
		)* );
Alexander Popiak's avatar
Alexander Popiak committed
148
149
150
151
152
153
		log::trace!(
			target: "xcm::TransactAsset::withdraw_asset",
			"did not withdraw asset: what: {:?}, who: {:?}",
			what, 
			who,
		);
Gavin Wood's avatar
Gavin Wood committed
154
155
		Err(XcmError::Unimplemented)
	}
Alexander Popiak's avatar
Alexander Popiak committed
156

Gavin Wood's avatar
Gavin Wood committed
157
158
159
160
	fn transfer_asset(what: &MultiAsset, from: &MultiLocation, to: &MultiLocation) -> Result<Assets, XcmError> {
		for_tuples!( #(
			match Tuple::transfer_asset(what, from, to) { o @ Ok(_) => return o, _ => () }
		)* );
Alexander Popiak's avatar
Alexander Popiak committed
161
162
163
164
165
166
167
		log::trace!(
			target: "xcm::TransactAsset::transfer_asset",
			"did not transfer asset: what: {:?}, from: {:?}, to: {:?}",
			what,
			from,
			to,
		);
Gavin Wood's avatar
Gavin Wood committed
168
169
170
		Err(XcmError::Unimplemented)
	}
}