Skip to content

Commit

Permalink
Solis: rollback order status if a verification fails
Browse files Browse the repository at this point in the history
  • Loading branch information
ybensacq committed Mar 8, 2024
1 parent ff10588 commit d1af386
Showing 1 changed file with 40 additions and 39 deletions.
79 changes: 40 additions & 39 deletions crates/solis/src/hooker.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Solis hooker on Katana transaction lifecycle.
//!
use crate::contracts::starknet_utils::{U256, ExecutionInfo};
use crate::contracts::starknet_utils::{ExecutionInfo, U256};
use async_trait::async_trait;
use cainome::cairo_serde::CairoSerde;
use cainome::rs::abigen;
Expand Down Expand Up @@ -49,10 +49,8 @@ pub struct SolisHooker<P: Provider + Sync + Send + 'static + std::fmt::Debug> {
}

impl<P: Provider + Sync + Send + 'static + std::fmt::Debug> SolisHooker<P> {

/// Verify the ownership of a token
async fn verify_ownership(&self, ownership_verifier: &OwnershipVerifier) -> bool {

let sn_utils_reader_nft_address = StarknetUtilsReader::new(
ownership_verifier.token_address.into(),
self.sn_utils_reader.provider(),
Expand All @@ -67,16 +65,15 @@ impl<P: Provider + Sync + Send + 'static + std::fmt::Debug> SolisHooker<P> {
if let Ok(owner_address) = owner {
if owner_address != ownership_verifier.current_owner {
tracing::trace!(
"\nOwner {:?} differs from offerer {:?} ",
owner,
ownership_verifier.current_owner
);
"\nOwner {:?} differs from offerer {:?} ",
owner,
ownership_verifier.current_owner
);

println!(
"\nOwner {:?} differs from offerer {:?} ",
owner,
ownership_verifier.current_owner
);
"\nOwner {:?} differs from offerer {:?} ",
owner, ownership_verifier.current_owner
);

return false;
}
Expand All @@ -98,10 +95,10 @@ impl<P: Provider + Sync + Send + 'static + std::fmt::Debug> SolisHooker<P> {
if let Ok(allowance) = allowance {
if allowance < balance_verifier.start_amount {
tracing::trace!(
"\nAllowance {:?} is not enough {:?} ",
allowance,
balance_verifier.start_amount
);
"\nAllowance {:?} is not enough {:?} ",
allowance,
balance_verifier.start_amount
);
println!(
"\nAllowance {:?} is not enough {:?} ",
allowance, balance_verifier.start_amount
Expand All @@ -118,10 +115,10 @@ impl<P: Provider + Sync + Send + 'static + std::fmt::Debug> SolisHooker<P> {
if let Ok(balance) = balance {
if balance < balance_verifier.start_amount {
tracing::trace!(
"\nBalance {:?} is not enough {:?} ",
balance,
balance_verifier.start_amount
);
"\nBalance {:?} is not enough {:?} ",
balance,
balance_verifier.start_amount
);
println!(
"\nBalance {:?} is not enough {:?} ",
balance, balance_verifier.start_amount
Expand Down Expand Up @@ -153,26 +150,28 @@ impl<P: Provider + Sync + Send + 'static + std::fmt::Debug> SolisHooker<P> {
let verifier = OwnershipVerifier {
token_address: ContractAddress(order.token_address.into()),
token_id: n_token_id,
current_owner: cainome::cairo_serde::ContractAddress(order.offerer.into())
current_owner: cainome::cairo_serde::ContractAddress(order.offerer.into()),
};

let owner_ship_verification = self.verify_ownership(&verifier).await;
if !owner_ship_verification {
return false;
}

}

// ERC20 to ERC721 : we check the allowance and the offerer balance.
if order.route == RouteType::Erc20ToErc721 {
if !self.verify_balance(&BalanceVerifier {
currency_address: ContractAddress(order.currency_address.into()),
offerer: cainome::cairo_serde::ContractAddress(order.offerer.into()),
start_amount: U256 {
low: order.start_amount.low,
high: order.start_amount.high,
},
}).await {
if !self
.verify_balance(&BalanceVerifier {
currency_address: ContractAddress(order.currency_address.into()),
offerer: cainome::cairo_serde::ContractAddress(order.offerer.into()),
start_amount: U256 {
low: order.start_amount.low,
high: order.start_amount.high,
},
})
.await
{
println!("verify balance for starknet before failed");
return false;
}
Expand Down Expand Up @@ -301,7 +300,6 @@ impl<P: Provider + Sync + Send + 'static + std::fmt::Debug> KatanaHooker for Sol
&self,
transaction: BroadcastedInvokeTransaction,
) -> bool {

let calls = match Vec::<TxCall>::cairo_deserialize(&transaction.calldata, 0) {
Ok(calls) => calls,
Err(e) => {
Expand Down Expand Up @@ -340,7 +338,7 @@ impl<P: Provider + Sync + Send + 'static + std::fmt::Debug> KatanaHooker for Sol
let verifier = OwnershipVerifier {
token_address: ContractAddress(execution_info.nft_address.into()),
token_id: execution_info.nft_token_id,
current_owner: cainome::cairo_serde::ContractAddress(execution_info.nft_to.into())
current_owner: cainome::cairo_serde::ContractAddress(execution_info.nft_to.into()),
};

let owner_ship_verification = self.verify_ownership(&verifier).await;
Expand All @@ -353,14 +351,17 @@ impl<P: Provider + Sync + Send + 'static + std::fmt::Debug> KatanaHooker for Sol
return false;
}

if !self.verify_balance(&BalanceVerifier {
currency_address: ContractAddress(execution_info.payment_currency_address.into()),
offerer: cainome::cairo_serde::ContractAddress(execution_info.nft_from.into()),
start_amount: U256 {
low: execution_info.payment_amount.low,
high: execution_info.payment_amount.high,
},
}).await {
if !self
.verify_balance(&BalanceVerifier {
currency_address: ContractAddress(execution_info.payment_currency_address.into()),
offerer: cainome::cairo_serde::ContractAddress(execution_info.nft_from.into()),
start_amount: U256 {
low: execution_info.payment_amount.low,
high: execution_info.payment_amount.high,
},
})
.await
{
// rollback the status
self.add_l1_handler_transaction_for_orderbook(
selector!("rollback_status_order"),
Expand Down

0 comments on commit d1af386

Please sign in to comment.