@buenos_andres/contracts 0.0.1-test.2 → 0.0.1-test.24

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.
Files changed (36) hide show
  1. package/README.md +1 -0
  2. package/package.json +3 -14
  3. package/{dist/utils → utils}/Utils.compact +1 -0
  4. package/utils/witnesses/UtilsWitnesses.js +4 -0
  5. package/utils/witnesses/UtilsWitnesses.js.map +1 -0
  6. package/dist/access/AccessControl.compact +0 -321
  7. package/dist/access/Ownable.compact +0 -212
  8. package/dist/access/witnesses/AccessControlWitnesses.d.ts +0 -2
  9. package/dist/access/witnesses/AccessControlWitnesses.js +0 -1
  10. package/dist/access/witnesses/AccessControlWitnesses.ts +0 -3
  11. package/dist/access/witnesses/OwnableWitnesses.d.ts +0 -2
  12. package/dist/access/witnesses/OwnableWitnesses.js +0 -1
  13. package/dist/access/witnesses/OwnableWitnesses.ts +0 -3
  14. package/dist/security/Initializable.compact +0 -59
  15. package/dist/security/Pausable.compact +0 -88
  16. package/dist/security/witnesses/InitializableWitnesses.d.ts +0 -2
  17. package/dist/security/witnesses/InitializableWitnesses.js +0 -1
  18. package/dist/security/witnesses/InitializableWitnesses.ts +0 -3
  19. package/dist/security/witnesses/PausableWitnesses.d.ts +0 -2
  20. package/dist/security/witnesses/PausableWitnesses.js +0 -1
  21. package/dist/security/witnesses/PausableWitnesses.ts +0 -3
  22. package/dist/token/FungibleToken.compact +0 -606
  23. package/dist/token/MultiToken.compact +0 -545
  24. package/dist/token/NonFungibleToken.compact +0 -805
  25. package/dist/token/witnesses/FungibleTokenWitnesses.d.ts +0 -2
  26. package/dist/token/witnesses/FungibleTokenWitnesses.js +0 -1
  27. package/dist/token/witnesses/FungibleTokenWitnesses.ts +0 -3
  28. package/dist/token/witnesses/MultiTokenWitnesses.d.ts +0 -2
  29. package/dist/token/witnesses/MultiTokenWitnesses.js +0 -1
  30. package/dist/token/witnesses/MultiTokenWitnesses.ts +0 -3
  31. package/dist/token/witnesses/NonFungibleTokenWitnesses.d.ts +0 -2
  32. package/dist/token/witnesses/NonFungibleTokenWitnesses.js +0 -1
  33. package/dist/token/witnesses/NonFungibleTokenWitnesses.ts +0 -3
  34. package/dist/utils/witnesses/UtilsWitnesses.js +0 -1
  35. package/dist/utils/witnesses/UtilsWitnesses.ts +0 -3
  36. /package/{dist/utils → utils}/witnesses/UtilsWitnesses.d.ts +0 -0
