-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathmain.leo
More file actions
241 lines (177 loc) · 8.64 KB
/
main.leo
File metadata and controls
241 lines (177 loc) · 8.64 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
import workshop_ofac.aleo;
/*
====================================================================================================================
TASK 0: Name Your Token
TODO:
1. Choose what your token program will be named! This should be longer than 10 characters to avoid additional
network fees and not conflict with any existing program on the network
- ex) `dogecoin_token.aleo`
2. Change the name of the program below
- ex) `program dogecoin_token.aleo {`
3. Change the "program" field in the program.json file to match the name of your new program
====================================================================================================================
*/
program sekinat_token.aleo {
mapping balances: address => u64; // On-chain mapping between user addresses and public token balances
record Token {
owner: address, // The token owner
amount: u64, // The token amount
}
@noupgrade
async constructor() {}
/*
====================================================================================================================
TASK 1: Mint (Public)
This feature mints new tokens by updating the public mapping value of the recipient
TODO:
`mint_public`:
1. Query the `address_check` function in `workshop_ofac.aleo` with the `recipient` field
2. Pass the returned Future and other appropriate fields to the `mint_public_onchain` function
`mint_public_onchain`:
1. Await the `address_check` Future
2. Set the value for `recipient` in the `balances` mapping to the current value plus `amount`
====================================================================================================================
*/
async transition mint_public(
public recipient: address,
public amount: u64
) -> Future {
let address_check : Future = workshop_ofac.aleo/address_check(recipient);
// TODO: Fill in here
return mint_public_onchain(recipient, amount, address_check);
}
async function mint_public_onchain(
public recipient: address,
public amount: u64,
public address_check : Future
) {
address_check.await();
// TODO: Fill in here
let current_amount: u64 = Mapping::get_or_use(balances, recipient, 0u64 );
Mapping::set(balances, recipient, current_amount + amount);
}
/*
====================================================================================================================
TASK 2: Mint (Private)
This feature mints new tokens by initializing a new record with the specified amount of tokens for the recipient.
TODOs:
`mint_private`:
1. Query the `address_check` function in `workshop_ofac.aleo` with the `recipient` field
2. Initialize a Token record with the `recipient` as the owner and the `amount` as the amount
3. Return the Token record and pass the Future to the `mint_private_onchain` function
`mint_private_onchain`:
1. Await the `address_check` Future
====================================================================================================================
*/
async transition mint_private(
private recipient: address,
private amount: u64
) -> (Token,Future) {
// TODO: Fill in here
let address_check:Future = workshop_ofac.aleo/address_check(recipient);
let token: Token = Token{
owner:recipient,
amount:amount,
};
return (token, mint_private_onchain(address_check));
}
async function mint_private_onchain(
address_check : Future
){
// TODO: Fill in here
address_check.await();
}
/*
====================================================================================================================
TASK 3: Transfer (Public)
This feature publicly transfers tokens between two users by deducting the transfer amount from the sender's balance
in the public mapping and adding the same value to the recipient's balance
TODO:
`transfer_public`:
1. Query the `address_check` function in `workshop_ofac.aleo` with the `recipient` field
2. Pass the returned Future and other appropriate fields to the `transfer_public_onchain` function
- For the `sender` field, you can use `self.signer` to specify the address that initialized the function call
`transfer_public_onchain`:
1. Await the `address_check` Future
2. Set the value for `sender` in the `balances` mapping to the sender's current value minus `amount`
3. Set the value for `recipient` in the `balances` mapping to the recipient's current value plus `amount`
====================================================================================================================
*/
async transition transfer_public(
public recipient: address,
public amount: u64
) -> Future {
// TODO: Fill in here
let address_check:Future = workshop_ofac.aleo/address_check(recipient);
return transfer_public_onchain(self.signer, recipient, amount, address_check);
}
async function transfer_public_onchain(
public sender: address,
public recipient: address,
public amount: u64,
public address_check : Future
) {
// TODO: Fill in here
address_check.await();
let sender_amount: u64 = Mapping::get_or_use(balances, sender, 0u64);
Mapping::set(balances, sender, sender_amount - amount);
let reciever_amount:u64 = Mapping::get_or_use(balances, recipient, 0u64);
Mapping::set(balances, recipient, reciever_amount + amount);
}
/*
====================================================================================================================
TASK 4: Transfer (Private)
This feature privately transfers tokens between two users by consuming the sender's Token record and producing two
new Token records. The first is a Token record for the recipient with the amount sent, and the second is a new Token
record for the sender with the leftover amount.
TODO:
`transfer_private:
1. Query the `address_check` function in `workshop_ofac.aleo` with the `recipient` field
2. Initialize a Token record with the `recipient` as the owner and the `amount` as the amount
3. Initialize a Token record with the `sender` as the owner and the remaining balance as the amount
4. Return both Token records and pass the Future to the `mint_private_onchain` function
`transfer_private_onchain`:
1. Await the `address_check` Future
====================================================================================================================
*/
async transition transfer_private(
private sender: Token,
private recipient: address,
private amount: u64
) -> (Token, Token, Future) {
// TODO: Fill in here
let address_check: Future = workshop_ofac.aleo/address_check(recipient);
let difference: u64 = sender.amount - amount;
let remaining: Token = Token {
owner: sender.owner,
amount: difference
};
let transferred: Token = Token {
owner: recipient,
amount: amount,
};
return (remaining, transferred, transfer_private_onchain(address_check));
}
async function transfer_private_onchain(
public address_check : Future
){
// TODO: Fill in here
address_check.await();
}
/*
====================================================================================================================
BONUS TASKS:
For those looking for an extra challenge, implement the following additional functions:
- transfer_private_to_public()
- transfer_public_to_private()
Additionally, in practice you'll likely want to have some advanced functionality, such as allowing authorized users and programs
to manage your funds:
- approve_public [will require an additional `allowances` mapping]
- approve_private
- transfer_from() [Aleo only supports public transfer_from due to how records are implemented]
- burn_public()
- burn_private()
Don't forget to include OFAC compliance checks!
====================================================================================================================
*/
}