-
Notifications
You must be signed in to change notification settings - Fork 59
/
main.mo
179 lines (155 loc) · 5.3 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
import Result "mo:base/Result";
import Buffer "mo:base/Buffer";
import HashMap "mo:base/HashMap";
import Iter "mo:base/Iter";
import Principal "mo:base/Principal";
import Option "mo:base/Option";
import Types "types";
actor {
// For this level we need to make use of the code implemented in the previous projects.
// The voting system will make use of previous data structures and functions.
/////////////////
// TYPES //
///////////////
type Member = Types.Member;
type Result<Ok, Err> = Types.Result<Ok, Err>;
type HashMap<K, V> = Types.HashMap<K, V>;
type Proposal = Types.Proposal;
type ProposalContent = Types.ProposalContent;
type ProposalId = Types.ProposalId;
type Vote = Types.Vote;
/////////////////
// PROJECT #1 //
///////////////
let goals = Buffer.Buffer<Text>(0);
let name = "Motoko Bootcamp";
var manifesto = "Empower the next generation of builders and make the DAO-revolution a reality";
public shared query func getName() : async Text {
return name;
};
public shared query func getManifesto() : async Text {
return manifesto;
};
public func setManifesto(newManifesto : Text) : async () {
manifesto := newManifesto;
return;
};
public func addGoal(newGoal : Text) : async () {
goals.add(newGoal);
return;
};
public shared query func getGoals() : async [Text] {
Buffer.toArray(goals);
};
/////////////////
// PROJECT #2 //
///////////////
let members = HashMap.HashMap<Principal, Member>(0, Principal.equal, Principal.hash);
public shared ({ caller }) func addMember(member : Member) : async Result<(), Text> {
switch (members.get(caller)) {
case (null) {
members.put(caller, member);
return #ok();
};
case (?member) {
return #err("Member already exists");
};
};
};
public shared ({ caller }) func updateMember(member : Member) : async Result<(), Text> {
switch (members.get(caller)) {
case (null) {
return #err("Member does not exist");
};
case (?member) {
members.put(caller, member);
return #ok();
};
};
};
public shared ({ caller }) func removeMember() : async Result<(), Text> {
switch (members.get(caller)) {
case (null) {
return #err("Member does not exist");
};
case (?member) {
members.delete(caller);
return #ok();
};
};
};
public query func getMember(p : Principal) : async Result<Member, Text> {
switch (members.get(p)) {
case (null) {
return #err("Member does not exist");
};
case (?member) {
return #ok(member);
};
};
};
public query func getAllMembers() : async [Member] {
return Iter.toArray(members.vals());
};
public query func numberOfMembers() : async Nat {
return members.size();
};
/////////////////
// PROJECT #3 //
///////////////
let ledger = HashMap.HashMap<Principal, Nat>(0, Principal.equal, Principal.hash);
public query func tokenName() : async Text {
return "Motoko Bootcamp Token";
};
public query func tokenSymbol() : async Text {
return "MBT";
};
public func mint(owner : Principal, amount : Nat) : async Result<(), Text> {
let balance = Option.get(ledger.get(owner), 0);
ledger.put(owner, balance + amount);
return #ok();
};
public func burn(owner : Principal, amount : Nat) : async Result<(), Text> {
let balance = Option.get(ledger.get(owner), 0);
if (balance < amount) {
return #err("Insufficient balance to burn");
};
ledger.put(owner, balance - amount);
return #ok();
};
public shared ({ caller }) func transfer(from : Principal, to : Principal, amount : Nat) : async Result<(), Text> {
let balanceFrom = Option.get(ledger.get(from), 0);
let balanceTo = Option.get(ledger.get(to), 0);
if (balanceFrom < amount) {
return #err("Insufficient balance to transfer");
};
ledger.put(from, balanceFrom - amount);
ledger.put(to, balanceTo + amount);
return #ok();
};
public query func balanceOf(owner : Principal) : async Nat {
return (Option.get(ledger.get(owner), 0));
};
public query func totalSupply() : async Nat {
var total = 0;
for (balance in ledger.vals()) {
total += balance;
};
return total;
};
/////////////////
// PROJECT #4 //
///////////////
public shared ({ caller }) func createProposal(content : ProposalContent) : async Result<ProposalId, Text> {
return #err("Not implemented");
};
public query func getProposal(proposalId : ProposalId) : async ?Proposal {
return null;
};
public shared ({ caller }) func voteProposal(proposalId : ProposalId, yesOrNo : Bool) : async Result<(), Text> {
return #err("Not implemented");
};
public query func getAllProposals() : async [Proposal] {
return [];
};
};