-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtoken.rs
132 lines (112 loc) · 5.26 KB
/
token.rs
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
/*
* Copyright 2021 Idavoll Network
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use codec::FullCodec;
// use frame_support::traits::{BalanceStatus, LockIdentifier};
use sp_runtime::{
traits::{AtLeast32BitUnsigned, MaybeSerializeDeserialize,Saturating},
DispatchResult,
};
use sp_std::{
cmp::{Eq, PartialEq},
fmt::Debug,
};
use crate::{Trait,Module};
/// Abstraction trait over a multiple currencies system, each currency type
/// is identified by a `AssetId`, if it is set to `None` when calling a
/// function the implementation should default to the native currency of the
/// runtime.
pub trait BaseToken<AccountId> {
/// The type used to identify currencies
type AssetId: FullCodec + Eq + PartialEq + Copy + Debug + Default;
/// The balance of an account.
type Balance: AtLeast32BitUnsigned
+ FullCodec
+ Copy
+ MaybeSerializeDeserialize
+ Debug
+ Default;
// PUBLIC IMMUTABLES
/// create the new token
fn create(owner: AccountId,total: Self::Balance) -> Self::AssetId;
/// The total amount of the asset.
fn total(aid: Self::AssetId) -> Self::Balance;
/// Reduce the total tokens by `amount` and remove the `amount` tokens by `who`'s
/// account, return error if not enough tokens.
fn burn(aid: Self::AssetId, who: &AccountId, amount: Self::Balance) -> DispatchResult;
/// Increase the balance of `who` by `amount`,if there have the permission。
fn mint(aid: Self::AssetId, who: &AccountId, amount: Self::Balance) -> DispatchResult;
/// The 'free' balance of a given account.
fn free_balance_of(aid: Self::AssetId, who: &AccountId) -> Self::Balance;
/// The 'lock' balance of a given account.
fn lock_balance_of(aid: Self::AssetId, who: &AccountId) -> Self::Balance;
/// The total balance of a given account.
fn total_balance_of(aid: Self::AssetId, who: &AccountId) -> Self::Balance;
/// Transfer some free balance to another account.
fn transfer(aid: Self::AssetId, from: &AccountId, to: &AccountId, value: Self::Balance) -> DispatchResult;
/// Lock `value` from the free balance,return `Err` if the free balance is lower than `value`.
/// otherwise return `ok`.
fn lock(aid: Self::AssetId, who: &AccountId, value: Self::Balance) -> DispatchResult;
/// Unlock `value` from locked balance to free balance. This function cannot fail.
/// If the locked balance of `who` is less than `value`, then the remaining amount will be returned.
fn unlock(aid: Self::AssetId, who: &AccountId, value: Self::Balance) -> DispatchResult;
}
impl<T: Trait> BaseToken<T::AccountId> for Module<T> {
type AssetId = T::AssetId;
type Balance = T::Balance;
fn create(owner: T::AccountId,total: Self::Balance) -> Self::AssetId {
Self::create_token(owner,total)
}
fn total(aid: Self::AssetId) -> Self::Balance {
Self::total_issuances(aid)
}
/// The 'free' balance of a given account.
fn free_balance_of(aid: Self::AssetId, who: &T::AccountId) -> Self::Balance {
Self::free_balance(aid,&who)
}
/// The 'lock' balance of a given account.
fn lock_balance_of(aid: Self::AssetId, who: &T::AccountId) -> Self::Balance {
let all = Self::total_balance(aid,who);
let free = Self::free_balance(aid,who);
all.saturating_sub(free)
}
/// The total balance of a given account.
fn total_balance_of(aid: Self::AssetId, who: &T::AccountId) -> Self::Balance {
Self::total_balance(aid,who)
}
/// Reduce the total tokens by `amount` and remove the `amount` tokens by `who`'s
/// account, return error if not enough tokens.
fn burn(aid: Self::AssetId, who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
Self::base_burn(aid,who,amount)
}
/// Increase the balance of `who` by `amount`,if there have the permission。
fn mint(aid: Self::AssetId, who: &T::AccountId, amount: Self::Balance) -> DispatchResult {
Self::base_mint(aid,who,amount)
}
/// Transfer some free balance to another account.
fn transfer(aid: Self::AssetId, from: &T::AccountId, to: &T::AccountId, value: Self::Balance) -> DispatchResult {
Self::base_transfer(aid,from,to,value)
}
/// Lock `value` from the free balance,return `Err` if the free balance is lower than `value`.
/// otherwise return `ok`.
fn lock(aid: Self::AssetId, who: &T::AccountId, value: Self::Balance) -> DispatchResult {
Self::base_lock(aid,who,value)
}
/// Unlock `value` from locked balance to free balance. This function cannot fail.
/// If the locked balance of `who` is less than `value`, then the remaining amount will be returned.
fn unlock(aid: Self::AssetId, who: &T::AccountId, value: Self::Balance) -> DispatchResult{
Self::base_unlock(aid,who,value)
}
}