Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking β€œSign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add erc-20 support to orderbook #476

Open
wants to merge 22 commits into
base: feat/contract-v2
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
finalize erc20 order supports
JoE11-y committed Oct 4, 2024
commit 36ce0b49690099b0b7e022ee4a0fccc4f64a738f
28 changes: 26 additions & 2 deletions contracts/ark_common/src/protocol/order_types.cairo
Original file line number Diff line number Diff line change
@@ -225,7 +225,7 @@ struct ExecutionInfo {
token_from: ContractAddress,
token_to: ContractAddress,
token_quantity: u256,
token_id: u256,
token_id: OptionU256,
payment_from: ContractAddress,
payment_to: ContractAddress,
payment_amount: u256,
@@ -297,4 +297,28 @@ impl Felt252TryIntoRoute of TryInto<felt252, RouteType> {
Option::None
}
}
}
}

#[derive(Drop, Copy)]
struct OptionU256 {
is_some: felt252,
value: u256,
}

trait OptionU256Trait<T, +Serde<T>, +Drop<T>> {
fn get_some(self: @T, value: u256) -> (felt252, u256);
fn is_some(self: @T) -> bool;
}

impl OptionU256Impl of OptionU256Trait<OptionU256> {
fn get_some(self: @OptionU256) -> (felt252, u256) {
(*self.is_some, *self.value)
}
fn is_some(self: @OptionU256) -> bool {
if *self.is_some == 1 {
true
} else {
false
}
}
}
2 changes: 1 addition & 1 deletion contracts/ark_common/src/protocol/order_v1.cairo
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ struct OrderV1 {
// The quantity of the token_id to be offerred (1 for NFTs).
quantity: u256,
// in wei. --> 10 | 10 | 10 |
start_amount: u256,
start_amount: u256, // amount to pay.
// in wei. --> 0 | 10 | 20 |
end_amount: u256,
// Start validity date of the offer, seconds since unix epoch.
57 changes: 33 additions & 24 deletions contracts/ark_component/src/orderbook/orderbook.cairo
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ pub mod OrderbookComponent {
};
use ark_common::protocol::order_types::{
OrderStatus, OrderTrait, OrderType, CancelInfo, FulfillInfo, ExecutionValidationInfo,
ExecutionInfo, RouteType, PriceLevel
ExecutionInfo, RouteType, OptionU256
};
use ark_common::protocol::order_v1::OrderV1;
use core::debug::PrintTrait;
@@ -17,7 +17,7 @@ pub mod OrderbookComponent {
use core::zeroable::Zeroable;
use starknet::ContractAddress;
use starknet::storage::Map;
use starknet::contract_address_to_felt252;

use super::super::interface::{IOrderbook, IOrderbookAction, orderbook_errors};

const EXTENSION_TIME_IN_SECONDS: u64 = 600;
@@ -466,7 +466,7 @@ pub mod OrderbookComponent {
token_address: order.token_address,
token_from: order.offerer,
token_to: related_order.offerer,
token_id: 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,
@@ -513,7 +513,7 @@ pub mod OrderbookComponent {
token_address: order.token_address,
token_from: fulfill_info.fulfiller,
token_to: order.offerer,
token_id: 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,
@@ -547,7 +547,7 @@ pub mod OrderbookComponent {
token_address: order.token_address,
token_from: order.offerer,
token_to: fulfill_info.fulfiller,
token_id: 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,
@@ -855,24 +855,25 @@ pub mod OrderbookComponent {
cancelled_order_hash
}

fn _create_execution_info(
fn _create_listing_execution_info(
order_hash: felt252,
buy_order: OrderV1,
sell_order: OrderV1,
fulfill_info: FulfillInfo,
token_quantity: u64,
token_quantity: u256,
listing_broker_address: ContractAddress,
price: u256
) -> ExecutionInfo {
ExecutionInfo {
order_hash,
token_address: buy_order.token_address,
token_from: sell_order.offerer,
token_to: buy_order.offerer,
token_id: 0,
token_id: OptionU256 { is_some: 0, value: 0},
token_quantity,
payment_from: buy_order.offerer,
payment_to: sell_order.offerer,
payment_amount: buy_order.start_amount * token_quantity,
payment_amount: price * token_quantity,
payment_currency_address: buy_order.currency_address,
payment_currency_chain_id: buy_order.currency_chain_id,
listing_broker_address: listing_broker_address,
@@ -930,8 +931,10 @@ pub mod OrderbookComponent {
);

// check that the price is the same
let order_price = order.start_amount / order.quantity;
let related_order_price = related_order.start_amount / related_order.quantity;
assert(
related_order.start_amount == order.start_amount,
order_price == related_order_price,
orderbook_errors::ORDER_PRICE_NOT_MATCH
)

@@ -942,7 +945,7 @@ pub mod OrderbookComponent {
// fulfilling a sell order with a buy order (related-order)
RouteType::Erc20ToErc20Sell => {
assert(
related_order.route == Erc20ToErc20Buy,
related_order.route == RouteType::Erc20ToErc20Buy,
orderbook_errors::ORDER_ROUTE_NOT_VALID
);
if order_quantity > related_order_quantity {
@@ -959,13 +962,14 @@ pub mod OrderbookComponent {
// set buy order as fufilled
order_status_write(related_order_hash, OrderStatus::Fulfilled);
// set execute info
let execute_info = self._create_execution_info(
let execute_info = self._create_listing_execution_info(
related_order_hash,
related_order,
order,
fulfill_info,
related_order_quantity,
related_order_hash.broker_id
related_order_hash.broker_id,
order_price
);
(Option::Some(execute_info), Option::Some(related_order_hash))
}else if related_order_quantity > order_quantity {
@@ -982,13 +986,14 @@ pub mod OrderbookComponent {
// set sell order as fulfilled
order_status_write(order_hash, OrderStatus::Fulfilled);
// generate execution info
let execute_info = self._create_execution_info(
let execute_info = self._create_listing_execution_info(
order_hash,
related_order,
order,
fulfill_info,
order_quantity,
order.broker_id
order.broker_id,
order_price
);
(Option::Some(execute_info), Option::Some(related_order_hash))
}else{
@@ -998,13 +1003,14 @@ pub mod OrderbookComponent {

// passing any of them as the order hash will fulfill both orders,
// so just one executioninfo will be sent.
let execute_info = self._create_execution_info(
let execute_info = self._create_listing_execution_info(
order_hash,
related_order,
order,
fulfill_info,
order_quantity,
order.broker_id
order.broker_id,
order_price
);
// return
(Option::Some(execute_info), Option::Some(related_order_hash))
@@ -1013,7 +1019,7 @@ pub mod OrderbookComponent {
// fulfilling a buy order with a sell order (related-order)
RouteType::Erc20ToErc20Buy => {
assert(
related_order.route == Erc20ToErc20Sell,
related_order.route == RouteType::Erc20ToErc20Sell,
orderbook_errors::ORDER_ROUTE_NOT_VALID
);

@@ -1030,13 +1036,14 @@ pub mod OrderbookComponent {
);
// set sell order as fufilled
order_status_write(related_order_hash, OrderStatus::Fulfilled);
let execute_info = self._create_execution_info(
let execute_info = self._create_listing_execution_info(
related_order_hash,
order,
related_order,
fulfill_info,
related_order_quantity,
related_order.broker_id
related_order.broker_id,
order_price
);
// return
(Option::Some(execute_info), Option::Some(related_order_hash))
@@ -1054,13 +1061,14 @@ pub mod OrderbookComponent {
// set buy order as fulfilled
order_status_write(order_hash, OrderStatus::Fulfilled);

let execute_info = self._create_execution_info(
let execute_info = self._create_listing_execution_info(
order_hash,
order,
related_order,
fulfill_info,
order_quantity,
order.broker_id
order.broker_id,
order_price
);
// return
(Option::Some(execute_info), Option::Some(related_order_hash))
@@ -1071,13 +1079,14 @@ pub mod OrderbookComponent {

// passing any of them as the order hash will fulfill both orders,
// so just one executioninfo will be sent
let execute_info = self._create_execution_info(
let execute_info = self._create_listing_execution_info(
order_hash,
order,
related_order,
fulfill_info,
order_quantity,
order.broker_id
order.broker_id,
order_price
);

(Option::Some(execute_info), Option::Some(related_order_hash))
Loading