Skip to content

Commit

Permalink
Show JIT orders on COW explorer (#2920)
Browse files Browse the repository at this point in the history
# Description

Fixes #2825

# Changes
<!-- List of detailed changes (how the change is accomplished) -->

- [x] Added `jit_orders` database table and read/write functions
- [x] Refactored `domain::Settlement` so that user orders and JIT orders
can be differentiated using `Trade` enum.
- [x] Saved JIT orders from OnSettlementEventUpdater
- [x] Return jit orders on /get_order and /get_trades

_Todo in follow up PR: return on get_order_by_tx and get_order_by_owner_

## How to test
1. Updated unit tests for surplus/fee calculation
2. Added one new unit test for settlement with cow amm.
3. Added unit test for read/write to the new database table.
4. Jit order expanded to prove that jit orders are returned on
endpoints.

I would suggest to review in IDE, since it's not easy to follow the
changes as code diff.
  • Loading branch information
sunce86 authored Sep 4, 2024
1 parent 907eff0 commit 41334ea
Show file tree
Hide file tree
Showing 22 changed files with 1,438 additions and 728 deletions.
38 changes: 38 additions & 0 deletions crates/autopilot/src/domain/auction/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,51 @@ pub enum Signature {
PreSign,
}

impl Signature {
pub fn scheme(&self) -> SigningScheme {
match self {
Signature::Eip712(_) => SigningScheme::Eip712,
Signature::EthSign(_) => SigningScheme::EthSign,
Signature::Eip1271(_) => SigningScheme::Eip1271,
Signature::PreSign => SigningScheme::PreSign,
}
}

pub fn to_bytes(&self) -> Vec<u8> {
match self {
Self::Eip712(signature) | Self::EthSign(signature) => signature.to_bytes().to_vec(),
Self::Eip1271(signature) => signature.clone(),
Self::PreSign => Vec::new(),
}
}
}

#[derive(Clone, Debug, PartialEq)]
pub enum SigningScheme {
Eip712,
EthSign,
Eip1271,
PreSign,
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct EcdsaSignature {
pub r: H256,
pub s: H256,
pub v: u8,
}

impl EcdsaSignature {
/// r + s + v
pub fn to_bytes(self) -> [u8; 65] {
let mut bytes = [0u8; 65];
bytes[..32].copy_from_slice(self.r.as_bytes());
bytes[32..64].copy_from_slice(self.s.as_bytes());
bytes[64] = self.v;
bytes
}
}

/// An amount denominated in the sell token for [`Side::Sell`] [`Order`]s, or in
/// the buy token for [`Side::Buy`] [`Order`]s.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
Expand Down
14 changes: 8 additions & 6 deletions crates/autopilot/src/domain/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ pub use primitive_types::{H160, H256, U256};
pub struct Address(pub H160);

/// Block number.
#[derive(Debug, Copy, Clone, From, PartialEq, PartialOrd)]
#[derive(Debug, Copy, Clone, From, PartialEq, PartialOrd, Default)]
pub struct BlockNo(pub u64);

/// A transaction ID, AKA transaction hash.
#[derive(Debug, Copy, Clone, From)]
#[derive(Debug, Copy, Clone, From, Default)]
pub struct TxId(pub H256);

/// An ERC20 token address.
Expand Down Expand Up @@ -123,7 +123,7 @@ pub struct Gas(pub U256);
/// The `effective_gas_price` as defined by EIP-1559.
///
/// https://eips.ethereum.org/EIPS/eip-1559#specification
#[derive(Debug, Clone, Copy, Display)]
#[derive(Debug, Clone, Copy, Display, Default)]
pub struct EffectiveGasPrice(pub Ether);

impl From<U256> for EffectiveGasPrice {
Expand Down Expand Up @@ -218,7 +218,7 @@ pub struct Asset {
}

/// An amount of native Ether tokens denominated in wei.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, From, Into, Display)]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, From, Into, Display, Default)]
pub struct Ether(pub U256);

impl std::ops::Add for Ether {
Expand Down Expand Up @@ -263,7 +263,7 @@ pub struct Event {
}

/// Any type of on-chain transaction.
#[derive(Debug)]
#[derive(Debug, Clone, Default)]
pub struct Transaction {
/// The hash of the transaction.
pub hash: TxId,
Expand All @@ -273,8 +273,10 @@ pub struct Transaction {
pub input: Calldata,
/// The block number of the block that contains the transaction.
pub block: BlockNo,
/// The timestamp of the block that contains the transaction.
pub timestamp: u32,
/// The gas used by the transaction.
pub gas: Gas,
/// The effective gas price of the transaction.
pub effective_gas_price: EffectiveGasPrice,
pub gas_price: EffectiveGasPrice,
}
19 changes: 0 additions & 19 deletions crates/autopilot/src/domain/settlement/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,3 @@ pub struct Auction {
/// surplus if settled.
pub surplus_capturing_jit_order_owners: HashSet<domain::eth::Address>,
}

impl Auction {
/// Protocol defines rules whether an order is eligible to contribute to the
/// surplus of a settlement.
pub fn is_surplus_capturing(&self, order: &domain::OrderUid) -> bool {
// All orders in the auction contribute to surplus
if self.orders.contains_key(order) {
return true;
}
// Some JIT orders contribute to surplus, for example COW AMM orders
if self
.surplus_capturing_jit_order_owners
.contains(&order.owner())
{
return true;
}
false
}
}
Loading

0 comments on commit 41334ea

Please sign in to comment.