Created
August 20, 2024 10:27
-
-
Save ptisserand/2620efbcf208d43657ffd2f8cc153d27 to your computer and use it in GitHub Desktop.
OZ 0.15.1 ERC721 custom token_uri
This file contains hidden or 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
// SPDX-License-Identifier: MIT | |
// Compatible with OpenZeppelin Contracts for Cairo ^0.15.0 | |
#[starknet::contract] | |
mod MyToken { | |
use openzeppelin::introspection::src5::SRC5Component; | |
use openzeppelin::token::erc721::ERC721Component; | |
use openzeppelin::token::erc721::ERC721HooksEmptyImpl; | |
use openzeppelin::token::erc721::interface::{IERC721Metadata, IERC721MetadataCamelOnly}; | |
component!(path: ERC721Component, storage: erc721, event: ERC721Event); | |
component!(path: SRC5Component, storage: src5, event: SRC5Event); | |
// ERC721Mixin can't be used since we have a custom implementation for Metadata | |
#[abi(embed_v0)] | |
impl ERC721Impl = ERC721Component::ERC721Impl<ContractState>; | |
#[abi(embed_v0)] | |
impl ERC721CamelOnly = ERC721Component::ERC721CamelOnlyImpl<ContractState>; | |
impl ERC721InternalImpl = ERC721Component::InternalImpl<ContractState>; | |
#[storage] | |
struct Storage { | |
#[substorage(v0)] | |
erc721: ERC721Component::Storage, | |
#[substorage(v0)] | |
src5: SRC5Component::Storage, | |
} | |
#[event] | |
#[derive(Drop, starknet::Event)] | |
enum Event { | |
#[flat] | |
ERC721Event: ERC721Component::Event, | |
#[flat] | |
SRC5Event: SRC5Component::Event, | |
} | |
#[constructor] | |
fn constructor(ref self: ContractState) { | |
self.erc721.initializer("MyToken", "MTK", ""); | |
} | |
#[abi(embed_v0)] | |
impl ERC721CustomMetadataImpl of IERC721Metadata<ContractState> { | |
fn name(self: @ContractState) -> ByteArray { | |
self.erc721.name() | |
} | |
fn symbol(self: @ContractState) -> ByteArray { | |
self.erc721.symbol() | |
} | |
fn token_uri(self: @ContractState, token_id: u256) -> ByteArray { | |
format!("boom/{}", token_id) | |
} | |
} | |
#[abi(embed_v0)] | |
impl ERC721CustomMetadataCamelOnlyImpl of IERC721MetadataCamelOnly<ContractState> { | |
fn tokenURI(self: @ContractState, tokenId: u256) -> ByteArray { | |
self.token_uri(tokenId) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment