forked from Scaffold-Stark/scaffold-stark-2
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add ERC721Enumerable component
- Loading branch information
1 parent
958b0c8
commit 25970fe
Showing
5 changed files
with
76 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use starknet::ContractAddress; | ||
|
||
#[starknet::interface] | ||
pub trait IERC721Enumerable<T> { | ||
fn token_of_owner_by_index(self: @T, owner: ContractAddress, index: u256) -> u256; | ||
fn total_supply(self: @T) -> u256; | ||
} | ||
|
||
#[starknet::component] | ||
pub mod ERC721EnumerableComponent { | ||
use super::{IERC721Enumerable, ContractAddress}; | ||
|
||
#[storage] | ||
struct Storage { | ||
// Mapping from owner to list of owned token IDs | ||
owned_tokens: LegacyMap<(ContractAddress, u256), u256>, | ||
// Mapping from token ID to index of the owner tokens list | ||
owned_tokens_index: LegacyMap<u256, u256>, | ||
// Mapping with all token ids, | ||
all_tokens: LegacyMap<u256, u256>, | ||
// Helper to get the length of `all_tokens` | ||
all_tokens_length: u256, | ||
// Mapping from token id to position in the allTokens array | ||
all_tokens_index: LegacyMap<u256, u256> | ||
} | ||
|
||
#[embeddable_as(ERC721EnumerableImpl)] | ||
impl ERC721Enumerable< | ||
TContractState, +HasComponent<TContractState> | ||
> of IERC721Enumerable<ComponentState<TContractState>> { | ||
fn token_of_owner_by_index( | ||
self: @ComponentState<TContractState>, owner: ContractAddress, index: u256 | ||
) -> u256 { | ||
// TODO: Add this check back in | ||
//assert(index < self.erc721.balance_of(owner), 'Owner index out of bounds'); | ||
self.owned_tokens.read((owner, index)) | ||
} | ||
fn total_supply(self: @ComponentState<TContractState>) -> u256 { | ||
self.all_tokens_length.read() | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod Counter; | ||
mod ERC721Enumerable; | ||
mod YourCollectible; | ||
mod mock_contracts { | ||
pub mod Receiver; | ||
|
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