@@ -1,805 +0,0 @@
1
- // SPDX-License-Identifier: MIT
2
-
3
- pragma language_version >= 0.16.0;
4
-
5
- /**
6
- * @module NonFungibleToken
7
- * @description An unshielded Non-Fungible Token library.
8
- *
9
- * @notice One notable difference regarding this implementation and the EIP721 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 _unsafeTransfer 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 _unsafeTransfer and remove `isContract` guard from `transfer`.
30
- * - By this point, anyone using _unsafeTransfer should have migrated to the now C2C-capable `transfer`.
31
- *
32
- * @notice Missing Features and Improvements:
33
- *
34
- * - Uint256 token IDs
35
- * - Transfer/Approval events
36
- * - safeTransfer functions
37
- * - _baseURI() support
38
- * - An ERC165-like interface
39
- */
40
-
41
- module NonFungibleToken {
42
- import CompactStandardLibrary;
43
- import "../security/Initializable" prefix Initializable_;
44
- import "../utils/Utils" prefix Utils_;
45
-
46
- /// Public state
47
- export sealed ledger _name: Opaque<"string">;
48
- export sealed ledger _symbol: Opaque<"string">;
49
-
50
- /**
51
- * @description Mapping from token IDs to their owner addresses.
52
- * @type {Uint<128>} tokenId - The unique identifier for a token.
53
- * @type {Either<ZswapCoinPublicKey, ContractAddress>} owner - The owner address (public key or contract).
54
- * @type {Map<tokenId, owner>}
55
- * @type {Map<Uint<128>, Either<ZswapCoinPublicKey, ContractAddress>>} _owners
56
- */
57
- export ledger _owners: Map<Uint<128>, Either<ZswapCoinPublicKey, ContractAddress>>;
58
-
59
- /**
60
- * @description Mapping from account addresses to their token balances.
61
- * @type {Either<ZswapCoinPublicKey, ContractAddress>} owner - The owner address.
62
- * @type {Uint<128>} balance - The balance of the owner.
63
- * @type {Map<owner, balance>}
64
- * @type {Map<Either<ZswapCoinPublicKey, ContractAddress>, Uint<128>>} _balances
65
- */
66
- export ledger _balances: Map<Either<ZswapCoinPublicKey, ContractAddress>, Uint<128>>;
67
-
68
- /**
69
- * @description Mapping from token IDs to approved addresses.
70
- * @type {Uint<128>} tokenId - The unique identifier for a token.
71
- * @type {Either<ZswapCoinPublicKey, ContractAddress>} approved - The approved address (public key or contract).
72
- * @type {Map<tokenId, approved>}
73
- * @type {Map<Uint<128>, Either<ZswapCoinPublicKey, ContractAddress>>} _tokenApprovals
74
- */
75
- export ledger _tokenApprovals: Map<Uint<128>, Either<ZswapCoinPublicKey, ContractAddress>>;
76
-
77
- /**
78
- * @description Mapping from owner addresses to operator approvals.
79
- * @type {Either<ZswapCoinPublicKey, ContractAddress>} owner - The owner address.
80
- * @type {Either<ZswapCoinPublicKey, ContractAddress>} operator - The operator address.
81
- * @type {Boolean} approved - Whether the operator is approved.
82
- * @type {Map<owner, Map<operator, approved>>}
83
- * @type {Map<Either<ZswapCoinPublicKey, ContractAddress>, Map<Either<ZswapCoinPublicKey, ContractAddress>, Boolean>>} _operatorApprovals
84
- */
85
- export ledger _operatorApprovals: Map<Either<ZswapCoinPublicKey, ContractAddress>, Map<Either<ZswapCoinPublicKey, ContractAddress>, Boolean>>;
86
-
87
- /**
88
- * @description Mapping from token IDs to their metadata URIs.
89
- * @type {Uint<128>} tokenId - The unique identifier for a token.
90
- * @type {Opaque<"string">} uri - The metadata URI for the token.
91
- * @type {Map<tokenId, uri>}
92
- * @type {Map<Uint<128>, Opaque<"string">>} _tokenURIs
93
- */
94
- export ledger _tokenURIs: Map<Uint<128>, Opaque<"string">>;
95
-
96
- /**
97
- * @description Initializes the contract by setting the name and symbol.
98
- *
99
- * This MUST be called in the implementing contract's constructor.
100
- * Failure to do so can lead to an irreparable contract.
101
- *
102
- * @circuitInfo k=10, rows=65
103
- *
104
- * Requirements:
105
- *
106
- * - Contract is not initialized.
107
- *
108
- * @param {Opaque<"string">} name_ - The name of the token.
109
- * @param {Opaque<"string">} symbol_ - The symbol of the token.
110
- * @return {[]} - Empty tuple.
111
- */
112
- export circuit initialize(name_: Opaque<"string">, symbol_: Opaque<"string">): [] {
113
- Initializable_initialize();
114
- _name = disclose(name_);
115
- _symbol = disclose(symbol_);
116
- }
117
-
118
- /**
119
- * @description Returns the number of tokens in `owner`'s account.
120
- *
121
- * @circuitInfo k=10, rows=309
122
- *
123
- * Requirements:
124
- *
125
- * - The contract is initialized.
126
- *
127
- * @param {Either<ZswapCoinPublicKey, ContractAddress>)} owner - The account to query.
128
- * @return {Uint<128>} - The number of tokens in `owner`'s account.
129
- */
130
- export circuit balanceOf(owner: Either<ZswapCoinPublicKey, ContractAddress>): Uint<128> {
131
- Initializable_assertInitialized();
132
- if (!_balances.member(disclose(owner))) {
133
- return 0;
134
- }
135
-
136
- return _balances.lookup(disclose(owner));
137
- }
138
-
139
- /**
140
- * @description Returns the owner of the `tokenId` token.
141
- *
142
- * @circuitInfo k=10, rows=290
143
- *
144
- * Requirements:
145
- *
146
- * - The contract is initialized.
147
- * - The `tokenId` must exist.
148
- *
149
- * @param {Uint<128>} tokenId - The identifier for a token.
150
- * @return {Either<ZswapCoinPublicKey, ContractAddress>} - The account that owns the token.
151
- */
152
- export circuit ownerOf(tokenId: Uint<128>): Either<ZswapCoinPublicKey, ContractAddress> {
153
- Initializable_assertInitialized();
154
- return _requireOwned(tokenId);
155
- }
156
-
157
- /**
158
- * @description Returns the token name.
159
- *
160
- * @circuitInfo k=10, rows=36
161
- *
162
- * Requirements:
163
- *
164
- * - The contract is initialized.
165
- *
166
- * @return {Opaque<"string">} - The token name.
167
- */
168
- export circuit name(): Opaque<"string"> {
169
- Initializable_assertInitialized();
170
- return _name;
171
- }
172
-
173
- /**
174
- * @description Returns the symbol of the token.
175
- *
176
- * @circuitInfo k=10, rows=36
177
- *
178
- * Requirements:
179
- *
180
- * - The contract is initialized.
181
- *
182
- * @return {Opaque<"string">} - The token symbol.
183
- */
184
- export circuit symbol(): Opaque<"string"> {
185
- Initializable_assertInitialized();
186
- return _symbol;
187
- }
188
-
189
- /**
190
- * @description Returns the token URI for the given `tokenId`. Returns the empty
191
- * string if a tokenURI does not exist.
192
- *
193
- * @circuitInfo k=10, rows=296
194
- *
195
- * Requirements:
196
- *
197
- * - The contract is initialized.
198
- * - The `tokenId` must exist.
199
- *
200
- * @notice Native strings and string operations aren't supported within the Compact language,
201
- * e.g. concatenating a base URI + token ID is not possible like in other NFT implementations.
202
- * Therefore, we propose the URI storage approach; whereby, NFTs may or may not have unique "base" URIs.
203
- * It's up to the implementation to decide on how to handle this.
204
- *
205
- * @param {Uint<128>} tokenId - The identifier for a token.
206
- * @return {Opaque<"string">} - the token id's URI.
207
- */
208
- export circuit tokenURI(tokenId: Uint<128>): Opaque<"string"> {
209
- Initializable_assertInitialized();
210
- _requireOwned(tokenId);
211
-
212
- if (!_tokenURIs.member(disclose(tokenId))) {
213
- return Utils_emptyString();
214
- }
215
-
216
- return _tokenURIs.lookup(disclose(tokenId));
217
- }
218
-
219
- /**
220
- * @description Sets the the URI as `tokenURI` for the given `tokenId`.
221
- *
222
- * @circuitInfo k=10, rows=253
223
- *
224
- * Requirements:
225
- *
226
- * - The contract is initialized.
227
- * - The `tokenId` must exist.
228
- *
229
- * @notice The URI for a given NFT is usually set when the NFT is minted.
230
- *
231
- * @param {Uint<128>} tokenId - The identifier of the token.
232
- * @param {Opaque<"string">} tokenURI - The URI of `tokenId`.
233
- * @return {[]} - Empty tuple.
234
- */
235
- export circuit _setTokenURI(tokenId: Uint<128>, tokenURI: Opaque<"string">): [] {
236
- Initializable_assertInitialized();
237
- _requireOwned(tokenId);
238
-
239
- return _tokenURIs.insert(disclose(tokenId), disclose(tokenURI));
240
- }
241
-
242
- /**
243
- * @description Gives permission to `to` to transfer `tokenId` token to another account.
244
- * The approval is cleared when the token is transferred.
245
- *
246
- * Only a single account can be approved at a time, so approving the zero address clears previous approvals.
247
- *
248
- * @circuitInfo k=10, rows=966
249
- *
250
- * Requirements:
251
- *
252
- * - The contract is initialized.
253
- * - The caller must either own the token or be an approved operator.
254
- * - `tokenId` must exist.
255
- *
256
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} to - The account receiving the approval
257
- * @param {Uint<128>} tokenId - The token `to` may be permitted to transfer
258
- * @return {[]} - Empty tuple.
259
- */
260
- export circuit approve(
261
- to: Either<ZswapCoinPublicKey, ContractAddress>,
262
- tokenId: Uint<128>
263
- ): [] {
264
- Initializable_assertInitialized();
265
- const auth = left<ZswapCoinPublicKey,ContractAddress>(ownPublicKey());
266
- _approve(
267
- to,
268
- tokenId,
269
- auth
270
- );
271
- }
272
-
273
- /**
274
- * @description Returns the account approved for `tokenId` token.
275
- *
276
- * @circuitInfo k=10, rows=409
277
- *
278
- * Requirements:
279
- *
280
- * - The contract is initialized.
281
- * - `tokenId` must exist.
282
- *
283
- * @param {Uint<128>} tokenId - The token an account may be approved to manage
284
- * @return {Either<ZswapCoinPublicKey, ContractAddress>} Operator- The account approved to manage the token
285
- */
286
- export circuit getApproved(tokenId: Uint<128>): Either<ZswapCoinPublicKey, ContractAddress> {
287
- Initializable_assertInitialized();
288
- _requireOwned(tokenId);
289
-
290
- return _getApproved(tokenId);
291
- }
292
-
293
- /**
294
- * @description Approve or remove `operator` as an operator for the caller.
295
- * Operators can call {transferFrom} for any token owned by the caller.
296
- *
297
- * @circuitInfo k=10, rows=409
298
- *
299
- * Requirements:
300
- *
301
- * - The contract is initialized.
302
- * - The `operator` cannot be the address zero.
303
- *
304
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} operator - An operator to manage the caller's tokens
305
- * @param {Boolean} approved - A boolean determining if `operator` may manage all tokens of the caller
306
- * @return {[]} - Empty tuple.
307
- */
308
- export circuit setApprovalForAll(
309
- operator: Either<ZswapCoinPublicKey, ContractAddress>,
310
- approved: Boolean
311
- ): [] {
312
- Initializable_assertInitialized();
313
- const owner = left<ZswapCoinPublicKey,ContractAddress>(ownPublicKey());
314
- _setApprovalForAll(
315
- owner,
316
- operator,
317
- approved
318
- );
319
- }
320
-
321
- /**
322
- * @description Returns if the `operator` is allowed to manage all of the assets of `owner`.
323
- *
324
- * @circuitInfo k=10, rows=621
325
- *
326
- * Requirements:
327
- *
328
- * - The contract is initialized.
329
- *
330
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} owner - The owner of a token
331
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} operator - An account that may operate on `owner`'s tokens
332
- * @return {Boolean} - A boolean determining if `operator` is allowed to manage all of the tokens of `owner`
333
- */
334
- export circuit isApprovedForAll(
335
- owner: Either<ZswapCoinPublicKey, ContractAddress>,
336
- operator: Either<ZswapCoinPublicKey, ContractAddress>
337
- ): Boolean {
338
- Initializable_assertInitialized();
339
- if (_operatorApprovals.member(disclose(owner)) && _operatorApprovals.lookup(owner).member(disclose(operator))) {
340
- return _operatorApprovals.lookup(owner).lookup(disclose(operator));
341
- } else {
342
- return false;
343
- }
344
- }
345
-
346
- /**
347
- * @description Transfers `tokenId` token from `from` to `to`.
348
- *
349
- * @notice Transfers to contract addresses are currently disallowed until contract-to-contract interactions
350
- * are supported in Compact. This restriction prevents assets from being inadvertently locked in contracts that cannot
351
- * currently handle token receipt.
352
- *
353
- * @circuitInfo k=11, rows=1966
354
- *
355
- * Requirements:
356
- *
357
- * - The contract is initialized.
358
- * - `from` is not the zero address.
359
- * - `to` is not the zero address.
360
- * - `to` is not a ContractAddress.
361
- * - `tokenId` token must be owned by `from`.
362
- * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
363
- *
364
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} from - The source account from which the token is being transfered
365
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} to - The target account to transfer token to
366
- * @param {Uint<128>} tokenId - The token being transfered
367
- * @return {[]} - Empty tuple.
368
- */
369
- export circuit transferFrom(
370
- from: Either<ZswapCoinPublicKey, ContractAddress>,
371
- to: Either<ZswapCoinPublicKey, ContractAddress>,
372
- tokenId: Uint<128>
373
- ): [] {
374
- Initializable_assertInitialized();
375
- assert(!Utils_isContractAddress(to), "NonFungibleToken: Unsafe Transfer");
376
-
377
- _unsafeTransferFrom(from, to, tokenId);
378
- }
379
-
380
- /**
381
- * @description Transfers `tokenId` token from `from` to `to`. It does NOT check if the recipient is a ContractAddress.
382
- *
383
- * WARNING: Transfers to contract addresses are considered unsafe because contract-to-contract calls
384
- * are not currently supported. Tokens sent to a contract address may become irretrievable.
385
- * Once contract-to-contract calls are supported, this circuit may be deprecated.
386
- *
387
- * @circuitInfo k=11, rows=1963
388
- *
389
- * Requirements:
390
- *
391
- * - The contract is initialized.
392
- * - `from` is not the zero address.
393
- * - `to` is not the zero address.
394
- * - `tokenId` token must be owned by `from`.
395
- * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
396
- *
397
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} from - The source account from which the token is being transfered
398
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} to - The target account to transfer token to
399
- * @param {Uint<128>} tokenId - The token being transfered
400
- * @return {[]} - Empty tuple.
401
- */
402
- export circuit _unsafeTransferFrom(
403
- from: Either<ZswapCoinPublicKey, ContractAddress>,
404
- to: Either<ZswapCoinPublicKey, ContractAddress>,
405
- tokenId: Uint<128>
406
- ): [] {
407
- Initializable_assertInitialized();
408
- assert(!Utils_isKeyOrAddressZero(to), "NonFungibleToken: Invalid Receiver");
409
- // Setting an "auth" arguments enables the `_isAuthorized` check which verifies that the token exists
410
- // (from != 0). Therefore, it is not needed to verify that the return value is not 0 here.
411
- const auth = left<ZswapCoinPublicKey,ContractAddress>(ownPublicKey());
412
- const previousOwner = _update(
413
- to,
414
- tokenId,
415
- auth
416
- );
417
- assert(previousOwner == from, "NonFungibleToken: Incorrect Owner");
418
- }
419
-
420
- /**
421
- * @description Returns the owner of the `tokenId`. Does NOT revert if token doesn't exist
422
- *
423
- * @circuitInfo k=10, rows=253
424
- *
425
- * Requirements:
426
- *
427
- * - The contract is initialized.
428
- *
429
- * @param {Uint<128>} tokenId - The target token of the owner query
430
- * @return {Either<ZswapCoinPublicKey, ContractAddress>} - The owner of the token
431
- */
432
- export circuit _ownerOf(tokenId: Uint<128>): Either<ZswapCoinPublicKey, ContractAddress> {
433
- Initializable_assertInitialized();
434
- if (!_owners.member(disclose(tokenId))) {
435
- return burnAddress();
436
- }
437
-
438
- return _owners.lookup(disclose(tokenId));
439
- }
440
-
441
- /**
442
- * @description Returns the approved address for `tokenId`. Returns the zero address if `tokenId` is not minted.
443
- *
444
- * @circuitInfo k=10, rows=253
445
- *
446
- * Requirements:
447
- *
448
- * - The contract is initialized.
449
- *
450
- * @param {Uint<128>} tokenId - The token to query
451
- * @return {Either<ZswapCoinPublicKey, ContractAddress>} - An account approved to spend `tokenId`
452
- */
453
- export circuit _getApproved(tokenId: Uint<128>): Either<ZswapCoinPublicKey, ContractAddress> {
454
- Initializable_assertInitialized();
455
- if (!_tokenApprovals.member(disclose(tokenId))) {
456
- return burnAddress();
457
- }
458
- return _tokenApprovals.lookup(disclose(tokenId));
459
- }
460
-
461
- /**
462
- * @description Returns whether `spender` is allowed to manage `owner`'s tokens, or `tokenId` in
463
- * particular (ignoring whether it is owned by `owner`).
464
- *
465
- * @circuitInfo k=11, rows=1098
466
- *
467
- * Requirements:
468
- *
469
- * - The contract is initialized.
470
- *
471
- * WARNING: This function assumes that `owner` is the actual owner of `tokenId` and does not verify this
472
- * assumption.
473
- *
474
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} owner - Owner of the token
475
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} spender - Account that wishes to spend `tokenId`
476
- * @param {Uint<128>} tokenId - Token to spend
477
- * @return {Boolean} - A boolean determining if `spender` may manage `tokenId`
478
- */
479
- export circuit _isAuthorized(
480
- owner: Either<ZswapCoinPublicKey, ContractAddress>,
481
- spender: Either<ZswapCoinPublicKey, ContractAddress>,
482
- tokenId: Uint<128>
483
- ): Boolean {
484
- Initializable_assertInitialized();
485
- return (
486
- !Utils_isKeyOrAddressZero(disclose(spender)) &&
487
- (disclose(owner) == disclose(spender) || isApprovedForAll(owner, spender) || _getApproved(tokenId) == disclose(spender))
488
- );
489
- }
490
-
491
- /**
492
- * @description Checks if `spender` can operate on `tokenId`, assuming the provided `owner` is the actual owner.
493
- *
494
- * @circuitInfo k=11, rows=1121
495
- *
496
- * Requirements:
497
- *
498
- * - The contract is initialized.
499
- * - `spender` has approval from `owner` for `tokenId` OR `spender` has approval to manage all of `owner`'s assets.
500
- *
501
- * WARNING: This function assumes that `owner` is the actual owner of `tokenId` and does not verify this
502
- * assumption.
503
- *
504
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} owner - Owner of the token
505
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} spender - Account operating on `tokenId`
506
- * @param {Uint<128>} tokenId - The token to spend
507
- * @return {[]} - Empty tuple.
508
- */
509
- export circuit _checkAuthorized(
510
- owner: Either<ZswapCoinPublicKey, ContractAddress>,
511
- spender: Either<ZswapCoinPublicKey, ContractAddress>,
512
- tokenId: Uint<128>
513
- ): [] {
514
- Initializable_assertInitialized();
515
- if (!_isAuthorized(owner, spender, tokenId)) {
516
- assert(!Utils_isKeyOrAddressZero(owner), "NonFungibleToken: Nonexistent Token");
517
- assert(false, "NonFungibleToken: Insufficient Approval");
518
- }
519
- }
520
-
521
- /**
522
- * @description Transfers `tokenId` from its current owner to `to`, or alternatively mints (or burns) if the current owner
523
- * (or `to`) is the zero address. Returns the owner of the `tokenId` before the update.
524
- *
525
- * @circuitInfo k=11, rows=1959
526
- *
527
- * Requirements:
528
- *
529
- * - The contract is initialized.
530
- * - If `auth` is non 0, then this function will check that `auth` is either the owner of the token,
531
- * or approved to operate on the token (by the owner).
532
- *
533
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} to - The intended recipient of the token transfer
534
- * @param {Uint<128>} tokenId - The token being transfered
535
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} auth - An account authorized to transfer the token
536
- * @return {Either<ZswapCoinPublicKey, ContractAddress>} - Owner of the token before it was transfered
537
- */
538
- circuit _update(
539
- to: Either<ZswapCoinPublicKey, ContractAddress>,
540
- tokenId: Uint<128>,
541
- auth: Either<ZswapCoinPublicKey, ContractAddress>
542
- ): Either<ZswapCoinPublicKey, ContractAddress> {
543
- Initializable_assertInitialized();
544
- const from = _ownerOf(tokenId);
545
-
546
- // Perform (optional) operator check
547
- if (!Utils_isKeyOrAddressZero(disclose(auth))) {
548
- _checkAuthorized(from, auth, tokenId);
549
- }
550
-
551
- // Execute the update
552
- if (!Utils_isKeyOrAddressZero(disclose(from))) {
553
- // Clear approval. No need to re-authorize
554
- _approve(burnAddress(), tokenId, burnAddress());
555
- const newBalance = _balances.lookup(disclose(from)) - 1 as Uint<128>;
556
- _balances.insert(disclose(from), disclose(newBalance));
557
- }
558
-
559
- if (!Utils_isKeyOrAddressZero(disclose(to))) {
560
- if (!_balances.member(disclose(to))) {
561
- _balances.insert(disclose(to), 0);
562
- }
563
- const newBalance = _balances.lookup(disclose(to)) + 1 as Uint<128>;
564
- _balances.insert(disclose(to), disclose(newBalance));
565
- }
566
-
567
- _owners.insert(disclose(tokenId), disclose(to));
568
-
569
- return from;
570
- }
571
-
572
- /**
573
- * @description Mints `tokenId` and transfers it to `to`.
574
- *
575
- * @circuitInfo k=11, rows=1013
576
- *
577
- * Requirements:
578
- *
579
- * - The contract is initialized.
580
- * - `tokenId` must not exist.
581
- * - `to` is not the zero address.
582
- * - `to` is not a ContractAddress.
583
- *
584
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} to - The account receiving `tokenId`
585
- * @param {Uint<128>} tokenId - The token to transfer
586
- * @return {[]} - Empty tuple.
587
- */
588
- export circuit _mint(
589
- to: Either<ZswapCoinPublicKey, ContractAddress>,
590
- tokenId: Uint<128>
591
- ): [] {
592
- Initializable_assertInitialized();
593
- assert(!Utils_isContractAddress(to), "NonFungibleToken: Unsafe Transfer");
594
-
595
- _unsafeMint(to, tokenId);
596
- }
597
-
598
- /**
599
- * @description Mints `tokenId` and transfers it to `to`. It does NOT check if the recipient is a ContractAddress.
600
- *
601
- * @circuitInfo k=11, rows=1010
602
- *
603
- * Requirements:
604
- *
605
- * - The contract is initialized.
606
- * - `tokenId` must not exist.
607
- * - `to` is not the zero address.
608
- *
609
- * WARNING: Transfers to contract addresses are considered unsafe because contract-to-contract
610
- * calls are not currently supported. Tokens sent to a contract address may become irretrievable.
611
- * Once contract-to-contract calls are supported, this circuit may be deprecated.
612
- *
613
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} to - The account receiving `tokenId`
614
- * @param {Uint<128>} tokenId - The token to transfer
615
- * @return {[]} - Empty tuple.
616
- */
617
- export circuit _unsafeMint(
618
- to: Either<ZswapCoinPublicKey, ContractAddress>,
619
- tokenId: Uint<128>
620
- ): [] {
621
- Initializable_assertInitialized();
622
- assert(!Utils_isKeyOrAddressZero(to), "NonFungibleToken: Invalid Receiver");
623
-
624
- const previousOwner = _update(to, tokenId, burnAddress());
625
-
626
- assert(Utils_isKeyOrAddressZero(previousOwner), "NonFungibleToken: Invalid Sender");
627
- }
628
-
629
- /**
630
- * @description Destroys `tokenId`.
631
- * The approval is cleared when the token is burned.
632
- * This circuit does not check if the sender is authorized to operate on the token.
633
- *
634
- * @circuitInfo k=10, rows=479
635
- *
636
- * Requirements:
637
- *
638
- * - The contract is initialized.
639
- * - `tokenId` must exist.
640
- *
641
- * @param {Uint<128>} tokenId - The token to burn
642
- * @return {[]} - Empty tuple.
643
- */
644
- export circuit _burn(tokenId: Uint<128>): [] {
645
- Initializable_assertInitialized();
646
- const previousOwner = _update(burnAddress(), tokenId, burnAddress());
647
- assert(!Utils_isKeyOrAddressZero(previousOwner), "NonFungibleToken: Invalid Sender");
648
- }
649
-
650
- /**
651
- * @description Transfers `tokenId` from `from` to `to`.
652
- * As opposed to {transferFrom}, this imposes no restrictions on ownPublicKey().
653
- *
654
- * @notice Transfers to contract addresses are currently disallowed until contract-to-contract
655
- * interactions are supported in Compact. This restriction prevents assets from being inadvertently
656
- * locked in contracts that cannot currently handle token receipt.
657
- *
658
- * @circuitInfo k=11, rows=1224
659
- *
660
- * Requirements:
661
- *
662
- * - The contract is initialized.
663
- * - `to` is not the zero address.
664
- * - `to` is not a ContractAddress.
665
- * - `tokenId` token must be owned by `from`.
666
- *
667
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} from - The source account of the token transfer
668
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} to - The target account of the token transfer
669
- * @param {Uint<128>} tokenId - The token to transfer
670
- * @return {[]} - Empty tuple.
671
- */
672
- export circuit _transfer(
673
- from: Either<ZswapCoinPublicKey, ContractAddress>,
674
- to: Either<ZswapCoinPublicKey, ContractAddress>,
675
- tokenId: Uint<128>
676
- ): [] {
677
- Initializable_assertInitialized();
678
- assert(!Utils_isContractAddress(to), "NonFungibleToken: Unsafe Transfer");
679
-
680
- _unsafeTransfer(from, to, tokenId);
681
- }
682
-
683
- /**
684
- * @description Transfers `tokenId` from `from` to `to`.
685
- * As opposed to {_unsafeTransferFrom}, this imposes no restrictions on ownPublicKey().
686
- * It does NOT check if the recipient is a ContractAddress.
687
- *
688
- * @circuitInfo k=11, rows=1221
689
- *
690
- * Requirements:
691
- *
692
- * - The contract is initialized.
693
- * - `to` is not the zero address.
694
- * - `tokenId` token must be owned by `from`.
695
- *
696
- * WARNING: Transfers to contract addresses are considered unsafe because contract-to-contract
697
- * calls are not currently supported. Tokens sent to a contract address may become irretrievable.
698
- * Once contract-to-contract calls are supported, this circuit may be deprecated.
699
- *
700
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} from - The source account of the token transfer
701
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} to - The target account of the token transfer
702
- * @param {Uint<128>} tokenId - The token to transfer
703
- * @return {[]} - Empty tuple.
704
- */
705
- export circuit _unsafeTransfer(
706
- from: Either<ZswapCoinPublicKey, ContractAddress>,
707
- to: Either<ZswapCoinPublicKey, ContractAddress>,
708
- tokenId: Uint<128>
709
- ): [] {
710
- Initializable_assertInitialized();
711
- assert(!Utils_isKeyOrAddressZero(to), "NonFungibleToken: Invalid Receiver");
712
-
713
- const previousOwner = _update(to, tokenId, burnAddress());
714
-
715
- assert(!Utils_isKeyOrAddressZero(previousOwner), "NonFungibleToken: Nonexistent Token");
716
- assert(previousOwner == from, "NonFungibleToken: Incorrect Owner");
717
- }
718
-
719
- /**
720
- * @description Approve `to` to operate on `tokenId`
721
- *
722
- * @circuitInfo k=11, rows=1109
723
- *
724
- * Requirements:
725
- *
726
- * - The contract is initialized.
727
- * - If `auth` is non 0, then this function will check that `auth` is either the owner of the token,
728
- * or approved to operate on the token (by the owner).
729
- *
730
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} to - The target account to approve
731
- * @param {Uint<128>} tokenId - The token to approve
732
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} auth - An account authorized to operate on all tokens held by the owner the token
733
- * @return {[]} - Empty tuple.
734
- */
735
- export circuit _approve(
736
- to: Either<ZswapCoinPublicKey, ContractAddress>,
737
- tokenId: Uint<128>,
738
- auth: Either<ZswapCoinPublicKey, ContractAddress>
739
- ): [] {
740
- Initializable_assertInitialized();
741
- if (!Utils_isKeyOrAddressZero(disclose(auth))) {
742
- const owner = _requireOwned(tokenId);
743
-
744
- // We do not use _isAuthorized because single-token approvals should not be able to call approve
745
- assert((owner == disclose(auth) || isApprovedForAll(owner, auth)), "NonFungibleToken: Invalid Approver");
746
- }
747
-
748
- _tokenApprovals.insert(disclose(tokenId), disclose(to));
749
- }
750
-
751
- /**
752
- * @description Approve `operator` to operate on all of `owner` tokens
753
- *
754
- * @circuitInfo k=10, rows=524
755
- *
756
- * Requirements:
757
- *
758
- * - The contract is initialized.
759
- * - `operator` is not the address zero.
760
- *
761
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} owner - Owner of a token
762
- * @param {Either<ZswapCoinPublicKey, ContractAddress>} operator - The account to approve
763
- * @param {Boolean} approved - A boolean determining if `operator` may operate on all of `owner` tokens
764
- * @return {[]} - Empty tuple.
765
- */
766
- export circuit _setApprovalForAll(
767
- owner: Either<ZswapCoinPublicKey, ContractAddress>,
768
- operator: Either<ZswapCoinPublicKey, ContractAddress>,
769
- approved: Boolean
770
- ): [] {
771
- Initializable_assertInitialized();
772
- assert(!Utils_isKeyOrAddressZero(operator), "NonFungibleToken: Invalid Operator");
773
-
774
- if (!_operatorApprovals.member(disclose(owner))) {
775
- _operatorApprovals.insert(
776
- disclose(owner),
777
- default<Map<Either<ZswapCoinPublicKey, ContractAddress>, Boolean>>
778
- );
779
- }
780
-
781
- _operatorApprovals.lookup(owner).insert(disclose(operator), disclose(approved));
782
- }
783
-
784
- /**
785
- * @description Reverts if the `tokenId` doesn't have a current owner (it hasn't been minted, or it has been burned).
786
- * Returns the owner.
787
- *
788
- * @circuitInfo k=10, rows=288
789
- *
790
- * Requirements:
791
- *
792
- * - The contract is initialized.
793
- * - `tokenId` must exist.
794
- *
795
- * @param {Uint<128>} tokenId - The token that should be owned
796
- * @return {Either<ZswapCoinPublicKey, ContractAddress>} - The owner of `tokenId`
797
- */
798
- export circuit _requireOwned(tokenId: Uint<128>): Either<ZswapCoinPublicKey, ContractAddress> {
799
- Initializable_assertInitialized();
800
- const owner = _ownerOf(tokenId);
801
-
802
- assert(!Utils_isKeyOrAddressZero(owner), "NonFungibleToken: Nonexistent Token");
803
- return owner;
804
- }
805
- }