templar_common/vault/
params.rs

1use super::*;
2
3pub const DAY_NS: u64 = 86_400_000_000_000;
4pub const YEAR_NS: u64 = 365 * DAY_NS;
5
6pub const MIN_TIMELOCK_NS: u64 = 0;
7pub const MAX_TIMELOCK_NS: u64 = 30 * DAY_NS;
8pub const MAX_QUEUE_LEN: usize = 64;
9
10#[near(serializers = [borsh, json])]
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum TimelockKind {
13    Guardian,
14    Sentinel,
15    Config,
16    Cap,
17    MarketRemoval,
18}
19
20// Fetching a position
21const GET_SUPPLY_POSITION: u64 = 4;
22pub const GET_SUPPLY_POSITION_GAS: Gas = Gas::from_tgas(GET_SUPPLY_POSITION);
23
24// Create a withdrawal request
25pub const CREATE_WITHDRAW_REQ_GAS: Gas = buffer(5);
26
27// Balance reads against the underlying NEP-141
28pub const FT_BALANCE_OF_GAS: Gas = Gas::from_tgas(5);
29
30// Idle balance resync (ft_balance_of + callback)
31const RESYNC_IDLE_CALLBACK: u64 = 5;
32pub const RESYNC_IDLE_CALLBACK_GAS: Gas = buffer(RESYNC_IDLE_CALLBACK);
33
34// 5 TGAS for ft_balance_of + callback buffer
35pub const RESYNC_IDLE_GAS: Gas = buffer(5 + RESYNC_IDLE_CALLBACK);
36
37// Execute the next withdrawal request on a market
38const EXECUTE_NEXT_SUPPLY_WITHDRAW_REQ: u64 = 20;
39pub const EXECUTE_NEXT_SUPPLY_WITHDRAW_REQ_GAS: Gas =
40    Gas::from_tgas(EXECUTE_NEXT_SUPPLY_WITHDRAW_REQ);
41
42// Extra gas reserved for post-supply verification callbacks, used in
43// paths where we want a conservative safety margin beyond the base
44// estimate.
45pub const SUPPLY_POST_VERIFY_GAS: Gas = Gas::from_tgas(30);
46
47// Callback gas roots for withdraw/supply orchestration.
48
49// Root budget for callbacks after creating a market-side
50// supply-withdrawal request. Encodes: create request, read supply
51// position and settle withdraw accounting.
52pub const WITHDRAW_CREATE_REQUEST_CALLBACK_GAS: Gas =
53    buffer(EXECUTE_NEXT_SUPPLY_WITHDRAW_REQ + AFTER_EXECUTE_NEXT_SUPPLY_WITHDRAW_REQ);
54
55// Budget for the final "settle" phase of a withdraw execution:
56// reconcile principal and idle_balance, and potentially transition to
57// payout or the next market.
58const RECONCILE_PRINCIPAL: u64 = 5;
59const RECONCILE_IDLE_BALANCE: u64 = 5;
60const AFTER_EXECUTE_NEXT_WITHDRAW: u64 =
61    RECONCILE_PRINCIPAL + RECONCILE_IDLE_BALANCE + AFTER_SEND_TO_USER;
62pub const WITHDRAW_SETTLE_CALLBACK_GAS: Gas = buffer(AFTER_EXECUTE_NEXT_WITHDRAW);
63
64// Budget for executing the next supply-withdrawal request on a market
65// and fetching the updated supply position before the settle step.
66const AFTER_EXECUTE_NEXT_SUPPLY_WITHDRAW_REQ: u64 =
67    GET_SUPPLY_POSITION + AFTER_EXECUTE_NEXT_WITHDRAW;
68pub const WITHDRAW_EXECUTE_FETCH_POSITION_GAS: Gas = buffer(AFTER_EXECUTE_NEXT_SUPPLY_WITHDRAW_REQ);
69
70const AFTER_SUPPLY_2_READ: u64 = 5;
71pub const SUPPLY_POSITION_READ_CALLBACK_GAS: Gas = buffer(AFTER_SUPPLY_2_READ);
72pub const SUPPLY_AFTER_TRANSFER_CHECK_GAS: Gas = buffer(GET_SUPPLY_POSITION + AFTER_SUPPLY_2_READ);
73
74// NOTE: these are taken after running the contract with the gas report and ceiled to next whole TGAS.
75pub const SUPPLY_GAS: Gas = buffer(8);
76pub const ALLOCATE_GAS: Gas = buffer(20);
77pub const WITHDRAW_GAS: Gas = buffer(4);
78pub const EXECUTE_WITHDRAW_GAS: Gas = buffer(9);
79pub const SUBMIT_CAP_GAS: Gas = buffer(3);
80
81const AFTER_SEND_TO_USER: u64 = 5;
82pub const AFTER_SEND_TO_USER_GAS: Gas = Gas::from_tgas(AFTER_SEND_TO_USER);
83
84#[cfg(test)]
85mod tests {
86    use super::{
87        buffer, TimelockKind, AFTER_SEND_TO_USER_GAS, CREATE_WITHDRAW_REQ_GAS, DAY_NS,
88        MAX_QUEUE_LEN, MAX_TIMELOCK_NS, MIN_TIMELOCK_NS, RESYNC_IDLE_GAS, YEAR_NS,
89    };
90    use near_sdk::Gas;
91
92    #[test]
93    fn time_constants_match_expected_ranges() {
94        assert_eq!(YEAR_NS, 365 * DAY_NS);
95        assert_eq!(MIN_TIMELOCK_NS, 0);
96        assert_eq!(MAX_TIMELOCK_NS, 30 * DAY_NS);
97        assert_eq!(MAX_QUEUE_LEN, 64);
98    }
99
100    #[test]
101    fn gas_constants_use_buffered_roots() {
102        assert_eq!(CREATE_WITHDRAW_REQ_GAS, buffer(5));
103        assert_eq!(RESYNC_IDLE_GAS, buffer(10));
104        assert_eq!(AFTER_SEND_TO_USER_GAS, Gas::from_tgas(5));
105    }
106
107    #[test]
108    fn timelock_kind_variants_stay_stable() {
109        let variants = [
110            TimelockKind::Guardian,
111            TimelockKind::Sentinel,
112            TimelockKind::Config,
113            TimelockKind::Cap,
114            TimelockKind::MarketRemoval,
115        ];
116
117        assert_eq!(variants.len(), 5);
118    }
119}