|
| 1 | +#[starknet::contract] |
| 2 | +pub mod protocolNFT { |
| 3 | + // ************************************************************************* |
| 4 | + // IMPORTS |
| 5 | + // ************************************************************************* |
| 6 | + |
| 7 | + use openzeppelin_token::erc721::interface::IERC721Metadata; |
| 8 | +use ERC721Component::InternalTrait; |
| 9 | +use openzeppelin_token::erc721::interface::ERC721ABI; |
| 10 | + use starknet::{ContractAddress, get_block_timestamp}; |
| 11 | + use core::num::traits::zero::Zero; |
| 12 | + use openzeppelin_introspection::src5::SRC5Component; |
| 13 | + use openzeppelin_token::erc721::{ERC721Component, ERC721HooksEmptyImpl}; |
| 14 | + use openzeppelin_access::ownable::OwnableComponent; |
| 15 | + use openzeppelin_upgrades::UpgradeableComponent; |
| 16 | + |
| 17 | + |
| 18 | + use crate::mods::interfaces::ICustom::ICustom; |
| 19 | + use crate::mods::errors::Errors; |
| 20 | + use crate::mods::Utils::Convert_felt_to_bytearray::convert_into_byteArray; |
| 21 | + |
| 22 | + |
| 23 | + use starknet::storage::{ |
| 24 | + Map, StoragePointerWriteAccess, StoragePointerReadAccess, StorageMapReadAccess, |
| 25 | + StorageMapWriteAccess |
| 26 | + }; |
| 27 | + |
| 28 | + |
| 29 | + // ************************************************************************* |
| 30 | + // COMPONENTS |
| 31 | + // ************************************************************************* |
| 32 | + component!(path: OwnableComponent, storage: ownable, event: OwnableEvent); |
| 33 | + component!(path: ERC721Component, storage: erc721, event: ERC721Event); |
| 34 | + component!(path: SRC5Component, storage: src5, event: SRC5Event); |
| 35 | + component!(path: UpgradeableComponent, storage: upgradeable, event: UpgradeableEvent); |
| 36 | + |
| 37 | + // ERC721 Mixin |
| 38 | + impl ERC721MixinImpl = ERC721Component::ERC721MixinImpl<ContractState>; |
| 39 | + impl ERC721InternalImpl = ERC721Component::InternalImpl<ContractState>; |
| 40 | + |
| 41 | + // add an owner |
| 42 | + #[abi(embed_v0)] |
| 43 | + impl OwnableImpl = OwnableComponent::OwnableImpl<ContractState>; |
| 44 | + impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>; |
| 45 | + |
| 46 | + impl UpgradeableInternalImpl = UpgradeableComponent::InternalImpl<ContractState>; |
| 47 | + |
| 48 | + // ************************************************************************* |
| 49 | + // STORAGE |
| 50 | + // ************************************************************************* |
| 51 | + #[storage] |
| 52 | + struct Storage { |
| 53 | + #[substorage(v0)] |
| 54 | + erc721: ERC721Component::Storage, |
| 55 | + #[substorage(v0)] |
| 56 | + src5: SRC5Component::Storage, |
| 57 | + #[substorage(v0)] |
| 58 | + ownable: OwnableComponent::Storage, |
| 59 | + #[substorage(v0)] |
| 60 | + upgradeable: UpgradeableComponent::Storage, |
| 61 | + last_minted_id: u256, |
| 62 | + mint_timestamp: Map<u256, u64>, |
| 63 | + user_token_id: Map<ContractAddress, u256>, |
| 64 | + protocol_id: u256 |
| 65 | + } |
| 66 | + |
| 67 | + // ************************************************************************* |
| 68 | + // EVENTS |
| 69 | + // ************************************************************************* |
| 70 | + #[event] |
| 71 | + #[derive(Drop, starknet::Event)] |
| 72 | + enum Event { |
| 73 | + #[flat] |
| 74 | + ERC721Event: ERC721Component::Event, |
| 75 | + #[flat] |
| 76 | + SRC5Event: SRC5Component::Event, |
| 77 | + #[flat] |
| 78 | + OwnableEvent: OwnableComponent::Event, |
| 79 | + #[flat] |
| 80 | + UpgradeableEvent: UpgradeableComponent::Event |
| 81 | + } |
| 82 | + |
| 83 | + // ************************************************************************* |
| 84 | + // CONSTRUCTOR |
| 85 | + // ************************************************************************* |
| 86 | + #[constructor] |
| 87 | + fn constructor(ref self: ContractState, protocol_id: u256, admin: ContractAddress) { |
| 88 | + self.ownable.initializer(admin); |
| 89 | + self.protocol_id.write(protocol_id); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + #[abi(embed_v0)] |
| 94 | + impl IprotocolNFT of ICustom<ContractState> { |
| 95 | + fn mint_nft(ref self: ContractState, user_address: ContractAddress) -> u256 { |
| 96 | + let balance = self.erc721.balance_of(user_address); |
| 97 | + assert(balance.is_zero(), Errors::ALREADY_MINTED); |
| 98 | + let token_id = self.last_minted_id.read() + 1; |
| 99 | + self.erc721.mint(user_address, token_id); |
| 100 | + let timestamp: u64 = get_block_timestamp(); |
| 101 | + self.user_token_id.write(user_address, token_id); |
| 102 | + self.last_minted_id.write(token_id); |
| 103 | + self.mint_timestamp.write(token_id, timestamp); |
| 104 | + return token_id; |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + fn burn_nft(ref self: ContractState, user_address: ContractAddress, token_id: u256) { |
| 109 | + let user_token_id = self.user_token_id.read(user_address); |
| 110 | + assert(user_token_id == token_id, Errors::INVALID_TOKEN_ID); |
| 111 | + assert(self.erc721.exists(token_id), Errors::TOKEN_NOT_EXISTS); |
| 112 | + self.erc721.burn(token_id); |
| 113 | + self.user_token_id.write(user_address, 0); |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + // ************************************************************************* |
| 118 | + // GETTERS |
| 119 | + // ************************************************************************* |
| 120 | + |
| 121 | + fn get_user_token_id(self: @ContractState, user: ContractAddress) -> u256 { |
| 122 | + return self.user_token_id.read(user); |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + // ************************************************************************* |
| 127 | + // METADATA |
| 128 | + // ************************************************************************* |
| 129 | + |
| 130 | + |
| 131 | + fn name(self: @ContractState) -> ByteArray { |
| 132 | + let mut collection_name = ArrayTrait::<felt252>::new(); |
| 133 | + let protocol_id_felt252: felt252 = self.protocol_id.read().try_into().unwrap(); |
| 134 | + collection_name.append('PROTOCOL'); |
| 135 | + collection_name.append(protocol_id_felt252); |
| 136 | + let protocol_name_byte = convert_into_byteArray(ref collection_name); |
| 137 | + |
| 138 | + return protocol_name_byte; |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + fn symbol(self: @ContractState)-> ByteArray { |
| 144 | + return ""; |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + fn token_uri(self: @ContractState, token_id: u256) -> ByteArray { |
| 149 | + let token_uri = IERC721Metadata::token_uri(self.erc721, token_id); |
| 150 | + return token_uri; |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
0 commit comments