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
#![cfg_attr(not(feature = "std"), no_std)]
use sp_std::prelude::*;
use sp_core::{U256, RuntimeDebug};
use frame_system::{self as system, ensure_signed};
use frame_support::{
decl_error, decl_event, decl_module, decl_storage,
dispatch::{DispatchResult, DispatchError},
};
use codec::{Encode, Decode};
use artemis_core::BridgedAssetId;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
#[derive(Encode, Decode, Clone, PartialEq, Eq, Default, RuntimeDebug)]
pub struct AccountData {
pub free: U256
}
type AssetAccountData = AccountData;
pub trait Trait: system::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}
decl_storage! {
trait Store for Module<T: Trait> as Asset {
pub TotalIssuance: map hasher(blake2_128_concat) BridgedAssetId => U256;
pub Account: double_map hasher(blake2_128_concat) BridgedAssetId, hasher(blake2_128_concat) T::AccountId => AssetAccountData;
}
}
decl_event!(
pub enum Event<T>
where
AccountId = <T as system::Trait>::AccountId,
{
Burned(BridgedAssetId, AccountId, U256),
Minted(BridgedAssetId, AccountId, U256),
Transferred(BridgedAssetId, AccountId, AccountId, U256),
}
);
decl_error! {
pub enum Error for Module<T: Trait> {
FreeTransferOverflow,
TotalMintingOverflow,
FreeMintingOverflow,
TotalBurningUnderflow,
FreeBurningUnderflow,
InsufficientBalance,
}
}
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
type Error = Error<T>;
fn deposit_event() = default;
#[weight = 0]
pub fn transfer(origin, asset_id: BridgedAssetId, to: T::AccountId, amount: U256) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::do_transfer(asset_id, &who, &to, amount)?;
Ok(())
}
}
}
impl<T: Trait> Module<T> {
pub fn free_balance(asset_id: BridgedAssetId, who: &T::AccountId) -> U256 {
<Account<T>>::get(asset_id, who).free
}
pub fn do_mint(asset_id: BridgedAssetId, who: &T::AccountId, amount: U256) -> DispatchResult {
if amount.is_zero() {
return Ok(())
}
<Account<T>>::try_mutate(asset_id, who, |account| -> Result<(), DispatchError> {
let current_total_issuance = <TotalIssuance>::get(asset_id);
let new_total_issuance = current_total_issuance.checked_add(amount)
.ok_or(Error::<T>::TotalMintingOverflow)?;
account.free = account.free.checked_add(amount)
.ok_or(Error::<T>::FreeMintingOverflow)?;
<TotalIssuance>::insert(asset_id, new_total_issuance);
Ok(())
})?;
Self::deposit_event(RawEvent::Minted(asset_id, who.clone(), amount));
Ok(())
}
pub fn do_burn(asset_id: BridgedAssetId, who: &T::AccountId, amount: U256) -> DispatchResult {
if amount.is_zero() {
return Ok(())
}
<Account<T>>::try_mutate(asset_id, who, |account| -> Result<(), DispatchError> {
let current_total_issuance = <TotalIssuance>::get(asset_id);
let new_total_issuance = current_total_issuance.checked_sub(amount)
.ok_or(Error::<T>::TotalBurningUnderflow)?;
account.free = account.free.checked_sub(amount)
.ok_or(Error::<T>::FreeBurningUnderflow)?;
<TotalIssuance>::insert(asset_id, new_total_issuance);
Ok(())
})?;
Self::deposit_event(RawEvent::Burned(asset_id, who.clone(), amount));
Ok(())
}
fn do_transfer(
asset_id: BridgedAssetId,
from: &T::AccountId,
to: &T::AccountId,
amount: U256)
-> DispatchResult {
if amount.is_zero() || from == to {
return Ok(())
}
<Account<T>>::try_mutate(asset_id, from, |from_account| -> DispatchResult {
<Account<T>>::try_mutate(asset_id, to, |to_account| -> DispatchResult {
from_account.free = from_account.free.checked_sub(amount).ok_or(Error::<T>::InsufficientBalance)?;
to_account.free = to_account.free.checked_add(amount).ok_or(Error::<T>::FreeTransferOverflow)?;
Ok(())
})
})?;
Self::deposit_event(RawEvent::Transferred(asset_id, from.clone(), to.clone(), amount));
Ok(())
}
}