All files / src/contracts collection.ts

98.6% Statements 282/286
0% Branches 0/3
97.43% Functions 38/39
98.92% Lines 277/280

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986                6x                                         6x 6x                 6x                     6x           6x 6x 6x     6x 6x 6x     6x                                                                                                                         3x                   195x   195x   195x   195x         195x               2x 2x 2x 2x 2x   2x         2x 2x 2x                                         3x 12x 12x 12x     12x           195x                                                             64x       24x                 64x 64x     64x                 24x     24x     24x                     63x 63x 63x 63x 63x 63x                 149x     149x 148x                                   3x 12x 12x     12x   12x 12x               3x 9x 9x     9x   9x         9x     9x 9x                                             33x   33x 33x 33x 33x   33x 33x 33x     33x   33x         33x       33x           6x 6x 6x   33x 33x       33x                             33x                 33x 264x         33x       33x 33x                 3x       11x 11x     10x                 3x       12x     12x 12x       12x 12x 12x       12x 12x                   22x 22x 22x 22x 22x         22x 22x 22x 22x   22x 22x                 3x 11x 11x 10x                 3x       12x 12x   12x 12x 12x 12x 12x                     22x 22x 22x 22x 22x 22x 22x 22x                 3x 11x 11x 10x                 3x       12x 12x   12x 12x 12x           12x 12x                               22x 22x 22x 22x 22x 22x 22x 22x     22x 22x         22x   22x     22x       22x 22x 22x 22x                 3x 11x 11x     10x                 3x       21x 20x       20x         19x 19x         19x 19x                               30x 30x 30x 30x       30x 30x 30x 29x 29x         29x 29x                   3x       12x     12x 12x 12x       12x 12x 12x       12x 3x 3x 3x     3x   12x           12x     12x     12x 12x                 3x 12x     12x 12x 12x             12x         12x 3x 3x 3x     3x   12x           12x     12x     12x       12x   12x 12x             3x 9x 9x 9x 9x 9x                   3x 12x 12x 12x 12x 12x 12x             3x             12x 12x     12x 12x 12x 12x 12x                 3x 12x 12x 12x 12x                       3x 12x 12x 12x 12x                       3x     9x 9x 9x 9x 9x 9x         9x   9x         9x   9x         9x   9x         9x   9x         9x 9x                   3x 9x 9x 9x     9x 9x 9x             9x     3x    
/**
 * The NFT Collection Contract is responsible for managing a collection of NFTs.
 * It handles minting new NFTs, transferring ownership, buying, selling,
 * and interfacing with Admin Contracts for additional functionalities.
 *
 * @module CollectionContract
 */
 
import {
  Field,
  PublicKey,
  AccountUpdate,
  Bool,
  method,
  state,
  State,
  DeployArgs,
  Permissions,
  TokenContract,
  AccountUpdateForest,
  VerificationKey,
  UInt32,
  UInt64,
  SmartContract,
  Mina,
  Provable,
  Struct,
  NetworkId,
} from "o1js";
import { NFT } from "./nft";
import {
  MintParams,
  MintRequest,
  CollectionData,
  CollectionDataPacked,
  NFTUpdateProof,
  CollectionConfigurationUpdate,
  NFTStateStruct,
} from "./types";
import {
  MintEvent,
  TransferEvent,
  SellEvent,
  BuyEvent,
  UpdateEvent,
  UpgradeVerificationKeyEvent,
  LimitMintingEvent,
  PauseNFTEvent,
} from "./events";
import { NFTAdminBase, NFTAdminContractConstructor } from "./admin";
import {
  UpgradeAuthorityBase,
  VerificationKeyUpgradeData,
  UpgradeAuthorityContractConstructor,
  UpgradableContract,
} from "./upgradable";
import { PausableContract, PauseEvent } from "./pausable";
import { OwnableContract, OwnershipChangeEvent } from "./ownable";
import { NFTVerificationKeys, SupportedNetworkId } from "./vk";
export {
  CollectionDeployProps,
  CollectionContract,
  CollectionErrors,
  CollectionStateStruct,
};
 
