Skip to content

Commit

Permalink
🚧 Upgrade dojo to latest tag and WIP on presets
Browse files Browse the repository at this point in the history
  • Loading branch information
bal7hazar committed Nov 5, 2023
1 parent dec6a74 commit 98c1f48
Show file tree
Hide file tree
Showing 26 changed files with 147 additions and 258 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Check
on: [push, pull_request]

env:
DOJO_VERSION: v0.3.3
DOJO_VERSION: v0.3.4
SCARB_VERSION: v2.3.1

jobs:
Expand Down
2 changes: 1 addition & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ members = [
]

[workspace.dependencies]
dojo = { git = "https://github.com/dojoengine/dojo.git", tag = "v0.3.3" }
dojo = { git = "https://github.com/dojoengine/dojo.git", tag = "v0.3.4" }
File renamed without changes.
27 changes: 27 additions & 0 deletions presets/Scarb.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "dojo"
version = "0.3.4"
source = "git+https://github.com/dojoengine/dojo.git?rev=d62ec8a#d62ec8a522f92d0a0ccf226c6e929bb918b31128"
dependencies = [
"dojo_plugin",
]

[[package]]
name = "dojo_plugin"
version = "0.3.4"

[[package]]
name = "openzeppelin"
version = "0.8.0-beta.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?rev=4eafccf#4eafccf107f98d975b20cbb6544b2a998e5f403f"

[[package]]
name = "presets"
version = "0.0.0"
dependencies = [
"dojo",
"openzeppelin",
]
9 changes: 9 additions & 0 deletions presets/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "presets"
version = "0.0.0"
description = "Implementations of ERC standards for the Dojo framework."
homepage = "https://github.com/dojoengine/origami/tree/presets"

[dependencies]
dojo = { git = "https://github.com/dojoengine/dojo.git", rev = "d62ec8a" }
openzeppelin = { git = "https://github.com/OpenZeppelin/cairo-contracts.git", rev = "4eafccf" }
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,40 +1,21 @@
use starknet::ContractAddress;

#[starknet::interface]
trait IERC20<TState> {
fn name(self: @TState) -> felt252;
fn symbol(self: @TState) -> felt252;
fn decimals(self: @TState) -> u8;
fn total_supply(self: @TState) -> u256;
fn balance_of(self: @TState, account: ContractAddress) -> u256;
fn allowance(self: @TState, owner: ContractAddress, spender: ContractAddress) -> u256;
fn transfer(ref self: TState, recipient: ContractAddress, amount: u256) -> bool;
fn transfer_from(
ref self: TState, sender: ContractAddress, recipient: ContractAddress, amount: u256
) -> bool;
fn approve(ref self: TState, spender: ContractAddress, amount: u256) -> bool;
}
// External imports

trait IERC20CamelOnly<TState> {
fn totalSupply(self: @TState) -> u256;
fn balanceOf(self: @TState, account: ContractAddress) -> u256;
fn transferFrom(
ref self: TState, sender: ContractAddress, recipient: ContractAddress, amount: u256
) -> bool;
}
use openzeppelin::token::erc20::interface;

