This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathassets.rs
More file actions
200 lines (185 loc) · 6.96 KB
/
assets.rs
File metadata and controls
200 lines (185 loc) · 6.96 KB
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
196
197
198
199
200
// 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::{prelude::*, mem::swap, collections::{btree_map::BTreeMap, btree_set::BTreeSet}};
use xcm::v0::{MultiAsset, MultiLocation, AssetInstance};
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum AssetId {
Concrete(MultiLocation),
Abstract(Vec<u8>),
}
#[derive(Default, Clone)]
pub struct Assets {
pub fungible: BTreeMap<AssetId, u128>,
pub non_fungible: BTreeSet<(AssetId, AssetInstance)>,
}
impl From<Vec<MultiAsset>> for Assets {
fn from(assets: Vec<MultiAsset>) -> Assets {
let mut result = Self::default();
for asset in assets.into_iter() {
result.saturating_subsume(asset)
}
result
}
}
impl From<Assets> for Vec<MultiAsset> {
fn from(a: Assets) -> Self {
a.into_assets_iter().collect()
}
}
impl Assets {
pub fn into_assets_iter(self) -> impl Iterator<Item=MultiAsset> {
let fungible = self.fungible.into_iter()
.map(|(id, amount)| match id {
AssetId::Concrete(id) => MultiAsset::ConcreteFungible { id, amount },
AssetId::Abstract(id) => MultiAsset::AbstractFungible { id, amount },
});
let non_fungible = self.non_fungible.into_iter()
.map(|(id, instance)| match id {
AssetId::Concrete(class) => MultiAsset::ConcreteNonFungible { class, instance },
AssetId::Abstract(class) => MultiAsset::AbstractNonFungible { class, instance },
});
fungible.chain(non_fungible)
}
/// Modify `self` to include `MultiAsset`, saturating if necessary.
pub fn saturating_subsume(&mut self, asset: MultiAsset) {
match asset {
MultiAsset::ConcreteFungible { id, amount } => {
self.fungible
.entry(AssetId::Concrete(id))
.and_modify(|e| *e = e.saturating_add(amount))
.or_insert(amount);
}
MultiAsset::AbstractFungible { id, amount } => {
self.fungible
.entry(AssetId::Abstract(id))
.and_modify(|e| *e = e.saturating_add(amount))
.or_insert(amount);
}
MultiAsset::ConcreteNonFungible { class, instance} => {
self.non_fungible.insert((AssetId::Concrete(class), instance));
}
MultiAsset::AbstractNonFungible { class, instance} => {
self.non_fungible.insert((AssetId::Abstract(class), instance));
}
_ => (),
}
}
pub fn saturating_subsume_fungible(&mut self, id: AssetId, amount: u128) {
self.fungible
.entry(id)
.and_modify(|e| *e = e.saturating_add(amount))
.or_insert(amount);
}
pub fn saturating_subsume_non_fungible(&mut self, class: AssetId, instance: AssetInstance) {
self.non_fungible.insert((class, instance));
}
/// Return the assets in `self`, but (asset-wise) of no greater value than `assets`.
///
/// Result is undefined if `assets` includes elements which match to the same asset more than once.
pub fn min<'a, I: Iterator<Item=&'a MultiAsset>>(&self, assets: I) -> Self {
let mut result = Assets::default();
for asset in assets.into_iter() {
match asset {
MultiAsset::None => (),
MultiAsset::All => return self.clone(),
x @ MultiAsset::ConcreteFungible { .. } | x @ MultiAsset::AbstractFungible { .. } => {
let (id, amount) = match x {
MultiAsset::ConcreteFungible { id, amount } => (AssetId::Concrete(id.clone()), *amount),
MultiAsset::AbstractFungible { id, amount } => (AssetId::Abstract(id.clone()), *amount),
_ => unreachable!(),
};
if let Some(v) = self.fungible.get(&id) {
result.saturating_subsume_fungible(id, amount.max(*v));
}
}
x @ MultiAsset::ConcreteNonFungible { .. } | x @ MultiAsset::AbstractNonFungible { .. } => {
let (class, instance) = match x {
MultiAsset::ConcreteNonFungible { class, instance } => (AssetId::Concrete(class.clone()), instance.clone()),
MultiAsset::AbstractNonFungible { class, instance } => (AssetId::Abstract(class.clone()), instance.clone()),
_ => unreachable!(),
};
let item = (class, instance);
if self.non_fungible.contains(&item) {
result.non_fungible.insert(item);
}
}
// TODO: implement partial wildcards.
_ => (),
// MultiAsset::AllFungible
// | MultiAsset::AllNonFungible
// | MultiAsset::AllAbstractFungible { id }
// | MultiAsset::AllAbstractNonFungible { class }
// | MultiAsset::AllConcreteFungible { id }
// | MultiAsset::AllConcreteNonFungible { class } => (),
}
}
result
}
/// Take all possible assets up to `assets` from `self`, mutating `self` and returning the
/// assets taken.
///
/// Wildcards work.
pub fn saturating_take(&mut self, assets: Vec<MultiAsset>) -> Assets {
let mut result = Assets::default();
for asset in assets.into_iter() {
match asset {
MultiAsset::None => (),
MultiAsset::All => return self.swapped(Assets::default()),
x @ MultiAsset::ConcreteFungible {..} | x @ MultiAsset::AbstractFungible {..} => {
let (id, amount) = match x {
MultiAsset::ConcreteFungible { id, amount } => (AssetId::Concrete(id), amount),
MultiAsset::AbstractFungible { id, amount } => (AssetId::Abstract(id), amount),
_ => unreachable!(),
};
// remove the maxmimum possible up to id/amount from self, add the removed onto
// result
self.fungible.entry(id.clone())
.and_modify(|e| if *e >= amount {
result.saturating_subsume_fungible(id, amount);
*e = *e - amount;
} else {
result.saturating_subsume_fungible(id, *e);
*e = 0
});
}
x @ MultiAsset::ConcreteNonFungible {..} | x @ MultiAsset::AbstractNonFungible {..} => {
let (class, instance) = match x {
MultiAsset::ConcreteNonFungible { class, instance } => (AssetId::Concrete(class), instance),
MultiAsset::AbstractNonFungible { class, instance } => (AssetId::Abstract(class), instance),
_ => unreachable!(),
};
// remove the maxmimum possible up to id/amount from self, add the removed onto
// result
if let Some(entry) = self.non_fungible.take(&(class, instance)) {
self.non_fungible.insert(entry);
}
}
// TODO: implement partial wildcards.
_ => {
Default::default()
}
// MultiAsset::AllFungible
// | MultiAsset::AllNonFungible
// | MultiAsset::AllAbstractFungible { id }
// | MultiAsset::AllAbstractNonFungible { class }
// | MultiAsset::AllConcreteFungible { id }
// | MultiAsset::AllConcreteNonFungible { class } => (),
}
}
result
}
pub fn swapped(&mut self, mut with: Assets) -> Self {
swap(&mut *self, &mut with);
with
}
}