Skip to main content

tempo_contracts/precompiles/
tip_fee_manager.rs

1pub use IFeeManager::{IFeeManagerErrors as FeeManagerError, IFeeManagerEvents as FeeManagerEvent};
2pub use ITIPFeeAMM::{ITIPFeeAMMErrors as TIPFeeAMMError, ITIPFeeAMMEvents as TIPFeeAMMEvent};
3
4crate::sol! {
5    /// FeeManager interface for managing gas fee collection and distribution.
6    ///
7    /// IMPORTANT: FeeManager inherits from TIPFeeAMM and shares the same storage layout.
8    /// This means:
9    /// - FeeManager has all the functionality of TIPFeeAMM (pool management, swaps, liquidity operations)
10    /// - Both contracts use the same storage slots for AMM data (pools, reserves, liquidity balances)
11    /// - FeeManager extends TIPFeeAMM with additional storage slots (4-15) for fee-specific data
12    /// - When deployed, FeeManager IS a TIPFeeAMM with additional fee management capabilities
13    ///
14    /// Storage layout:
15    /// - Slots 0-3: TIPFeeAMM storage (pools, pool exists, liquidity data)
16    /// - Slots 4+: FeeManager-specific storage (validator tokens, user tokens, collected fees, etc.)
17    #[derive(Debug, PartialEq, Eq)]
18    #[sol(abi)]
19    interface IFeeManager {
20        // Structs
21        struct FeeInfo {
22            uint128 amount;
23            bool hasBeenSet;
24        }
25
26        // User preferences
27        function userTokens(address user) external view returns (address);
28        function validatorTokens(address validator) external view returns (address);
29        function setUserToken(address token) external;
30        function setValidatorToken(address token) external;
31
32        // Fee functions
33        function distributeFees(address validator, address token) external;
34        function collectedFees(address validator, address token) external view returns (uint256);
35        // NOTE: collectFeePreTx is a protocol-internal function called directly by the
36        // execution handler, not exposed via the dispatch interface.
37
38        // Events
39        event UserTokenSet(address indexed user, address indexed token);
40        event ValidatorTokenSet(address indexed validator, address indexed token);
41        event FeesDistributed(address indexed validator, address indexed token, uint256 amount);
42
43        // Errors
44        error InvalidToken();
45        error InsufficientFeeTokenBalance();
46        error CannotChangeWithinBlock();
47    }
48}
49
50sol! {
51    /// TIPFeeAMM interface defining the base AMM functionality for stablecoin pools.
52    /// This interface provides core liquidity pool management and swap operations.
53    ///
54    /// NOTE: The FeeManager contract inherits from TIPFeeAMM and shares the same storage layout.
55    /// When FeeManager is deployed, it effectively "is" a TIPFeeAMM with additional fee management
56    /// capabilities layered on top. Both contracts operate on the same storage slots.
57    #[derive(Debug, PartialEq, Eq)]
58    #[sol(abi)]
59    #[allow(clippy::too_many_arguments)]
60    interface ITIPFeeAMM {
61        // Structs
62        struct Pool {
63            uint128 reserveUserToken;
64            uint128 reserveValidatorToken;
65        }
66
67        struct PoolKey {
68            address token0;
69            address token1;
70        }
71
72
73        // Constants
74        function M() external view returns (uint256);
75        function N() external view returns (uint256);
76        function SCALE() external view returns (uint256);
77        function MIN_LIQUIDITY() external view returns (uint256);
78
79        // Pool Management
80        function getPoolId(address userToken, address validatorToken) external pure returns (bytes32);
81        function getPool(address userToken, address validatorToken) external view returns (Pool memory);
82        function pools(bytes32 poolId) external view returns (Pool memory);
83
84        // Liquidity Operations
85        function mint(address userToken, address validatorToken, uint256 amountValidatorToken, address to) external returns (uint256 liquidity);
86        function burn(address userToken, address validatorToken, uint256 liquidity, address to) external returns (uint256 amountUserToken, uint256 amountValidatorToken);
87
88        // Liquidity Balances
89        function totalSupply(bytes32 poolId) external view returns (uint256);
90        function liquidityBalances(bytes32 poolId, address user) external view returns (uint256);
91
92        // Swapping
93        function rebalanceSwap(address userToken, address validatorToken, uint256 amountOut, address to) external returns (uint256 amountIn);
94
95        // Events
96        event Mint(address sender, address indexed to, address indexed userToken, address indexed validatorToken, uint256 amountValidatorToken, uint256 liquidity);
97        event Burn(address indexed sender, address indexed userToken, address indexed validatorToken, uint256 amountUserToken, uint256 amountValidatorToken, uint256 liquidity, address to);
98        event RebalanceSwap(address indexed userToken, address indexed validatorToken, address indexed swapper, uint256 amountIn, uint256 amountOut);
99
100        // Errors
101        error IdenticalAddresses();
102        error InvalidToken();
103        error InsufficientLiquidity();
104        error InsufficientReserves();
105        error InvalidAmount();
106        error DivisionByZero();
107        error InvalidSwapCalculation();
108    }
109}
110
111impl FeeManagerError {
112    /// Creates an error for invalid token.
113    pub const fn invalid_token() -> Self {
114        Self::InvalidToken(IFeeManager::InvalidToken {})
115    }
116
117    /// Creates an error for insufficient fee token balance.
118    pub const fn insufficient_fee_token_balance() -> Self {
119        Self::InsufficientFeeTokenBalance(IFeeManager::InsufficientFeeTokenBalance {})
120    }
121
122    /// Creates an error for cannot change within block.
123    pub const fn cannot_change_within_block() -> Self {
124        Self::CannotChangeWithinBlock(IFeeManager::CannotChangeWithinBlock {})
125    }
126}
127
128impl TIPFeeAMMError {
129    /// Creates an error for identical token addresses.
130    pub const fn identical_addresses() -> Self {
131        Self::IdenticalAddresses(ITIPFeeAMM::IdenticalAddresses {})
132    }
133
134    /// Creates an error for invalid token.
135    pub const fn invalid_token() -> Self {
136        Self::InvalidToken(ITIPFeeAMM::InvalidToken {})
137    }
138
139    /// Creates an error for insufficient liquidity.
140    pub const fn insufficient_liquidity() -> Self {
141        Self::InsufficientLiquidity(ITIPFeeAMM::InsufficientLiquidity {})
142    }
143
144    /// Creates an error for insufficient reserves.
145    pub const fn insufficient_reserves() -> Self {
146        Self::InsufficientReserves(ITIPFeeAMM::InsufficientReserves {})
147    }
148    /// Creates an error for invalid amount.
149    pub const fn invalid_amount() -> Self {
150        Self::InvalidAmount(ITIPFeeAMM::InvalidAmount {})
151    }
152
153    /// Creates an error for invalid swap calculation.
154    pub const fn invalid_swap_calculation() -> Self {
155        Self::InvalidSwapCalculation(ITIPFeeAMM::InvalidSwapCalculation {})
156    }
157
158    /// Creates an error for division by zero.
159    pub const fn division_by_zero() -> Self {
160        Self::DivisionByZero(ITIPFeeAMM::DivisionByZero {})
161    }
162}