-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Contracts: ark_orderbook add whitelist broker
- Loading branch information
Showing
14 changed files
with
159 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//! Orders database. | ||
//! | ||
//! The order database uses the `storage_read` and `storage_write` | ||
//! syscalls directly to optimize how data are stored. | ||
//! | ||
//! The status is always stored independently of the seriliazed | ||
//! broker. This allows a quick and cheap storage/retrieval of the status | ||
//! without having to write/read the whole order. | ||
//! | ||
//! The only assumption for now is that, | ||
//! a single order serialized buffer must not exceed | ||
//! 256 felts. | ||
//! | ||
//! The storage layout is the following: | ||
//! | ||
//! 1. Compute the storage base address with [ORDER_DB_BASE_KEY, order_hash] | ||
//! 2. At base address + offset 0 => The order status. | ||
//! 3. At base address + offset 1 => The length of the serialized order. | ||
//! 4. At base addresss + offset 2 => First felt of the serialized order. | ||
use starknet::SyscallResultTrait; | ||
|
||
|
||
/// Must remain equal to 0 for now. | ||
const ADDRESS_DOMAIN: u32 = 0; | ||
/// A constant value used in the base key hash. | ||
const BROKER_DB_BASE_KEY: felt252 = 'broker whitelist'; | ||
|
||
|
||
|
||
/// Reads whitelist status of broker. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `broker_id` - ID of the broker. | ||
fn broker_whitelist_read(broker_id: felt252) -> bool { | ||
let key = array![BROKER_DB_BASE_KEY, broker_id]; | ||
|
||
let base = starknet::storage_base_address_from_felt252( | ||
poseidon::poseidon_hash_span(key.span()) | ||
); | ||
|
||
// First offset is the status. | ||
let whitelisted: felt252 = starknet::storage_read_syscall( | ||
ADDRESS_DOMAIN, starknet::storage_address_from_base(base) | ||
).unwrap_syscall(); | ||
|
||
whitelisted == 1 | ||
} | ||
|
||
/// Writes only the whitelisted brokers. | ||
/// It can be whitelisted or blacklisted. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `broker_id` - ID of the broker. | ||
/// * `status` - 1 if whitelisted, 0 if not. | ||
fn broker_whitelist_write(broker_id: felt252, status: felt252) -> bool { | ||
|
||
let key = array![BROKER_DB_BASE_KEY, broker_id]; | ||
|
||
let base = starknet::storage_base_address_from_felt252( | ||
poseidon::poseidon_hash_span(key.span()) | ||
); | ||
|
||
starknet::storage_write_syscall( | ||
ADDRESS_DOMAIN, starknet::storage_address_from_base(base), status | ||
); | ||
|
||
true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,9 @@ mod order { | |
mod order_v1; | ||
} | ||
|
||
mod broker { | ||
mod database; | ||
} | ||
|
||
mod orderbook; | ||
mod orderbook_event_mock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.