1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (C) 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::convert::Infallible;
use xcm::prelude::*;

#[derive(Debug)]
pub enum LockError {
	NotApplicable,
	WouldClobber,
	BadOrigin,
	NotLocked,
	NotEnoughLocked,
	Unimplemented,
	NotTrusted,
	BadOwner,
	UnknownAsset,
	AssetNotOwned,
	NoResources,
	UnexpectedState,
	InUse,
}

impl From<LockError> for XcmError {
	fn from(e: LockError) -> XcmError {
		use LockError::*;
		match e {
			NotApplicable => XcmError::AssetNotFound,
			BadOrigin => XcmError::BadOrigin,
			WouldClobber | NotLocked | NotEnoughLocked | Unimplemented | NotTrusted |
			BadOwner | UnknownAsset | AssetNotOwned | NoResources | UnexpectedState | InUse =>
				XcmError::LockError,
		}
	}
}

pub trait Enact {
	/// Enact a lock. This should generally be infallible if called immediately after being
	/// received.
	fn enact(self) -> Result<(), LockError>;
}

impl Enact for Infallible {
	fn enact(self) -> Result<(), LockError> {
		unreachable!()
	}
}

/// Define a handler for notification of an asset being locked and for the unlock instruction.
pub trait AssetLock {
	/// `Enact` implementer for `prepare_lock`. This type may be dropped safely to avoid doing the
	/// lock.
	type LockTicket: Enact;

	/// `Enact` implementer for `prepare_unlock`. This type may be dropped safely to avoid doing the
	/// unlock.
	type UnlockTicket: Enact;

	/// `Enact` implementer for `prepare_reduce_unlockable`. This type may be dropped safely to
	/// avoid doing the unlock.
	type ReduceTicket: Enact;

	/// Prepare to lock an asset. On success, a `Self::LockTicket` it returned, which can be used
	/// to actually enact the lock.
	///
	/// WARNING: Don't call this with an undropped instance of `Self::LockTicket` or
	/// `Self::UnlockTicket`.
	fn prepare_lock(
		unlocker: MultiLocation,
		asset: MultiAsset,
		owner: MultiLocation,
	) -> Result<Self::LockTicket, LockError>;

	/// Prepare to unlock an asset. On success, a `Self::UnlockTicket` it returned, which can be
	/// used to actually enact the lock.
	///
	/// WARNING: Don't call this with an undropped instance of `Self::LockTicket` or
	/// `Self::UnlockTicket`.
	fn prepare_unlock(
		locker: MultiLocation,
		asset: MultiAsset,
		owner: MultiLocation,
	) -> Result<Self::UnlockTicket, LockError>;

	/// Handler for when a location reports to us that an asset has been locked for us to unlock
	/// at a later stage.
	///
	/// If there is no way to handle the lock report, then this should return an error so that the
	/// sending chain can ensure the lock does not remain.
	///
	/// We should only act upon this message if we believe that the `origin` is honest.
	fn note_unlockable(
		locker: MultiLocation,
		asset: MultiAsset,
		owner: MultiLocation,
	) -> Result<(), LockError>;

	/// Handler for when an owner wishes to unlock an asset on a remote chain.
	///
	/// Returns a ticket which can be used to actually note the reduction in unlockable assets that
	/// `owner` commands on `locker`.
	///
	/// WARNING: Don't call this with an undropped instance of `Self::ReduceTicket`.
	fn prepare_reduce_unlockable(
		locker: MultiLocation,
		asset: MultiAsset,
		owner: MultiLocation,
	) -> Result<Self::ReduceTicket, LockError>;
}

impl AssetLock for () {
	type LockTicket = Infallible;
	type UnlockTicket = Infallible;
	type ReduceTicket = Infallible;
	fn prepare_lock(
		_: MultiLocation,
		_: MultiAsset,
		_: MultiLocation,
	) -> Result<Self::LockTicket, LockError> {
		Err(LockError::NotApplicable)
	}
	fn prepare_unlock(
		_: MultiLocation,
		_: MultiAsset,
		_: MultiLocation,
	) -> Result<Self::UnlockTicket, LockError> {
		Err(LockError::NotApplicable)
	}
	fn note_unlockable(_: MultiLocation, _: MultiAsset, _: MultiLocation) -> Result<(), LockError> {
		Err(LockError::NotApplicable)
	}
	fn prepare_reduce_unlockable(
		_: MultiLocation,
		_: MultiAsset,
		_: MultiLocation,
	) -> Result<Self::ReduceTicket, LockError> {
		Err(LockError::NotApplicable)
	}
}