forked from SiLioLabs/PayFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.rs
More file actions
67 lines (57 loc) · 2.1 KB
/
Copy pathvalidation.rs
File metadata and controls
67 lines (57 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use soroban_sdk::{token, Address, Env};
use crate::errors::ContractError;
use crate::Subscription;
pub fn check_allowance(env: &Env, user: &Address, token: &Address, min_amount: i128) {
let client = token::Client::new(env, token);
let allowance = client.allowance(user, &env.current_contract_address());
if allowance < min_amount {
env.panic_with_error(ContractError::InsufficientAllowance);
}
}
/// Composable helper that asserts a subscription is ready to be used:
/// the subscription must be active and the user must have sufficient
/// allowance for the subscription's token and amount.
#[allow(dead_code)]
pub fn validate_subscription_readiness(env: &Env, user: &Address, sub: &Subscription) {
if !sub.active {
env.panic_with_error(ContractError::SubscriptionNotActive);
}
check_allowance(env, user, &sub.token, sub.amount);
}
pub fn require_valid_amount(env: &Env, new_amount: i128) {
if new_amount <= 0 {
env.panic_with_error(ContractError::AmountMustBePositive);
}
if new_amount > crate::MAX_SUBSCRIPTION_AMOUNT {
env.panic_with_error(ContractError::AmountExceedsMaximum);
}
}
pub fn require_valid_interval(env: &Env, new_interval: u64) {
validate_interval(env, new_interval);
}
pub fn validate_interval(env: &Env, interval: u64) {
if interval == 0 {
env.panic_with_error(ContractError::IntervalMustBePositive);
}
if interval < crate::min_interval::get_min_interval(env) {
env.panic_with_error(ContractError::IntervalTooShort);
}
}
#[allow(dead_code)]
pub fn require_positive_interval(env: &Env, interval: u64) {
if interval == 0 {
env.panic_with_error(ContractError::IntervalMustBePositive);
}
}
#[allow(dead_code)]
pub fn require_active_subscription(env: &Env, active: bool) {
if !active {
env.panic_with_error(ContractError::SubscriptionInactive);
}
}
#[allow(dead_code)]
pub fn require_charge_interval_elapsed(env: &Env, now: u64, last_charged: u64, interval: u64) {
if now < last_charged + interval {
env.panic_with_error(ContractError::IntervalNotElapsed);
}
}