-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.mo
433 lines (363 loc) · 19.5 KB
/
main.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import Int "mo:base/Int";
import Cycles "mo:base/ExperimentalCycles";
import Option "mo:base/Option";
import Buffer "mo:base/Buffer";
import Result "mo:base/Result";
import Nat "mo:base/Nat";
import Text "mo:base/Text";
import Bool "mo:base/Bool";
import HashMap "mo:base/HashMap";
import TrieMap "mo:base/TrieMap";
import Hash "mo:base/Hash";
import Iter "mo:base/Iter";
import Principal "mo:base/Principal";
import Order "mo:base/Order";
import Array "mo:base/Array";
import Debug "mo:base/Debug";
import Time "mo:base/Time";
import Float "mo:base/Float";
import Random "mo:base/Random";
import Log "resources/day6/Log";
import Types "resources/day6/Types";
import Coin "resources/day4/Coin";
// NOTE: only use for local dev,
// when deploying to IC, import from "rww3b-zqaaa-aaaam-abioa-cai"
import BootcampLocalActor "resources/day4/BootcampLocalActor";
// Actor class with the capability to accept init bootstrap data.
// The init has been made optional, so that we can deploy without bootstrapping as well.
shared ({ caller = creator }) actor class bc2305(init : ?Types.InitPayload) = Self {
//---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
// REGION: PARAMETERS ---------- ---------- ---------- ---------- ---------- ----------
//---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
//=============== DAY 6 - THE MOTOKO COIN - UPGRADED ===============\\
// https://github.com/motoko-bootcamp/motoko-starter/tree/main/days/day-6/project
type Account = Coin.Account;
// dfx identity get-principal
let owner : Account = Coin.getAccountFromPrincipal(Principal.fromText("jxic7-kzwkr-4kcyk-2yql7-uqsrg-lvrzb-k7avx-e4nbh-nfmli-rddvs-mqe"));
// Max supply = 1B
let maxSupply : Nat = 1_000_000_000;
let ownerInitialSupply : Nat = 1_000_000;
let airdropAmount : Nat = 100;
let tokenDesc : Text = "MotoCoin";
let tokenSymbol : Text = "MOC";
var logMessage : Text = "";
let bcICNetworkCanister = actor ("rww3b-zqaaa-aaaam-abioa-cai") : actor {
getAllStudentsPrincipal : shared () -> async [Principal]
};
// Stable stores
stable var ledgerEntries : [(Account, Nat)] = [];
stable var airdropEntries : [(Text, [Account])] = [];
stable var logsEntries : [(Time.Time, Text)] = [];
stable var system_params : Types.SystemParams = switch (init) {
case null { Types.defaulSystemParams };
case (?i) { {} }
};
// In-memory stores that we can utilise during canister operation. These will be saved to stable memory during upgrades.
let ledger = TrieMap.fromEntries<Account, Nat>(ledgerEntries.vals(), Coin.accountsEqual, Coin.accountsHash);
let airdrops = TrieMap.fromEntries<Text, [Account]>(airdropEntries.vals(), Text.equal, Text.hash);
let logs = HashMap.fromIter<Time.Time, Text>(logsEntries.vals(), Iter.size(logsEntries.vals()), Int.equal, Int.hash);
// Give the owner the initial allocated supply. This duplicate call is needed so that the first time the canister is deployed
// the owner is properly given the initial allocated supply. This is because on first deployment, the system preupgrade / postupgrade
// methods do not get called, so the code within there will have no effect.
if (ledger.size() < 1) {
ledger.put(owner, ownerInitialSupply)
};
//---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
// REGION: TOKEN RELATED ---------- ---------- ---------- ---------- ----------
//---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
// Returns the name of the token
public query func getTokenName() : async Text {
return tokenDesc
};
// Returns the symbol of the token
public query func getTokenSymbol() : async Text {
return tokenSymbol
};
// Returns the the total number of tokens - i.e. the max supply.
public query func getTokenTotalSupply() : async Nat {
return maxSupply
};
// Returns the the total number of tokens on all accounts.
public query func getTokenClaimedSupply() : async Nat {
var claimed : Nat = Coin.totalClaimedSupply(ledger);
logAndDebug("Total claimed supply " # debug_show (claimed));
return claimed
};
// Returns the the total number of remaining tokens that can be claimed.
public query func getTokenRemainingSupply() : async Nat {
let claimed : Nat = Coin.totalClaimedSupply(ledger);
let remaining : Nat = maxSupply - claimed;
logAndDebug("Remaining supply " # debug_show (remaining));
return remaining
};
//---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
// REGION: LEDGER RELATED ---------- ---------- ---------- ---------- ----------
//---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
public query func getAccountFromPrincipal(principal : Principal) : async Account {
return Coin.getAccountFromPrincipal(principal)
};
public func addPrincipalToLedger(principal : Principal) : async () {
let account : Account = Coin.getAccountFromPrincipal(principal);
switch (ledger.get(account)) {
// Only add the account to ledger if it doesn't already exist.
case (null) { ledger.put(account, 0) };
case (_) {}
}
};
public query func getLedgerAccounts() : async [Principal] {
let newMap = TrieMap.map<Account, Nat, Principal>(ledger, Coin.accountsEqual, Coin.accountsHash, func(key, value) = key.owner);
logAndDebug("Getting accounts. Total = " # debug_show (newMap.size()));
return Iter.toArray<Principal>(newMap.vals())
};
public query func getLedgerBalanceFor(account : Account) : async (Nat) {
return getLedgerBalanceForAccount(account)
};
public shared ({ caller }) func getMyLedgerBalance() : async (Nat) {
return getLedgerBalanceForAccount(Coin.getAccountFromPrincipal(caller))
};
private func getLedgerBalanceForAccount(account : Account) : (Nat) {
let balance : Nat = switch (ledger.get(account)) {
case (null) { 0 };
case (?amount) { amount }
};
logAndDebug(debug_show (("balance of", account, "is", balance)));
return balance
};
// Transfer tokens to another account
public shared ({ caller }) func transfer(from : Account, to : Account, amount : Nat) : async Result.Result<(), Text> {
var senderBalance : Nat = getLedgerBalanceForAccount(from);
var receiverBalance : Nat = getLedgerBalanceForAccount(to);
logAndDebug(debug_show (("transferring", amount, "from", from, "who has a current balance of", senderBalance)));
if (senderBalance >= amount) {
// Do the transfer
let newFromBalance : Nat = senderBalance - amount;
ledger.put(from, newFromBalance);
let toBalance : Nat = getLedgerBalanceForAccount(to);
let newToBalance : Nat = toBalance + amount;
ledger.put(to, newToBalance);
logAndDebug(debug_show (("transferred", amount, "to", to, "who had a balance of", toBalance, "and now has a balance of", newToBalance)));
return #ok()
} else {
logAndDebug(debug_show (("cannot transfer", amount, "to", to, "because", from, "has insufficient balance", senderBalance)));
return #err("Insufficient balance. Cannot transfer " # Nat.toText(amount) # " as only " # Nat.toText(senderBalance) # " is available.")
}
};
//---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
// REGION: AIRDROP RELATED ---------- ---------- ---------- ---------- ----------
//---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
// Airdrop {airdropAmount} tokens to any student that is part of the Bootcamp.
// dfx canister --network ic call rww3b-zqaaa-aaaam-abioa-cai getAllStudentsPrincipal '()'
public func airdrop(id : Text, max : Nat) : async Result.Result<(), Text> {
let principals : [Principal] = shuffle(await getAllAccounts());
var count : Nat = 0;
label principalsLoop for (principal in principals.vals()) {
try {
// logAndDebug("Starting airdrop for " # debug_show(principal));
let account : Account = Coin.getAccountFromPrincipal(principal);
// logAndDebug("Fetched account " # debug_show(account));
// Only airdrop if the account hasn't received an airdrop of this type before.
let airdropAccounts : [Account] = getAirdropAccounts(id);
var receivedAirdrop : Bool = false;
for (airdropAccount in airdropAccounts.vals()) {
if (Coin.accountsEqual(account, airdropAccount)) {
receivedAirdrop := true
}
};
if (receivedAirdrop == false) {
// Flag up this account as having received an airdrop.
Debug.print("airdrop - airdropAccounts array size: " # debug_show (airdropAccounts.size()));
Debug.print(debug_show (airdropAccounts));
let buffer = Buffer.fromArray<Account>(airdropAccounts);
buffer.add(account);
airdrops.put(id, Buffer.toArray<Account>(buffer));
// Update balance after airdrop
let existingBalance : Nat = getLedgerBalanceForAccount(account);
ledger.put(account, existingBalance + airdropAmount);
count += 1;
logAndDebug(debug_show ("Airdropped", airdropAmount, "to", account, "for the", id, "airdrop."));
if (max > 0 and count >= max) {
break principalsLoop
}
} else {
logAndDebug(debug_show ("Cannot airdrop", airdropAmount, "to", account, "because the account has already received an", id, "airdrop."))
}
} catch (e) {
logAndDebug("An error occured when perforing the airdrop for principal: " # Principal.toText(principal));
return #err("An error occured when perforing the airdrop.")
}
};
return #ok()
};
public query func doesAccountHaveAirdrop(id : Text, account : Account) : async Bool {
let airdropAccounts : [Account] = getAirdropAccounts(id);
var receivedAirdrop : Bool = false;
for (airdropAccount in airdropAccounts.vals()) {
if (Coin.accountsEqual(account, airdropAccount)) {
receivedAirdrop := true
}
};
if (receivedAirdrop == true) {
logAndDebug(debug_show (("Account", account, "has received the", id, "airdrop.")))
};
return receivedAirdrop
};
public shared ({ caller }) func getMyAirdrops() : async [Text] {
var airdropIDs = Buffer.Buffer<Text>(0);
for ((airdropID, airdropAccounts) in airdrops.entries()) {
for (airdropAccount in airdropAccounts.vals()) {
if (Coin.accountsEqual(Coin.getAccountFromPrincipal(caller), airdropAccount)) {
airdropIDs.add(airdropID)
}
}
};
let airdropIDsArray = Buffer.toArray<Text>(airdropIDs);
if (airdropIDs.size() > 0) {
logAndDebug(debug_show ("Caller", caller, "has received the following airdrops: ", airdropIDsArray))
};
return airdropIDsArray
};
private func getAirdropAccounts(id : Text) : [Account] {
let airdropAccounts : [Account] = switch (airdrops.get(id)) {
case (null) { [] };
case (?result) { result }
};
logAndDebug(debug_show (("Total accounts that have received the", id, "airdrop:", airdropAccounts.size())));
return airdropAccounts;
};
//---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
// REGION: MISC ---------- ---------- ---------- ---------- ---------- ----------
//---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
// dfx canister call bc2305 getAllAccounts '()'
private func getAllAccounts() : async [Principal] {
// TODO: Change this to "true" for local testing.
let isLocal : Bool = true;
if (isLocal) {
let bootcampTestActor = await BootcampLocalActor.BootcampLocalActor();
let principals : [Principal] = await bootcampTestActor.getAllStudentsPrincipal();
for (principal in principals.vals()) {
// Debug.print("Creating ledger entry for principal: " # debug_show(principal));
let account : Account = Coin.getAccountFromPrincipal(principal);
// Debug.print("Account: " # debug_show(account));
ledger.put(account, 0)
};
// Debug.print("getAllAccounts() - ledger entries done");
return principals
} else {
// For the IC network.
let principals : [Principal] = await bcICNetworkCanister.getAllStudentsPrincipal();
for (principal in principals.vals()) {
ledger.put(Coin.getAccountFromPrincipal(principal), 0)
};
return principals
}
};
private func shuffle<T>(elements : [T]) : [T] {
var currentIndex = elements.size();
let seed : Blob = "\14\C9\72\09\03\D4\D5\72\82\95\E5\43\AF\FA\A9\44\49\2F\25\56\13\F3\6E\C7\B0\87\DC\76\08\69\14\CF";
let random = Random.rangeFrom(32, seed);
// between 0..4294967295
let max : Float = 4294967295;
let newRandom = Float.fromInt(random) / max;
let shuffled : [var T] = Array.thaw<T>(elements);
while (0 != currentIndex) {
var randomIndex = Int.abs(Float.toInt(Float.floor(newRandom * Float.fromInt(currentIndex))));
currentIndex -= 1;
var tempValue = elements[currentIndex];
shuffled[currentIndex] := shuffled[randomIndex];
shuffled[randomIndex] := tempValue;
// D.print(debug_show (randomIndex));
// D.print(debug_show (currentIndex));
// D.print(debug_show (tempValue.id))
};
return Array.freeze<T>(shuffled);
};
public query func seeAllLogMessages() : async [(Time.Time, Text)] {
logAndDebug("Displaying all log messages.");
return Iter.toArray<(Time.Time, Text)>(logs.entries())
};
public func clearLogMessages() : async () {
logAndDebug("Clearing all log messages.");
for (key in logs.keys()) {
logs.delete(key)
};
logsEntries := []
};
private func logAndDebug(message : Text) {
Log.logAndOrDebug(logs, message, true)
};
//---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
// REGION: SYSTEM MANAGEMENT ---------- ---------- ---------- ---------- ----------
//---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
// https://internetcomputer.org/docs/current/developer-docs/build/cdks/motoko-dfinity/heartbeats
// Called on every Internet Computer subnet heartbeat, by scheduling an asynchronous call to the heartbeat function.
// Due to its async return type, a heartbeat function may send further messages and await results.
// The result of a heartbeat call, including any trap or thrown error, is ignored.
// The implicit context switch inherent to calling every Motoko async function, means that the time the heartbeat body is executed may be later than the time the heartbeat was issued by the subnet.
// There are issues around hearbeat cycle burn rate (https://forum.dfinity.org/t/cycle-burn-rate-heartbeat/12090), so we won't be enabling this.
/*system func heartbeat() : async () {
let timestamp = Time.now();
// Debug.print("heartbeat - timestamp: " # debug_show(timestamp));
// await execute_accepted_proposals()
};*/
// Get the current system params
public query func get_system_params() : async Types.SystemParams {
system_params
};
func getMyPrincipal() : Principal {
return Principal.fromActor(Self)
};
// Update system params
public shared ({ caller }) func update_system_params(payload : Types.UpdateSystemParamsPayload) : async () {
// Only callable via proposal execution by this actor itself
if (caller != getMyPrincipal()) {
return
};
system_params := {}
};
// Get the cycles balance
public query func cycle_balance() : async Nat {
let balance = Cycles.balance();
Debug.print("Cycles balance: " # debug_show (balance));
return balance
};
// Receive cycles
public shared ({ caller }) func receive_cycles() : async Result.Result<Text, Text> {
let cycles = Cycles.available();
Debug.print("Received and accepted cycles: " # debug_show (cycles));
ignore Cycles.accept(cycles);
return #ok("Thanks. Accepted " # debug_show (cycles) # " cycles.")
};
public shared ({ caller }) func send_cycles(principalID : Text) : async Result.Result<Text, Text> {
Debug.print("Current balance: " # Nat.toText(Cycles.balance()));
let recipient : actor {
receive_cycles : () -> async Result.Result<Text, Text>
} = actor (principalID);
Cycles.add(1_000_000_100);
let send = await recipient.receive_cycles();
Debug.print("Unused balance: " # Nat.toText(Cycles.refunded()));
send
};
//---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
// REGION: UPGARDE MANAGEMENT ---------- ---------- ---------- ---------- ----------
//---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
// Make a final update to stable variables, before the runtime commits their values to Internet Computer stable memory, and performs an upgrade.
// So, here we want to take our values that are in our HashMap (and not stable) and put them into the stable array instead.
// Ref: https://internetcomputer.org/docs/current/developer-docs/build/cdks/motoko-dfinity/upgrades#preupgrade-and-postupgrade-system-methods
system func preupgrade() {
ledgerEntries := Iter.toArray(ledger.entries());
airdropEntries := Iter.toArray(airdrops.entries());
logsEntries := Iter.toArray(logs.entries())
};
// Runs after an upgrade has initialized the replacement actor, including its stable variables, but before executing any shared function call (or message) on that actor.
// Here, we want to reset the stable var, as we'll be storing the data to be used in our HashMap.
system func postupgrade() {
ledgerEntries := [];
airdropEntries := [];
logsEntries := [];
// Give the owner the initial allocated supply. This ensures the owner is only supplied with the initial once,
// and not on each deployment of the canister.
if (ledger.size() < 1) {
ledger.put(owner, ownerInitialSupply)
}
};
}