forked from Toniq-Labs/extendable-token
-
Notifications
You must be signed in to change notification settings - Fork 0
/
standard.mo
187 lines (167 loc) · 6.3 KB
/
standard.mo
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
/*
Basic single token per canister
*/
import Cycles "mo:base/ExperimentalCycles";
import HashMap "mo:base/HashMap";
import Principal "mo:base/Principal";
import Result "mo:base/Result";
import Iter "mo:base/Iter";
//Get the path right
import AID "../motoko/util/AccountIdentifier";
import ExtCore "../motoko/ext/Core";
import ExtCommon "../motoko/ext/Common";
actor class standard_token(init_name: Text, init_symbol: Text, init_decimals: Nat8, init_supply: ExtCore.Balance, init_owner: Principal) = this{
// Types
type AccountIdentifier = ExtCore.AccountIdentifier;
type SubAccount = ExtCore.SubAccount;
type User = ExtCore.User;
type Balance = ExtCore.Balance;
type TokenIdentifier = ExtCore.TokenIdentifier;
type Extension = ExtCore.Extension;
type CommonError = ExtCore.CommonError;
type NotifyService = ExtCore.NotifyService;
type BalanceRequest = ExtCore.BalanceRequest;
type BalanceResponse = ExtCore.BalanceResponse;
type TransferRequest = ExtCore.TransferRequest;
type TransferResponse = ExtCore.TransferResponse;
type Metadata = ExtCommon.Metadata;
private let EXTENSIONS : [Extension] = ["@ext/common"];
//State work
private stable var _balancesState : [(AccountIdentifier, Balance)] = [(AID.fromPrincipal(init_owner, null), init_supply)];
private var _balances : HashMap.HashMap<AccountIdentifier, Balance> = HashMap.fromIter(_balancesState.vals(), 0, AID.equal, AID.hash);
private stable let METADATA : Metadata = #fungible({
name = init_name;
symbol = init_symbol;
decimals = init_decimals;
metadata = null;
});
private stable var _supply : Balance = init_supply;
//State functions
system func preupgrade() {
_balancesState := Iter.toArray(_balances.entries());
};
system func postupgrade() {
_balancesState := [];
};
public shared(msg) func transfer(request: TransferRequest) : async TransferResponse {
if (ExtCore.TokenIdentifier.isPrincipal(request.token, Principal.fromActor(this)) == false) {
return #err(#InvalidToken(request.token));
};
if (ExtCore.TokenIdentifier.getIndex(request.token) != 0) {
return #err(#InvalidToken(request.token));
};
let sender = ExtCore.User.toAID(request.from);
let spender = AID.fromPrincipal(msg.caller, request.subaccount);
let receiver = ExtCore.User.toAID(request.to);
if (AID.equal(sender, spender) == false) {
return #err(#Unauthorized(spender));
};
switch (_balances.get(sender)) {
case (?sender_balance) {
if (sender_balance >= request.amount) {
//Remove from sender first
var sender_balance_new : Balance = sender_balance - request.amount;
_balances.put(sender, sender_balance_new);
var provisional_amount : Balance = request.amount;
if (request.notify) {
switch(ExtCore.User.toPrincipal(request.to)) {
case (?canisterId) {
let notifier : NotifyService = actor(Principal.toText(canisterId));
switch(await notifier.tokenTransferNotification(request.token, request.from, request.amount, request.memo)) {
case (?balance) {
provisional_amount := balance;
};
case (_) {
var sender_balance_new2 = switch (_balances.get(sender)) {
case (?sender_balance) {
sender_balance + request.amount;
};
case (_) {
request.amount;
};
};
_balances.put(sender, sender_balance_new2);
return #err(#Rejected);
};
};
};
case (_) {
var sender_balance_new2 = switch (_balances.get(sender)) {
case (?sender_balance) {
sender_balance + request.amount;
};
case (_) {
request.amount;
};
};
_balances.put(sender, sender_balance_new2);
return #err(#CannotNotify(receiver));
}
};
};
assert(provisional_amount <= request.amount); //should never hit
var receiver_balance_new = switch (_balances.get(receiver)) {
case (?receiver_balance) {
receiver_balance + provisional_amount;
};
case (_) {
provisional_amount;
};
};
_balances.put(receiver, receiver_balance_new);
//Process sender refund
if (provisional_amount < request.amount) {
var sender_refund : Balance = request.amount - provisional_amount;
var sender_balance_new2 = switch (_balances.get(sender)) {
case (?sender_balance) {
sender_balance + sender_refund;
};
case (_) {
sender_refund;
};
};
_balances.put(sender, sender_balance_new2);
};
return #ok(provisional_amount);
} else {
return #err(#InsufficientBalance);
};
};
case (_) {
return #err(#InsufficientBalance);
};
};
};
public query func extensions() : async [Extension] {
EXTENSIONS;
};
public query func balance(request : BalanceRequest) : async BalanceResponse {
let aid = ExtCore.User.toAID(request.user);
switch (_balances.get(aid)) {
case (?balance) {
return #ok(balance);
};
case (_) {
return #ok(0);
};
}
};
public query func supply(token : TokenIdentifier) : async Result.Result<Balance, CommonError> {
#ok(_supply);
};
public query func metadata(token : TokenIdentifier) : async Result.Result<Metadata, CommonError> {
#ok(METADATA);
};
public query func registry() : async [(AccountIdentifier, Balance)] {
Iter.toArray(_balances.entries());
};
//Internal cycle management - good general case
public func acceptCycles() : async () {
let available = Cycles.available();
let accepted = Cycles.accept(available);
assert (accepted == available);
};
public query func availableCycles() : async Nat {
return Cycles.balance();
};
}