Skip to content

Commit

Permalink
add emitted details in events
Browse files Browse the repository at this point in the history
  • Loading branch information
lana-shanghai committed Dec 18, 2024
1 parent ce9b254 commit 86ebc1c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
8 changes: 6 additions & 2 deletions packages/onchain/src/orderbook/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ pub trait IOrderbook<TContractState> {
fn cancel_inscription(ref self: TContractState, inscription_id: u32, currency_fee: felt252);
fn lock_inscription(ref self: TContractState, inscription_id: u32, tx_hash: ByteArray);
fn submit_inscription(ref self: TContractState, inscription_id: u32, tx_hash: ByteArray);
fn query_inscription(self: @TContractState, inscription_id: u32) -> (ByteArray, u256);
fn query_inscription(
self: @TContractState, inscription_id: u32,
) -> (ContractAddress, ByteArray, u256);
}

#[starknet::interface]
Expand All @@ -37,7 +39,9 @@ pub trait OrderbookABI<TContractState> {
fn cancel_inscription(ref self: TContractState, inscription_id: u32, currency_fee: felt252);
fn lock_inscription(ref self: TContractState, inscription_id: u32, tx_hash: ByteArray);
fn submit_inscription(ref self: TContractState, inscription_id: u32, tx_hash: ByteArray);
fn query_inscription(self: @TContractState, inscription_id: u32) -> (ByteArray, u256);
fn query_inscription(
self: @TContractState, inscription_id: u32,
) -> (ContractAddress, ByteArray, u256);

// ERC20 functions
fn balance_of(self: @TContractState, account: ContractAddress) -> felt252;
Expand Down
60 changes: 46 additions & 14 deletions packages/onchain/src/orderbook/orderbook.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ mod Orderbook {
struct Storage {
// ID of the next inscription.
new_inscription_id: u32,
// A map from the inscription ID to a tuple with the inscribed
// data and submitter fee.
inscriptions: Map<u32, (ByteArray, u256)>,
// A map from the inscription ID to a tuple with the caller, the
// inscribed data, and submitter fee.
inscriptions: Map<u32, (ContractAddress, ByteArray, u256)>,
// A map from the inscription ID to status. Possible values:
// 'Open', 'Locked', 'Canceled', 'Closed'.
inscription_statuses: Map<u32, Status>,
Expand Down Expand Up @@ -53,22 +53,31 @@ mod Orderbook {
#[derive(Drop, starknet::Event)]
pub struct RequestCreated {
pub id: u32,
pub data: ByteArray,
pub caller: ContractAddress,
pub inscription_data: ByteArray,
pub receiving_address: ByteArray,
pub currency_fee: felt252,
pub submitter_fee: u256,
}

#[derive(Drop, starknet::Event)]
pub struct RequestCanceled {
pub id: u32,
pub currency_fee: felt252,
}

#[derive(Drop, starknet::Event)]
pub struct RequestLocked {
pub id: u32,
pub submitter: ContractAddress,
pub tx_hash: ByteArray,
}

#[derive(Drop, starknet::Event)]
pub struct RequestCompleted {
pub id: u32,
pub submitter: ContractAddress,
pub tx_hash: ByteArray,
}

#[abi(embed_v0)]
Expand Down Expand Up @@ -100,17 +109,29 @@ mod Orderbook {
);
}
let id = self.new_inscription_id.read();
self.inscriptions.write(id, (inscription_data.clone(), submitter_fee));
self.inscriptions.write(id, (caller, inscription_data.clone(), submitter_fee));
self.inscription_statuses.write(id, Status::Open);
self.emit(RequestCreated { id: id, data: inscription_data });
self
.emit(
RequestCreated {
id: id,
caller: caller,
inscription_data: inscription_data,
receiving_address: receiving_address,
currency_fee: currency_fee,
submitter_fee: submitter_fee,
},
);
id
}

/// Inputs:
/// - `inscription_id: felt252`, the ID of the inscription.
/// Returns:
/// - `(ByteArray, felt252)`, the tuple with the inscribed data and the fee.
fn query_inscription(self: @ContractState, inscription_id: u32) -> (ByteArray, u256) {
fn query_inscription(
self: @ContractState, inscription_id: u32,
) -> (ContractAddress, ByteArray, u256) {
self.inscriptions.read(inscription_id)
}

Expand All @@ -120,24 +141,27 @@ mod Orderbook {
/// cancel.
/// - `currency_fee: felt252`, the token that the user paid the submitter fee in.
fn cancel_inscription(ref self: ContractState, inscription_id: u32, currency_fee: felt252) {
let caller = get_caller_address();
let (request_creator, inscription_data, amount) = self
.inscriptions
.read(inscription_id);
assert(caller == request_creator, 'Caller cannot cancel this id');

let status = self.inscription_statuses.read(inscription_id);
assert(status != Status::Undefined, 'Inscription does not exist');
assert(status != Status::Locked, 'The inscription is locked');
assert(status != Status::Canceled, 'The inscription is canceled');
assert(status != Status::Closed, 'The inscription has been closed');

let caller = get_caller_address();
// TODO: change the address to the actual escrow contract once it's implemented.
let escrow_address = get_contract_address();
if (currency_fee == 'STRK'.into()) {
let strk_token = self.strk_token.read();
let (_, amount) = self.inscriptions.read(inscription_id);
strk_token.transfer_from(sender: escrow_address, recipient: caller, amount: amount);
}
let (inscription_data, _) = self.inscriptions.read(inscription_id);
self.inscriptions.write(inscription_id, (inscription_data, 0));
self.inscriptions.write(inscription_id, (caller, inscription_data, 0));
self.inscription_statuses.write(inscription_id, Status::Canceled);
self.emit(RequestCanceled { id: inscription_id });
self.emit(RequestCanceled { id: inscription_id, currency_fee: currency_fee });
}

/// Called by a submitter. Multiple submitters are allowed to lock the
Expand Down Expand Up @@ -167,7 +191,7 @@ mod Orderbook {
submitters.write(submitter, submitter);

self.inscription_statuses.write(inscription_id, Status::Locked);
self.emit(RequestLocked { id: inscription_id });
self.emit(RequestLocked { id: inscription_id, submitter: submitter, tx_hash: tx_hash });
}

/// Called by a submitter. The fee is transferred to the submitter if
Expand All @@ -178,6 +202,11 @@ mod Orderbook {
/// - `inscription_id: felt252`, the ID of the inscription being locked.
/// - `tx_hash: ByteArray`, the hash of the transaction submitted to Bitcoin.
fn submit_inscription(ref self: ContractState, inscription_id: u32, tx_hash: ByteArray) {
let caller = get_caller_address();
let submitters = self.submitters.entry(inscription_id);
let submitter = submitters.read(caller);
assert(caller == submitter, 'Caller does not match submitter');

let (_, precomputed_tx_hash, _) = self.inscription_locks.read(inscription_id);
assert(precomputed_tx_hash == tx_hash, 'Precomputed hash != submitted');

Expand All @@ -191,7 +220,10 @@ mod Orderbook {
// TODO: assert that the witness data contains the requested inscription

self.inscription_statuses.write(inscription_id, Status::Closed);
self.emit(RequestCompleted { id: inscription_id });
self
.emit(
RequestCompleted { id: inscription_id, submitter: submitter, tx_hash: tx_hash },
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/onchain/src/orderbook/test_orderbook.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn test_request_inscription_stored_and_retrieved() {

orderbook_dispatcher.request_inscription(test_data, test_taproot_address, 'STRK'.into(), 10);

let expected = ("data", 10); // the inscription data and the submitter fee
let expected = (test_address(), "data", 10); // the inscription data and the submitter fee
let actual = orderbook_dispatcher.query_inscription(0);
assert_eq!(expected, actual);

Expand Down

0 comments on commit 86ebc1c

Please sign in to comment.