Skip to content

Commit

Permalink
scarb fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
kfastov authored and tekkac committed Mar 27, 2024
1 parent 8474110 commit 7d1f276
Show file tree
Hide file tree
Showing 19 changed files with 344 additions and 215 deletions.
4 changes: 3 additions & 1 deletion src/extensions/metadata/module.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ mod ERC3525MetadataComponent {
use openzeppelin::introspection::src5::SRC5Component;

// Local deps
use cairo_erc_3525::extensions::metadata::interface::{IERC3525_METADATA_ID, IERC3525Metadata, IERC3525MetadataCamelOnly};
use cairo_erc_3525::extensions::metadata::interface::{
IERC3525_METADATA_ID, IERC3525Metadata, IERC3525MetadataCamelOnly
};

#[storage]
struct Storage {
Expand Down
63 changes: 36 additions & 27 deletions src/extensions/slotapprovable/module.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ mod ERC3525SlotApprovableComponent {
// [Check] Caller is owner or approved for all
let erc721_comp = get_dep_component!(@self, ERC721Comp);
let is_approved_for_all = erc721_comp.is_approved_for_all(operator, owner);
assert(caller == owner || is_approved_for_all, ERC3525Component::Errors::CALLER_NOT_ALLOWED);
assert(
caller == owner || is_approved_for_all, ERC3525Component::Errors::CALLER_NOT_ALLOWED
);

// [Check] No self approval
assert(caller != operator, Errors::SELF_APPROVAL);
Expand All @@ -86,7 +88,10 @@ mod ERC3525SlotApprovableComponent {
}

fn is_approved_for_slot(
self: @ComponentState<TContractState>, owner: ContractAddress, slot: u256, operator: ContractAddress
self: @ComponentState<TContractState>,
owner: ContractAddress,
slot: u256,
operator: ContractAddress
) -> bool {
self._erc3525_slot_approvals.read((owner, slot, operator))
}
Expand All @@ -112,7 +117,10 @@ mod ERC3525SlotApprovableComponent {
}

fn isApprovedForSlot(
self: @ComponentState<TContractState>, owner: ContractAddress, slot: u256, operator: ContractAddress
self: @ComponentState<TContractState>,
owner: ContractAddress,
slot: u256,
operator: ContractAddress
) -> bool {
self.is_approved_for_slot(owner, slot, operator)
}
Expand All @@ -139,7 +147,10 @@ mod ERC3525SlotApprovableComponent {
}

fn approve_value(
ref self: ComponentState<TContractState>, token_id: u256, operator: ContractAddress, value: u256
ref self: ComponentState<TContractState>,
token_id: u256,
operator: ContractAddress,
value: u256
) {
// [Check] Caller and operator are not null addresses
let caller = get_caller_address();
Expand Down Expand Up @@ -172,7 +183,8 @@ mod ERC3525SlotApprovableComponent {

// [Check] Disambiguate function call: only one of `to_token_id` and `to` must be set
assert(
to_token_id == 0.into() || to.is_zero(), ERC3525Component::Errors::INVALID_EXCLUSIVE_ARGS
to_token_id == 0.into() || to.is_zero(),
ERC3525Component::Errors::INVALID_EXCLUSIVE_ARGS
);

// [Effect] Spend allowance if possible
Expand All @@ -184,19 +196,13 @@ mod ERC3525SlotApprovableComponent {
// Into felt252 works
match token_id {
// If token_id is zero, transfer value to address
0 => erc3525_comp._transfer_value_to(
from_token_id, to, value
),
0 => erc3525_comp._transfer_value_to(from_token_id, to, value),
// Otherwise, transfer value to token
_ => erc3525_comp._transfer_value_to_token(
from_token_id, to_token_id, value
),
_ => erc3525_comp._transfer_value_to_token(from_token_id, to_token_id, value),
}
} else {
// Into felt252 fails, so token_id is not zero
erc3525_comp._transfer_value_to_token(
from_token_id, to_token_id, value
)
erc3525_comp._transfer_value_to_token(from_token_id, to_token_id, value)
}
}
}
Expand All @@ -217,14 +223,15 @@ mod ERC3525SlotApprovableComponent {
}

fn _spend_allowance(
ref self: ComponentState<TContractState>, spender: ContractAddress, token_id: u256, value: u256
ref self: ComponentState<TContractState>,
spender: ContractAddress,
token_id: u256,
value: u256
) {
// [Compute] Spender allowance
let is_approved = self._is_allowed(spender, token_id);
let mut erc3525_comp = get_dep_component_mut!(ref self, ERC3525Comp);
let current_allowance = erc3525_comp.allowance(
token_id, spender
);
let current_allowance = erc3525_comp.allowance(token_id, spender);
let infinity: u256 = BoundedInt::max();

// [Effect] Update allowance if the rights are limited
Expand All @@ -233,18 +240,16 @@ mod ERC3525SlotApprovableComponent {
}
assert(current_allowance >= value, ERC3525Component::Errors::INSUFFICIENT_ALLOWANCE);
let new_allowance = current_allowance - value;
erc3525_comp._approve_value(
token_id, spender, new_allowance
);
erc3525_comp._approve_value(token_id, spender, new_allowance);
}

fn _is_allowed(self: @ComponentState<TContractState>, operator: ContractAddress, token_id: u256) -> bool {
fn _is_allowed(
self: @ComponentState<TContractState>, operator: ContractAddress, token_id: u256
) -> bool {
// [Compute] Operator is owner or approved for all
let erc721_comp = get_dep_component!(self, ERC721Comp);
let owner = erc721_comp.owner_of(token_id);
let is_owner_or_approved = erc721_comp._is_approved_or_owner(
operator, token_id
);
let is_owner_or_approved = erc721_comp._is_approved_or_owner(operator, token_id);

// [Compute] Operator is approved for slot
let erc3525_comp = get_dep_component!(self, ERC3525Comp);
Expand All @@ -265,9 +270,13 @@ mod ERC3525SlotApprovableComponent {
impl SRC5: SRC5Component::HasComponent<TContractState>,
impl ERC721Comp: ERC721Component::HasComponent<TContractState>
> of AssertTrait<TContractState> {
fn _assert_allowed(self: @ComponentState<TContractState>, operator: ContractAddress, token_id: u256) {
fn _assert_allowed(
self: @ComponentState<TContractState>, operator: ContractAddress, token_id: u256
) {
// [Check] Operator is allowed
assert(self._is_allowed(operator, token_id), ERC3525Component::Errors::CALLER_NOT_ALLOWED);
assert(
self._is_allowed(operator, token_id), ERC3525Component::Errors::CALLER_NOT_ALLOWED
);
}
}
}
22 changes: 17 additions & 5 deletions src/extensions/slotenumerable/module.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ mod ERC3525SlotEnumerableComponent {
fn token_supply_in_slot(self: @ComponentState<TContractState>, slot: u256) -> u256 {
self._erc3525_slot_tokens_len.read(slot)
}
fn token_in_slot_by_index(self: @ComponentState<TContractState>, slot: u256, index: u256) -> u256 {
fn token_in_slot_by_index(
self: @ComponentState<TContractState>, slot: u256, index: u256
) -> u256 {
// [Check] Index is in range
let supply = self._erc3525_slot_tokens_len.read(slot);
assert(index < supply, Errors::INDEX_OUT_OF_BOUNDS);
Expand All @@ -87,7 +89,9 @@ mod ERC3525SlotEnumerableComponent {
fn tokenSupplyInSlot(self: @ComponentState<TContractState>, slot: u256) -> u256 {
self.token_supply_in_slot(slot)
}
fn tokenInSlotByIndex(self: @ComponentState<TContractState>, slot: u256, index: u256) -> u256 {
fn tokenInSlotByIndex(
self: @ComponentState<TContractState>, slot: u256, index: u256
) -> u256 {
self.token_in_slot_by_index(slot, index)
}
}
Expand All @@ -112,7 +116,9 @@ mod ERC3525SlotEnumerableComponent {
self._erc3525_slot_enumerables.read(index) == slot && slot != 0
}

fn _token_exists(self: @ComponentState<TContractState>, slot: u256, token_id: u256) -> bool {
fn _token_exists(
self: @ComponentState<TContractState>, slot: u256, token_id: u256
) -> bool {
let index = self._erc3525_slot_tokens_index.read((slot, token_id));
self._erc3525_slot_tokens.read((slot, index)) == token_id && token_id != 0
}
Expand Down Expand Up @@ -148,7 +154,11 @@ mod ERC3525SlotEnumerableComponent {
}

fn _mint(
ref self: ComponentState<TContractState>, to: ContractAddress, token_id: u256, slot: u256, value: u256
ref self: ComponentState<TContractState>,
to: ContractAddress,
token_id: u256,
slot: u256,
value: u256
) {
// [Effect] Mint
let mut erc3525_comp = get_dep_component_mut!(ref self, ERC3525Comp);
Expand Down Expand Up @@ -182,7 +192,9 @@ mod ERC3525SlotEnumerableComponent {
self._erc3525_slot_enumerables_index.write(slot, index);
}

fn _add_token_to_slot_enumeration(ref self: ComponentState<TContractState>, slot: u256, token_id: u256) {
fn _add_token_to_slot_enumeration(
ref self: ComponentState<TContractState>, slot: u256, token_id: u256
) {
// [Effect] Store new token
let index = self._erc3525_slot_tokens_len.read(slot);
self._erc3525_slot_tokens_len.write(slot, index + 1);
Expand Down
65 changes: 47 additions & 18 deletions src/module.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ mod ERC3525Component {
}

fn approve_value(
ref self: ComponentState<TContractState>, token_id: u256, operator: ContractAddress, value: u256
ref self: ComponentState<TContractState>,
token_id: u256,
operator: ContractAddress,
value: u256
) {
// [Check] Caller and operator are not null addresses
let caller = get_caller_address();
Expand All @@ -128,16 +131,15 @@ mod ERC3525Component {
let erc721_comp = get_dep_component!(@self, ERC721);
let owner = erc721_comp.owner_of(token_id);
assert(owner != operator, Errors::APPROVAL_TO_OWNER);
assert(
erc721_comp._is_approved_or_owner(caller, token_id),
Errors::CALLER_NOT_ALLOWED
);
assert(erc721_comp._is_approved_or_owner(caller, token_id), Errors::CALLER_NOT_ALLOWED);

// [Effect] Store approved value
self._approve_value(token_id, operator, value);
}

fn allowance(self: @ComponentState<TContractState>, token_id: u256, operator: ContractAddress) -> u256 {
fn allowance(
self: @ComponentState<TContractState>, token_id: u256, operator: ContractAddress
) -> u256 {
// [Check]
let erc721_comp = get_dep_component!(self, ERC721);
let owner = erc721_comp.owner_of(token_id);
Expand Down Expand Up @@ -200,7 +202,10 @@ mod ERC3525Component {
ERC3525::slot_of(self, tokenId)
}
fn approveValue(
ref self: ComponentState<TContractState>, tokenId: u256, operator: ContractAddress, value: u256
ref self: ComponentState<TContractState>,
tokenId: u256,
operator: ContractAddress,
value: u256
) {
ERC3525::approve_value(ref self, tokenId, operator, value)
}
Expand Down Expand Up @@ -241,7 +246,10 @@ mod ERC3525Component {
}

fn _approve_value(
ref self: ComponentState<TContractState>, token_id: u256, operator: ContractAddress, value: u256
ref self: ComponentState<TContractState>,
token_id: u256,
operator: ContractAddress,
value: u256
) {
// [Effect] Store approved value
let erc721_comp = get_dep_component!(@self, ERC721);
Expand All @@ -253,7 +261,10 @@ mod ERC3525Component {
}

fn _transfer_value_to(
ref self: ComponentState<TContractState>, from_token_id: u256, to: ContractAddress, value: u256
ref self: ComponentState<TContractState>,
from_token_id: u256,
to: ContractAddress,
value: u256
) -> u256 {
// [Effect] Mint new token and transfer value
let token_id = self._get_new_token_id();
Expand All @@ -264,24 +275,28 @@ mod ERC3525Component {
}

fn _transfer_value_to_token(
ref self: ComponentState<TContractState>, from_token_id: u256, to_token_id: u256, value: u256
ref self: ComponentState<TContractState>,
from_token_id: u256,
to_token_id: u256,
value: u256
) -> u256 {
// [Effect] Transfer value
self._transfer_value(from_token_id, to_token_id, value);
to_token_id
}

fn _spend_allowance(
ref self: ComponentState<TContractState>, spender: ContractAddress, token_id: u256, value: u256
ref self: ComponentState<TContractState>,
spender: ContractAddress,
token_id: u256,
value: u256
) {
// [Compute] Spender allowance
let erc721_comp = get_dep_component!(@self, ERC721);
let owner = erc721_comp.owner_of(token_id);
let current_allowance = self._erc3525_approved_values.read((owner, token_id, spender));
let infinity: u256 = BoundedInt::max();
let is_approved = erc721_comp._is_approved_or_owner(
spender, token_id
);
let is_approved = erc721_comp._is_approved_or_owner(spender, token_id);

// [Effect] Update allowance if the rights are limited
if current_allowance == infinity || is_approved {
Expand All @@ -302,7 +317,11 @@ mod ERC3525Component {
}

fn _mint(
ref self: ComponentState<TContractState>, to: ContractAddress, token_id: u256, slot: u256, value: u256
ref self: ComponentState<TContractState>,
to: ContractAddress,
token_id: u256,
slot: u256,
value: u256
) {
// [Check] Token id not already exists
self._assert_not_minted(token_id);
Expand All @@ -317,7 +336,12 @@ mod ERC3525Component {
self._mint_value(token_id, value);
}

fn _mint_token(ref self: ComponentState<TContractState>, to: ContractAddress, token_id: u256, slot: u256) {
fn _mint_token(
ref self: ComponentState<TContractState>,
to: ContractAddress,
token_id: u256,
slot: u256
) {
// [Effect] Mint a new enumerable token if supported, standard token otherwise
let mut erc721_comp = get_dep_component_mut!(ref self, ERC721);
erc721_comp._mint(to, token_id);
Expand Down Expand Up @@ -349,7 +373,10 @@ mod ERC3525Component {
}

fn _transfer_value(
ref self: ComponentState<TContractState>, from_token_id: u256, to_token_id: u256, value: u256
ref self: ComponentState<TContractState>,
from_token_id: u256,
to_token_id: u256,
value: u256
) {
// [Check] Tokens exist and not null
self._assert_minted(from_token_id);
Expand Down Expand Up @@ -445,7 +472,9 @@ mod ERC3525Component {
contract.supports_interface(ISRC6_ID)
}

fn _split(ref self: ComponentState<TContractState>, token_id: u256, amounts: @Array<u256>) -> Array<u256> {
fn _split(
ref self: ComponentState<TContractState>, token_id: u256, amounts: @Array<u256>
) -> Array<u256> {
// [Check] Token exists
self._assert_minted(token_id);

Expand Down
3 changes: 2 additions & 1 deletion src/presets/erc3525_mintable_burnable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ mod ERC3525MintableBurnable {
#[abi(embed_v0)]
impl ERC3525Impl = ERC3525Component::ERC3525Impl<ContractState>;
#[abi(embed_v0)]
impl ERC3525CamelOnlyImpl = ERC3525Component::ERC3525CamelOnlyImpl<ContractState>;
impl ERC3525CamelOnlyImpl =
ERC3525Component::ERC3525CamelOnlyImpl<ContractState>;
impl ERC3525InternalImpl = ERC3525Component::InternalImpl<ContractState>;

#[storage]
Expand Down
Loading

0 comments on commit 7d1f276

Please sign in to comment.