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
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::unused_unit)]

use frame_support::pallet_prelude::*;
use sp_std::vec::Vec;
use xcm::latest::prelude::*;

use orml_xcm_support::UnknownAsset;

pub use module::*;

mod mock;
mod tests;

#[frame_support::pallet]
pub mod module {
	use super::*;

	#[pallet::config]
	pub trait Config: frame_system::Config {
		type Event: From<Event> + IsType<<Self as frame_system::Config>::Event>;
	}

	#[pallet::event]
	#[pallet::generate_deposit(pub(crate) fn deposit_event)]
	pub enum Event {
		/// Deposit success.
		Deposited { asset: MultiAsset, who: MultiLocation },
		/// Withdraw success.
		Withdrawn { asset: MultiAsset, who: MultiLocation },
	}

	#[pallet::error]
	pub enum Error<T> {
		/// The balance is too low.
		BalanceTooLow,
		/// The operation will cause balance to overflow.
		BalanceOverflow,
		/// Unhandled asset.
		UnhandledAsset,
	}

	#[pallet::pallet]
	#[pallet::generate_store(pub(super) trait Store)]
	#[pallet::without_storage_info]
	pub struct Pallet<T>(_);

	#[pallet::hooks]
	impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {}

	/// Concrete fungible balances under a given location and a concrete
	/// fungible id.
	///
	/// double_map: who, asset_id => u128
	#[pallet::storage]
	#[pallet::getter(fn concrete_fungible_balances)]
	pub(crate) type ConcreteFungibleBalances<T> =
		StorageDoubleMap<_, Blake2_128Concat, MultiLocation, Blake2_128Concat, MultiLocation, u128, ValueQuery>;

	/// Abstract fungible balances under a given location and a abstract
	/// fungible id.
	///
	/// double_map: who, asset_id => u128
	#[pallet::storage]
	#[pallet::getter(fn abstract_fungible_balances)]
	pub(crate) type AbstractFungibleBalances<T> =
		StorageDoubleMap<_, Blake2_128Concat, MultiLocation, Blake2_128Concat, Vec<u8>, u128, ValueQuery>;

	#[pallet::call]
	impl<T: Config> Pallet<T> {}
}

impl<T: Config> UnknownAsset for Pallet<T> {
	fn deposit(asset: &MultiAsset, to: &MultiLocation) -> DispatchResult {
		match asset {
			MultiAsset {
				fun: Fungible(amount),
				id: Concrete(location),
			} => ConcreteFungibleBalances::<T>::try_mutate(to, location, |b| -> DispatchResult {
				*b = b.checked_add(*amount).ok_or(Error::<T>::BalanceOverflow)?;
				Ok(())
			}),
			MultiAsset {
				fun: Fungible(amount),
				id: Abstract(key),
			} => AbstractFungibleBalances::<T>::try_mutate(to, key, |b| -> DispatchResult {
				*b = b.checked_add(*amount).ok_or(Error::<T>::BalanceOverflow)?;
				Ok(())
			}),
			_ => Err(Error::<T>::UnhandledAsset.into()),
		}?;

		Self::deposit_event(Event::Deposited {
			asset: asset.clone(),
			who: to.clone(),
		});

		Ok(())
	}

	fn withdraw(asset: &MultiAsset, from: &MultiLocation) -> DispatchResult {
		match asset {
			MultiAsset {
				fun: Fungible(amount),
				id: Concrete(location),
			} => ConcreteFungibleBalances::<T>::try_mutate(from, location, |b| -> DispatchResult {
				*b = b.checked_sub(*amount).ok_or(Error::<T>::BalanceTooLow)?;
				Ok(())
			}),
			MultiAsset {
				fun: Fungible(amount),
				id: Abstract(key),
			} => AbstractFungibleBalances::<T>::try_mutate(from, key, |b| -> DispatchResult {
				*b = b.checked_sub(*amount).ok_or(Error::<T>::BalanceTooLow)?;
				Ok(())
			}),
			_ => Err(Error::<T>::UnhandledAsset.into()),
		}?;

		Self::deposit_event(Event::Withdrawn {
			asset: asset.clone(),
			who: from.clone(),
		});

		Ok(())
	}
}