From b4a5fd77f097bf42c6de5ced9c1b17d1b8c3b4a9 Mon Sep 17 00:00:00 2001 From: JoE11-y Date: Tue, 8 Oct 2024 09:56:32 +0100 Subject: [PATCH] fix fmt --- .../ark_common/src/protocol/order_types.cairo | 14 +- .../ark_common/src/protocol/order_v1.cairo | 34 +++-- .../src/orderbook/orderbook.cairo | 144 ++++++++---------- contracts/ark_starknet/src/executor.cairo | 75 ++++----- contracts/ark_starknet/src/interfaces.cairo | 7 +- .../ark_starknet/tests/common/setup.cairo | 6 +- .../tests/integration/cancel_order.cairo | 5 +- .../tests/integration/create_order.cairo | 18 ++- .../tests/integration/fees_amount.cairo | 10 +- .../tests/integration/fulfill_order.cairo | 89 +++++++---- contracts/ark_tokens/src/lib.cairo | 2 +- 11 files changed, 212 insertions(+), 192 deletions(-) diff --git a/contracts/ark_common/src/protocol/order_types.cairo b/contracts/ark_common/src/protocol/order_types.cairo index 2673dc08..68e8f561 100644 --- a/contracts/ark_common/src/protocol/order_types.cairo +++ b/contracts/ark_common/src/protocol/order_types.cairo @@ -38,9 +38,9 @@ impl Felt252TryIntoOrderType of TryInto { Option::Some(OrderType::Offer) } else if self == 'COLLECTION_OFFER' { Option::Some(OrderType::CollectionOffer) - } else if self == 'LIMIT_BUY'{ + } else if self == 'LIMIT_BUY' { Option::Some(OrderType::LimitBuy) - }else if self == 'LIMIT_SELL'{ + } else if self == 'LIMIT_SELL' { Option::Some(OrderType::LimitSell) } else { Option::None @@ -292,9 +292,9 @@ impl Felt252TryIntoRoute of TryInto { Option::Some(RouteType::Erc20ToErc721) } else if self == 'ERC721TOERC20' { Option::Some(RouteType::Erc721ToErc20) - } else if self == 'ERC20TOERC20BUY'{ + } else if self == 'ERC20TOERC20BUY' { Option::Some(RouteType::Erc20ToErc20Buy) - } else if self == 'ERC20TOERC20SELL'{ + } else if self == 'ERC20TOERC20SELL' { Option::Some(RouteType::Erc20ToErc20Sell) } else { Option::None @@ -304,8 +304,8 @@ impl Felt252TryIntoRoute of TryInto { #[derive(starknet::Store, Serde, Drop, PartialEq, Copy, Debug)] struct OptionU256 { - is_some: felt252, - value: u256, + is_some: felt252, + value: u256, } trait OptionU256Trait, +Drop> { @@ -319,7 +319,7 @@ impl OptionU256Impl of OptionU256Trait { } fn is_some(self: @OptionU256) -> bool { if *self.is_some == 1 { - true + true } else { false } diff --git a/contracts/ark_common/src/protocol/order_v1.cairo b/contracts/ark_common/src/protocol/order_v1.cairo index 7dcf86c3..7c2b34c4 100644 --- a/contracts/ark_common/src/protocol/order_v1.cairo +++ b/contracts/ark_common/src/protocol/order_v1.cairo @@ -64,13 +64,14 @@ impl OrderTraitOrderV1 of OrderTrait { } // check for expiry only if not erc20 buys or sells - if (*self.route != RouteType::Erc20ToErc20Buy && *self.route != RouteType::Erc20ToErc20Sell) { + if (*self.route != RouteType::Erc20ToErc20Buy + && *self.route != RouteType::Erc20ToErc20Sell) { let end_date = *self.end_date; if end_date < block_timestamp { return Result::Err(OrderValidationError::EndDateInThePast); } - + // End date -> block_timestamp + 30 days. let max_end_date = block_timestamp + (30 * 24 * 60 * 60); if end_date > max_end_date { @@ -79,12 +80,12 @@ impl OrderTraitOrderV1 of OrderTrait { } // check that the start amount is not zero for sell erc20 orders - if(*self.route != RouteType::Erc20ToErc20Sell){ - if (*self.start_amount).is_zero(){ + if (*self.route != RouteType::Erc20ToErc20Sell) { + if (*self.start_amount).is_zero() { return Result::Err(OrderValidationError::InvalidContent); } - }else{ - if (*self.end_amount).is_zero(){ + } else { + if (*self.end_amount).is_zero() { return Result::Err(OrderValidationError::InvalidContent); } } @@ -98,9 +99,9 @@ impl OrderTraitOrderV1 of OrderTrait { } if (*self.offerer).is_zero() - || (*self.token_address).is_zero() - || (*self.salt).is_zero() - || (*self.quantity).is_zero() { + || (*self.token_address).is_zero() + || (*self.salt).is_zero() + || (*self.quantity).is_zero() { return Result::Err(OrderValidationError::InvalidContent); } @@ -135,16 +136,16 @@ impl OrderTraitOrderV1 of OrderTrait { return Result::Ok(OrderType::CollectionOffer); } - // Limit Buy Order - if(*self.quantity) > 0 + // Limit Buy Order + if (*self.quantity) > 0 && (*self.start_amount) > 0 // amount to pay && (*self.end_amount).is_zero() && (*self.route == RouteType::Erc20ToErc20Buy) { return Result::Ok(OrderType::LimitBuy); } - // Limit Sell Order - if(*self.quantity) > 0 + // Limit Sell Order + if (*self.quantity) > 0 && (*self.start_amount).is_zero() && (*self.end_amount) > 0 // amount to receive && (*self.route == RouteType::Erc20ToErc20Sell) { @@ -161,13 +162,14 @@ impl OrderTraitOrderV1 of OrderTrait { } fn compute_token_hash(self: @OrderV1) -> felt252 { - if (*self.route == RouteType::Erc20ToErc20Buy || *self.route == RouteType::Erc20ToErc20Sell) { + if (*self.route == RouteType::Erc20ToErc20Buy + || *self.route == RouteType::Erc20ToErc20Sell) { let mut buf: Array = array![]; // used quantity, start_date and the offerer as the identifiers buf.append((*self.token_address).into()); buf.append(*self.token_chain_id); poseidon_hash_span(buf.span()) - }else{ + } else { assert(OptionTrait::is_some(self.token_id), 'Token ID expected'); let token_id = (*self.token_id).unwrap(); let mut buf: Array = array![]; @@ -184,4 +186,4 @@ impl OrderTraitOrderV1 of OrderTrait { self.serialize(ref buf); poseidon_hash_span(buf.span()) } -} \ No newline at end of file +} diff --git a/contracts/ark_component/src/orderbook/orderbook.cairo b/contracts/ark_component/src/orderbook/orderbook.cairo index 3fc19b04..34038f65 100644 --- a/contracts/ark_component/src/orderbook/orderbook.cairo +++ b/contracts/ark_component/src/orderbook/orderbook.cairo @@ -56,7 +56,7 @@ pub mod OrderbookComponent { } // precision for erc20 price division - const PRECISION : u256 = 1000000000000000000; + const PRECISION: u256 = 1000000000000000000; // must be increased when `OrderPlaced` content change pub const ORDER_PLACED_EVENT_VERSION: u8 = 1; @@ -283,12 +283,8 @@ pub mod OrderbookComponent { OrderType::CollectionOffer => { self._create_collection_offer(order, order_type, order_hash); }, - OrderType::LimitBuy => { - self._create_limit_order(order, order_type, order_hash); - }, - OrderType::LimitSell => { - self._create_limit_order(order, order_type, order_hash); - } + OrderType::LimitBuy => { self._create_limit_order(order, order_type, order_hash); }, + OrderType::LimitSell => { self._create_limit_order(order, order_type, order_hash); } }; HooksCreateOrder::after_create_order(ref self, order); @@ -302,13 +298,15 @@ pub mod OrderbookComponent { let order = order_option.unwrap(); assert(order.offerer == cancel_info.canceller, 'not the same offerrer'); match order_status_read(order_hash) { - Option::Some(s) => assert(s == OrderStatus::Open, orderbook_errors::ORDER_FULFILLED), + Option::Some(s) => assert( + s == OrderStatus::Open, orderbook_errors::ORDER_FULFILLED + ), Option::None => panic_with_felt252(orderbook_errors::ORDER_NOT_FOUND), }; let block_ts = starknet::get_block_timestamp(); let order_type = match order_type_read(order_hash) { Option::Some(order_type) => { - if order_type == OrderType::Auction { + if order_type == OrderType::Auction { let auction_token_hash = order.compute_token_hash(); let (_, auction_end_date, _) = self.auctions.read(auction_token_hash); assert( @@ -317,7 +315,7 @@ pub mod OrderbookComponent { self.auctions.write(auction_token_hash, (0, 0, 0)); } else if order_type == OrderType::LimitBuy { self.buy_orders.write(order_hash, (0, 0)); - }else if order_type == OrderType::LimitSell { + } else if order_type == OrderType::LimitSell { self.sell_orders.write(order_hash, (0, 0)); } else { assert(block_ts < order.end_date, orderbook_errors::ORDER_IS_EXPIRED); @@ -474,7 +472,7 @@ pub mod OrderbookComponent { token_address: order.token_address, token_from: order.offerer, token_to: related_order.offerer, - token_id: OptionU256 { is_some: 1, value: order.token_id.unwrap()}, + token_id: OptionU256 { is_some: 1, value: order.token_id.unwrap() }, token_quantity: 1, payment_from: related_order.offerer, payment_to: fulfill_info.fulfiller, @@ -521,7 +519,7 @@ pub mod OrderbookComponent { token_address: order.token_address, token_from: fulfill_info.fulfiller, token_to: order.offerer, - token_id: OptionU256 { is_some: 1, value: fulfill_info.token_id.unwrap()}, + token_id: OptionU256 { is_some: 1, value: fulfill_info.token_id.unwrap() }, token_quantity: 1, payment_from: order.offerer, payment_to: fulfill_info.fulfiller, @@ -555,7 +553,7 @@ pub mod OrderbookComponent { token_address: order.token_address, token_from: order.offerer, token_to: fulfill_info.fulfiller, - token_id: OptionU256 { is_some: 1, value: order.token_id.unwrap()}, + token_id: OptionU256 { is_some: 1, value: order.token_id.unwrap() }, token_quantity: 1, payment_from: fulfill_info.fulfiller, payment_to: order.offerer, @@ -831,9 +829,9 @@ pub mod OrderbookComponent { /// Creates a limit buy order fn _create_limit_order( - ref self: ComponentState, - order: OrderV1, - order_type: OrderType, + ref self: ComponentState, + order: OrderV1, + order_type: OrderType, order_hash: felt252 ) { // revert if order is fulfilled or Open @@ -845,9 +843,9 @@ pub mod OrderbookComponent { ); } let cancelled_order_hash = self._process_previous_order(order_hash, order.offerer); - + order_write(order_hash, order_type, order); - + match order_type { OrderType::LimitBuy => { let price = order.start_amount / order.quantity * PRECISION; @@ -859,7 +857,7 @@ pub mod OrderbookComponent { }, _ => () } - + self .emit( OrderPlaced { @@ -888,7 +886,7 @@ pub mod OrderbookComponent { token_address: buy_order.token_address, token_from: sell_order.offerer, token_to: buy_order.offerer, - token_id: OptionU256 { is_some: 0, value: 0}, + token_id: OptionU256 { is_some: 0, value: 0 }, token_quantity, payment_from: buy_order.offerer, payment_to: sell_order.offerer, @@ -902,15 +900,12 @@ pub mod OrderbookComponent { /// Fulfill limit order fn _fulfill_limit_order( - ref self: ComponentState, - fulfill_info: FulfillInfo, - order: OrderV1 + ref self: ComponentState, fulfill_info: FulfillInfo, order: OrderV1 ) -> (Option, Option) { let order_hash = order.compute_order_hash(); - + assert( - order_hash == fulfill_info.order_hash, - orderbook_errors::ORDER_HASH_DOES_NOT_MATCH + order_hash == fulfill_info.order_hash, orderbook_errors::ORDER_HASH_DOES_NOT_MATCH ); let related_order_hash = fulfill_info @@ -948,18 +943,17 @@ pub mod OrderbookComponent { orderbook_errors::ORDER_TOKEN_HASH_DOES_NOT_MATCH ); - let (buy_order, sell_order) = match order.route { + let (buy_order, sell_order) = match order.route { RouteType::Erc20ToErc20Sell => { assert( - related_order.route == RouteType::Erc20ToErc20Buy, + related_order.route == RouteType::Erc20ToErc20Buy, orderbook_errors::ORDER_ROUTE_NOT_VALID ); (related_order, order) }, - RouteType::Erc20ToErc20Buy => { assert( - related_order.route == RouteType::Erc20ToErc20Sell, + related_order.route == RouteType::Erc20ToErc20Sell, orderbook_errors::ORDER_ROUTE_NOT_VALID ); (order, related_order) @@ -975,11 +969,8 @@ pub mod OrderbookComponent { let buy_order_hash = buy_order.compute_order_hash(); let sell_order_hash = sell_order.compute_order_hash(); - - assert( - buy_price == sell_price, - orderbook_errors::ORDER_PRICE_NOT_MATCH - ); + + assert(buy_price == sell_price, orderbook_errors::ORDER_PRICE_NOT_MATCH); let (_, buy_order_quantity) = self.buy_orders.read(buy_order_hash); let (_, sell_order_quantity) = self.sell_orders.read(sell_order_hash); @@ -987,67 +978,58 @@ pub mod OrderbookComponent { if buy_order_quantity > sell_order_quantity { // reduce buy quantity order and execute sell order self - .buy_orders - .write( - buy_order_hash, - ( - buy_price, - buy_order_quantity - sell_order_quantity - ) - ); + .buy_orders + .write(buy_order_hash, (buy_price, buy_order_quantity - sell_order_quantity)); // set buy order as fufilled order_status_write(sell_order_hash, OrderStatus::Fulfilled); // set execute info - let execute_info = self._create_listing_execution_info( - sell_order_hash, - buy_order, - sell_order, - fulfill_info, - sell_order_quantity, - related_order.broker_id, - buy_price - ); + let execute_info = self + ._create_listing_execution_info( + sell_order_hash, + buy_order, + sell_order, + fulfill_info, + sell_order_quantity, + related_order.broker_id, + buy_price + ); (Option::Some(execute_info), Option::Some(related_order_hash)) - }else if sell_order_quantity > buy_order_quantity { + } else if sell_order_quantity > buy_order_quantity { // reduce sell quantity, and execute buy order self - .sell_orders - .write( - sell_order_hash, - ( - sell_price, - sell_order_quantity - buy_order_quantity - ) - ); + .sell_orders + .write(sell_order_hash, (sell_price, sell_order_quantity - buy_order_quantity)); // set sell order as fulfilled order_status_write(buy_order_hash, OrderStatus::Fulfilled); // generate execution info - let execute_info = self._create_listing_execution_info( - buy_order_hash, - buy_order, - sell_order, - fulfill_info, - buy_order_quantity, - order.broker_id, - buy_price - ); + let execute_info = self + ._create_listing_execution_info( + buy_order_hash, + buy_order, + sell_order, + fulfill_info, + buy_order_quantity, + order.broker_id, + buy_price + ); (Option::Some(execute_info), Option::Some(related_order_hash)) - }else{ + } else { // execute both orders order_status_write(buy_order_hash, OrderStatus::Fulfilled); order_status_write(sell_order_hash, OrderStatus::Fulfilled); // passing any of them as the order hash will fulfill both orders, // so just one executioninfo will be sent. - let execute_info = self._create_listing_execution_info( - buy_order_hash, - buy_order, - sell_order, - fulfill_info, - buy_order_quantity, - order.broker_id, - buy_price - ); - // return + let execute_info = self + ._create_listing_execution_info( + buy_order_hash, + buy_order, + sell_order, + fulfill_info, + buy_order_quantity, + order.broker_id, + buy_price + ); + // return (Option::Some(execute_info), Option::Some(related_order_hash)) } } diff --git a/contracts/ark_starknet/src/executor.cairo b/contracts/ark_starknet/src/executor.cairo index ae414064..96c81463 100644 --- a/contracts/ark_starknet/src/executor.cairo +++ b/contracts/ark_starknet/src/executor.cairo @@ -17,7 +17,7 @@ struct OrderInfo { // address making the order offerer: ContractAddress, // number of tokens - quantity: u256,// 0 for ERC721, + quantity: u256, // 0 for ERC721, // route type route: RouteType } @@ -334,9 +334,7 @@ mod executor { }, RouteType::Erc20ToErc20Sell => { assert!( - _check_erc20_amount( - order.token_address, *(order.quantity), order.offerer - ), + _check_erc20_amount(order.token_address, *(order.quantity), order.offerer), "Oferrer does not own enough ERC20 tokens to sell" ) } @@ -391,14 +389,10 @@ mod executor { ); }, OrderType::LimitBuy => { - _verify_limit_order( - self, order_info, fulfill_info, contract_address - ) + _verify_limit_order(self, order_info, fulfill_info, contract_address) }, OrderType::LimitSell => { - _verify_limit_order( - self, order_info, fulfill_info, contract_address - ) + _verify_limit_order(self, order_info, fulfill_info, contract_address) }, _ => panic!("Order not supported") } @@ -545,20 +539,17 @@ mod executor { "Order and related order use different currency" ); - let (buyer_order, seller_order) = match order_info.route { + let (buyer_order, seller_order) = match order_info.route { RouteType::Erc20ToErc20Sell => { assert( - related_order_info.route == RouteType::Erc20ToErc20Buy, - 'Order route not valid' + related_order_info.route == RouteType::Erc20ToErc20Buy, 'Order route not valid' ); - + (related_order_info, order_info) }, - RouteType::Erc20ToErc20Buy => { assert( - related_order_info.route == RouteType::Erc20ToErc20Sell, - 'Order route not valid' + related_order_info.route == RouteType::Erc20ToErc20Sell, 'Order route not valid' ); (order_info, related_order_info) }, @@ -569,9 +560,7 @@ mod executor { //checks for buyer assert!( - _check_erc20_amount( - @buyer_order.currency_address, buyer_order.start_amount, @buyer - ), + _check_erc20_amount(@buyer_order.currency_address, buyer_order.start_amount, @buyer), "Buyer does not own enough ERC20 tokens" ); @@ -589,18 +578,13 @@ mod executor { // checks for seller assert!( - _check_erc20_amount( - @seller_order.token_address, seller_order.quantity, @seller - ), + _check_erc20_amount(@seller_order.token_address, seller_order.quantity, @seller), "Seller does not own enough ERC20 tokens" ); assert!( _check_erc20_allowance( - @seller_order.token_address, - seller_order.quantity, - @seller, - @get_contract_address() + @seller_order.token_address, seller_order.quantity, @seller, @get_contract_address() ), "Seller's allowance of executor is not enough" ); @@ -655,7 +639,6 @@ mod executor { 'Chain ID is not SN_MAIN' ); - let currency_contract = IERC20Dispatcher { contract_address: execution_info.payment_currency_address.try_into().unwrap() }; @@ -675,7 +658,7 @@ mod executor { execution_info.token_id, execution_info.payment_amount ); - + assert!( execution_info .payment_amount > (fulfill_broker_fees_amount @@ -746,14 +729,12 @@ mod executor { let vinfo = if is_some == 1 { let nft_contract = IERC721Dispatcher { contract_address: execution_info.token_address }; nft_contract - .transfer_from( - execution_info.token_from, execution_info.token_to, token_id - ); - + .transfer_from(execution_info.token_from, execution_info.token_to, token_id); + let tx_info = starknet::get_tx_info().unbox(); let transaction_hash = tx_info.transaction_hash; let block_timestamp = starknet::info::get_block_timestamp(); - + ExecutionValidationInfo { order_hash: execution_info.order_hash, transaction_hash, @@ -761,18 +742,21 @@ mod executor { from: execution_info.token_from, to: execution_info.token_to, } - } else { - let erc20_contract = IERC20Dispatcher { contract_address: execution_info.token_address }; + let erc20_contract = IERC20Dispatcher { + contract_address: execution_info.token_address + }; erc20_contract .transfer_from( - execution_info.token_from, execution_info.token_to, execution_info.token_quantity + execution_info.token_from, + execution_info.token_to, + execution_info.token_quantity ); - + let tx_info = starknet::get_tx_info().unbox(); let transaction_hash = tx_info.transaction_hash; let block_timestamp = starknet::info::get_block_timestamp(); - + ExecutionValidationInfo { order_hash: execution_info.order_hash, transaction_hash, @@ -821,13 +805,16 @@ mod executor { } fn _compute_creator_fees_amount( - self: @ContractState, token_address: @ContractAddress, payment_amount: u256, token_id: OptionU256 + self: @ContractState, + token_address: @ContractAddress, + payment_amount: u256, + token_id: OptionU256 ) -> (ContractAddress, u256) { let (is_some, token_id) = token_id.get_some(); if is_some == 0 { _fallback_compute_creator_fees_amount(self, token_address, payment_amount) - }else{ - // check if nft support 2981 interface + } else { + // check if nft support 2981 interface let dispatcher = ISRC5Dispatcher { contract_address: *token_address }; if dispatcher.supports_interface(IERC2981_ID) { IERC2981Dispatcher { contract_address: *token_address } @@ -835,7 +822,7 @@ mod executor { } else { _fallback_compute_creator_fees_amount(self, token_address, payment_amount) } - } + } } fn _fallback_compute_creator_fees_amount( @@ -871,7 +858,7 @@ mod executor { let fulfill_broker_fees_amount = fulfill_broker_fees.compute_amount(payment_amount); let listing_broker_fees_amount = listing_broker_fees.compute_amount(payment_amount); let (_, creator_fees_amount) = _compute_creator_fees_amount( - self, @token_address, payment_amount, token_id, + self, @token_address, payment_amount, token_id, ); let ark_fees_amount = ark_fees.compute_amount(payment_amount); ( diff --git a/contracts/ark_starknet/src/interfaces.cairo b/contracts/ark_starknet/src/interfaces.cairo index 9a5b887f..b4bc5c4d 100644 --- a/contracts/ark_starknet/src/interfaces.cairo +++ b/contracts/ark_starknet/src/interfaces.cairo @@ -1,6 +1,6 @@ use ark_common::protocol::order_types::ExecutionInfo; -use ark_common::protocol::order_types::{OrderV1, OptionU256, OptionU256Trait}; use ark_common::protocol::order_types::{FulfillInfo, CancelInfo}; +use ark_common::protocol::order_types::{OrderV1, OptionU256, OptionU256Trait}; use ark_oz::erc2981::FeesRatio; //! Interfaces for arkchain operator. use starknet::{ClassHash, ContractAddress}; @@ -32,7 +32,10 @@ trait IExecutor { self: @T, token_address: ContractAddress ) -> (ContractAddress, FeesRatio); fn set_collection_creator_fees( - ref self: T, token_address: ContractAddress, receiver: ContractAddress, fees_ratio: FeesRatio + ref self: T, + token_address: ContractAddress, + receiver: ContractAddress, + fees_ratio: FeesRatio ); fn get_fees_amount( diff --git a/contracts/ark_starknet/tests/common/setup.cairo b/contracts/ark_starknet/tests/common/setup.cairo index 112e0aa8..dd87c5c4 100644 --- a/contracts/ark_starknet/tests/common/setup.cairo +++ b/contracts/ark_starknet/tests/common/setup.cairo @@ -383,12 +383,10 @@ fn create_limit_sell_order( IFreeMintDispatcher { contract_address: token_address }.mint(offerer, quantity); - let order = setup_limit_sell_order( - erc20_address, token_address, offerer, end_amount, quantity - ); + let order = setup_limit_sell_order(erc20_address, token_address, offerer, end_amount, quantity); cheat_caller_address(executor_address, offerer, CheatSpan::TargetCalls(1)); IExecutorDispatcher { contract_address: executor_address }.create_order(order); (order.compute_order_hash(), offerer, quantity) -} \ No newline at end of file +} diff --git a/contracts/ark_starknet/tests/integration/cancel_order.cairo b/contracts/ark_starknet/tests/integration/cancel_order.cairo index 02d97257..d2252e0e 100644 --- a/contracts/ark_starknet/tests/integration/cancel_order.cairo +++ b/contracts/ark_starknet/tests/integration/cancel_order.cairo @@ -15,8 +15,9 @@ use snforge_std::{cheat_caller_address, CheatSpan, spy_events, EventSpyAssertion use starknet::{ContractAddress, contract_address_const}; use super::super::common::setup::{ create_auction_order, create_collection_offer_order, create_listing_order, create_offer_order, - setup, setup_erc20_order, setup_default_order, setup_auction_order, setup_collection_offer_order, - setup_listing_order, setup_offer_order, create_limit_buy_order, create_limit_sell_order + setup, setup_erc20_order, setup_default_order, setup_auction_order, + setup_collection_offer_order, setup_listing_order, setup_offer_order, create_limit_buy_order, + create_limit_sell_order }; #[test] diff --git a/contracts/ark_starknet/tests/integration/create_order.cairo b/contracts/ark_starknet/tests/integration/create_order.cairo index 010a289d..dfdf04e7 100644 --- a/contracts/ark_starknet/tests/integration/create_order.cairo +++ b/contracts/ark_starknet/tests/integration/create_order.cairo @@ -23,8 +23,8 @@ use snforge_std::{ use starknet::{ContractAddress, contract_address_const}; use super::super::common::setup::{ - setup, setup_erc20_order, setup_auction_order, setup_collection_offer_order, setup_listing_order, setup_offer_order, - setup_limit_sell_order, setup_limit_buy_order + setup, setup_erc20_order, setup_auction_order, setup_collection_offer_order, + setup_listing_order, setup_offer_order, setup_limit_sell_order, setup_limit_buy_order }; @@ -238,7 +238,9 @@ fn test_create_limit_buy_order_ok() { let quantity = 5_000_000; Erc20Dispatcher { contract_address: erc20_address }.mint(offerer, start_amount); - let order = setup_limit_buy_order(erc20_address, token_address, offerer, start_amount, quantity); + let order = setup_limit_buy_order( + erc20_address, token_address, offerer, start_amount, quantity + ); let order_hash = order.compute_order_hash(); let order_version = order.get_version(); @@ -583,7 +585,9 @@ fn test_create_limit_buy_order_offerer_not_enough_erc20_tokens() { Erc20Dispatcher { contract_address: erc20_address }.mint(offerer, minted); - let order = setup_limit_buy_order(erc20_address, token_address, offerer, start_amount, quantity); + let order = setup_limit_buy_order( + erc20_address, token_address, offerer, start_amount, quantity + ); cheat_caller_address(executor_address, offerer, CheatSpan::TargetCalls(1)); IExecutorDispatcher { contract_address: executor_address }.create_order(order); @@ -615,7 +619,9 @@ fn test_create_limit_buy_order_twice() { let quantity = 5_000_000; Erc20Dispatcher { contract_address: erc20_address }.mint(offerer, start_amount); - let order = setup_limit_buy_order(erc20_address, token_address, offerer, start_amount, quantity); + let order = setup_limit_buy_order( + erc20_address, token_address, offerer, start_amount, quantity + ); cheat_caller_address(executor_address, offerer, CheatSpan::TargetCalls(2)); IExecutorDispatcher { contract_address: executor_address }.create_order(order); @@ -636,4 +642,4 @@ fn test_create_limit_sell_order_twice() { cheat_caller_address(executor_address, offerer, CheatSpan::TargetCalls(2)); IExecutorDispatcher { contract_address: executor_address }.create_order(order); IExecutorDispatcher { contract_address: executor_address }.create_order(order); -} \ No newline at end of file +} diff --git a/contracts/ark_starknet/tests/integration/fees_amount.cairo b/contracts/ark_starknet/tests/integration/fees_amount.cairo index 83726ed0..740d5dbb 100644 --- a/contracts/ark_starknet/tests/integration/fees_amount.cairo +++ b/contracts/ark_starknet/tests/integration/fees_amount.cairo @@ -1,7 +1,7 @@ +use ark_common::protocol::order_types::{OptionU256, OptionU256Trait}; use ark_starknet::interfaces::{ IExecutorDispatcher, IExecutorDispatcherTrait, FeesAmount, FeesRatio }; -use ark_common::protocol::order_types::{OptionU256, OptionU256Trait}; use snforge_std::{cheat_caller_address, CheatSpan}; use starknet::{ContractAddress, contract_address_const}; @@ -35,7 +35,9 @@ fn test_get_fees_amount_default_creator() { executor.set_default_creator_fees(creator, default_creator_fees_ratio); let fees_amount = executor - .get_fees_amount(fulfill_broker, listing_broker, nft_address, OptionU256 {is_some: 0, value: 1}, amount); + .get_fees_amount( + fulfill_broker, listing_broker, nft_address, OptionU256 { is_some: 0, value: 1 }, amount + ); assert_eq!(fees_amount.fulfill_broker, 1_000_000, "Wrong amount for fulfill broker"); assert_eq!(fees_amount.listing_broker, 500_000, "Wrong amount for listing broker"); @@ -72,7 +74,9 @@ fn test_get_fees_amount_collection_creator() { executor.set_collection_creator_fees(nft_address, creator, collection_creator_fees_ratio); let fees_amount = executor - .get_fees_amount(fulfill_broker, listing_broker, nft_address, OptionU256 {is_some: 0, value: 1}, amount); + .get_fees_amount( + fulfill_broker, listing_broker, nft_address, OptionU256 { is_some: 0, value: 1 }, amount + ); assert_eq!(fees_amount.fulfill_broker, 1_000_000, "Wrong amount for fulfill broker"); assert_eq!(fees_amount.listing_broker, 500_000, "Wrong amount for listing broker"); diff --git a/contracts/ark_starknet/tests/integration/fulfill_order.cairo b/contracts/ark_starknet/tests/integration/fulfill_order.cairo index 9c9a767d..8b22d1c4 100644 --- a/contracts/ark_starknet/tests/integration/fulfill_order.cairo +++ b/contracts/ark_starknet/tests/integration/fulfill_order.cairo @@ -19,13 +19,16 @@ use starknet::{ContractAddress, contract_address_const}; use super::super::common::setup::{ create_auction_order, create_collection_offer_order, create_listing_order, create_offer_order, setup, setup_default_order, setup_auction_order, setup_collection_offer_order, - setup_listing_order, setup_offer_order, setup_erc20_order, create_limit_buy_order, create_limit_sell_order, setup_limit_sell_order, - setup_limit_buy_order + setup_listing_order, setup_offer_order, setup_erc20_order, create_limit_buy_order, + create_limit_sell_order, setup_limit_sell_order, setup_limit_buy_order }; fn create_fulfill_info( - order_hash: felt252, fulfiller: ContractAddress, token_address: ContractAddress, token_id: Option + order_hash: felt252, + fulfiller: ContractAddress, + token_address: ContractAddress, + token_id: Option ) -> FulfillInfo { FulfillInfo { order_hash: order_hash, @@ -54,7 +57,9 @@ fn test_fulfill_offer_order_ok() { cheat_caller_address(erc20_address, offerer, CheatSpan::TargetCalls(1)); IERC20Dispatcher { contract_address: erc20_address }.approve(executor_address, start_amount); - let fulfill_info = create_fulfill_info(order_hash, fulfiller, nft_address, Option::Some(token_id)); + let fulfill_info = create_fulfill_info( + order_hash, fulfiller, nft_address, Option::Some(token_id) + ); cheat_caller_address(nft_address, fulfiller, CheatSpan::TargetCalls(1)); IERC721Dispatcher { contract_address: nft_address } @@ -80,7 +85,9 @@ fn test_fulfill_listing_order_ok() { IERC721Dispatcher { contract_address: nft_address } .set_approval_for_all(executor_address, true); - let fulfill_info = create_fulfill_info(order_hash, fulfiller, nft_address, Option::Some(token_id)); + let fulfill_info = create_fulfill_info( + order_hash, fulfiller, nft_address, Option::Some(token_id) + ); cheat_caller_address(erc20_address, fulfiller, CheatSpan::TargetCalls(1)); IERC20Dispatcher { contract_address: erc20_address }.approve(executor_address, start_amount); @@ -115,7 +122,9 @@ fn test_fulfill_listing_order_fulfiller_not_enough_erc20_token() { IFreeMintDispatcher { contract_address: erc20_address }.mint(fulfiller, start_amount - 100); - let fulfill_info = create_fulfill_info(order_hash, fulfiller, nft_address, Option::Some(token_id)); + let fulfill_info = create_fulfill_info( + order_hash, fulfiller, nft_address, Option::Some(token_id) + ); cheat_caller_address(executor_address, fulfiller, CheatSpan::TargetCalls(1)); IExecutorDispatcher { contract_address: executor_address }.fulfill_order(fulfill_info); @@ -136,7 +145,9 @@ fn test_fulfill_offer_order_fulfiller_not_owner() { executor_address, erc20_address, nft_address, token_id ); - let fulfill_info = create_fulfill_info(order_hash, fulfiller, nft_address, Option::Some(token_id)); + let fulfill_info = create_fulfill_info( + order_hash, fulfiller, nft_address, Option::Some(token_id) + ); cheat_caller_address(executor_address, fulfiller, CheatSpan::TargetCalls(1)); IExecutorDispatcher { contract_address: executor_address }.fulfill_order(fulfill_info); @@ -172,7 +183,9 @@ fn test_fulfill_offer_order_offerer_not_enough_allowance() { IERC20Dispatcher { contract_address: erc20_address } .approve(executor_address, start_amount - 10); - let fulfill_info = create_fulfill_info(order_hash, fulfiller, nft_address, Option::Some(token_id)); + let fulfill_info = create_fulfill_info( + order_hash, fulfiller, nft_address, Option::Some(token_id) + ); cheat_caller_address(nft_address, fulfiller, CheatSpan::TargetCalls(1)); IERC721Dispatcher { contract_address: nft_address } @@ -199,7 +212,9 @@ fn test_fulfill_listing_order_fulfiller_not_enough_allowance() { IERC721Dispatcher { contract_address: nft_address } .set_approval_for_all(executor_address, true); - let fulfill_info = create_fulfill_info(order_hash, fulfiller, nft_address, Option::Some(token_id)); + let fulfill_info = create_fulfill_info( + order_hash, fulfiller, nft_address, Option::Some(token_id) + ); cheat_caller_address(erc20_address, fulfiller, CheatSpan::TargetCalls(1)); IERC20Dispatcher { contract_address: erc20_address } @@ -222,7 +237,9 @@ fn test_fulfill_listing_order_offerer_not_approved() { IFreeMintDispatcher { contract_address: erc20_address }.mint(fulfiller, start_amount); - let fulfill_info = create_fulfill_info(order_hash, fulfiller, nft_address, Option::Some(token_id)); + let fulfill_info = create_fulfill_info( + order_hash, fulfiller, nft_address, Option::Some(token_id) + ); cheat_caller_address(erc20_address, fulfiller, CheatSpan::TargetCalls(1)); IERC20Dispatcher { contract_address: erc20_address }.approve(executor_address, start_amount); @@ -248,7 +265,9 @@ fn test_fulfill_offer_order_fulfiller_not_approved() { cheat_caller_address(erc20_address, offerer, CheatSpan::TargetCalls(1)); IERC20Dispatcher { contract_address: erc20_address }.approve(executor_address, start_amount); - let fulfill_info = create_fulfill_info(order_hash, fulfiller, nft_address, Option::Some(token_id)); + let fulfill_info = create_fulfill_info( + order_hash, fulfiller, nft_address, Option::Some(token_id) + ); cheat_caller_address(executor_address, fulfiller, CheatSpan::TargetCalls(1)); IExecutorDispatcher { contract_address: executor_address }.fulfill_order(fulfill_info); @@ -272,7 +291,9 @@ fn test_fulfill_offer_order_fulfiller_same_as_offerer() { cheat_caller_address(erc20_address, offerer, CheatSpan::TargetCalls(1)); IERC20Dispatcher { contract_address: erc20_address }.approve(executor_address, start_amount); - let fulfill_info = create_fulfill_info(order_hash, fulfiller, nft_address, Option::Some(token_id)); + let fulfill_info = create_fulfill_info( + order_hash, fulfiller, nft_address, Option::Some(token_id) + ); cheat_caller_address(nft_address, fulfiller, CheatSpan::TargetCalls(1)); IERC721Dispatcher { contract_address: nft_address } @@ -299,7 +320,9 @@ fn test_fulfill_listing_order_fulfiller_same_as_offerer() { IERC721Dispatcher { contract_address: nft_address } .set_approval_for_all(executor_address, true); - let fulfill_info = create_fulfill_info(order_hash, fulfiller, nft_address, Option::Some(token_id)); + let fulfill_info = create_fulfill_info( + order_hash, fulfiller, nft_address, Option::Some(token_id) + ); cheat_caller_address(erc20_address, fulfiller, CheatSpan::TargetCalls(1)); IERC20Dispatcher { contract_address: erc20_address }.approve(executor_address, start_amount); @@ -331,7 +354,9 @@ fn test_fulfill_auction_order_ok() { IERC721Dispatcher { contract_address: nft_address } .set_approval_for_all(executor_address, true); - let mut fulfill_info = create_fulfill_info(order_hash, fulfiller, nft_address, Option::Some(token_id)); + let mut fulfill_info = create_fulfill_info( + order_hash, fulfiller, nft_address, Option::Some(token_id) + ); fulfill_info.related_order_hash = Option::Some(buyer_order.compute_order_hash()); cheat_caller_address(erc20_address, buyer, CheatSpan::TargetCalls(1)); @@ -365,7 +390,9 @@ fn test_fulfill_auction_order_fulfiller_same_as_offerer() { IERC721Dispatcher { contract_address: nft_address } .set_approval_for_all(executor_address, true); - let mut fulfill_info = create_fulfill_info(order_hash, fulfiller, nft_address, Option::Some(token_id)); + let mut fulfill_info = create_fulfill_info( + order_hash, fulfiller, nft_address, Option::Some(token_id) + ); fulfill_info.related_order_hash = Option::Some(buyer_order.compute_order_hash()); cheat_caller_address(erc20_address, buyer, CheatSpan::TargetCalls(1)); @@ -393,7 +420,9 @@ fn test_fulfill_order_not_enabled() { cheat_caller_address(erc20_address, offerer, CheatSpan::TargetCalls(1)); IERC20Dispatcher { contract_address: erc20_address }.approve(executor_address, start_amount); - let fulfill_info = create_fulfill_info(order_hash, fulfiller, nft_address, Option::Some(token_id)); + let fulfill_info = create_fulfill_info( + order_hash, fulfiller, nft_address, Option::Some(token_id) + ); cheat_caller_address(nft_address, fulfiller, CheatSpan::TargetCalls(1)); IERC721Dispatcher { contract_address: nft_address } @@ -423,7 +452,9 @@ fn test_fulfill_limit_buy_order_ok() { IFreeMintDispatcher { contract_address: token_address }.mint(seller, quantity); - let seller_order = setup_limit_sell_order(erc20_address, token_address, seller, start_amount, quantity); + let seller_order = setup_limit_sell_order( + erc20_address, token_address, seller, start_amount, quantity + ); cheat_caller_address(executor_address, seller, CheatSpan::TargetCalls(1)); IExecutorDispatcher { contract_address: executor_address }.create_order(seller_order); @@ -436,7 +467,6 @@ fn test_fulfill_limit_buy_order_ok() { cheat_caller_address(erc20_address, buyer, CheatSpan::TargetCalls(1)); IERC20Dispatcher { contract_address: erc20_address }.approve(executor_address, start_amount); - let mut fulfill_info = create_fulfill_info(order_hash, fulfiller, token_address, Option::None); fulfill_info.related_order_hash = Option::Some(seller_order.compute_order_hash()); @@ -461,8 +491,10 @@ fn test_fulfill_limit_sell_order_ok() { IFreeMintDispatcher { contract_address: erc20_address }.mint(buyer, start_amount); - let buyer_order = setup_limit_buy_order(erc20_address, token_address, buyer, start_amount, quantity); - + let buyer_order = setup_limit_buy_order( + erc20_address, token_address, buyer, start_amount, quantity + ); + cheat_caller_address(executor_address, buyer, CheatSpan::TargetCalls(1)); IExecutorDispatcher { contract_address: executor_address }.create_order(buyer_order); @@ -498,7 +530,9 @@ fn test_fulfill_limit_buy_order_with_buy_order() { IFreeMintDispatcher { contract_address: erc20_address }.mint(wrong_seller, start_amount); - let wrong_order = setup_limit_buy_order(erc20_address, token_address, wrong_seller, start_amount, quantity); + let wrong_order = setup_limit_buy_order( + erc20_address, token_address, wrong_seller, start_amount, quantity + ); cheat_caller_address(executor_address, wrong_seller, CheatSpan::TargetCalls(1)); IExecutorDispatcher { contract_address: executor_address }.create_order(wrong_order); @@ -511,7 +545,6 @@ fn test_fulfill_limit_buy_order_with_buy_order() { cheat_caller_address(erc20_address, buyer, CheatSpan::TargetCalls(1)); IERC20Dispatcher { contract_address: erc20_address }.approve(executor_address, start_amount); - let mut fulfill_info = create_fulfill_info(order_hash, fulfiller, token_address, Option::None); fulfill_info.related_order_hash = Option::Some(wrong_order.compute_order_hash()); @@ -537,8 +570,10 @@ fn test_fulfill_limit_sell_order_with_sell_order_ok() { IFreeMintDispatcher { contract_address: token_address }.mint(wrong_buyer, quantity); - let wrong_order = setup_limit_sell_order(erc20_address, token_address, wrong_buyer, end_amount, quantity); - + let wrong_order = setup_limit_sell_order( + erc20_address, token_address, wrong_buyer, end_amount, quantity + ); + cheat_caller_address(executor_address, wrong_buyer, CheatSpan::TargetCalls(1)); IExecutorDispatcher { contract_address: executor_address }.create_order(wrong_order); @@ -575,8 +610,10 @@ fn test_fulfill_limit_order_without_matching_price_ok() { IFreeMintDispatcher { contract_address: erc20_address }.mint(buyer, start_amount); - let buyer_order = setup_limit_buy_order(erc20_address, token_address, buyer, start_amount, quantity); - + let buyer_order = setup_limit_buy_order( + erc20_address, token_address, buyer, start_amount, quantity + ); + cheat_caller_address(executor_address, buyer, CheatSpan::TargetCalls(1)); IExecutorDispatcher { contract_address: executor_address }.create_order(buyer_order); diff --git a/contracts/ark_tokens/src/lib.cairo b/contracts/ark_tokens/src/lib.cairo index 72904daf..bd79f450 100644 --- a/contracts/ark_tokens/src/lib.cairo +++ b/contracts/ark_tokens/src/lib.cairo @@ -1,4 +1,4 @@ mod erc20; +mod erc20_trade; mod erc721; mod erc721_royalty; -mod erc20_trade; \ No newline at end of file