Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ERC721 preset #811

Merged
merged 76 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from 71 commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
103375f
consolidate mocks
andrew-fleming Oct 16, 2023
7968708
add erc721abi
andrew-fleming Oct 16, 2023
4e09fb6
migrate to component
andrew-fleming Oct 16, 2023
8da4c00
use abi dispatcher
andrew-fleming Oct 16, 2023
5cc8a6d
start updating tests
andrew-fleming Oct 16, 2023
1f29ac1
add pop_log_comp_indexed
andrew-fleming Oct 16, 2023
8d14e0f
update tests
andrew-fleming Oct 16, 2023
18f5f23
fix formatting
andrew-fleming Oct 16, 2023
3b2259b
add erc721camelabi
andrew-fleming Oct 16, 2023
4396dcc
remove unnecessary trait
andrew-fleming Oct 17, 2023
c820965
add code comments
andrew-fleming Oct 17, 2023
b42744b
change import style
andrew-fleming Oct 17, 2023
aac3940
fix conflicts
andrew-fleming Oct 19, 2023
816a28a
Update src/token/erc721/erc721.cairo
andrew-fleming Oct 19, 2023
682887c
remove unnecessary fn
andrew-fleming Oct 19, 2023
4499d24
change back to pop_log
andrew-fleming Oct 19, 2023
0c9427b
flatten event in mock
andrew-fleming Oct 19, 2023
4e58863
flatten events in mocks
andrew-fleming Oct 26, 2023
e8b696a
Merge branch 'main' into component-erc721
andrew-fleming Oct 26, 2023
4e459f9
change receiver to component
andrew-fleming Oct 27, 2023
c01445f
fix formatting
andrew-fleming Oct 27, 2023
04be9e9
simplify camel return id
andrew-fleming Oct 27, 2023
d89af43
fix imports
andrew-fleming Oct 30, 2023
3f5c77a
fix imports
andrew-fleming Oct 30, 2023
ab72343
fix tokenURI
andrew-fleming Oct 30, 2023
2a853c4
add Component suffix
andrew-fleming Oct 31, 2023
4893df9
add Component suffix to receiver
andrew-fleming Oct 31, 2023
3cf4940
fix formatting
andrew-fleming Oct 31, 2023
74ce94e
fix formatting
andrew-fleming Oct 31, 2023
1c106a6
add receiver tests
andrew-fleming Nov 1, 2023
0941a61
fix formatting and test
andrew-fleming Nov 1, 2023
cf7b7d6
remove import
andrew-fleming Nov 1, 2023
d5feed7
add code comments
andrew-fleming Nov 1, 2023
0cc9420
fix conflicts
andrew-fleming Nov 2, 2023
35f144c
fix conflicts
andrew-fleming Nov 2, 2023
e8f6a3a
simplify with get_dep_component_mut
andrew-fleming Nov 2, 2023
50e7f26
add recipient to constructor
andrew-fleming Nov 4, 2023
cd86147
remove unused mocks
andrew-fleming Nov 4, 2023
9ee7511
simplify mocks
andrew-fleming Nov 4, 2023
8d0cd9c
fix formatting
andrew-fleming Nov 4, 2023
974c942
add erc721 preset
andrew-fleming Nov 6, 2023
65ef95e
add erc721 preset
andrew-fleming Nov 6, 2023
eb9c55c
start preset tests
andrew-fleming Nov 6, 2023
f0d7a0f
fix formatting
andrew-fleming Nov 6, 2023
9baca0d
fix test
andrew-fleming Nov 6, 2023
53c5ac5
finish tests
andrew-fleming Nov 6, 2023
dbe5f61
fix formatting
andrew-fleming Nov 6, 2023
81d7971
remove import
andrew-fleming Nov 6, 2023
d518fcf
Update src/tests/token/test_erc721.cairo
andrew-fleming Nov 6, 2023
8094a96
fix impl order
andrew-fleming Nov 6, 2023
8efe05e
Merge branch 'component-erc721' into add-erc721-preset
andrew-fleming Nov 6, 2023
0f5ae41
Apply suggestions from code review
andrew-fleming Nov 8, 2023
aa24838
change param to span
andrew-fleming Nov 8, 2023
1f13281
add constructor tests
andrew-fleming Nov 8, 2023
2905387
fix formatting
andrew-fleming Nov 8, 2023
263defe
add in-code comments
andrew-fleming Nov 8, 2023
f917443
fix fn order
andrew-fleming Nov 9, 2023
a048b8a
fix title
andrew-fleming Nov 9, 2023
b4591ab
Apply suggestions from code review
andrew-fleming Nov 10, 2023
baff1e8
fix formatting
andrew-fleming Nov 10, 2023
dffc3b9
fix impl order
andrew-fleming Nov 10, 2023
24460d0
fix src5 comment
andrew-fleming Nov 10, 2023
5b65a6e
add info in comments
andrew-fleming Nov 10, 2023
00eaad7
fix component impl order
andrew-fleming Nov 10, 2023
d5d5592
change back import style
andrew-fleming Nov 10, 2023
76ba674
Merge branch 'component-erc721' into add-erc721-preset
andrew-fleming Nov 10, 2023
6238c1e
fix conflicts
andrew-fleming Nov 10, 2023
b838c61
fix component order
andrew-fleming Nov 10, 2023
bad7662
remove extra line
andrew-fleming Nov 10, 2023
403cc85
remove unused imports
andrew-fleming Nov 10, 2023
9dc86dc
fix comment
andrew-fleming Nov 10, 2023
314ba29
add drop_events
andrew-fleming Nov 22, 2023
3f3e237
reorder preset components
andrew-fleming Nov 22, 2023
cde00a4
fix conflicts
andrew-fleming Nov 22, 2023
b13e02e
fix tests
andrew-fleming Nov 24, 2023
bc45dec
set owner as caller in setup
andrew-fleming Nov 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/presets.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod account;
mod erc721;

