Skip to content

Commit

Permalink
♻️ Refactor presets
Browse files Browse the repository at this point in the history
- Rename into token
- Add token into workspace
  • Loading branch information
bal7hazar committed Nov 28, 2023
1 parent 204d2f8 commit 88f68d4
Show file tree
Hide file tree
Showing 25 changed files with 212 additions and 188 deletions.
9 changes: 8 additions & 1 deletion Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies = [

[[package]]
name = "origami"
version = "0.1.0"
version = "0.0.0"
dependencies = [
"cubit",
"dojo",
Expand All @@ -52,3 +52,10 @@ dependencies = [
"dojo",
"origami",
]

[[package]]
name = "token"
version = "0.0.0"
dependencies = [
"dojo",
]
4 changes: 2 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[workspace]
members = ["crates", "examples/*"]
members = ["crates", "examples/*", "token"]

[workspace.package]
version = "0.1.0"
version = "0.0.0"
description = "Community-maintained libraries for Cairo"
homepage = "https://github.com/dojoengine/origami"
authors = ["[email protected]"]
Expand Down
9 changes: 0 additions & 9 deletions presets/Scarb.toml

This file was deleted.

5 changes: 0 additions & 5 deletions presets/src/erc1155/erc1155.cairo

This file was deleted.

13 changes: 0 additions & 13 deletions presets/src/lib.cairo

This file was deleted.

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

[dependencies]
dojo.workspace = true
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[starknet::contract]
mod ERC1155 {
use dojo_erc::token::erc1155::models::{ERC1155Meta, ERC1155OperatorApproval, ERC1155Balance};
use dojo_erc::token::erc1155::interface;
use dojo_erc::token::erc1155::interface::{IERC1155, IERC1155CamelOnly};
use token::erc1155::models::{ERC1155Meta, ERC1155OperatorApproval, ERC1155Balance};
use token::erc1155::interface;
use token::erc1155::interface::{IERC1155, IERC1155CamelOnly};
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use starknet::ContractAddress;
use starknet::{get_caller_address, get_contract_address};
Expand Down Expand Up @@ -73,26 +73,6 @@ mod ERC1155 {
self.initializer(name, symbol, base_uri);
}

//
// 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 ERC1155MetadataImpl of interface::IERC1155Metadata<ContractState> {
fn name(self: @ContractState) -> felt252 {
Expand Down Expand Up @@ -297,11 +277,6 @@ mod ERC1155 {
fn initializer(ref self: ContractState, name: felt252, symbol: felt252, base_uri: felt252) {
let meta = ERC1155Meta { 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
// );
}

fn _is_approved_for_all_or_owner(
Expand Down Expand Up @@ -329,9 +304,6 @@ mod ERC1155 {
data: Array<u8>
) {
self.update_balances(from, to, id, amount);
// assert(
// _check_on_erc1155_received(from, to, id, data), Errors::SAFE_TRANSFER_FAILED
// );

self
.emit_event(
Expand Down Expand Up @@ -359,9 +331,6 @@ mod ERC1155 {
let id = *ids_span.pop_front().unwrap();
let amount = *amounts_span.pop_front().unwrap();
self.update_balances(from, to, id, amount);
// assert(
// _check_on_erc1155_received(from, to, id, data), Errors::SAFE_TRANSFER_FAILED
// );
};

self
Expand Down Expand Up @@ -413,25 +382,6 @@ mod ERC1155 {
data: Span<felt252>
) {
self._mint(to, id, amount);
// assert(
// _check_on_erc1155_received(Zeroable::zero(), to, id, data),
// Errors::SAFE_MINT_FAILED
// );
}
}
//#[internal]
// fn _check_on_erc1155_received(
// from: ContractAddress, to: ContractAddress, token_id: u256, data: Span<felt252>
// ) -> bool {
// if (DualCaseSRC5 { contract_address: to }
// .supports_interface(interface::IERC1155_RECEIVER_ID)) {
// DualCaseERC1155Receiver { contract_address: to }
// .on_erc1155_received(
// get_caller_address(), from, token_id, data
// ) == interface::IERC1155_RECEIVER_ID
// } else {
// DualCaseSRC5 { contract_address: to }.supports_interface(account::interface::ISRC6_ID)
// }
// }

}
File renamed without changes.
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
22 changes: 11 additions & 11 deletions presets/src/erc1155/tests.cairo → token/src/erc1155/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
use dojo_erc::tests::utils;
use dojo_erc::tests::constants::{
use token::tests::utils;
use token::tests::constants::{
ZERO, OWNER, SPENDER, RECIPIENT, OPERATOR, OTHER, NAME, SYMBOL, URI, TOKEN_ID, TOKEN_AMOUNT,
TOKEN_ID_2, TOKEN_AMOUNT_2
};

use dojo_erc::token::erc1155::ERC1155::ERC1155Impl;
use dojo_erc::token::erc1155::ERC1155::ERC1155CamelOnlyImpl;
use dojo_erc::token::erc1155::ERC1155::ERC1155MetadataImpl;
use dojo_erc::token::erc1155::ERC1155::InternalImpl;
use dojo_erc::token::erc1155::ERC1155::WorldInteractionsImpl;
use dojo_erc::token::erc1155::ERC1155::{TransferSingle, TransferBatch, ApprovalForAll};
use dojo_erc::token::erc1155::ERC1155;
use token::erc1155::ERC1155::ERC1155Impl;
use token::erc1155::ERC1155::ERC1155CamelOnlyImpl;
use token::erc1155::ERC1155::ERC1155MetadataImpl;
use token::erc1155::ERC1155::InternalImpl;
use token::erc1155::ERC1155::WorldInteractionsImpl;
use token::erc1155::ERC1155::{TransferSingle, TransferBatch, ApprovalForAll};
use token::erc1155::ERC1155;
use starknet::ContractAddress;
use starknet::contract_address_const;
use starknet::testing;
use zeroable::Zeroable;
use dojo::test_utils::spawn_test_world;
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

use dojo_erc::token::erc1155::models::{
use token::erc1155::models::{
ERC1155Meta, erc_1155_meta, ERC1155OperatorApproval, erc_1155_operator_approval, ERC1155Balance,
erc_1155_balance
};
use dojo_erc::token::erc1155::ERC1155::_worldContractMemberStateTrait;
use token::erc1155::ERC1155::_worldContractMemberStateTrait;
use debug::PrintTrait;

//
Expand Down
16 changes: 4 additions & 12 deletions presets/src/erc20/erc20.cairo → token/src/erc20/erc20.cairo
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
// External imports

use openzeppelin::token::erc20::interface;

#[starknet::contract]
mod ERC20 {
use dojo_erc::token::erc20_models::{ERC20Allowance, ERC20Balance, ERC20Meta};
use token::erc20::models::{ERC20Allowance, ERC20Balance, ERC20Meta};
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};
use token::erc20::interface;
use integer::BoundedInt;
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]
Expand Down Expand Up @@ -71,7 +63,7 @@ mod ERC20 {
//

#[external(v0)]
impl ERC20Impl of IERC20<ContractState> {
impl ERC20Impl of interface::IERC20<ContractState> {
fn name(self: @ContractState) -> felt252 {
self.get_meta().name
}
Expand Down Expand Up @@ -127,7 +119,7 @@ mod ERC20 {
}

#[external(v0)]
impl ERC20CamelOnlyImpl of IERC20CamelOnly<ContractState> {
impl ERC20CamelOnlyImpl of interface::IERC20CamelOnly<ContractState> {
fn totalSupply(self: @ContractState) -> u256 {
ERC20Impl::total_supply(self)
}
Expand Down
24 changes: 24 additions & 0 deletions token/src/erc20/interface.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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;
}

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;
}
16 changes: 10 additions & 6 deletions presets/src/erc20/models.cairo → token/src/erc20/models.cairo
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
// Starknet imports

use starknet::ContractAddress;

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

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

#[derive(Model, Copy, Drop, Serde)]
struct ERC20Meta {
#[key]
token: starknet::ContractAddress,
token: ContractAddress,
name: felt252,
symbol: felt252,
total_supply: u256,
Expand Down
18 changes: 9 additions & 9 deletions presets/src/erc20/tests.cairo → token/src/erc20/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
use integer::BoundedInt;
use integer::u256;
use integer::u256_from_felt252;
use dojo_erc::tests::utils;
use dojo_erc::tests::constants::{
use token::tests::utils;
use token::tests::constants::{
ZERO, OWNER, SPENDER, RECIPIENT, NAME, SYMBOL, DECIMALS, SUPPLY, VALUE
};
use dojo_erc::token::erc20::ERC20::Approval;
use dojo_erc::token::erc20::ERC20::ERC20Impl;
use dojo_erc::token::erc20::ERC20::InternalImpl;
use dojo_erc::token::erc20::ERC20::Transfer;
use dojo_erc::token::erc20::ERC20;
use token::erc20::ERC20::Approval;
use token::erc20::ERC20::ERC20Impl;
use token::erc20::ERC20::InternalImpl;
use token::erc20::ERC20::Transfer;
use token::erc20::ERC20;
use starknet::ContractAddress;
use starknet::contract_address_const;
use starknet::testing;
use zeroable::Zeroable;
use dojo::test_utils::spawn_test_world;
use dojo::world::{IWorldDispatcher, IWorldDispatcherTrait};

use dojo_erc::token::erc20_models::{
use token::erc20::models::{
ERC20Allowance, erc_20_allowance, ERC20Balance, erc_20_balance, ERC20Meta, erc_20_meta
};
use dojo_erc::token::erc20::ERC20::_worldContractMemberStateTrait;
use token::erc20::ERC20::_worldContractMemberStateTrait;
use debug::PrintTrait;

//
Expand Down
Loading

0 comments on commit 88f68d4

Please sign in to comment.