Skip to main content

tempo_contracts/precompiles/
stablecoin_dex.rs

1pub use IStablecoinDEX::{
2    IStablecoinDEXErrors as StablecoinDEXError, IStablecoinDEXEvents as StablecoinDEXEvents,
3};
4
5/// Minimum tick value for the orderbook price grid.
6pub const MIN_TICK: i16 = -2000;
7/// Maximum tick value for the orderbook price grid.
8pub const MAX_TICK: i16 = 2000;
9/// Price scale factor for tick-to-price conversions.
10pub const PRICE_SCALE: u32 = 100_000;
11
12crate::sol! {
13    /// StablecoinDEX interface for managing orderbook based trading of stablecoins.
14    ///
15    /// The StablecoinDEX provides a limit orderbook system where users can:
16    /// - Place limit orders (buy/sell) with specific price ticks
17    /// - Place flip orders that automatically create opposite-side orders when filled
18    /// - Execute swaps against existing liquidity
19    /// - Manage internal balances for trading
20    ///
21    /// The exchange operates on pairs between base tokens and their designated quote tokens,
22    /// using a tick-based pricing system for precise order matching.
23    #[derive(Debug, PartialEq, Eq)]
24    #[sol(abi)]
25    interface IStablecoinDEX {
26        // Structs
27        struct Order {
28            uint128 orderId;
29            address maker;
30            bytes32 bookKey;
31            bool isBid;
32            int16 tick;
33            uint128 amount;
34            uint128 remaining;
35            uint128 prev;
36            uint128 next;
37            bool isFlip;
38            int16 flipTick;
39        }
40
41        struct PriceLevel {
42            uint128 head;
43            uint128 tail;
44            uint128 totalLiquidity;
45        }
46
47        struct Orderbook {
48            address base;
49            address quote;
50            int16 bestBidTick;
51            int16 bestAskTick;
52        }
53
54        // Core Trading Functions
55        function createPair(address base) external returns (bytes32 key);
56        function place(address token, uint128 amount, bool isBid, int16 tick) external returns (uint128 orderId);
57        function placeFlip(address token, uint128 amount, bool isBid, int16 tick, int16 flipTick) external returns (uint128 orderId);
58        function cancel(uint128 orderId) external;
59        function cancelStaleOrder(uint128 orderId) external;
60
61        // Swap Functions
62        function swapExactAmountIn(address tokenIn, address tokenOut, uint128 amountIn, uint128 minAmountOut) external returns (uint128 amountOut);
63        function swapExactAmountOut(address tokenIn, address tokenOut, uint128 amountOut, uint128 maxAmountIn) external returns (uint128 amountIn);
64        function quoteSwapExactAmountIn(address tokenIn, address tokenOut, uint128 amountIn) external view returns (uint128 amountOut);
65        function quoteSwapExactAmountOut(address tokenIn, address tokenOut, uint128 amountOut) external view returns (uint128 amountIn);
66
67        // Balance Management
68        function balanceOf(address user, address token) external view returns (uint128);
69        function withdraw(address token, uint128 amount) external;
70
71        // View Functions
72        function getOrder(uint128 orderId) external view returns (Order memory);
73
74        function getTickLevel(address base, int16 tick, bool isBid) external view returns (uint128 head, uint128 tail, uint128 totalLiquidity);
75        function pairKey(address tokenA, address tokenB) external pure returns (bytes32);
76        function nextOrderId() external view returns (uint128);
77        function books(bytes32 pairKey) external view returns (Orderbook memory);
78
79        // Constants (exposed as view functions)
80        function MIN_TICK() external pure returns (int16);
81        function MAX_TICK() external pure returns (int16);
82        function TICK_SPACING() external pure returns (int16);
83        function PRICE_SCALE() external pure returns (uint32);
84        function MIN_ORDER_AMOUNT() external pure returns (uint128);
85        function MIN_PRICE() external pure returns (uint32);
86        function MAX_PRICE() external pure returns (uint32);
87
88        // Price conversion functions
89        function tickToPrice(int16 tick) external pure returns (uint32 price);
90        function priceToTick(uint32 price) external pure returns (int16 tick);
91
92        // Events
93        event PairCreated(bytes32 indexed key, address indexed base, address indexed quote);
94        event OrderPlaced(uint128 indexed orderId, address indexed maker, address indexed token, uint128 amount, bool isBid, int16 tick, bool isFlipOrder, int16 flipTick);
95        event OrderFilled(uint128 indexed orderId, address indexed maker, address indexed taker, uint128 amountFilled, bool partialFill);
96        event OrderFlipped(uint128 indexed orderId, address indexed maker, address indexed token, uint128 amount, bool isBid, int16 tick, int16 flipTick);
97        event FlipFailed(uint128 indexed orderId, address indexed maker, bytes4 reason);
98        event OrderCancelled(uint128 indexed orderId);
99
100        // Errors
101        error Unauthorized();
102        error PairDoesNotExist();
103        error PairAlreadyExists();
104        error OrderDoesNotExist();
105        error IdenticalTokens();
106        error InvalidToken();
107        error TickOutOfBounds(int16 tick);
108        error InvalidTick();
109        error InvalidFlipTick();
110        error InsufficientBalance();
111        error InsufficientLiquidity();
112        error InsufficientOutput();
113        error MaxInputExceeded();
114        error BelowMinimumOrderSize(uint128 amount);
115        error InvalidBaseToken();
116        error OrderNotStale();
117    }
118}