#[starknet::contract]
mod ERC20 {
use dojo_erc::token::erc20_models::{ERC20Allowance, ERC20Balance, ERC20Meta};
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use integer::BoundedInt;
use super::IERC20;
use super::IERC20CamelOnly;
use starknet::ContractAddress;
use starknet::{get_caller_address, get_contract_address};
use zeroable::Zeroable;
use debug::PrintTrait;

// Local imports

use super::interface::{IERC20, IERC20CamelOnly};


#[storage]
struct Storage {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
use starknet::ContractAddress;

#[derive(Model, Copy, Drop, Serde)]
struct ERC20Balance {
#[key]
token: ContractAddress,
token: starknet::ContractAddress,
#[key]
account: ContractAddress,
account: starknet::ContractAddress,
amount: u256,
}

#[derive(Model, Copy, Drop, Serde)]
struct ERC20Allowance {
#[key]
token: ContractAddress,
token: starknet::ContractAddress,
#[key]
owner: ContractAddress,
owner: starknet::ContractAddress,
#[key]
spender: ContractAddress,
spender: starknet::ContractAddress,
amount: u256,
}

#[derive(Model, Copy, Drop, Serde)]
struct ERC20Meta {
#[key]
token: ContractAddress,
token: starknet::ContractAddress,
name: felt252,
symbol: felt252,
total_supply: u256,
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,29 +1,71 @@
// External imports

use openzeppelin::token::erc721::interface;

#[starknet::contract]
mod ERC721 {
use dojo_erc::token::erc721::models::{
ERC721Meta, ERC721OperatorApproval, ERC721Owner, ERC721Balance, ERC721TokenApproval
};
use dojo_erc::token::erc721::interface;
use dojo_erc::token::erc721::interface::{IERC721, IERC721CamelOnly};
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
// Core imports

use integer::BoundedInt;
use zeroable::Zeroable;

// Starknet imports

use starknet::ContractAddress;
use starknet::{get_caller_address, get_contract_address};
use zeroable::Zeroable;
use debug::PrintTrait;

// Dojo imports

use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

// External imports

use openzeppelin::token::erc721::erc721::ERC721;
use openzeppelin::introspection::interface::{ISRC5, ISRC5Camel};
use openzeppelin::introspection::src5::SRC5Component;

// Internal imports

use presets::erc721::models::{
ERC721Meta,
ERC721OperatorApproval,
ERC721Owner,
ERC721Balance,
ERC721TokenApproval,
};

// Local imports

use super::interface;

// Components

component!(path: SRC5Component, storage: src5, event: SRC5Event);
#[abi(embed_v0)]
impl SRC5Impl = SRC5Component::SRC5Impl<ContractState>;
#[abi(embed_v0)]
impl SRC5CamelImpl = SRC5Component::SRC5CamelImpl<ContractState>;
impl SRC5InternalImpl = SRC5Component::InternalImpl<ContractState>;
impl SRC5EventCopy of Copy<SRC5Component::Event> {}

// Storage

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

// Events

#[event]
#[derive(Copy, Drop, starknet::Event)]
enum Event {
Transfer: Transfer,
Approval: Approval,
ApprovalForAll: ApprovalForAll
ApprovalForAll: ApprovalForAll,
SRC5Event: SRC5Component::Event,
}

#[derive(Copy, Drop, starknet::Event)]
Expand Down Expand Up @@ -56,8 +98,6 @@ mod ERC721 {
const INVALID_RECEIVER: felt252 = 'ERC721: invalid receiver';
const ALREADY_MINTED: felt252 = 'ERC721: token already minted';
const WRONG_SENDER: felt252 = 'ERC721: wrong sender';
const SAFE_MINT_FAILED: felt252 = 'ERC721: safe mint failed';
const SAFE_TRANSFER_FAILED: felt252 = 'ERC721: safe transfer failed';
}

#[constructor]
Expand All @@ -79,22 +119,6 @@ mod ERC721 {
// External
//

// #[external(v0)]
// impl SRC5Impl of ISRC5<ContractState> {
// fn supports_interface(self: @ContractState, interface_id: felt252) -> bool {
// let unsafe_state = src5::SRC5::unsafe_new_contract_state();
// src5::SRC5::SRC5Impl::supports_interface(@unsafe_state, interface_id)
// }
// }

// #[external(v0)]
// impl SRC5CamelImpl of ISRC5Camel<ContractState> {
// fn supportsInterface(self: @ContractState, interfaceId: felt252) -> bool {
// let unsafe_state = src5::SRC5::unsafe_new_contract_state();
// src5::SRC5::SRC5CamelImpl::supportsInterface(@unsafe_state, interfaceId)
// }
// }

#[external(v0)]
impl ERC721MetadataImpl of interface::IERC721Metadata<ContractState> {
fn name(self: @ContractState) -> felt252 {
Expand All @@ -115,8 +139,7 @@ mod ERC721 {
#[external(v0)]
impl ERC721MetadataCamelOnlyImpl of interface::IERC721MetadataCamelOnly<ContractState> {
fn tokenURI(self: @ContractState, tokenId: u256) -> felt252 {
assert(self._exists(tokenId), Errors::INVALID_TOKEN_ID);
self.get_uri(tokenId)
self.token_uri(tokenId)
}
}

Expand Down Expand Up @@ -178,7 +201,8 @@ mod ERC721 {
assert(
self._is_approved_or_owner(get_caller_address(), token_id), Errors::UNAUTHORIZED
);
self._safe_transfer(from, to, token_id, data);
// TODO: move to real safe transfer when support of SRC6 is enabled
self.transfer_from(from, to, token_id);
}
}

Expand Down Expand Up @@ -312,11 +336,8 @@ mod ERC721 {
fn initializer(ref self: ContractState, name: felt252, symbol: felt252, base_uri: felt252) {
let meta = ERC721Meta { token: get_contract_address(), name, symbol, base_uri };
set!(self.world(), (meta));
// let mut unsafe_state = src5::SRC5::unsafe_new_contract_state();
// src5::SRC5::InternalImpl::register_interface(ref unsafe_state, interface::IERC721_ID);
// src5::SRC5::InternalImpl::register_interface(
// ref unsafe_state, interface::IERC721_METADATA_ID
// );
self.src5.register_interface(interface::IERC721_ID);
self.src5.register_interface(interface::IERC721_METADATA_ID);
}

fn _owner_of(self: @ContractState, token_id: u256) -> ContractAddress {
Expand Down Expand Up @@ -397,47 +418,5 @@ mod ERC721 {

self.emit_event(Transfer { from: owner, to: Zeroable::zero(), token_id });
}

fn _safe_mint(
ref self: ContractState, to: ContractAddress, token_id: u256, data: Span<felt252>
) {
self._mint(to, token_id);
// assert(
// _check_on_erc721_received(Zeroable::zero(), to, token_id, data),
// Errors::SAFE_MINT_FAILED
// );
}

fn _safe_transfer(
ref self: ContractState,
from: ContractAddress,
to: ContractAddress,
token_id: u256,
data: Span<felt252>
) {
self._transfer(from, to, token_id);
// assert(
// _check_on_erc721_received(from, to, token_id, data), Errors::SAFE_TRANSFER_FAILED
// );
}
// fn _set_token_uri(ref self: ContractState, token_id: u256, token_uri: felt252) {
// assert(self._exists(token_id), Errors::INVALID_TOKEN_ID);
// self._token_uri.write(token_id, token_uri)
// }
}

//#[internal]
// fn _check_on_erc721_received(
// from: ContractAddress, to: ContractAddress, token_id: u256, data: Span<felt252>
// ) -> bool {
// if (DualCaseSRC5 { contract_address: to }
// .supports_interface(interface::IERC721_RECEIVER_ID)) {
// DualCaseERC721Receiver { contract_address: to }
// .on_erc721_received(
// get_caller_address(), from, token_id, data
// ) == interface::IERC721_RECEIVER_ID
// } else {
// DualCaseSRC5 { contract_address: to }.supports_interface(account::interface::ISRC6_ID)
// }
// }
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Starknet imports

use starknet::ContractAddress;

#[derive(Model, Copy, Drop, Serde)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
// Core imports

use integer::u256;
use integer::u256_from_felt252;
use dojo_erc::tests::utils;
use dojo_erc::tests::constants::{
ZERO, OWNER, SPENDER, RECIPIENT, OPERATOR, OTHER, NAME, SYMBOL, URI, TOKEN_ID
};
use zeroable::Zeroable;
use debug::PrintTrait;

// Starknet imports

use dojo_erc::token::erc721::ERC721::ERC721Impl;
use dojo_erc::token::erc721::ERC721::ERC721CamelOnlyImpl;
use dojo_erc::token::erc721::ERC721::ERC721MetadataImpl;
use dojo_erc::token::erc721::ERC721::InternalImpl;
use dojo_erc::token::erc721::ERC721::WorldInteractionsImpl;
use dojo_erc::token::erc721::ERC721::{Approval, ApprovalForAll, Transfer};
use dojo_erc::token::erc721::ERC721;
use starknet::ContractAddress;
use starknet::contract_address_const;
use starknet::testing;
use zeroable::Zeroable;

// Dojo imports

use dojo::test_utils::spawn_test_world;
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

use dojo_erc::token::erc721::models::{
// External imports

// Internal imports

use presets::tests::utils;
use presets::tests::constants::{
ZERO, OWNER, SPENDER, RECIPIENT, OPERATOR, OTHER, NAME, SYMBOL, URI, TOKEN_ID
};
use presets::erc721::ERC721::ERC721Impl;
use presets::erc721::ERC721::ERC721CamelOnlyImpl;
use presets::erc721::ERC721::ERC721MetadataImpl;
use presets::erc721::ERC721::InternalImpl;
use presets::erc721::ERC721::WorldInteractionsImpl;
use presets::erc721::ERC721::{Approval, ApprovalForAll, Transfer};
use presets::erc721::ERC721;
use presets::erc721::models::{
ERC721Meta, erc_721_meta, ERC721OperatorApproval, erc_721_operator_approval, ERC721Owner,
erc_721_owner, ERC721Balance, erc_721_balance, ERC721TokenApproval, erc_721_token_approval
};
use dojo_erc::token::erc721::ERC721::_worldContractMemberStateTrait;
use debug::PrintTrait;
use presets::erc721::ERC721::_worldContractMemberStateTrait;

//
// Setup
Expand Down
Loading

0 comments on commit 98c1f48

Please sign in to comment.