forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.rs
More file actions
137 lines (117 loc) · 3.79 KB
/
Copy pathstorage.rs
File metadata and controls
137 lines (117 loc) · 3.79 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//! Storage for path-payment contract: pairs, rates, admin, swap router.
use soroban_sdk::{contracttype, Address, Env, Vec};
use crate::types::AssetPair;
#[contracttype]
#[derive(Clone)]
pub enum DataKey {
Admin,
Initialized,
/// Optional swap router contract: swap(from, to, amount_in) -> amount_out
SwapRouter,
/// Registered pair (from_asset, to_asset) for path finding. Value unused.
Pair(Address, Address),
/// Conversion rate from_asset -> to_asset (amount_out per 1e7 amount_in)
Rate(Address, Address),
}
const LEDGER_TTL_PERSISTENT: u32 = 31_536_000;
const LEDGER_TTL_THRESHOLD: u32 = 86_400;
#[allow(dead_code)]
pub fn has_admin(env: &Env) -> bool {
env.storage().persistent().has(&DataKey::Admin)
}
pub fn get_admin(env: &Env) -> Address {
env.storage()
.persistent()
.get(&DataKey::Admin)
.expect("admin not set")
}
pub fn set_admin(env: &Env, admin: &Address) {
env.storage().persistent().set(&DataKey::Admin, admin);
env.storage().persistent().extend_ttl(
&DataKey::Admin,
LEDGER_TTL_THRESHOLD,
LEDGER_TTL_PERSISTENT,
);
}
pub fn set_initialized(env: &Env) {
env.storage().persistent().set(&DataKey::Initialized, &true);
env.storage().persistent().extend_ttl(
&DataKey::Initialized,
LEDGER_TTL_THRESHOLD,
LEDGER_TTL_PERSISTENT,
);
}
pub fn is_initialized(env: &Env) -> bool {
env.storage().persistent().has(&DataKey::Initialized)
}
pub fn get_swap_router(env: &Env) -> Option<Address> {
env.storage().persistent().get(&DataKey::SwapRouter)
}
pub fn set_swap_router(env: &Env, router: &Address) {
env.storage().persistent().set(&DataKey::SwapRouter, router);
env.storage().persistent().extend_ttl(
&DataKey::SwapRouter,
LEDGER_TTL_THRESHOLD,
LEDGER_TTL_PERSISTENT,
);
}
#[allow(dead_code)]
pub fn clear_swap_router(env: &Env) {
env.storage().persistent().remove(&DataKey::SwapRouter);
}
/// Register a directed pair (from_asset -> to_asset) for path finding.
pub fn register_pair(env: &Env, from: &Address, to: &Address) {
let key = DataKey::Pair(from.clone(), to.clone());
env.storage().persistent().set(&key, &true);
env.storage()
.persistent()
.extend_ttl(&key, LEDGER_TTL_THRESHOLD, LEDGER_TTL_PERSISTENT);
}
#[allow(dead_code)]
pub fn has_pair(env: &Env, from: &Address, to: &Address) -> bool {
env.storage()
.persistent()
.has(&DataKey::Pair(from.clone(), to.clone()))
}
/// Set conversion rate: amount of to_asset per 1e7 units of from_asset.
pub fn set_rate(env: &Env, from: &Address, to: &Address, rate: i128) {
let key = DataKey::Rate(from.clone(), to.clone());
env.storage().persistent().set(&key, &rate);
env.storage()
.persistent()
.extend_ttl(&key, LEDGER_TTL_THRESHOLD, LEDGER_TTL_PERSISTENT);
}
pub fn get_rate(env: &Env, from: &Address, to: &Address) -> Option<i128> {
env.storage()
.persistent()
.get(&DataKey::Rate(from.clone(), to.clone()))
}
#[contracttype]
#[derive(Clone)]
pub enum PairListKey {
List,
}
pub fn get_pair_list(env: &Env) -> Vec<AssetPair> {
env.storage()
.persistent()
.get(&PairListKey::List)
.unwrap_or_else(|| Vec::new(env))
}
pub fn set_pair_list(env: &Env, pairs: &Vec<AssetPair>) {
env.storage().persistent().set(&PairListKey::List, pairs);
env.storage().persistent().extend_ttl(
&PairListKey::List,
LEDGER_TTL_THRESHOLD,
LEDGER_TTL_PERSISTENT,
);
}
/// Append a pair to the list and register it.
pub fn add_pair(env: &Env, from: &Address, to: &Address) {
register_pair(env, from, to);
let mut list = get_pair_list(env);
list.push_back(AssetPair {
from: from.clone(),
to: to.clone(),
});
set_pair_list(env, &list);
}