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 | 4x 4x 4x 3x 99x 99x 99x 99x 2x 2x 2x 2x 2x 2x 2x 99x 15x 15x 15x 15x 15x 15x 6x 6x 3x 6x 6x 6x 3x 3x 3x 3x 6x 6x 6x 6x 6x 6x 3x 3x 3x 7x 7x 3x 16x 16x 3x 7x 7x 3x 7x 7x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x | import { Bool, DeployArgs, method, Permissions, PublicKey, SmartContract, State, state, VerificationKey, UInt64, Provable, Field, AccountUpdate, Mina, } from "o1js"; import { MintRequest, NFTState, NFTAdminBase, MintParamsOption, UpgradeAuthorityBase, VerificationKeyUpgradeData, UpgradableContract, UpgradeAuthorityContractConstructor, PausableContract, PauseEvent, OwnershipChangeEvent, OwnableContract, } from "../contracts"; export { NFTAdminContract }; interface NFTAdminDeployProps extends Exclude<DeployArgs, undefined> { admin: PublicKey; upgradeAuthority: PublicKey; uri: string; canPause: Bool; isPaused: Bool; } function NFTAdminContract( params: { upgradeContract?: UpgradeAuthorityContractConstructor; } = {} ) { const { upgradeContract } = params; /** * The **NFTAdmin** contract serves as the foundational administrative layer for NFT collections on the Mina Protocol. * It provides essential functionalities such as contract upgrades, pausing and resuming operations, and ownership management. * This contract can be extended by custom admin contracts to implement specific administrative logic, * ensuring flexibility while maintaining a standardized interface. */ class NFTAdmin extends SmartContract implements NFTAdminBase, UpgradableContract, PausableContract, OwnableContract { /** * The public key of the contract's administrator. * This account has the authority to perform administrative actions such as pausing the contract or upgrading the verification key. */ @state(PublicKey) admin = State<PublicKey>(); /** * The public key of the upgrade authority contract. * This is the contract responsible for validating and authorizing upgrades to the verification key. */ @state(PublicKey) upgradeAuthority = State<PublicKey>(); /** * A boolean flag indicating whether the contract is currently paused. * When `true`, certain operations are disabled. */ @state(Bool) isPaused = State<Bool>(); /** * A boolean flag indicating whether the contract has the ability to be paused. * This allows for disabling the pause functionality if desired. */ @state(Bool) canPause = State<Bool>(); /** * Deploys the contract with initial settings. * @param props - Deployment properties including admin, upgradeAuthority, uri, canPause, and isPaused. */ async deploy(props: NFTAdminDeployProps) { await super.deploy(props); this.admin.set(props.admin); this.upgradeAuthority.set(props.upgradeAuthority); this.isPaused.set(props.isPaused); this.canPause.set(props.canPause); this.account.zkappUri.set(props.uri); this.account.permissions.set({ ...Permissions.default(), // Allow the upgrade authority to set the verification key even without a protocol upgrade, // enabling upgrades in case of o1js breaking changes. setVerificationKey: Permissions.VerificationKey.proofDuringCurrentVersion(), setPermissions: Permissions.impossible(), }); } /** * Contract events emitted during various operations. */ events = { /** Emitted when the verification key is upgraded. */ upgradeVerificationKey: Field, /** Emitted when the contract is paused. */ pause: PauseEvent, /** Emitted when the contract is resumed. */ resume: PauseEvent, /** Emitted when ownership of the contract changes. */ ownershipChange: OwnershipChangeEvent, }; /** * Ensures that the transaction is authorized by the contract owner. * @returns A signed `AccountUpdate` from the admin. */ async ensureOwnerSignature(): Promise<AccountUpdate> { const sender = this.sender.getUnconstrained(); const admin = this.admin.getAndRequireEquals(); admin.assertEquals(sender); const adminUpdate = AccountUpdate.createSigned(admin); adminUpdate.body.useFullCommitment = Bool(true); // Prevent memo and fee change return adminUpdate; } /** * Retrieves the associated upgrade authority contract. * @returns An instance of `UpgradeAuthorityBase`. * @throws If the upgrade contract is not provided. */ async getUpgradeContract(): Promise<UpgradeAuthorityBase> { Iif (!upgradeContract) { throw Error("Upgrade contract not provided"); } return new upgradeContract(this.upgradeAuthority.getAndRequireEquals()); } /** * Upgrades the contract's verification key after validating with the upgrade authority. * @param vk - The new verification key to upgrade to. */ @method async upgradeVerificationKey(vk: VerificationKey) { await this.ensureOwnerSignature(); const upgradeContract = await this.getUpgradeContract(); // Fetch the previous verification key hash 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; }); // Create the upgrade data const data = new VerificationKeyUpgradeData({ address: this.address, tokenId: this.tokenId, previousVerificationKeyHash, newVerificationKeyHash: vk.hash, }); // Verify the upgrade data with the upgrade authority const upgradeAuthorityAnswer = await upgradeContract.verifyUpgradeData( data ); upgradeAuthorityAnswer.isVerified.assertTrue( "Cannot upgrade verification key" ); // Set the new verification key this.account.verificationKey.set(vk); // Update the upgrade authority if provided this.upgradeAuthority.set( upgradeAuthorityAnswer.nextUpgradeAuthority.orElse( this.upgradeAuthority.getAndRequireEquals() ) ); // Emit the upgrade event this.emitEvent("upgradeVerificationKey", vk.hash); } /** * Determines whether minting is allowed for the given request. * Returns mint parameters if allowed, or none if not allowed. * @param mintRequest - The minting request details. * @returns A `MintParamsOption` indicating if minting is permitted. */ @method.returns(MintParamsOption) async canMint(mintRequest: MintRequest): Promise<MintParamsOption> { // Only the creator can mint by default return MintParamsOption.none(); } /** * Checks whether the NFT state can be updated. * Typically returns true if the contract is not paused. * @param input - The current state of the NFT. * @param output - The desired new state of the NFT. * @returns A `Bool` indicating whether the update is allowed. */ @method.returns(Bool) async canUpdate(input: NFTState, output: NFTState) { const isPaused = this.isPaused.getAndRequireEquals(); return isPaused.not(); } /** * Determines whether a transfer between the specified addresses is permitted. * @param address - The NFT contract address. * @param from - The sender's public key. * @param to - The recipient's public key. * @returns A `Bool` indicating whether the transfer is allowed. */ @method.returns(Bool) async canTransfer(address: PublicKey, from: PublicKey, to: PublicKey) { const isPaused = this.isPaused.getAndRequireEquals(); return isPaused.not(); } /** * Determines whether the NFT can be listed for sale at the given price. * @param address - The NFT contract address. * @param seller - The seller's public key. * @param price - The listing price. * @returns A `Bool` indicating whether the sale is permitted. */ @method.returns(Bool) async canSell(address: PublicKey, seller: PublicKey, price: UInt64) { const isPaused = this.isPaused.getAndRequireEquals(); return isPaused.not(); } /** * Determines whether the NFT can be purchased by the buyer from the seller at the given price. * @param address - The NFT contract address. * @param seller - The seller's public key. * @param buyer - The buyer's public key. * @param price - The purchase price. * @returns A `Bool` indicating whether the purchase is allowed. */ @method.returns(Bool) async canBuy( address: PublicKey, seller: PublicKey, buyer: PublicKey, price: UInt64 ) { const isPaused = this.isPaused.getAndRequireEquals(); return isPaused.not(); } /** * Pauses the contract, disabling certain administrative actions. * Can only be called by the admin if `canPause` is `true`. */ @method async pause(): Promise<void> { await this.ensureOwnerSignature(); this.canPause.getAndRequireEquals().assertTrue(); this.isPaused.set(Bool(true)); this.emitEvent("pause", new PauseEvent({ isPaused: Bool(true) })); } /** * Resumes the contract, re-enabling administrative actions. * Can only be called by the admin if `canPause` is `true`. */ @method async resume(): Promise<void> { await this.ensureOwnerSignature(); this.canPause.getAndRequireEquals().assertTrue(); this.isPaused.set(Bool(false)); this.emitEvent("resume", new PauseEvent({ isPaused: Bool(false) })); } /** * Transfers ownership of the contract to a new admin. * @param newOwner - The public key of the new owner. * @returns The public key of the previous owner. */ @method.returns(PublicKey) async transferOwnership(newOwner: PublicKey): Promise<PublicKey> { await this.ensureOwnerSignature(); const oldOwner = this.admin.getAndRequireEquals(); this.admin.set(newOwner); this.emitEvent( "ownershipChange", new OwnershipChangeEvent({ oldOwner, newOwner, }) ); return oldOwner; } } return NFTAdmin; } |