Skip to main content

max / makenotwork

7.0 KB · 176 lines History Blame Raw
1 //! SyncKit developer billing: pricing formula and constants.
2 //!
3 //! Two modes:
4 //!
5 //! bulk — price = storage_gb_cap × $0.03
6 //! per_key — price = key_cap × gb_per_key × $0.03
7 //!
8 //! Both are pure GB-based pricing. Egress and ingress are absorbed by the
9 //! storage rate's ~2× margin against Cloudflare R2 ($0.015/GB) where SyncKit
10 //! blobs are hosted.
11 //!
12 //! Invoices are floored at a Stripe-fee-cover threshold so we never lose money
13 //! on a transaction. See `BASE_FLOOR_CENTS` for the math.
14
15 /// Storage rate in cents per GB per month. Calibrated to ~2× R2 cost
16 /// ($0.015/GB storage, $0 egress on R2). The 2× margin spread absorbs any
17 /// ingress/egress cost variance, so we don't need a separate egress price.
18 pub const STORAGE_RATE_CENTS_PER_GB: i64 = 3;
19
20 /// Stripe-fee-cover floor in cents. Stripe charges 2.9% + $0.30 per
21 /// successful charge. We pick the smallest invoice `F` (cents) such that the
22 /// remainder after Stripe fees is non-negative:
23 ///
24 /// F × (1 − 0.029) − 30 ≥ 0 ⇒ F ≥ 30 / 0.971 ⇒ F ≥ 30.9¢
25 ///
26 /// Round up to 31¢. At the floor, MNW nets ~$0 — covered, not profitable.
27 pub const BASE_FLOOR_CENTS: i64 = 31;
28
29 /// Warning thresholds (percent of storage cap). Matches CHECK constraint on
30 /// `sync_app_usage_current.last_warning_pct`. Only storage is enforced, so
31 /// these thresholds apply to storage usage only.
32 pub const WARNING_THRESHOLDS_PCT: &[i16] = &[75, 90, 100];
33
34 /// Compute the monthly Stripe invoice amount in cents for a given knob set.
35 ///
36 /// In bulk mode: `storage_gb_cap` is set, others are `None`.
37 /// In per_key mode: `key_cap` and `gb_per_key` are set, `storage_gb_cap` is `None`.
38 ///
39 /// Floors at `BASE_FLOOR_CENTS` so we never invoice below the Stripe-fee
40 /// break-even amount.
41 pub fn monthly_price_cents(
42 enforcement_mode: &str,
43 storage_gb_cap: Option<u32>,
44 key_cap: Option<u32>,
45 gb_per_key: Option<u32>,
46 ) -> i64 {
47 // Pure integer-cents arithmetic. The rate is a whole number of cents and
48 // the caps are whole GB, so there is no fractional money to round; the old
49 // `(gb as f64 * 3.0).ceil()` was an unnecessary trip through f64. Saturating
50 // multiplies keep absurd admin-set caps from overflowing i64 instead of
51 // wrapping to a negative invoice.
52 let gb: i64 = match enforcement_mode {
53 "bulk" => i64::from(storage_gb_cap.unwrap_or(0)),
54 "per_key" => {
55 let k = i64::from(key_cap.unwrap_or(0));
56 let g = i64::from(gb_per_key.unwrap_or(0));
57 k.saturating_mul(g)
58 }
59 _ => 0,
60 };
61 let raw = gb.saturating_mul(STORAGE_RATE_CENTS_PER_GB);
62 raw.max(BASE_FLOOR_CENTS)
63 }
64
65 /// Storage cap in bytes for the given GB cap.
66 pub fn storage_cap_bytes(storage_gb: u32) -> i64 {
67 i64::from(storage_gb) * 1024 * 1024 * 1024
68 }
69
70 #[cfg(test)]
71 mod tests {
72 use super::*;
73
74 #[test]
75 fn bulk_mode_pricing() {
76 // 100 GB bulk → 100 × 3 = 300 cents.
77 assert_eq!(monthly_price_cents("bulk", Some(100), None, None), 300);
78 // 1000 GB → $30.
79 assert_eq!(monthly_price_cents("bulk", Some(1000), None, None), 3000);
80 }
81
82 #[test]
83 fn per_key_mode_pricing() {
84 // 50 keys × 2 GB = 100 GB equivalent → 300 cents. Matches 100 GB bulk.
85 assert_eq!(monthly_price_cents("per_key", None, Some(50), Some(2)), 300);
86 // 1000 keys × 1 GB → $30.
87 assert_eq!(monthly_price_cents("per_key", None, Some(1000), Some(1)), 3000);
88 }
89
90 #[test]
91 fn floor_kicks_in_for_small_accounts() {
92 // 1 GB bulk → 3¢ raw, floored to 31¢.
93 assert_eq!(monthly_price_cents("bulk", Some(1), None, None), 31);
94 // 10 GB → 30¢, also floored to 31¢ (one cent short).
95 assert_eq!(monthly_price_cents("bulk", Some(10), None, None), 31);
96 // 11 GB → 33¢, above floor.
97 assert_eq!(monthly_price_cents("bulk", Some(11), None, None), 33);
98 // 1 key × 1 GB → 3¢ raw, floored.
99 assert_eq!(monthly_price_cents("per_key", None, Some(1), Some(1)), 31);
100 }
101
102 #[test]
103 fn heavy_workload_pricing() {
104 // 10 TB bulk → 10240 × 3 = 30720¢ = $307.20.
105 assert_eq!(monthly_price_cents("bulk", Some(10_240), None, None), 30_720);
106 // 10k keys × 1 GB → same.
107 assert_eq!(monthly_price_cents("per_key", None, Some(10_000), Some(1)), 30_000);
108 }
109
110 #[test]
111 fn missing_knobs_drop_to_floor() {
112 // Mode is set but no knobs provided — should hit the floor.
113 assert_eq!(monthly_price_cents("bulk", None, None, None), BASE_FLOOR_CENTS);
114 assert_eq!(monthly_price_cents("per_key", None, None, None), BASE_FLOOR_CENTS);
115 }
116
117 #[test]
118 fn unknown_mode_drops_to_floor() {
119 // Defensive: an unrecognized mode shouldn't blow up; it lands at the floor.
120 assert_eq!(monthly_price_cents("unknown", Some(100), None, None), BASE_FLOOR_CENTS);
121 }
122
123 #[test]
124 fn floor_amount_covers_stripe_fee() {
125 // 31¢ × 0.971 = 30.10¢, minus 30¢ fixed fee = 0.10¢ net. Verifies the
126 // documented math: the floor covers Stripe's fee with ~0 margin.
127 let net = (BASE_FLOOR_CENTS as f64) * 0.971 - 30.0;
128 assert!(net >= 0.0, "floor must net ≥ 0 after Stripe fees, got {net}");
129 assert!(net < 1.0, "floor should be tight, not overshoot — got {net}");
130 }
131
132 #[test]
133 fn storage_cap_in_bytes() {
134 assert_eq!(storage_cap_bytes(10), 10 * 1024 * 1024 * 1024);
135 }
136
137 // ── Edge cases (test-fuzz) ──
138
139 #[test]
140 fn pricing_at_u32_max_does_not_panic() {
141 // u32::MAX GB × 3¢ ≈ 1.3e10 cents, fits in i64. The cast must not panic.
142 let p = monthly_price_cents("bulk", Some(u32::MAX), None, None);
143 assert!(p > 0, "huge price should be positive, got {p}");
144 }
145
146 #[test]
147 fn per_key_pricing_at_u32_max_saturates_cleanly() {
148 // u32::MAX × u32::MAX overflows f64 precision but Rust's f64-as-i64 cast
149 // saturates at i64::MAX rather than UB. Must not panic.
150 let p = monthly_price_cents("per_key", None, Some(u32::MAX), Some(u32::MAX));
151 assert!(p > 0, "saturated price should still be positive, got {p}");
152 }
153
154 #[test]
155 fn storage_cap_at_u32_max_fits_in_i64() {
156 // u32::MAX × 2^30 = ~4.6e18, well under i64::MAX (~9.2e18).
157 let bytes = storage_cap_bytes(u32::MAX);
158 assert!(bytes > 0, "u32::MAX GB should produce a positive i64");
159 assert_eq!(bytes, (u32::MAX as i64) * 1024 * 1024 * 1024);
160 }
161
162 #[test]
163 fn bulk_with_zero_gb_drops_to_floor() {
164 // Defensive: validate_knobs rejects gb=0 at the route layer, but the
165 // pure function should still produce the floor rather than 0.
166 assert_eq!(monthly_price_cents("bulk", Some(0), None, None), BASE_FLOOR_CENTS);
167 }
168
169 #[test]
170 fn per_key_one_dimension_zero_drops_to_floor() {
171 // If only one of key_cap/gb_per_key is 0, the product is 0 → floor.
172 assert_eq!(monthly_price_cents("per_key", None, Some(0), Some(10)), BASE_FLOOR_CENTS);
173 assert_eq!(monthly_price_cents("per_key", None, Some(10), Some(0)), BASE_FLOOR_CENTS);
174 }
175 }
176