-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.leo
350 lines (311 loc) · 11.4 KB
/
main.leo
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
// The 'arc20_0001' program.
/// Program implementing proposed token standard
///
/// This program is capable to send private credits to a contract that is driven by mpc address.
/// This program is has also transfer_from_public function that can be used by contracts to transfer tokens to themselves.
program arc20_0001.aleo {
/// Mapping of addresses to balances
mapping account: address => u64;
/// Mapping of total supply
mapping total_supply: bool => u64;
/// Name of the token 'Aleo Credits'
const NAME: field = 29591854713254390051098558719843710286620636002170764447558050801690808942592field;
/// Symbol of the token 'ALEO'
const SYMBOL: field = 29535093885169187707116277799267314899879263164026335641338301482164084015104field;
/// Signature
/// Signature is used to connect the company website with contract to minimize fraud.
/// See 2.2.3 of the standard
/// const COMPANY_SIGNATURE: signature = sign069ju4e8s66unu25celqycvsv3k9chdyz4n4sy62tx6wxj0u25vqp58hgu9hwyqc63qzxvjwesf2wz0krcvvw9kd9x0rsk4lwqn2acqhp9v0pdkhx6gvkanuuwratqmxa3du7l43c05253hhed9eg6ppzzfnjt06fpzp6msekdjxd36smjltndmxjndvv9x2uecsgngcwsc2qkns4afd;
/// Decimals of the token
/// The number of decimal digits expressing fractional units of the token.
const DECIMALS: u8 = 6u8;
/// ZERO_ADDRESS is the result of BHP256::hash_to_address(0group)
/// Used to signal that no valid address is pressent
const ZERO_ADDRESS : address = aleo1yr9ls3d7cn2x2sr359kqq7vjkwnzr4x0e24ju6ysj2pte4c99c8qx0rwwl;
/// A record that represents a credit.
record credits {
/// Mandatory owner of the credits
owner : address,
/// Optional contract that can control the credits
/// If contract is ZERO_ADDRESS then the credits can be fully controlled by the owner
/// If contract is not ZERO_ADDRESS then the credits will be encoded with owners keys, but only contract can call any of the functions of this program
contract : address,
/// Amount of credits
microcredits: u64 ,
}
/// A struct that represents a transfer of credits.
struct transfer_from {
/// Address from which credits are transferred
from : address,
/// Address to which credits are transferred
to : address,
/// Amount of credits
amount: u64 ,
/// Expiration block height of the transfer
expire: u32 ,
}
/// Transfer of credits in a publicly visible way
///
/// # Arguments
///
/// * `to` - Address from which credits are transferred
/// * `amount` - Amount of credits to be transferred
transition transfer_public(public to: address, public amount: u64) {
return then finalize(self.caller, to, amount);
} finalize transfer_public(caller:address, to: address, amount: u64) {
Mapping::set(account, caller, Mapping::get (account , caller ) - amount);
Mapping::set(account, to , Mapping::get_or_use(account , to , 0u64) + amount);
}
/// Transfer of credits in a publicly visible way
///
/// This transition can be initiated by a third party. Which makes it possible to transfer funds to contracts in one step.
///
/// # Arguments
///
/// * `to` - Address to which credits are transferred
/// * `amount` - Amount of credits to be transferred
/// * `from` - Address from which credits are transferred
/// * `expire` - Expiration block height of the transfer after which the authorization is not valid anymore
/// * `authorization` - Autorization signature created by the private key associated with the `from` address
transition transfer_from_public(
public to: address,
public amount: u64,
public from: address,
public expire: u32,
authorization: signature,
) {
let tx_hash : field = BHP256::hash_to_field(transfer_from { from, to, amount, expire });
assert(authorization.verify(from, tx_hash));
return then finalize(to, amount, from, expire);
} finalize transfer_from_public(to: address, amount: u64, from: address, expire: u32) {
assert(block.height < expire);
Mapping::set(account, from, Mapping::get (account , from ) - amount);
Mapping::set(account, to , Mapping::get_or_use(account , to , 0u64) + amount);
}
/// Transfer of credits to accounts in a way that all tx data is hidden from the public
///
/// # Arguments
///
/// * `to` - Address from which credits are transferred
/// * `amount` - Amount of credits to be transferred
/// * `credit` - Credits to be transferred
transition transfer_private(
to: address,
amount: u64,
credit: credits
) -> (credits, credits) {
if self.caller == self.signer {
assert(credit.contract == ZERO_ADDRESS);
} else {
assert(
self.caller == credit.contract ||
self.signer == credit.owner &&
credit.contract == ZERO_ADDRESS
);
}
return (
credits {
owner : credit.owner ,
contract : credit.contract ,
microcredits: credit.microcredits - amount,
} ,
credits {
owner : to ,
contract : ZERO_ADDRESS ,
microcredits: amount ,
}
);
}
/// Transfer of credits to contracts in a way that all tx data is hidden from the public
///
/// This transition can only be called by a contract, otherwise it will fail.
///
/// To transfer private credits implemented in TOKEN.aleo to contract HANDLER.aleo that is driven by mpc address: `mpc_address`, the following must be done:
/// user must call Handler.aleo/deposit(record, amount)
/// The transition looks like this:
/// program HANDLER.aleo {
/// transition deposit(record, amount) {
/// ...
/// TOKEN.aleo/transfer_private(mpc_address, self_address, amount, record);
/// ...
/// }
/// }
/// # Arguments
///
/// * `to` - Address from which credits are transferred
///
/// * `contract` - Address of the contract that will handle the credits,
/// if sent from an account it always must be `ZERO_ADDRESS`
///
/// * `amount` - Amount of credits to be transferred
/// * `credit` - Credits to be transferred
transition transfer_private_contract(
to: address,
contract: address,
amount: u64,
credit: credits
) -> (credits, credits) {
assert(self.caller != self.signer);
assert(
self.caller == credit.contract ||
self.signer == credit.owner &&
credit.contract == ZERO_ADDRESS
);
return (
credits {
owner : credit.owner ,
contract : credit.contract ,
microcredits: credit.microcredits - amount,
} ,
credits {
owner : to ,
contract ,
microcredits: amount ,
}
);
}
/// Transfer of credits from private to public domain
///
/// # Arguments
///
/// * `credit` - Credits record to be transferred
/// * `to` - Address from which credits are transferred
/// * `amount` - Amount of credits to be transferred
///
/// # Returns
///
/// * `credits` - Remining private credit record
transition transfer_private_to_public(credit: credits, public to: address, public amount: u64) -> credits {
if self.caller == self.signer {
assert(credit.contract == ZERO_ADDRESS );
} else {
assert(
self.caller == credit.contract ||
self.signer == credit.owner &&
credit.contract == ZERO_ADDRESS );
}
let remaining : credits =
credits {
owner : credit.owner ,
contract : credit.contract ,
microcredits: credit.microcredits - amount,
};
return remaining then finalize(to, amount);
} finalize transfer_private_to_public(to: address, amount: u64) {
Mapping::set(account, to, Mapping::get_or_use(account, to, 0u64) + amount);
}
/// Transfer of credits from public to private domain
///
/// # Arguments
///
/// * `to` - Address from which credits are transferred
/// * `amount` - Amount of credits to be transferred
///
/// # Returns
///
/// * `credits` - Private credit record
transition transfer_public_to_private(to: address, public amount: u64) -> credits {
let contract : address = ZERO_ADDRESS;
if self.caller != self.signer {
contract = self.caller;
}
return credits {
owner : to ,
contract ,
microcredits: amount,
} then finalize(self.caller, to, amount);
} finalize transfer_public_to_private(caller:address, to: address, amount: u64) {
Mapping::set(account, caller, Mapping::get(account, caller) - amount);
}
/// Join two credit records into one
///
/// # Arguments
///
/// * `credit1` - First credit record
/// * `credit2` - Second credit record
///
/// # Returns
///
/// * `credits` - Joined credit record
transition join(credit1: credits, credit2: credits) -> credits {
if(self.caller != self.signer) {
assert(
credit1.contract == self.caller &&
credit2.contract == self.caller
);
}
return credits {
owner : credit1.owner ,
contract : credit1.contract ,
microcredits: credit1.microcredits + credit2.microcredits,
};
}
/// Split credit record into two
///
/// # Arguments
///
/// * `credit` - Credit record to be split
/// * `amount` - Amount of credits to be split off
///
/// # Returns
///
/// * `credits` - Remaining credit record
/// * `credits` - Split off credit record
transition split(credit: credits, amount: u64) -> (credits, credits) {
if(self.caller != self.signer) {
assert(credit.contract == self.caller);
}
return (
credits {
owner : credit.owner ,
contract : credit.contract ,
microcredits: credit.microcredits - amount,
},
credits {
owner : credit.owner ,
contract : credit.contract,
microcredits: amount ,
}
);
}
/// Mint new credits
///
/// # Arguments
///
/// * `amount` - Amount of credits to be minted
transition mint(amount: u64) {
return then finalize(self.caller, amount);
} finalize mint(caller:address, amount: u64) {
Mapping::set(account , caller, Mapping::get_or_use(account , caller, 0u64) + amount);
Mapping::set(total_supply, true , Mapping::get_or_use(total_supply, true , 0u64) + amount);
}
/// Burn credits
///
/// # Arguments
///
/// * `amount` - Amount of credits to be minted
///
transition burn(amount: u64) {
return then finalize(self.caller, amount);
} finalize burn(caller:address, amount: u64) {
Mapping::set(account , caller, Mapping::get( account , caller ) - amount);
Mapping::set(total_supply, true , Mapping::get_or_use(total_supply, true , 0u64) - amount);
}
//can't be implemented currently as there is no access to mappings in transitions
//transition total_supply() -> u64 {}
/// Create a hash of transfer parameters for signing
///
/// # Arguments
///
/// * `to` - Address to which credits are transferred
/// * `amount` - Amount of credits to be transferred
/// * `from` - Address from which credits are transferred
/// * `expire` - Expiration block height of the transfer after which the authorization is not valid anymore
///
/// # Returns
///
/// * `field` - Hash of transfer parameters that can be signed offline later
transition hash_to_sign(to: address, amount: u64, from: address, expire: u32) -> field {
return BHP256::hash_to_field(transfer_from { from, to, amount, expire });
}
}