-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NFTs module, minting from the board, and color based pixel quests
- Loading branch information
1 parent
5c14814
commit 9c53672
Showing
13 changed files
with
458 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#[starknet::contract] | ||
mod CanvasNFT { | ||
use openzeppelin::token::erc721::ERC721Component; | ||
use openzeppelin::introspection::src5::SRC5Component; | ||
use starknet::ContractAddress; | ||
use art_peace::nfts::component::CanvasNFTStoreComponent; | ||
use art_peace::nfts::{ICanvasNFTAdditional, NFTMetadata}; | ||
|
||
component!(path: ERC721Component, storage: erc721, event: ERC721Event); | ||
component!(path: SRC5Component, storage: src5, event: SRC5Event); | ||
component!(path: CanvasNFTStoreComponent, storage: nfts, event: NFTEvent); | ||
|
||
#[abi(embed_v0)] | ||
impl ERC721Impl = ERC721Component::ERC721Impl<ContractState>; | ||
#[abi(embed_v0)] | ||
impl ERC721MetadataImpl = ERC721Component::ERC721MetadataImpl<ContractState>; | ||
#[abi(embed_v0)] | ||
impl SRC5Impl = SRC5Component::SRC5Impl<ContractState>; | ||
#[abi(embed_v0)] | ||
impl CanvasNFTStoreImpl = CanvasNFTStoreComponent::CanvasNFTStoreImpl<ContractState>; | ||
|
||
impl InternalImpl = ERC721Component::InternalImpl<ContractState>; | ||
|
||
#[storage] | ||
struct Storage { | ||
art_peace: ContractAddress, | ||
#[substorage(v0)] | ||
erc721: ERC721Component::Storage, | ||
#[substorage(v0)] | ||
src5: SRC5Component::Storage, | ||
#[substorage(v0)] | ||
nfts: CanvasNFTStoreComponent::Storage, | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
enum Event { | ||
#[flat] | ||
ERC721Event: ERC721Component::Event, | ||
#[flat] | ||
SRC5Event: SRC5Component::Event, | ||
#[flat] | ||
NFTEvent: CanvasNFTStoreComponent::Event, | ||
} | ||
|
||
#[constructor] | ||
fn constructor(ref self: ContractState, name: ByteArray, symbol: ByteArray, art_peace_addr: ContractAddress) { | ||
self.art_peace.write(art_peace_addr); | ||
let base_uri = format!("{:?}", art_peace_addr); | ||
self.erc721.initializer(name, symbol, base_uri); | ||
} | ||
|
||
#[abi(embed_v0)] | ||
impl CanvasNFTAdditional of ICanvasNFTAdditional<ContractState> { | ||
fn mint(ref self: ContractState, metadata: NFTMetadata, receiver: ContractAddress) { | ||
assert(self.art_peace.read() == starknet::get_caller_address(), 'Only ArtPeace contract can mint'); | ||
let token_id = self.nfts.get_nfts_count(); | ||
self.nfts.nfts_data.write(token_id, metadata); | ||
self.erc721._mint(receiver, token_id); | ||
self.nfts.nfts_count.write(token_id + 1); | ||
// TODO: self.emit(Event::NFTEvent::CanvasNFTMinted { token_id, metadata }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#[starknet::component] | ||
pub mod CanvasNFTStoreComponent { | ||
use art_peace::nfts::interfaces::{ICanvasNFTStore, NFTMetadata}; | ||
|
||
#[storage] | ||
struct Storage { | ||
nfts_count: u256, | ||
// Map: nft's token_id -> nft's metadata | ||
nfts_data: LegacyMap::<u256, NFTMetadata>, | ||
} | ||
|
||
#[event] | ||
#[derive(Drop, starknet::Event)] | ||
pub enum Event { | ||
CanvasNFTMinted: CanvasNFTMinted, | ||
} | ||
|
||
#[derive(Drop, starknet::Event)] | ||
struct CanvasNFTMinted { | ||
#[key] | ||
token_id: u256, | ||
metadata: NFTMetadata, | ||
} | ||
|
||
#[embeddable_as(CanvasNFTStoreImpl)] | ||
impl CanvasNFTStore< | ||
TContractState, +HasComponent<TContractState> | ||
> of ICanvasNFTStore<ComponentState<TContractState>> { | ||
fn get_nfts_count(self: @ComponentState<TContractState>) -> u256 { | ||
return self.nfts_count.read(); | ||
} | ||
|
||
fn get_nft_metadata(self: @ComponentState<TContractState>, token_id: u256) -> NFTMetadata { | ||
return self.nfts_data.read(token_id); | ||
} | ||
|
||
fn get_nft_minter(self: @ComponentState<TContractState>, token_id: u256) -> starknet::ContractAddress { | ||
let metadata: NFTMetadata = self.nfts_data.read(token_id); | ||
return metadata.minter; | ||
} | ||
|
||
fn get_nft_image_hash(self: @ComponentState<TContractState>, token_id: u256) -> felt252 { | ||
let metadata: NFTMetadata = self.nfts_data.read(token_id); | ||
return metadata.image_hash; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#[derive(Drop, Serde)] | ||
pub struct NFTMintParams { | ||
pub position: u128, | ||
pub width: u128, | ||
pub height: u128, | ||
} | ||
|
||
#[derive(Drop, Serde, PartialEq, starknet::Store)] | ||
pub struct NFTMetadata { | ||
pub position: u128, | ||
pub width: u128, | ||
pub height: u128, | ||
pub image_hash: felt252, | ||
pub block_number: u64, | ||
pub minter: starknet::ContractAddress, | ||
} | ||
|
||
#[starknet::interface] | ||
pub trait ICanvasNFTStore<TContractState> { | ||
// Returns the on-chain metadata of the NFT. | ||
fn get_nft_metadata(self: @TContractState, token_id: u256) -> NFTMetadata; | ||
fn get_nft_minter(self: @TContractState, token_id: u256) -> starknet::ContractAddress; | ||
fn get_nft_image_hash(self: @TContractState, token_id: u256) -> felt252; | ||
|
||
// Returns the number of NFTs stored in the contract state. | ||
fn get_nfts_count(self: @TContractState) -> u256; | ||
} | ||
|
||
#[starknet::interface] | ||
pub trait ICanvasNFTAdditional<TContractState> { | ||
// Mint a new NFT called by the ArtPeaceNFTMinter contract. | ||
fn mint(ref self: TContractState, metadata: NFTMetadata, receiver: starknet::ContractAddress); | ||
} | ||
|
||
#[starknet::interface] | ||
pub trait IArtPeaceNFTMinter<TContractState> { | ||
// Mints a new NFT from the canvas using init params, and returns the token ID. | ||
fn mint_nft(self: @TContractState, mint_params: NFTMintParams); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.