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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2019-2022 PureStake Inc.
// This file is part of Moonbeam.

// Moonbeam 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.

// Moonbeam 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 Moonbeam.  If not, see <http://www.gnu.org/licenses/>.

//! Module that provides types to extend xcm holding.

use core::marker::PhantomData;
use sp_core::{H160, U256};
use sp_std::collections::btree_map::BTreeMap;
use sp_std::vec::Vec;
use xcm_executor::traits::XcmAssetTransfers;

environmental::environmental!(XCM_HOLDING_ERC20_ORIGINS: XcmHoldingErc20sOrigins);

#[cfg_attr(test, derive(PartialEq, Debug))]
pub(crate) enum DrainError {
	AssetNotFound,
	NotEnoughFounds,
	SplitError,
}

/// Xcm holding erc20 origins extension.
/// This extension track down the origin of alls erc20 tokens in the xcm holding.
#[derive(Default)]
pub(crate) struct XcmHoldingErc20sOrigins {
	map: BTreeMap<H160, Vec<(H160, U256)>>,
}
impl XcmHoldingErc20sOrigins {
	/// Take and remove a given amounts of erc20 tokens from the XCM holding.
	/// These tokens can come from one or more holders that we had tracked earlier in the XCM
	/// execution, so we return an array of (holder, balance).
	pub(crate) fn drain(
		&mut self,
		contract_address: H160,
		amount: U256,
	) -> Result<Vec<(H160, U256)>, DrainError> {
		let tokens_to_transfer = self.drain_inner(&contract_address, amount)?;

		self.map
			.entry(contract_address)
			.and_modify(|erc20_origins| {
				*erc20_origins = erc20_origins.split_off(tokens_to_transfer.len());
			});

		Ok(tokens_to_transfer)
	}
	fn drain_inner(
		&self,
		contract_address: &H160,
		mut amount: U256,
	) -> Result<Vec<(H160, U256)>, DrainError> {
		let mut tokens_to_transfer = Vec::new();
		if let Some(erc20_origins) = self.map.get(contract_address) {
			for (from, subamount) in erc20_origins {
				if &amount > subamount {
					tokens_to_transfer.push((*from, *subamount));
					amount -= *subamount;
				} else if &amount == subamount {
					tokens_to_transfer.push((*from, *subamount));
					return Ok(tokens_to_transfer);
				} else {
					// Each insertion of tokens must be drain at once
					return Err(DrainError::SplitError);
				}
			}
			// If there were enough tokens, we had to return in the for loop
			Err(DrainError::NotEnoughFounds)
		} else {
			Err(DrainError::AssetNotFound)
		}
	}
	pub(crate) fn insert(&mut self, contract_address: H160, who: H160, amount: U256) {
		self.map
			.entry(contract_address)
			.or_default()
			.push((who, amount));
	}
	pub(crate) fn with<R, F>(f: F) -> Option<R>
	where
		F: FnOnce(&mut Self) -> R,
	{
		XCM_HOLDING_ERC20_ORIGINS::with(|erc20s_origins| f(erc20s_origins))
	}
}

/// Xcm executor wrapper that inject xcm holding extension "XcmHoldingErc20sOrigins"
pub struct XcmExecutorWrapper<Config, InnerXcmExecutor>(PhantomData<(Config, InnerXcmExecutor)>);
impl<Config, InnerXcmExecutor> xcm::latest::ExecuteXcm<Config::RuntimeCall>
	for XcmExecutorWrapper<Config, InnerXcmExecutor>
where
	Config: xcm_executor::Config,
	InnerXcmExecutor: xcm::latest::ExecuteXcm<Config::RuntimeCall>,
{
	type Prepared = InnerXcmExecutor::Prepared;

	fn prepare(
		message: xcm::latest::Xcm<Config::RuntimeCall>,
	) -> Result<Self::Prepared, xcm::latest::Xcm<Config::RuntimeCall>> {
		InnerXcmExecutor::prepare(message)
	}

	fn execute(
		origin: impl Into<xcm::latest::Location>,
		pre: Self::Prepared,
		hash: &mut xcm::latest::XcmHash,
		weight_credit: xcm::latest::Weight,
	) -> xcm::latest::Outcome {
		let mut erc20s_origins = Default::default();
		XCM_HOLDING_ERC20_ORIGINS::using(&mut erc20s_origins, || {
			InnerXcmExecutor::execute(origin, pre, hash, weight_credit)
		})
	}

	fn charge_fees(
		location: impl Into<xcm::latest::Location>,
		fees: xcm::latest::Assets,
	) -> Result<(), xcm::latest::Error> {
		InnerXcmExecutor::charge_fees(location, fees)
	}
}

impl<Config, InnerXcmExecutor> XcmAssetTransfers for XcmExecutorWrapper<Config, InnerXcmExecutor>
where
	Config: xcm_executor::Config,
{
	type IsReserve = Config::IsReserve;
	type IsTeleporter = Config::IsTeleporter;
	type AssetTransactor = Config::AssetTransactor;
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn test_xcm_holding_ext_erc20s_origins() {
		const TOKEN1: H160 = H160([1; 20]);
		const TOKEN2: H160 = H160([2; 20]);
		const USER1: H160 = H160([3; 20]);
		const USER2: H160 = H160([4; 20]);

		// Simple case
		let mut erc20s_origins_ = Default::default();
		XCM_HOLDING_ERC20_ORIGINS::using(&mut erc20s_origins_, || {
			XcmHoldingErc20sOrigins::with(|erc20s_origins| {
				erc20s_origins.insert(TOKEN1, USER1, U256::from(100));
				assert_eq!(
					erc20s_origins.drain(TOKEN2, U256::from(1)),
					Err(DrainError::AssetNotFound)
				);
				assert_eq!(
					erc20s_origins.drain(TOKEN1, U256::from(100)),
					Ok(vec![(USER1, U256::from(100))])
				);
			})
		});

		// Complex case
		let mut erc20s_origins_ = Default::default();
		XCM_HOLDING_ERC20_ORIGINS::using(&mut erc20s_origins_, || {
			XcmHoldingErc20sOrigins::with(|erc20s_origins| {
				erc20s_origins.insert(TOKEN1, USER1, U256::from(100));
				erc20s_origins.insert(TOKEN1, USER2, U256::from(200));
				assert_eq!(
					erc20s_origins.drain(TOKEN1, U256::from(100)),
					Ok(vec![(USER1, U256::from(100))])
				);
				assert_eq!(
					erc20s_origins.drain(TOKEN1, U256::from(201)),
					Err(DrainError::NotEnoughFounds)
				);
				assert_eq!(
					erc20s_origins.drain(TOKEN1, U256::from(199)),
					Err(DrainError::SplitError)
				);
				assert_eq!(
					erc20s_origins.drain(TOKEN1, U256::from(200)),
					Ok(vec![(USER2, U256::from(200))])
				);
			})
		});
	}
}