const CollectionErrors = {
  wrongMasterNFTaddress:
    "Master NFT address should be the same as the collection address",
  transferNotAllowed:
    "Transfers of tokens are not allowed, change the owner instead",
  collectionPaused: "Collection is currently paused",
  mintApprovalRequired: "Mint approval is required",
  mintApprovalNotRequired: "Mint approval is not required",
  cannotMintMasterNFT: "Only the creator can mint the Master NFT",
  cannotMint: "Admin contract did not provide permission to mint",
  noPermissionToPause: "Not allowed to pause collection",
  noPermissionToResume: "Not allowed to resume collection",
  collectionNotPaused: "Collection is not paused",
  transferApprovalRequired: "Transfer approval is required",
  transferApprovalNotRequired: "Transfer approval is not required",
  updateApprovalRequired: "Update approval is required",
  updateApprovalNotRequired: "Update approval is not required",
  noPermissionToChangeName: "Not allowed to change collection name",
  noPermissionToChangeBaseUri: "Not allowed to change collection base URI",
  noPermissionToChangeCreator: "Not allowed to change collection creator",
  noPermissionToChangeRoyalty: "Not allowed to change royalty fee",
  noPermissionToChangeTransferFee: "Not allowed to change transfer fee",
  noPermissionToSetAdmin: "Not allowed to set admin contract",
  cannotUpgradeVerificationKey: "Cannot upgrade verification key",
  creatorSignatureRequiredToUpgradeCollection:
    "Creator signature is required to upgrade collection",
  creatorSignatureRequiredToUpgradeNFT:
    "Creator signature is required to upgrade NFT",
  upgradeContractAddressNotSet: "Upgrade contract address is not set",
  adminContractAddressNotSet: "Admin contract address is not set",
};
 
interface CollectionDeployProps extends Exclude<DeployArgs, undefined> {
  collectionName: Field;
  creator: PublicKey;
  admin: PublicKey;
  baseURL: Field;
  symbol: string;
  url: string;
}
 
// The order of the fields is important; should be the same as in the Collection
class CollectionStateStruct extends Struct({
  collectionName: Field,
  creator: PublicKey,
  admin: PublicKey,
  baseURL: Field,
  packedData: CollectionDataPacked,
}) {}
 
/**
 * Creates a new NFT Collection Contract class.
 *
 * @param params - Constructor parameters including admin and upgrade contracts, and network ID.
 * @returns The Collection class extending TokenContract and implementing required interfaces.
 */