use account::Account;
use erc721::ERC721;
99 changes: 99 additions & 0 deletions src/presets/erc721.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts for Cairo v0.8.0-beta.0 (presets/erc721.cairo)

/// # ERC721 Preset
///
/// The ERC721 contract offers a batch-mint mechanism that
/// can only be executed once upon contract construction.
#[starknet::contract]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized that it may be easier to add the license with X.Y.Z version, because before releasing it would be easier to find these new files with a quick search, instead of having to implement other mechanisms for not forgetting some. what do you think cc @martriay

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we care about new files only? we want to update them all even if they're v0.7.0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean adding the XYZ instead of removing the license line, because files without the license line are harder to find with Ctrl+Shift+F

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but why do we want to identify new files specifically?

mod ERC721 {
use openzeppelin::introspection::src5::SRC5Component;
use openzeppelin::token::erc721::ERC721Component;
use starknet::ContractAddress;

component!(path: SRC5Component, storage: src5, event: SRC5Event);
component!(path: ERC721Component, storage: erc721, event: ERC721Event);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should swap the components to make ERC721 appear first (this applies to impls below too). That is consistent with the Account Preset as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree in the sense that it's the "main" component, but I switched it because Access appears first in the imports and IMO it appears more organized that way. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm, got your point, but I still think it makes more sense to have the main component first, instead of letting the imports decide. We still need to define the rule for this, but I don't like the one coming from import orders that much 😢

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fair, and we also already have the account preset using the suggested approach. I'll update and we can maybe revisit in the future

Copy link
Contributor

@martriay martriay Nov 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i actually like SRC5 first, as it's a dependency of ERC721. also, not sure if there's always going to be a "main component", and it's not clear to me that's a concept we want to suggest or push.

+1 to not inheriting order from imports

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, not sure if there's always going to be a "main component", and it's not clear to me that's a concept we want to suggest or push.

+1 to not inheriting order from imports

Let's say we create an ownable, pausable ERC20 preset. There's no component dependency like with SRC5. If we're not using the main component approach and we're not inheriting order from imports, how else can we organize the component declarations?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we don't need a rule for that


// SRC5
#[abi(embed_v0)]
impl SRC5Impl = SRC5Component::SRC5Impl<ContractState>;

// ERC721
#[abi(embed_v0)]
impl ERC721Impl = ERC721Component::ERC721Impl<ContractState>;
#[abi(embed_v0)]
impl ERC721MetadataImpl = ERC721Component::ERC721MetadataImpl<ContractState>;
#[abi(embed_v0)]
impl ERC721CamelOnly = ERC721Component::ERC721CamelOnlyImpl<ContractState>;
#[abi(embed_v0)]
impl ERC721MetadataCamelOnly =
ERC721Component::ERC721MetadataCamelOnlyImpl<ContractState>;
impl ERC721InternalImpl = ERC721Component::InternalImpl<ContractState>;

#[storage]
struct Storage {
#[substorage(v0)]
src5: SRC5Component::Storage,
#[substorage(v0)]
erc721: ERC721Component::Storage
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
SRC5Event: SRC5Component::Event,
#[flat]
ERC721Event: ERC721Component::Event
}

mod Errors {
const UNEQUAL_ARRAYS: felt252 = 'Array lengths do not match';
}

/// Sets the token `name` and `symbol`.
/// Mints the `token_ids` tokens to `recipient` and sets
/// each token's URI.
#[constructor]
fn constructor(
ref self: ContractState,
name: felt252,
symbol: felt252,
recipient: ContractAddress,
token_ids: Span<u256>,
token_uris: Span<felt252>
) {
self.erc721.initializer(name, symbol);
self._mint_assets(recipient, token_ids, token_uris);
}

/// Mints `token_ids` to `recipient`.
/// Sets the token URI from `token_uris` to the corresponding
/// token ID of `token_ids`.
///
/// Requirements:
///
/// - `token_ids` must be equal in length to `token_uris`.
#[generate_trait]
impl InternalImpl of InternalTrait {
fn _mint_assets(
ref self: ContractState,
recipient: ContractAddress,
mut token_ids: Span<u256>,
mut token_uris: Span<felt252>
) {
assert(token_ids.len() == token_uris.len(), Errors::UNEQUAL_ARRAYS);

loop {
if token_ids.len() == 0 {
break;
}
let id = *token_ids.pop_front().unwrap();
let uri = *token_uris.pop_front().unwrap();

self.erc721._mint(recipient, id);
self.erc721._set_token_uri(id, uri);
}
}
}
}
1 change: 1 addition & 0 deletions src/tests/presets.cairo
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod test_account;
mod test_erc721;
Loading