Skip to content

Commit

Permalink
Add ERC721 preset (#811)
Browse files Browse the repository at this point in the history
* consolidate mocks

* add erc721abi

* migrate to component

* use abi dispatcher

* start updating tests

* add pop_log_comp_indexed

* update tests

* fix formatting

* add erc721camelabi

* remove unnecessary trait

* add code comments

* change import style

* Update src/token/erc721/erc721.cairo

Co-authored-by: Eric Nordelo <[email protected]>

* remove unnecessary fn

* change back to pop_log

* flatten event in mock

* flatten events in mocks

* change receiver to component

* fix formatting

* simplify camel return id

* fix imports

* fix imports

* fix tokenURI

* add Component suffix

* add Component suffix to receiver

* fix formatting

* fix formatting

* add receiver tests

* fix formatting and test

* remove import

* add code comments

* fix conflicts

* simplify with get_dep_component_mut

* add recipient to constructor

* remove unused mocks

* simplify mocks

* fix formatting

* add erc721 preset

* add erc721 preset

* start preset tests

* fix formatting

* fix test

* finish tests

* fix formatting

* remove import

* Update src/tests/token/test_erc721.cairo

Co-authored-by: Eric Nordelo <[email protected]>

* fix impl order

* Apply suggestions from code review

Co-authored-by: Eric Nordelo <[email protected]>

* change param to span

* add constructor tests

* fix formatting

* add in-code comments

* fix fn order

* fix title

* Apply suggestions from code review

Co-authored-by: Martín Triay <[email protected]>

* fix formatting

* fix impl order

* fix src5 comment

* add  info in comments

* fix component impl order

* change back import style

* fix component order

* remove extra line

* remove unused imports

* fix comment

* add drop_events

* reorder preset components

* fix tests

* set owner as caller in setup

---------

Co-authored-by: Eric Nordelo <[email protected]>
Co-authored-by: Martín Triay <[email protected]>
  • Loading branch information
3 people authored Nov 25, 2023
1 parent 7a7be63 commit 20c91c2
Show file tree
Hide file tree
Showing 5 changed files with 1,231 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/presets.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod account;
mod erc20;
mod erc721;

use account::Account;
use erc20::ERC20;
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]
mod ERC721 {
use openzeppelin::introspection::src5::SRC5Component;
use openzeppelin::token::erc721::ERC721Component;
use starknet::ContractAddress;

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

// 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>;

// SRC5
#[abi(embed_v0)]
impl SRC5Impl = SRC5Component::SRC5Impl<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
}

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,2 +1,3 @@
mod test_account;
mod test_erc20;
mod test_erc721;
Loading

0 comments on commit 20c91c2

Please sign in to comment.