function CollectionContract(params: {
  adminContract: NFTAdminContractConstructor;
  upgradeContract: UpgradeAuthorityContractConstructor;
  networkId: SupportedNetworkId;
}) {
  const { adminContract, upgradeContract, networkId } = params;
  /**
   * The NFT Collection Contract manages a collection of NFTs.
   * It handles minting, transferring, buying, selling, and integrates with Admin Contracts.
   */
  class Collection
    extends TokenContract
    implements UpgradableContract, OwnableContract, PausableContract
  {
    /** The name of the NFT collection. */
    @state(Field) collectionName = State<Field>();
    /** The public key of the creator of the collection. */
    @state(PublicKey) creator = State<PublicKey>();
    /** The public key of the Admin Contract. */
    @state(PublicKey) admin = State<PublicKey>();
    /** The base URL for the metadata of the NFTs in the collection. */
    @state(Field) baseURL = State<Field>();
    /**
     * A packed data field containing additional collection parameters,
     * such as flags and fee configurations.
     */
    @state(CollectionDataPacked) packedData = State<CollectionDataPacked>();
 
    /**
     * Deploys the NFT Collection Contract with the initial settings.
     *
     * @param props - Deployment properties including collection name, creator, admin, baseURL, symbol, and URL.
     */
    async deploy(props: CollectionDeployProps): Promise<void> {
      await super.deploy(props);
      this.collectionName.set(props.collectionName);
      this.creator.set(props.creator);
      this.admin.set(props.admin);
      this.baseURL.set(props.baseURL);
      // Set the collection to be paused by default
      this.packedData.set(
        CollectionData.new({
          isPaused: true,
        }).pack()
      );
      this.account.zkappUri.set(props.url);
      this.account.tokenSymbol.set(props.symbol);
      this.account.permissions.set({
        ...Permissions.default(),
        // Allow the upgrade authority to set the verification key
        // even when there is no protocol upgrade
        setVerificationKey:
          Permissions.VerificationKey.proofDuringCurrentVersion(),
        setPermissions: Permissions.impossible(),
        access: Permissions.proof(),
        send: Permissions.proof(),
        setZkappUri: Permissions.none(),
        setTokenSymbol: Permissions.none(),
      });
    }
 
    /**
     * Initializes the collection with a master NFT and initial data.
     *
     * @param masterNFT - The master NFT parameters.
     * @param collectionData - Initial collection data including flags and configurations.
     */
    @method
    async initialize(masterNFT: MintParams, collectionData: CollectionData) {
      this.account.provedState.requireEquals(Bool(false));
      this.packedData.set(collectionData.pack());
      masterNFT.address
        .equals(this.address)
        .assertTrue(CollectionErrors.wrongMasterNFTaddress);
      await this._mint(masterNFT, collectionData);
    }
 
    /**
     * Defines the events emitted by the contract.
     */
    events = {
      mint: MintEvent,
      update: PublicKey,
      transfer: TransferEvent,
      sell: SellEvent,
      buy: BuyEvent,
      approveBuy: BuyEvent,
      approveSell: SellEvent,
      approveTransfer: TransferEvent,
      approveMint: MintEvent,
      approveUpdate: PublicKey,
      upgradeNFTVerificationKey: UpgradeVerificationKeyEvent,
      upgradeVerificationKey: Field,
      limitMinting: LimitMintingEvent,
      pause: PauseEvent,
      resume: PauseEvent,
      pauseNFT: PauseNFTEvent,
      resumeNFT: PauseNFTEvent,
      ownershipChange: OwnershipChangeEvent,
    };
 
    /**
     * Overrides the approveBase method to prevent transfers of tokens.
     *
     * @param forest - The account update forest.
     */
    async approveBase(forest: AccountUpdateForest) {
      throw Error(CollectionErrors.transferNotAllowed);
    }
 
    get getAdminContractConstructor() {
      return adminContract;
    }
 
    get getUpgradeContractConstructor() {
      return upgradeContract;
    }
 
    /**
     * Retrieves the Admin Contract instance.
     *
     * @returns The Admin Contract instance implementing NFTAdminBase.
     */
    getAdminContract(): NFTAdminBase {
      const admin = this.admin.getAndRequireEquals();
      admin
        .equals(PublicKey.empty())
        .assertFalse(CollectionErrors.adminContractAddressNotSet);
      return new this.getAdminContractConstructor(admin);
    }
 
    /**
     * Retrieves the Upgrade Authority Contract instance.
     *
     * @returns The Upgrade Authority Contract instance implementing UpgradeAuthorityBase.
     */
    async getUpgradeContract(): Promise<UpgradeAuthorityBase> {
      const collectionData = CollectionData.unpack(
        this.packedData.getAndRequireEquals()
      );
      collectionData.upgradeAuthority
        .equals(PublicKey.empty())
        .assertFalse(CollectionErrors.upgradeContractAddressNotSet);
      return new this.getUpgradeContractConstructor(
        collectionData.upgradeAuthority
      );
    }
 
    /**
     * Ensures that the transaction is authorized by the contract owner.
     *
     * @returns The AccountUpdate of the creator.
     */
    async ensureOwnerSignature(): Promise<AccountUpdate> {
      const sender = this.sender.getUnconstrained();
      const creator = this.creator.getAndRequireEquals();
      creator.assertEquals(sender);
      const creatorUpdate = AccountUpdate.createSigned(creator);
      creatorUpdate.body.useFullCommitment = Bool(true); // Prevent memo and fee change
      return creatorUpdate;
    }
 
    /**
     * Ensures that the collection is not paused.
     *
     * @returns The current collection data.
     */
    async ensureNotPaused(): Promise<CollectionData> {
      const collectionData = CollectionData.unpack(
        this.packedData.getAndRequireEquals()
      );
      collectionData.isPaused.assertFalse(CollectionErrors.collectionPaused);
      return collectionData;
    }
 
    /**
     * Mints a new NFT directly by the creator.
     *
     * This method allows the creator of the collection to mint an NFT without requiring approval
     * from the admin contract. It ensures that the collection is not paused and that the caller
     * is the creator of the collection. A fee of 1 MINA is deducted from the creator's balance
     * to cover the cost of creating a new account.
     *
     * We do not constrain here the address of the NFT to allow for the Master NFT to be minted.
     * The Master NFT is the NFT with the same address as the Collection contract and it holds
     * the metadata for the collection. It can be minted only by the creator of the collection.
     *
     * @param params - The mint parameters containing details of the NFT to be minted.
     */
 
    @method async mintByCreator(params: MintParams): Promise<void> {
      const collectionData = await this.ensureNotPaused();
      collectionData.canMint.assertTrue(
        CollectionErrors.mintApprovalNotRequired
      );
      const creatorUpdate = await this.ensureOwnerSignature();
      // Pay 1 MINA fee for a new account
      creatorUpdate.balance.subInPlace(1_000_000_000);
      await this._mint(params, collectionData);
    }
 
    /**
     * Mints a new NFT with approval.
     *
     * @param mintRequest - The minting request containing parameters and proofs.
     */
    @method async mint(mintRequest: MintRequest): Promise<void> {
      const collectionData = await this.ensureNotPaused();
      collectionData.canMint.assertTrue(
        CollectionErrors.mintApprovalNotRequired
      );
      const adminContract = this.getAdminContract();
      // The admin contract checks that the sender is allowed to mint
      const mintParams = (await adminContract.canMint(mintRequest)).assertSome(
        CollectionErrors.cannotMint
      );
 
      // Prevent minting the Master NFT using this method
      mintParams.address
        .equals(this.address)
        .assertFalse(CollectionErrors.cannotMintMasterNFT);
      const event = await this._mint(mintParams, collectionData);
      this.emitEvent("approveMint", event);
    }
 
    /**
     * Internal method to mint an NFT.
     *
     * @param params - The mint parameters.
     * @param collectionData - The current collection data.
     * @returns The MintEvent emitted.
     */
    async _mint(
      params: MintParams,
      collectionData: CollectionData
    ): Promise<MintEvent> {
      const {
        name,
        address,
        owner,
        data,
        metadata,
        storage,
        metadataVerificationKeyHash,
        expiry,
      } = params;
 
      this.network.globalSlotSinceGenesis.requireBetween(UInt32.zero, expiry);
      data.version.assertEquals(UInt32.zero);
      const packedData = data.pack();
      const tokenId = this.deriveTokenId();
 
      const update = AccountUpdate.createSigned(address, tokenId);
      update.body.useFullCommitment = Bool(true); // Prevent memo and fee change
      update.account.isNew.getAndRequireEquals().assertTrue();
 
      // Mint 1 NFT
      this.internal.mint({ address: update, amount: 1_000_000_000 });
 
      const nftVerificationKey = new VerificationKey({
        data: NFTVerificationKeys[networkId].nft.data,
        hash: Field(NFTVerificationKeys[networkId].nft.hash),
      });
      // Constrain the NFT verification key to the correct one for the network
      nftVerificationKey.hash.assertEquals(
        Field(NFTVerificationKeys[networkId].nft.hash)
      );
 
      const compiledVerificationKeyHash = Provable.witness(Field, () => {
        // We check the verification key to prevent the case when the verification key
        // is incorrect due to breaking changes in o1js
        // This check does NOT create a constraint on the verification key
        // as this witness can be replaced during runtime
        // and is useful only for debugging
        const vkHash = NFT._verificationKey?.hash;
        Iif (!vkHash) throw Error("NFT verification key is incorrect");
        return vkHash;
      });
      nftVerificationKey.hash.assertEquals(compiledVerificationKeyHash);
      update.body.update.verificationKey = {
        isSome: Bool(true),
        value: nftVerificationKey,
      };
      update.body.update.permissions = {
        isSome: Bool(true),
        value: {
          ...Permissions.default(),
          send: Permissions.proof(),
          // Allow the upgrade authority to set the verification key
          // even when there is no protocol upgrade
          setVerificationKey:
            Permissions.VerificationKey.proofDuringCurrentVersion(),
          setPermissions: Permissions.impossible(),
          access: Permissions.proof(),
          setZkappUri: Permissions.none(),
          setTokenSymbol: Permissions.none(),
        },
      };
      const initialState = new NFTStateStruct({
        name,
        metadata,
        owner,
        storage,
        packedData,
        metadataVerificationKeyHash,
      });
 
      update.body.update.appState = NFTStateStruct.toFields(initialState).map(
        (field) => ({
          isSome: Bool(true),
          value: field,
        })
      );
      const event = new MintEvent({
        initialState,
        address,
      });
      this.emitEvent("mint", event);
      return event;
    }
 
    /**
     * Updates the NFT without admin approval.
     *
     * @param proof - The proof of the NFT update.
     * @param vk - The verification key.
     */
    @method async update(
      proof: NFTUpdateProof,
      vk: VerificationKey
    ): Promise<void> {
      const collectionData = await this.ensureNotPaused();
      collectionData.requireUpdateApproval.assertFalse(
        CollectionErrors.updateApprovalRequired
      );
      await this._update(proof, vk);
    }
 
    /**
     * Updates the NFT with admin approval.
     *
     * @param proof - The proof of the NFT update.
     * @param vk - The verification key.
     */
    @method async updateWithApproval(
      proof: NFTUpdateProof,
      vk: VerificationKey
    ): Promise<void> {
      const collectionData = CollectionData.unpack(
        this.packedData.getAndRequireEquals()
      );
      collectionData.isPaused.assertFalse(CollectionErrors.collectionPaused);
      collectionData.requireUpdateApproval.assertTrue(
        CollectionErrors.updateApprovalNotRequired
      );
 
      const event = await this._update(proof, vk);
      const adminContract = this.getAdminContract();
      const canUpdate = await adminContract.canUpdate(
        proof.publicInput,
        proof.publicOutput
      );
      canUpdate.assertTrue();
      this.emitEvent("approveUpdate", proof.publicInput.immutableState.address);
    }
 
    /**
     * Internal method to update an NFT.
     *
     * @param proof - The proof of the NFT update.
     * @param vk - The verification key.
     */
    async _update(proof: NFTUpdateProof, vk: VerificationKey): Promise<void> {
      const creator = this.creator.getAndRequireEquals();
      const tokenId = this.deriveTokenId();
      tokenId.assertEquals(proof.publicInput.immutableState.tokenId);
      const nft = new NFT(proof.publicInput.immutableState.address, tokenId);
      const metadataVerificationKeyHash = await nft.update(
        proof.publicInput,
        proof.publicOutput,
        creator
      );
      this.emitEvent("update", proof.publicInput.immutableState.address);
      const sender = this.sender.getUnconstrained();
      const senderUpdate = AccountUpdate.createSigned(sender);
      senderUpdate.body.useFullCommitment = Bool(true); // Prevent memo and fee change
      // Verify the metadata update proof
      metadataVerificationKeyHash.assertEquals(vk.hash);
      proof.verify(vk);
    }
 
    /**
     * Lists an NFT for sale without approval.
     *
     * @param address - The address of the NFT.
     * @param price - The price at which to list the NFT.
     */
    @method async sell(address: PublicKey, price: UInt64): Promise<void> {
      const collectionData = await this.ensureNotPaused();
      collectionData.requireSaleApproval.assertFalse();
      await this._sell(address, price);
    }
 
    /**
     * Lists an NFT for sale with admin approval.
     *
     * @param address - The address of the NFT.
     * @param price - The price at which to list the NFT.
     */
    @method async sellWithApproval(
      address: PublicKey,
      price: UInt64
    ): Promise<void> {
      const collectionData = await this.ensureNotPaused();
      collectionData.requireSaleApproval.assertTrue();
 
      const event = await this._sell(address, price);
      const adminContract = this.getAdminContract();
      const canSell = await adminContract.canSell(address, event.seller, price);
      canSell.assertTrue();
      this.emitEvent("approveSell", event);
    }
 
    /**
     * Internal method to list an NFT for sale.
     *
     * @param address - The address of the NFT.
     * @param price - The price at which to list the NFT.
     * @returns The SellEvent emitted.
     */
    async _sell(address: PublicKey, price: UInt64): Promise<SellEvent> {
      const tokenId = this.deriveTokenId();
      const nft = new NFT(address, tokenId);
      const seller = this.sender.getUnconstrained();
      const sellerUpdate = AccountUpdate.createSigned(seller);
      sellerUpdate.body.useFullCommitment = Bool(true); // Prevent memo and fee change
      const event = await nft.sell(price, seller);
      this.emitEvent("sell", event);
      return event;
    }
 
    /**
     * Purchases an NFT without admin approval.
     *
     * @param address - The address of the NFT.
     * @param price - The price at which to purchase the NFT.
     */
    @method async buy(address: PublicKey, price: UInt64): Promise<void> {
      const collectionData = await this.ensureNotPaused();
      collectionData.requireBuyApproval.assertFalse();
      await this._buy(address, price, collectionData.royaltyFee);
    }
 
    /**
     * Purchases an NFT with admin approval.
     *
     * @param address - The address of the NFT.
     * @param price - The price at which to purchase the NFT.
     */
    @method async buyWithApproval(
      address: PublicKey,
      price: UInt64
    ): Promise<void> {
      const collectionData = await this.ensureNotPaused();
      collectionData.requireBuyApproval.assertTrue();
 
      const event = await this._buy(address, price, collectionData.royaltyFee);
      const adminContract = this.getAdminContract();
      const canBuy = await adminContract.canBuy(
        address,
        event.seller,
        event.buyer,
        price
      );
      canBuy.assertTrue();
      this.emitEvent("approveBuy", event);
    }
 
    /**
     * Internal method to purchase an NFT.
     *
     * @param address - The address of the NFT.
     * @param price - The price at which to purchase the NFT.
     * @param royaltyFee - The royalty fee percentage.
     * @returns The BuyEvent emitted.
     */
    async _buy(
      address: PublicKey,
      price: UInt64,
      royaltyFee: UInt32
    ): Promise<BuyEvent> {
      royaltyFee.assertLessThanOrEqual(UInt32.from(100_000)); // Max 100%
      const creator = this.creator.getAndRequireEquals();
      const buyer = this.sender.getUnconstrained();
      const buyerUpdate = AccountUpdate.createSigned(buyer);
      buyerUpdate.body.useFullCommitment = Bool(true); // Prevent memo and fee change
      const tokenId = this.deriveTokenId();
      const nft = new NFT(address, tokenId);
      const event = await nft.buy(price, buyer);
 
      // If the seller is the creator, then the commission is 0
      const isSellerCreator = event.seller.equals(creator);
      const commission = Provable.if(
        isSellerCreator,
        UInt64.zero,
        price.div(100_000).mul(UInt64.from(royaltyFee))
      );
      const payment = price.sub(commission);
 
      buyerUpdate.send({ to: event.seller, amount: payment });
 
      // If the seller is not the creator, then send the commission to the creator
      const creatorUpdate = AccountUpdate.createIf(
        isSellerCreator.not(),
        creator
      );
      creatorUpdate.balance.addInPlace(commission);
      buyerUpdate.balance.subInPlace(commission);
      this.emitEvent("buy", event);
      return event;
    }
 
    /**
     * Transfers ownership of an NFT without admin approval.
     *
     * @param address - The address of the NFT.
     * @param to - The recipient's public key.
     */
    @method async transfer(address: PublicKey, to: PublicKey): Promise<void> {
      const collectionData = await this.ensureNotPaused();
      collectionData.requireTransferApproval.assertFalse(
        CollectionErrors.transferApprovalRequired
      );
      await this._transfer(address, to, collectionData.transferFee);
    }
 
    /**
     * Transfers ownership of an NFT with admin approval.
     *
     * @param address - The address of the NFT.
     * @param to - The recipient's public key.
     */
    @method async transferWithApproval(
      address: PublicKey,
      to: PublicKey
    ): Promise<void> {
      const collectionData = await this.ensureNotPaused();
      collectionData.requireTransferApproval.assertTrue(
        CollectionErrors.transferApprovalNotRequired
      );
 
      const event = await this._transfer(
        address,
        to,
        collectionData.transferFee
      );
      const adminContract = this.getAdminContract();
      const canTransfer = await adminContract.canTransfer(
        address,
        event.from,
        event.to
      );
      canTransfer.assertTrue();
      this.emitEvent("approveTransfer", event);
    }
 
    /**
     * Internal method to transfer an NFT.
     *
     * @param address - The address of the NFT.
     * @param to - The recipient's public key.
     * @param transferFee - The transfer fee amount.
     * @returns The TransferEvent emitted.
     */
    async _transfer(
      address: PublicKey,
      to: PublicKey,
      transferFee: UInt64
    ): Promise<TransferEvent> {
      const sender = this.sender.getUnconstrained();
      const senderUpdate = AccountUpdate.createSigned(sender);
      senderUpdate.body.useFullCommitment = Bool(true); // Prevent memo and fee change
      senderUpdate.send({
        to: this.creator.getAndRequireEquals(),
        amount: transferFee,
      });
      const tokenId = this.deriveTokenId();
      const nft = new NFT(address, tokenId);
      const oldOwner = await nft.transfer(sender, to);
      oldOwner.assertEquals(sender);
      const transferEvent = new TransferEvent({
        from: oldOwner,
        to,
        address,
      });
      this.emitEvent("transfer", transferEvent);
      return transferEvent;
    }
 
    /**
     * Upgrades the verification key of a specific NFT.
     *
     * @param address - The address of the NFT.
     * @param vk - The new verification key.
     */
    @method
    async upgradeNFTVerificationKey(
      address: PublicKey,
      vk: VerificationKey
    ): Promise<void> {
      const collectionData = CollectionData.unpack(
        this.packedData.getAndRequireEquals()
      );
      const sender = this.sender.getAndRequireSignature();
      const creator = this.creator.getAndRequireEquals();
      creator
        .equals(sender)
        .or(collectionData.requireCreatorSignatureToUpgradeNFT.not())
        .assertTrue(CollectionErrors.creatorSignatureRequiredToUpgradeNFT);
      const upgradeContract = await this.getUpgradeContract();
      const tokenId = this.deriveTokenId();
      const nft = new NFT(address, tokenId);
      // fetchAccount() should be called before calling this method
      // this code should be changed after verification key precondition
      // will be added to the Mina protocol
      const previousVerificationKeyHash = Provable.witness(Field, () => {
        const account = Mina.getAccount(address, tokenId);
        const vkHash = account.zkapp?.verificationKey?.hash;
        Iif (!vkHash) {
          throw Error("Verification key hash not found");
        }
        return vkHash;
      });
      const data = new VerificationKeyUpgradeData({
        address: address,
        tokenId: tokenId,
        previousVerificationKeyHash,
        newVerificationKeyHash: vk.hash,
      });
      const upgradeAuthorityAnswer = await upgradeContract.verifyUpgradeData(
        data
      );
      upgradeAuthorityAnswer.isVerified.assertTrue(
        CollectionErrors.cannotUpgradeVerificationKey
      );
      const event = await nft.upgradeVerificationKey(vk, sender);
      this.emitEvent("upgradeNFTVerificationKey", event);
    }
 
    /**
     * Upgrades the verification key of the collection contract.
     *
     * @param vk - The new verification key.
     */
    @method
    async upgradeVerificationKey(vk: VerificationKey): Promise<void> {
      const collectionData = CollectionData.unpack(
        this.packedData.getAndRequireEquals()
      );
      const sender = this.sender.getAndRequireSignature();
      const creator = this.creator.getAndRequireEquals();
      creator
        .equals(sender)
        .or(collectionData.requireCreatorSignatureToUpgradeCollection.not())
        .assertTrue(
          CollectionErrors.creatorSignatureRequiredToUpgradeCollection
        );
 
      const upgradeContract = await this.getUpgradeContract();
      // fetchAccount() should be called before calling this method
      // this code should be changed after verification key precondition
      // will be added to the Mina protocol as this code does NOT
      // create a constraint on the verification key
      const previousVerificationKeyHash = Provable.witness(Field, () => {
        const account = Mina.getAccount(this.address);
        const vkHash = account.zkapp?.verificationKey?.hash;
        Iif (!vkHash) {
          throw Error("Verification key hash not found");
        }
        return vkHash;
      });
      const data = new VerificationKeyUpgradeData({
        address: this.address,
        tokenId: this.tokenId,
        previousVerificationKeyHash,
        newVerificationKeyHash: vk.hash,
      });
      const upgradeAuthorityAnswer = await upgradeContract.verifyUpgradeData(
        data
      );
      upgradeAuthorityAnswer.isVerified.assertTrue(
        CollectionErrors.cannotUpgradeVerificationKey
      );
      collectionData.upgradeAuthority =
        upgradeAuthorityAnswer.nextUpgradeAuthority.orElse(
          collectionData.upgradeAuthority
        );
      this.account.verificationKey.set(vk);
 
      this.packedData.set(collectionData.pack());
      this.emitEvent("upgradeVerificationKey", vk.hash);
    }
 
    /**
     * Limits further minting of NFTs in the collection.
     */
    @method
    async limitMinting(): Promise<void> {
      await this.ensureOwnerSignature();
      const collectionData = await this.ensureNotPaused();
      collectionData.canMint = Bool(false);
      this.packedData.set(collectionData.pack());
      this.emitEvent(
        "limitMinting",
        new LimitMintingEvent({ mintingLimited: Bool(true) })
      );
    }
 
    /**
     * Pauses the collection, disabling certain actions.
     */
    @method
    async pause(): Promise<void> {
      await this.ensureOwnerSignature();
      const collectionData = await this.ensureNotPaused();
      collectionData.canPause.assertTrue(CollectionErrors.noPermissionToPause);
      collectionData.isPaused = Bool(true);
      this.packedData.set(collectionData.pack());
      this.emitEvent("pause", new PauseEvent({ isPaused: Bool(true) }));
    }
 
    /**
     * Resumes the collection, re-enabling actions.
     */
    @method
    async resume(): Promise<void> {
      /*
      Should we have a mechanism where everyone can resume the collection after some time period
      to prevent pausing it indefinitely?
      We can define maxPausePeriod and if the collection is paused for the maxPausePeriod time
      then anyone can resume it.
      */
      await this.ensureOwnerSignature();
      const collectionData = CollectionData.unpack(
        this.packedData.getAndRequireEquals()
      );
      collectionData.canPause.assertTrue(CollectionErrors.noPermissionToResume);
      collectionData.isPaused.assertTrue(CollectionErrors.collectionNotPaused);
      collectionData.isPaused = Bool(false);
      this.packedData.set(collectionData.pack());
      this.emitEvent("resume", new PauseEvent({ isPaused: Bool(false) }));
    }
 
    /**
     * Pauses a specific NFT, disabling its actions.
     *
     * @param address - The address of the NFT to pause.
     */
    @method
    async pauseNFT(address: PublicKey): Promise<void> {
      const tokenId = this.deriveTokenId();
      const nft = new NFT(address, tokenId);
      await nft.pause();
      this.emitEvent(
        "pauseNFT",
        new PauseNFTEvent({ isPaused: Bool(true), address })
      );
    }
 
    /**
     * Resumes a specific NFT, re-enabling its actions.
     *
     * @param address - The address of the NFT to resume.
     */
    @method
    async resumeNFT(address: PublicKey): Promise<void> {
      const tokenId = this.deriveTokenId();
      const nft = new NFT(address, tokenId);
      await nft.resume();
      this.emitEvent(
        "resumeNFT",
        new PauseNFTEvent({ isPaused: Bool(false), address })
      );
    }
 
    /**
     * Updates the collection's configuration (e.g., name, base URL, fees).
     *
     * @param configuration - The new configuration settings.
     */
    @method
    async updateConfiguration(
      configuration: CollectionConfigurationUpdate
    ): Promise<void> {
      await this.ensureOwnerSignature();
      const collectionData = await this.ensureNotPaused();
      const name = this.collectionName.getAndRequireEquals();
      const baseURL = this.baseURL.getAndRequireEquals();
      const admin = this.admin.getAndRequireEquals();
      name
        .equals(configuration.name)
        .not()
        .and(collectionData.canChangeName.not())
        .assertFalse(CollectionErrors.noPermissionToChangeName);
      this.collectionName.set(configuration.name);
 
      baseURL
        .equals(configuration.baseURL)
        .not()
        .and(collectionData.canChangeBaseUri.not())
        .assertFalse(CollectionErrors.noPermissionToChangeBaseUri);
      this.baseURL.set(configuration.baseURL);
 
      admin
        .equals(configuration.admin)
        .not()
        .and(collectionData.canSetAdmin.not())
        .assertFalse(CollectionErrors.noPermissionToSetAdmin);
      this.admin.set(configuration.admin);
 
      collectionData.royaltyFee
        .equals(configuration.royaltyFee)
        .not()
        .and(collectionData.canChangeRoyalty.not())
        .assertFalse(CollectionErrors.noPermissionToChangeRoyalty);
      collectionData.royaltyFee = configuration.royaltyFee;
 
      collectionData.transferFee
        .equals(configuration.transferFee)
        .not()
        .and(collectionData.canChangeTransferFee.not())
        .assertFalse(CollectionErrors.noPermissionToChangeTransferFee);
      collectionData.transferFee = configuration.transferFee;
      this.packedData.set(collectionData.pack());
    }
 
    /**
     * Transfers ownership of the collection to a new owner.
     *
     * @param newOwner - The public key of the new owner.
     * @returns The public key of the old owner.
     */
    @method.returns(PublicKey)
    async transferOwnership(newOwner: PublicKey): Promise<PublicKey> {
      await this.ensureOwnerSignature();
      const collectionData = await this.ensureNotPaused();
      collectionData.canChangeCreator.assertTrue(
        CollectionErrors.noPermissionToChangeCreator
      );
      const oldOwner = this.creator.getAndRequireEquals();
      this.creator.set(newOwner);
      this.emitEvent(
        "ownershipChange",
        new OwnershipChangeEvent({
          oldOwner,
          newOwner,
        })
      );
      return oldOwner;
    }
  }
  return Collection;
}