@claritydao/midnight-agora-contracts 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,570 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma language_version 0.18.0;
4
+
5
+ /**
6
+ * @module FungibleToken
7
+ * @description An unshielded FungibleToken library.
8
+ *
9
+ * @notice One notable difference regarding this implementation and the EIP20 spec
10
+ * consists of the token size. Uint<128> is used as the token size because Uint<256>
11
+ * cannot be supported.
12
+ * This is due to encoding limits on the midnight circuit backend:
13
+ * https://github.com/midnightntwrk/compactc/issues/929
14
+ *
15
+ * @notice At the moment Midnight does not support contract-to-contract communication, but
16
+ * there are ongoing efforts to enable this in the future. Thus, the main circuits of this module
17
+ * restrict developers from sending tokens to contracts; however, we provide developers
18
+ * the ability to experiment with sending tokens to contracts using the `_unsafe`
19
+ * transfer methods. Once contract-to-contract communication is available we will follow the
20
+ * deprecation plan outlined below:
21
+ *
22
+ * Initial Minor Version Change:
23
+ *
24
+ * - Mark _unsafeFN as deprecated and emit a warning if possible.
25
+ * - Keep its implementation intact so existing callers continue to work.
26
+ *
27
+ * Later Major Version Change:
28
+ *
29
+ * - Drop _unsafeFN and remove `isContract` guard from `FN`.
30
+ * - By this point, anyone using _unsafeFN should have migrated to the now C2C-capable `FN`.
31
+ *
32
+ * Due to the vast incompatibilities with the EIP20 spec, it is our
33
+ * opinion that this implementation should not be called ERC20 at this time
34
+ * as this would be both very confusing and misleading. This may change as more
35
+ * features become available. The list of missing features is as follows:
36
+ *
37
+ * - Full uint256 support.
38
+ * - Events.
39
+ * - Contract-to-contract calls.
40
+ */
41
+ module FungibleToken {
42
+ import CompactStandardLibrary;
43
+ import "../utils/Initializable";
44
+ import "../utils/Utils";
45
+
46
+ /**
47
+ * @description Mapping from account addresses to their token balances.
48
+ * @type {Either<ZswapCoinPublicKey, ContractAddress>} account - The account address.
49
+ * @type {Uint<128>} balance - The balance of the account.
50
+ * @type {Map<account, balance>}
51
+ * @type {Map<Either<ZswapCoinPublicKey, ContractAddress>, Uint<128>>} _balances
52
+ */
53
+ export ledger _balances: Map<Either<ZswapCoinPublicKey, ContractAddress>, Uint<128>>;
54
+ /**
55
+ * @description Mapping from owner accounts to spender accounts and their allowances.
56
+ * @type {Either<ZswapCoinPublicKey, ContractAddress>} account - The owner account address.
57
+ * @type {Either<ZswapCoinPublicKey, ContractAddress>} spender - The spender account address.
58
+ * @type {Uint<128>} allowance - The amount allowed to be spent by the spender.
59
+ * @type {Map<account, Map<spender, allowance>>}
60
+ * @type {Map<Either<ZswapCoinPublicKey, ContractAddress>, Map<Either<ZswapCoinPublicKey, ContractAddress>, Uint<128>>>} _allowances
61
+ */
62
+ export ledger _allowances: Map<Either<ZswapCoinPublicKey, ContractAddress>, Map<Either<ZswapCoinPublicKey, ContractAddress>, Uint<128>>>;
63
+
64
+ export ledger _totalSupply: Uint<128>;
65
+
66
+ export sealed ledger _name: Opaque<"string">;
67
+ export sealed ledger _symbol: Opaque<"string">;
68
+ export sealed ledger _decimals: Uint<8>;
69
+
70
+ /**
71
+ * @description Initializes the contract by setting the name, symbol, and decimals.
72
+ * @dev This MUST be called in the implementing contract's constructor. Failure to do so
73
+ * can lead to an irreparable contract.
74
+ *
75
+ * @param {Opaque<"string">} name_ - The name of the token.
76
+ * @param {Opaque<"string">} symbol_ - The symbol of the token.
77
+ * @param {Uint<8>} decimals_ - The number of decimals used to get the user representation.
78
+ * @return {[]} - None.
79
+ */
80
+ export circuit initialize(
81
+ name_: Opaque<"string">,
82
+ symbol_: Opaque<"string">,
83
+ decimals_:Uint<8>
84
+ ): [] {
85
+ _name = name_;
86
+ _symbol = symbol_;
87
+ _decimals = decimals_;
88
+ }
89
+
90
+ /**
91
+ * @description Returns the token name.
92
+ *
93
+ * Requirements:
94
+ *
95
+ * - Contract is initialized.
96
+ *
97
+ * @return {Opaque<"string">} - The token name.
98
+ */
99
+ export circuit name(): Opaque<"string"> {
100
+
101
+ return _name;
102
+ }
103
+
104
+ /**
105
+ * @description Returns the symbol of the token.
106
+ *
107
+ * Requirements:
108
+ *
109
+ * - Contract is initialized.
110
+ *
111
+ * @return {Opaque<"string">} - The token name.
112
+ */
113
+ export circuit symbol(): Opaque<"string"> {
114
+
115
+ return _symbol;
116
+ }
117
+
118
+ /**
119
+ * @description Returns the number of decimals used to get its user representation.
120
+ *
121
+ * Requirements:
122
+ *
123
+ * - Contract is initialized.
124
+ *
125
+ * @return {Uint<8>} - The account's token balance.
126
+ */
127
+ export circuit decimals(): Uint<8> {
128
+
129
+ return _decimals;
130
+ }
131
+
132
+ /**
133
+ * @description Returns the value of tokens in existence.
134
+ *
135
+ * Requirements:
136
+ *
137
+ * - Contract is initialized.
138
+ *
139
+ * @return {Uint<128>} - The total supply of tokens.
140
+ */
141
+ export circuit totalSupply(): Uint<128> {
142
+
143
+ return _totalSupply;
144
+ }
145
+
146
+ /**
147
+ * @description Returns the value of tokens owned by `account`.
148
+ *
149
+ * @dev Manually checks if `account` is a key in the map and returns 0 if it is not.
150
+ *
151
+ * Requirements:
152
+ *
153
+ * - Contract is initialized.
154
+ *
155
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} account - The public key or contract address to query.
156
+ * @return {Uint<128>} - The account's token balance.
157
+ */
158
+ export circuit balanceOf(account: Either<ZswapCoinPublicKey, ContractAddress>): Uint<128> {
159
+
160
+ if (!_balances.member(disclose(account))) {
161
+ return 0;
162
+ }
163
+
164
+ return _balances.lookup(disclose(account));
165
+ }
166
+
167
+ /**
168
+ * @description Moves a `value` amount of tokens from the caller's account to `to`.
169
+ *
170
+ * @notice Transfers to contract addresses are currently disallowed until contract-to-contract
171
+ * interactions are supported in Compact. This restriction prevents assets from
172
+ * being inadvertently locked in contracts that cannot currently handle token receipt.
173
+ *
174
+ * Requirements:
175
+ *
176
+ * - Contract is initialized.
177
+ * - `to` is not a ContractAddress.
178
+ * - `to` is not the zero address.
179
+ * - The caller has a balance of at least `value`.
180
+ *
181
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} to - The recipient of the transfer, either a user or a contract.
182
+ * @param {Uint<128>} value - The amount to transfer.
183
+ * @return {Boolean} - As per the IERC20 spec, this MUST return true.
184
+ */
185
+ export circuit transfer(to: Either<ZswapCoinPublicKey, ContractAddress>, value: Uint<128>): Boolean {
186
+
187
+ assert (!isContractAddress(to), "FungibleToken: Unsafe Transfer");
188
+ return _unsafeTransfer(to, value);
189
+ }
190
+
191
+ /**
192
+ * @description Unsafe variant of `transfer` which allows transfers to contract addresses.
193
+ *
194
+ * @warning Transfers to contract addresses are considered unsafe because contract-to-contract
195
+ * calls are not currently supported. Tokens sent to a contract address may become irretrievable.
196
+ * Once contract-to-contract calls are supported, this circuit may be deprecated.
197
+ *
198
+ * Requirements:
199
+ *
200
+ * - Contract is initialized.
201
+ * - `to` is not the zero address.
202
+ * - The caller has a balance of at least `value`.
203
+ *
204
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} to - The recipient of the transfer, either a user or a contract.
205
+ * @param {Uint<128>} value - The amount to transfer.
206
+ * @return {Boolean} - As per the IERC20 spec, this MUST return true.
207
+ */
208
+ export circuit _unsafeTransfer(to: Either<ZswapCoinPublicKey, ContractAddress>, value: Uint<128>): Boolean {
209
+
210
+ const owner = left<ZswapCoinPublicKey, ContractAddress>(ownPublicKey());
211
+ _unsafeUncheckedTransfer(owner, to, value);
212
+ return true;
213
+ }
214
+
215
+ /**
216
+ * @description Returns the remaining number of tokens that `spender` will be allowed to spend on behalf of `owner`
217
+ * through `transferFrom`. This value changes when `approve` or `transferFrom` are called.
218
+ *
219
+ * @dev Manually checks if `owner` and `spender` are keys in the map and returns 0 if they are not.
220
+ *
221
+ * Requirements:
222
+ *
223
+ * - Contract is initialized.
224
+ *
225
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} owner - The public key or contract address of approver.
226
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} spender - The public key or contract address of spender.
227
+ * @return {Uint<128>} - The `spender`'s allowance over `owner`'s tokens.
228
+ */
229
+ export circuit allowance(
230
+ owner: Either<ZswapCoinPublicKey, ContractAddress>,
231
+ spender: Either<ZswapCoinPublicKey, ContractAddress>
232
+ ): Uint<128> {
233
+
234
+ if (!_allowances.member(disclose(owner))) {
235
+ return 0;
236
+ }
237
+ if (!_allowances.lookup(disclose(owner)).member(disclose(spender))) {
238
+ return 0;
239
+ }
240
+ return _allowances.lookup(disclose(owner)).lookup(disclose(spender));
241
+ }
242
+
243
+ /**
244
+ * @description Sets a `value` amount of tokens as allowance of `spender` over the caller's tokens.
245
+ *
246
+ * Requirements:
247
+ *
248
+ * - Contract is initialized.
249
+ * - `spender` is not the zero address.
250
+ *
251
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} spender - The Zswap key or ContractAddress that may spend on behalf of the caller.
252
+ * @param {Uint<128>} value - The amount of tokens the `spender` may spend.
253
+ * @return {Boolean} - Returns a boolean value indicating whether the operation succeeded.
254
+ */
255
+ export circuit approve(spender: Either<ZswapCoinPublicKey, ContractAddress>, value: Uint<128>): Boolean {
256
+
257
+
258
+ const owner = left<ZswapCoinPublicKey, ContractAddress>(ownPublicKey());
259
+ _approve(disclose(owner), disclose(spender), disclose(value));
260
+ return true;
261
+ }
262
+
263
+ /**
264
+ * @description Moves `value` tokens from `from` to `to` using the allowance mechanism.
265
+ * `value` is the deducted from the caller's allowance.
266
+ *
267
+ * @notice Transfers to contract addresses are currently disallowed until contract-to-contract
268
+ * interactions are supported in Compact. This restriction prevents assets from
269
+ * being inadvertently locked in contracts that cannot currently handle token receipt.
270
+ *
271
+ * Requirements:
272
+ *
273
+ * - Contract is initialized.
274
+ * - `from` is not the zero address.
275
+ * - `from` must have a balance of at least `value`.
276
+ * - `to` is not the zero address.
277
+ * - `to` is not a ContractAddress.
278
+ * - The caller has an allowance of `from`'s tokens of at least `value`.
279
+ *
280
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} from - The current owner of the tokens for the transfer, either a user or a contract.
281
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} to - The recipient of the transfer, either a user or a contract.
282
+ * @param {Uint<128>} value - The amount to transfer.
283
+ * @return {Boolean} - As per the IERC20 spec, this MUST return true.
284
+ */
285
+ export circuit transferFrom(
286
+ from: Either<ZswapCoinPublicKey, ContractAddress>,
287
+ to: Either<ZswapCoinPublicKey, ContractAddress>,
288
+ value: Uint<128>
289
+ ): Boolean {
290
+
291
+ assert (!isContractAddress(to), "FungibleToken: Unsafe Transfer");
292
+ return _unsafeTransferFrom(disclose(from), disclose(to), disclose(value));
293
+ }
294
+
295
+ /**
296
+ * @description Unsafe variant of `transferFrom` which allows transfers to contract addresses.
297
+ *
298
+ * @warning Transfers to contract addresses are considered unsafe because contract-to-contract
299
+ * calls are not currently supported. Tokens sent to a contract address may become irretrievable.
300
+ * Once contract-to-contract calls are supported, this circuit may be deprecated.
301
+ *
302
+ * Requirements:
303
+ *
304
+ * - Contract is initialized.
305
+ * - `from` is not the zero address.
306
+ * - `from` must have a balance of at least `value`.
307
+ * - `to` is not the zero address.
308
+ * - The caller has an allowance of `from`'s tokens of at least `value`.
309
+ *
310
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} from - The current owner of the tokens for the transfer, either a user or a contract.
311
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} to - The recipient of the transfer, either a user or a contract.
312
+ * @param {Uint<128>} value - The amount to transfer.
313
+ * @return {Boolean} - As per the IERC20 spec, this MUST return true.
314
+ */
315
+ export circuit _unsafeTransferFrom(
316
+ from: Either<ZswapCoinPublicKey, ContractAddress>,
317
+ to: Either<ZswapCoinPublicKey, ContractAddress>,
318
+ value: Uint<128>
319
+ ): Boolean {
320
+
321
+
322
+ const spender = left<ZswapCoinPublicKey, ContractAddress>(ownPublicKey());
323
+ _spendAllowance(disclose(from), disclose(spender), disclose(value));
324
+ _unsafeUncheckedTransfer(disclose(from), disclose(to), disclose(value));
325
+ return true;
326
+ }
327
+
328
+ /**
329
+ * @description Moves a `value` amount of tokens from `from` to `to`.
330
+ * This circuit is equivalent to {transfer}, and can be used to
331
+ * e.g. implement automatic token fees, slashing mechanisms, etc.
332
+ *
333
+ * @notice Transfers to contract addresses are currently disallowed until contract-to-contract
334
+ * interactions are supported in Compact. This restriction prevents assets from
335
+ * being inadvertently locked in contracts that cannot currently handle token receipt.
336
+ *
337
+ * Requirements:
338
+ *
339
+ * - Contract is initialized.
340
+ * - `from` is not be the zero address.
341
+ * - `from` must have at least a balance of `value`.
342
+ * - `to` must not be the zero address.
343
+ * - `to` must not be a ContractAddress.
344
+ *
345
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} from - The owner of the tokens to transfer.
346
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} to - The receipient of the transferred tokens.
347
+ * @param {Uint<128>} value - The amount of tokens to transfer.
348
+ * @return {[]} - None.
349
+ */
350
+ export circuit _transfer(
351
+ from: Either<ZswapCoinPublicKey, ContractAddress>,
352
+ to: Either<ZswapCoinPublicKey, ContractAddress>,
353
+ value: Uint<128>
354
+ ): [] {
355
+
356
+ assert (!isContractAddress(to), "FungibleToken: Unsafe Transfer");
357
+ _unsafeUncheckedTransfer(from, to, value);
358
+ }
359
+
360
+ /**
361
+ * @description Unsafe variant of `transferFrom` which allows transfers to contract addresses.
362
+ *
363
+ * @warning Transfers to contract addresses are considered unsafe because contract-to-contract
364
+ * calls are not currently supported. Tokens sent to a contract address may become irretrievable.
365
+ * Once contract-to-contract calls are supported, this circuit may be deprecated.
366
+ *
367
+ * Requirements:
368
+ *
369
+ * - Contract is initialized.
370
+ * - `from` is not the zero address.
371
+ * - `to` is not the zero address.
372
+ *
373
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} from - The owner of the tokens to transfer.
374
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} to - The receipient of the transferred tokens.
375
+ * @param {Uint<128>} value - The amount of tokens to transfer.
376
+ * @return {[]} - None.
377
+ */
378
+ export circuit _unsafeUncheckedTransfer(
379
+ from: Either<ZswapCoinPublicKey, ContractAddress>,
380
+ to: Either<ZswapCoinPublicKey, ContractAddress>,
381
+ value: Uint<128>
382
+ ): [] {
383
+
384
+ assert (!isKeyOrAddressZero(disclose(from)), "FungibleToken: invalid sender");
385
+ assert (!isKeyOrAddressZero(disclose(to)), "FungibleToken: invalid receiver");
386
+
387
+ _update(disclose(from), disclose(to), disclose(value));
388
+ }
389
+
390
+ /**
391
+ * @description Transfers a `value` amount of tokens from `from` to `to`, or alternatively mints (or burns) if `from`
392
+ * (or `to`) is the zero address.
393
+ * @dev Checks for a mint overflow in order to output a more readable error message.
394
+ *
395
+ * Requirements:
396
+ *
397
+ * - Contract is initialized.
398
+ *
399
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} from - The original owner of the tokens moved (which is 0 if tokens are minted).
400
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} to - The recipient of the tokens moved (which is 0 if tokens are burned).
401
+ * @param {Uint<128>} value - The amount of tokens moved from `from` to `to`.
402
+ * @return {[]} - None.
403
+ */
404
+ circuit _update(
405
+ from: Either<ZswapCoinPublicKey, ContractAddress>,
406
+ to: Either<ZswapCoinPublicKey, ContractAddress>,
407
+ value: Uint<128>
408
+ ): [] {
409
+
410
+ if (isKeyOrAddressZero(disclose(from))) {
411
+ // Mint
412
+ const MAX_UINT128 = 340282366920938463463374607431768211455;
413
+ assert (MAX_UINT128 - _totalSupply >= disclose(value), "FungibleToken: arithmetic overflow");
414
+
415
+ _totalSupply = _totalSupply + disclose(value) as Uint<128>;
416
+ } else {
417
+ const fromBal = balanceOf(disclose(from));
418
+ assert (fromBal >= disclose(value), "FungibleToken: insufficient balance");
419
+ _balances.insert(disclose(from), fromBal - disclose(value) as Uint<128>);
420
+ }
421
+
422
+ if (isKeyOrAddressZero(disclose(to))) {
423
+ // Burn
424
+ _totalSupply = _totalSupply - disclose(value) as Uint<128>;
425
+ } else {
426
+ const toBal = balanceOf(disclose(to));
427
+ _balances.insert(disclose(to), toBal + disclose(value) as Uint<128>);
428
+ }
429
+ }
430
+
431
+ /**
432
+ * @description Creates a `value` amount of tokens and assigns them to `account`,
433
+ * by transferring it from the zero address. Relies on the `update` mechanism.
434
+ *
435
+ * @notice Transfers to contract addresses are currently disallowed until contract-to-contract
436
+ * interactions are supported in Compact. This restriction prevents assets from
437
+ * being inadvertently locked in contracts that cannot currently handle token receipt.
438
+ *
439
+ * Requirements:
440
+ *
441
+ * - Contract is initialized.
442
+ * - `to` is not a ContractAddress.
443
+ * - `account` is not the zero address.
444
+ *
445
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} account - The recipient of tokens minted.
446
+ * @param {Uint<128>} value - The amount of tokens minted.
447
+ * @return {[]} - None.
448
+ */
449
+ export circuit _mint(
450
+ account: Either<ZswapCoinPublicKey, ContractAddress>,
451
+ value: Uint<128>
452
+ ): [] {
453
+
454
+ assert (!isContractAddress(disclose(account)), "FungibleToken: Unsafe Transfer");
455
+ _unsafeMint(disclose(account), disclose(value));
456
+ }
457
+
458
+ /**
459
+ * @description Unsafe variant of `_mint` which allows transfers to contract addresses.
460
+ *
461
+ * @warning Transfers to contract addresses are considered unsafe because contract-to-contract
462
+ * calls are not currently supported. Tokens sent to a contract address may become irretrievable.
463
+ * Once contract-to-contract calls are supported, this circuit may be deprecated.
464
+ *
465
+ * Requirements:
466
+ *
467
+ * - Contract is initialized.
468
+ * - `account` is not the zero address.
469
+ *
470
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} account - The recipient of tokens minted.
471
+ * @param {Uint<128>} value - The amount of tokens minted.
472
+ * @return {[]} - None.
473
+ */
474
+ export circuit _unsafeMint(
475
+ account: Either<ZswapCoinPublicKey, ContractAddress>,
476
+ value: Uint<128>
477
+ ): [] {
478
+
479
+ assert (!isKeyOrAddressZero(disclose(account)), "FungibleToken: invalid receiver");
480
+ _update(burnAddress(), disclose(account), disclose(value));
481
+ }
482
+
483
+ /**
484
+ * @description Destroys a `value` amount of tokens from `account`, lowering the total supply.
485
+ * Relies on the `_update` mechanism.
486
+ *
487
+ * Requirements:
488
+ *
489
+ * - Contract is initialized.
490
+ * - `account` is not the zero address.
491
+ * - `account` must have at least a balance of `value`.
492
+ *
493
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} account - The target owner of tokens to burn.
494
+ * @param {Uint<128>} value - The amount of tokens to burn.
495
+ * @return {[]} - None.
496
+ */
497
+ export circuit _burn(
498
+ account: Either<ZswapCoinPublicKey, ContractAddress>,
499
+ value: Uint<128>
500
+ ): [] {
501
+
502
+ assert (!isKeyOrAddressZero(disclose(account)), "FungibleToken: invalid sender");
503
+ _update(disclose(account), burnAddress(), disclose(value));
504
+ }
505
+
506
+ /**
507
+ * @description Sets `value` as the allowance of `spender` over the `owner`'s tokens.
508
+ * This circuit is equivalent to `approve`, and can be used to
509
+ * e.g. set automatic allowances for certain subsystems, etc.
510
+ *
511
+ * Requirements:
512
+ *
513
+ * - Contract is initialized.
514
+ * - `owner` is not the zero address.
515
+ * - `spender` is not the zero address.
516
+ *
517
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} owner - The owner of the tokens.
518
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} spender - The spender of the tokens.
519
+ * @param {Uint<128>} value - The amount of tokens `spender` may spend on behalf of `owner`.
520
+ * @return {[]} - None.
521
+ */
522
+ export circuit _approve(
523
+ owner: Either<ZswapCoinPublicKey, ContractAddress>,
524
+ spender: Either<ZswapCoinPublicKey, ContractAddress>,
525
+ value: Uint<128>
526
+ ): [] {
527
+
528
+ assert (!isKeyOrAddressZero(disclose(owner)), "FungibleToken: invalid owner");
529
+ assert (!isKeyOrAddressZero(disclose(spender)), "FungibleToken: invalid spender");
530
+ if (!_allowances.member(disclose(owner))) {
531
+ // If owner doesn't exist, create and insert a new sub-map directly
532
+ _allowances.insert(disclose(owner), default<Map<Either<ZswapCoinPublicKey, ContractAddress>, Uint<128>>>);
533
+ }
534
+ _allowances.lookup(disclose(owner)).insert(disclose(spender), disclose(value));
535
+ }
536
+
537
+ /**
538
+ * @description Updates `owner`'s allowance for `spender` based on spent `value`.
539
+ * Does not update the allowance value in case of infinite allowance.
540
+ *
541
+ * Requirements:
542
+ *
543
+ * - Contract is initialized.
544
+ * - `spender` must have at least an allowance of `value` from `owner`.
545
+ *
546
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} owner - The owner of the tokens.
547
+ * @param {Either<ZswapCoinPublicKey, ContractAddress>} spender - The spender of the tokens.
548
+ * @param {Uint<128>} value - The amount of token allowance to spend.
549
+ * @return {[]} - None.
550
+ */
551
+ export circuit _spendAllowance(
552
+ owner: Either<ZswapCoinPublicKey, ContractAddress>,
553
+ spender: Either<ZswapCoinPublicKey, ContractAddress>,
554
+ value: Uint<128>
555
+ ): [] {
556
+
557
+ assert (
558
+ _allowances.member(disclose(owner)) &&
559
+ _allowances.lookup(disclose(owner)).member(disclose(spender)),
560
+ "FungibleToken: insufficient allowance"
561
+ );
562
+
563
+ const currentAllowance = _allowances.lookup(disclose(owner)).lookup(disclose(spender));
564
+ const MAX_UINT128 = 340282366920938463463374607431768211455;
565
+ if (currentAllowance < MAX_UINT128) {
566
+ assert (currentAllowance >= disclose(value), "FungibleToken: insufficient allowance");
567
+ _approve(disclose(owner), disclose(spender), currentAllowance - disclose(value) as Uint<128>);
568
+ }
569
+ }
570
+ }
@@ -0,0 +1,39 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma language_version 0.18.0;
3
+
4
+
5
+ module DivModule {
6
+
7
+ import CompactStandardLibrary;
8
+
9
+ pure circuit safeAdd<#n>(a: Uint<n>, b: Uint<n>): Uint<n> {
10
+ return a + b as Uint<n>;
11
+ }
12
+
13
+ export struct DivResult {
14
+ quotient: Uint<128>;
15
+ remainder: Uint<128>;
16
+ }
17
+ // division by subtraction
18
+ export circuit div(
19
+ dividend: Uint<128>,
20
+ divisor: Uint<128>,
21
+ divLoop: Vector<1000, Uint<1>>
22
+ ): DivResult {
23
+ assert(divisor != 0, "division by zero");
24
+
25
+ const [remainder, quotient]: [Uint<128>, Uint<128>] = fold((acc, _) => {
26
+ const [remaining, counter] = acc;
27
+ if (remaining < divisor) {
28
+ return acc;
29
+ } else {
30
+ return [(remaining - divisor), safeAdd<128>(counter, 1)];
31
+ }
32
+ }, [dividend, 0 as Uint<128>], divLoop);
33
+
34
+ return DivResult {
35
+ quotient: quotient,
36
+ remainder: remainder
37
+ };
38
+ }
39
+ }
@@ -0,0 +1,42 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma language_version 0.18.0;
3
+
4
+ /**
5
+ * @module Initializable
6
+ * @description Initializable provides a simple mechanism that mimics the functionality of a constructor.
7
+ */
8
+ module Initializable {
9
+ import CompactStandardLibrary;
10
+
11
+ export ledger _isInitialized: Boolean;
12
+
13
+ /**
14
+ * @description Initializes the state thus ensuring the calling circuit can only be called once.
15
+ *
16
+ * @returns None.
17
+ */
18
+ export circuit initializeUtils(): [] {
19
+ assertNotInitialized();
20
+ _isInitialized = true;
21
+ }
22
+
23
+ /**
24
+ * @description Asserts that the contract has been initialized, throwing an error if not.
25
+ *
26
+ * @returns None.
27
+ * @throws Will throw "Initializable: contract not initialized" if the contract is not initialized.
28
+ */
29
+ export circuit assertInitialized(): [] {
30
+ assert (_isInitialized, "Initializable: contract not initialized");
31
+ }
32
+
33
+ /**
34
+ * @description Asserts that the contract has not been initialized, throwing an error if it has.
35
+ *
36
+ * @returns None.
37
+ * @throws Will throw "Initializable: Contract already initialized" if the contract is already initialized.
38
+ */
39
+ export circuit assertNotInitialized(): [] {
40
+ assert (!_isInitialized, "Initializable: contract already initialized");
41
+ }
42
+ }