-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreasury.mo
More file actions
321 lines (286 loc) · 13.1 KB
/
treasury.mo
File metadata and controls
321 lines (286 loc) · 13.1 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import Types "types";
import { now } "mo:base/Time";
import Array "mo:base/Array";
import List "mo:base/List";
import Map "mo:map/Map";
import { phash; nhash; thash } "mo:map/Map";
import Principal "mo:base/Principal";
import Buffer "mo:base/Buffer";
import Nat64 "mo:base/Nat64";
import Nat "mo:base/Nat";
import Text "mo:base/Text";
import Iter "mo:base/Iter";
import LedgerTypes "../interfaces/ICP_Token/ledger_icp";
import { print } "mo:base/Debug";
// import Utils "../backend/utils";
shared ({caller = superAdmin}) persistent actor class Treasury(initArgs: Types.InitArgs) = this {
/////////////////////////////////////////// Types ////////////////////////////////////////////////////////
type Account = Types.Account;
type ActivityLogEntry = Types.ActivityLogEntry;
type WhitdrawalLogEntry = Types.WhitdrawalLogEntry;
/////////////////////////////////////// Variables state /////////////////////////////////////////////////
var admins: [Principal] = [superAdmin, initArgs.mainPlatform];
var activityLog: List.List<ActivityLogEntry> = List.nil<ActivityLogEntry>();
var _withdrawalLog: List.List<WhitdrawalLogEntry> = List.nil<Types.WhitdrawalLogEntry>();
var _internalSubaccounts: [Blob] = [
"feecollector00000000000000000000",
"escrows0000000000000000000000000",
];
let escrows = Map.new<Types.EscrowId, Types.Escrow>();
let supportedTokens = Map.make<Text, Types.Token>(thash, "Internet Computer", Types.icpToken());
var lastEscrowId = 0;
var lastMemoTransactionId = 0;
var withdrawableBalances = Map.new<Principal, [Types.Balance]>();
//////////////////////////////////// Private Functions ///////////////////////////////////////////////////
func isAdmin(caller: Principal): Bool {
for(a in admins.vals()) if(a == caller ) return true ;
return false;
};
func getAccountByUser(u: Principal) : Account {
{ owner = Principal.fromActor(this); subaccount = ? Principal.toLedgerAccount(u, null)}
};
func remoteLedger(p: Principal): actor {
icrc1_balance_of : shared query Account -> async Nat;
icrc1_transfer : shared LedgerTypes.TransferArg -> async LedgerTypes.Result;
query_blocks : shared query LedgerTypes.GetBlocksArgs -> async LedgerTypes.QueryBlocksResponse;
icrc1_fee : shared query () -> async Nat;
icrc1_symbol: shared query () -> async Text;
} {
actor(Principal.toText(p))
};
func newMemo() : Blob {
lastMemoTransactionId += 1;
let txnText = Nat.toText(lastMemoTransactionId % 1000000000);
Text.encodeUtf8("ITP_WithdrawalTRX:" # txnText)
};
//////////////////////////////////// Admin Functions ////////////////////////////////////////////////////
public shared ({ caller }) func addAdmin(a: Principal): async Bool{
assert isAdmin(caller);
if(not isAdmin(a)) {
admins := Array.tabulate<Principal>(
admins.size() + 1,
func i = if (i < admins.size()) admins[i] else a
);
let newLogEntry: Types.ActivityLogEntry = {
timestamp = now();
caller = caller;
action = #AddAdmin(a)
};
activityLog := List.push<Types.ActivityLogEntry>( newLogEntry, activityLog);
return true;
};
return false;
};
public shared ({ caller }) func removeAdmin(a: Principal): async Bool{
assert isAdmin(caller) and a != superAdmin;
for( admin in admins.vals()){
if (admin == a) {
admins := Array.filter<Principal>(admins, func x = x != a);
let newLogEntry: Types.ActivityLogEntry = {
timestamp = now();
caller = caller;
action = #RemoveAdmin(a)
};
activityLog := List.push<Types.ActivityLogEntry>(newLogEntry, activityLog);
return true;
};
};
return false;
};
public shared ({ caller }) func addToken(canisterId: Principal): async Bool {
assert isAdmin(caller);
let tokenLedger = actor(Principal.toText(canisterId)): actor {
icrc1_metadata : shared query () -> async [(Text, Types.MetadataValue)];
};
let metadata = await tokenLedger.icrc1_metadata();
var name = "";
var logo = "";
var symbol = "";
var decimals = 0;
var fee = 0;
for (field in metadata.vals()) {
switch (field) {
case ("icrc1:name", #Text(_name)) { name := _name };
case ("icrc1:symbol", #Text(_symbol)) { symbol := _symbol };
case ("icrc1:logo", #Text(_logo)) {logo := _logo};
case ("icrc1:fee", #Nat(_fee)) { fee := _fee };
case ("icrc1:decimals", #Nat(_decimals)) { decimals := _decimals };
case _ {}
}
};
if (name == "" or symbol == "" or decimals == 0 or fee == 0) {
return false
};
let newToken: Types.Token = {
name;
symbol;
logo;
fee;
decimals;
canisterId;
};
ignore Map.put<Text, Types.Token>(supportedTokens, thash, name, newToken);
true
};
public shared query ({ caller }) func balancesOf(p: Principal): async ?[Types.Balance] {
assert isAdmin(caller);
Map.get<Principal, [Types.Balance]>(withdrawableBalances, phash, p)
};
public shared query ({ caller }) func getDepositAccount(u: Principal): async Account{
assert isAdmin(caller);
getAccountByUser(u)
};
//////////////////////////////////// Public Functions ///////////////////////////////////////////////////
public query func getAdmins(): async [Principal]{
admins
};
public query func getSupportedTokens(): async [Types.Token] {
Iter.toArray<Types.Token>(Map.vals<Text, Types.Token>(supportedTokens))
};
public shared query ({ caller }) func getMyDepositAccount(): async Account {
assert (not Principal.isAnonymous(caller));
getAccountByUser(caller)
};
func verifyTransaction(args: LedgerTypes.TransferArg, blocks: [LedgerTypes.CandidBlock]): Bool {
for ({ transaction } in blocks.vals()){
switch (transaction.operation){
case (? #Transfer(t)) {
let toVerified = t.to == Principal.toLedgerAccount(args.to.owner, args.to.subaccount);
let amountVerified = t.amount == {e8s = Nat64.fromNat(args.amount)};
let window = 60000000000: Nat64;
let signature_delay = switch (args.created_at_time){
case null 0: Nat64;
case (?_created_at_time) {
transaction.created_at_time.timestamp_nanos - _created_at_time
}
};
// let created_at_time_verified = ?transaction.created_at_time.timestamp_nanos == args.created_at_time;
let created_at_time_verified = window >= signature_delay;
print(debug_show(toVerified));
print(debug_show(amountVerified));
print(debug_show(created_at_time_verified));
print(debug_show({transaction= transaction.created_at_time.timestamp_nanos}));
print(debug_show({args = args.created_at_time}));
if (toVerified and amountVerified and created_at_time_verified){
return true
};
};
case _ {};
}
};
false
};
public shared ({ caller }) func createEscrow(args: Types.CreateEscrowArgs): async {#Ok: Types.EscrowId; #Err: Text }{
if(caller != initArgs.mainPlatform){
return #Err("Only the main platform can create escrows");
};
let ledger = remoteLedger(args.token);
let { blocks } = await ledger.query_blocks({ start = args.index; length = args.index});
var transactionVerified = verifyTransaction(args.transferArg, blocks);
if (not transactionVerified) {
return #Err("Transaction verification failed");
};
lastEscrowId += 1;
let newEscrow: Types.Escrow = {
amount = args.transferArg.amount;
platformFee = args.platformFee;
fromSubaccount = "escrows0000000000000000000000000";
id = lastEscrowId;
userAssigned = args.userAssigned;
token = args.token;
};
ignore Map.put<Types.EscrowId, Types.Escrow>(escrows, nhash, lastEscrowId, newEscrow);
#Ok(newEscrow.id)
};
public shared ({ caller }) func releaseEscrow(id: Types.EscrowId): async Bool {
if(caller != initArgs.mainPlatform) return false ;
let escrow = Map.remove<Types.EscrowId, Types.Escrow>(escrows, nhash, id);
switch (escrow) {
case null { return false };
case ( ?escrow ) {
let currentUserBalances = switch (Map.get<Principal, [Types.Balance]>(withdrawableBalances, phash, escrow.userAssigned)) {
case null { [] };
case ( ?balances ) { balances }
};
let bufferBalances = Buffer.Buffer<Types.Balance>(0);
var currentBalanceOfTokenUsed = 0;
var tokenSymbol = "";
for (currentBal in currentUserBalances.vals() ) {
if (currentBal.token != escrow.token) {
bufferBalances.add(currentBal);
} else {
currentBalanceOfTokenUsed := currentBal.balance;
tokenSymbol := currentBal.symbol
};
};
bufferBalances.add({
symbol = if (tokenSymbol == "") {
await remoteLedger(escrow.token).icrc1_symbol()
} else {
tokenSymbol;
};
token = escrow.token;
balance = currentBalanceOfTokenUsed + escrow.amount - escrow.platformFee: Nat
});
let updatedBalances = Buffer.toArray<Types.Balance>(bufferBalances);
ignore Map.put<Principal, [Types.Balance]>(withdrawableBalances, phash, escrow.userAssigned, updatedBalances);
return true
}
};
};
public shared query ({ caller }) func getMyBalances(): async [Types.Balance] {
switch (Map.get<Principal, [Types.Balance]>(withdrawableBalances, phash, caller)){
case null [];
case ( ?bal ) {bal}
}
};
public shared ({ caller }) func withdrawal({token: Principal; amount: Nat; to: Account}): async {#Ok: Nat; #Err} {
let balances = Map.get<Principal, [Types.Balance]>(withdrawableBalances, phash, caller);
switch (balances) {
case null {
return #Err;
};
case ( ?userBalances ) {
for (b in userBalances.vals()){
if (b.token == token ) {
let ledger = remoteLedger(token);
let feeToken = await ledger.icrc1_fee();
if (b.balance >= amount + feeToken) {
let transferResponse = await ledger.icrc1_transfer({
amount;
created_at_time = null;
fee = null;
from_subaccount = ?"escrows0000000000000000000000000";
memo = ?newMemo();
to : Account
});
switch (transferResponse) {
case (#Err(_)) {
return #Err;
};
case (#Ok(index)) {
let updatedUserBalances = Array.tabulate<Types.Balance>(
userBalances.size(),
func i = if (userBalances[i].token == token) {
{userBalances[i] with balance = userBalances[i].balance - (amount + feeToken): Nat}
} else {
userBalances[i]
}
);
ignore Map.put<Principal, [Types.Balance]>(
withdrawableBalances,
phash,
caller,
updatedUserBalances
);
return #Ok(index);
}
}
}
}
};
return #Err;
};
};
}
}