forked from Vero-protocol/vero-core-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathburn.rs
More file actions
78 lines (65 loc) · 2.34 KB
/
Copy pathburn.rs
File metadata and controls
78 lines (65 loc) · 2.34 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
68
69
70
71
72
73
74
75
76
77
78
//! Burn module — zero-address and invalid-amount guards.
use crate::event_struct::{ACT_BURN_SAFE, MOD_BURN};
use crate::event_utils::{publish_event, zero_hash};
use soroban_sdk::{contracterror, panic_with_error, Address, Env, String};
#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub enum BurnError {
ZeroAddress = 1,
InvalidAmount = 2,
}
const ZERO_ADDRESS: &str = "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF";
/// Panics when `to` is the Stellar zero address.
pub fn reject_zero_address(env: &Env, to: &Address) {
let zero = String::from_str(env, ZERO_ADDRESS);
if to.to_string() == zero {
panic_with_error!(env, BurnError::ZeroAddress);
}
}
fn amount_to_event_value(env: &Env, amount: i128) -> u64 {
if amount <= 0 || amount > u64::MAX as i128 {
panic_with_error!(env, BurnError::InvalidAmount);
}
amount as u64
}
/// Burn-safe transfer wrapper. Validates recipient/amount before emitting.
pub fn burn_to(env: &Env, to: &Address, amount: i128) {
crate::circuit_breaker::assert_closed(env);
reject_zero_address(env, to);
let value = amount_to_event_value(env, amount);
publish_event(env, MOD_BURN | ACT_BURN_SAFE, value, zero_hash(env));
}
#[cfg(test)]
mod tests {
use super::*;
use soroban_sdk::{contract, contractimpl, testutils::Address as _, Env};
#[contract]
pub struct TestContract;
#[contractimpl]
impl TestContract {}
#[test]
fn valid_address_passes() {
let env = Env::default();
let contract_id = env.register_contract(None, TestContract);
let addr = Address::generate(&env);
env.as_contract(&contract_id, || reject_zero_address(&env, &addr));
}
#[test]
#[should_panic]
fn zero_address_rejected() {
let env = Env::default();
let contract_id = env.register_contract(None, TestContract);
env.as_contract(&contract_id, || {
let zero = Address::from_string(&String::from_str(&env, ZERO_ADDRESS));
reject_zero_address(&env, &zero);
});
}
#[test]
#[should_panic]
fn invalid_amount_rejected() {
let env = Env::default();
let contract_id = env.register_contract(None, TestContract);
let addr = Address::generate(&env);
env.as_contract(&contract_id, || burn_to(&env, &addr, 0));
}
}