forked from BETAIL-BOYS/TradeFlow-Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
189 lines (157 loc) · 6.92 KB
/
Copy pathlib.rs
File metadata and controls
189 lines (157 loc) · 6.92 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#![no_std]
use soroban_sdk::{contract, contractimpl, contracttype, symbol_short, Address, Bytes, BytesN, Env, Map, Symbol, vec};
mod tests;
#[contracttype]
#[derive(Clone)]
pub struct Pool {
pub address: Address,
pub token_a: Address,
pub token_b: Address,
pub fee_tier: u32, // Fee tier in basis points (5, 30, or 100)
pub paused: bool,
}
#[contracttype]
pub enum DataKey {
FeeTo, // The address that receives protocol fees
Pools, // Map of (TokenA, TokenB) -> Pool
PoolWasmHash, // The Wasm hash of the Pool contract to deploy
Admin, // The address of the factory admin
}
#[contract]
pub struct FactoryContract;
#[contractimpl]
impl FactoryContract {
/// Initializes the factory contract. This can only be called once.
///
/// # Arguments
/// * `env` - The Soroban environment.
/// * `admin` - The address of the factory admin.
/// * `fee_to` - The address where protocol fees will be sent.
/// * `pool_wasm_hash` - The WASM hash of the liquidity pool contract code.
///
/// # Panics
/// If the contract has already been initialized.
pub fn initialize_factory(env: Env, admin: Address, fee_to: Address, pool_wasm_hash: BytesN<32>) {
if env.storage().instance().has(&DataKey::FeeTo) {
panic!("Factory has already been initialized");
}
env.storage().instance().set(&DataKey::FeeTo, &fee_to);
env.storage().instance().set(&DataKey::Admin, &admin);
env.storage().instance().set(&DataKey::PoolWasmHash, &pool_wasm_hash);
}
/// Helper function to sort two token addresses, creating a canonical pair
/// to ensure that get_pool(A, B) == get_pool(B, A).
fn sort_tokens(token_a: Address, token_b: Address) -> (Address, Address) {
if token_a < token_b {
(token_a, token_b)
} else {
(token_b, token_a)
}
}
/// Returns the address of the liquidity pool for a given pair of tokens.
pub fn get_pool(env: Env, token_a: Address, token_b: Address) -> Option<Address> {
let sorted_tokens = Self::sort_tokens(token_a, token_b);
let pools: Map<(Address, Address), Pool> =
env.storage().instance().get(&DataKey::Pools).unwrap_or_else(|| Map::new(&env));
pools.get(sorted_tokens).map(|p| p.address)
}
/// Deploys a new liquidity pool for the given token pair with a specific fee tier.
///
/// # Arguments
/// * `env` - The Soroban environment.
/// * `token_a` - The first token address.
/// * `token_b` - The second token address.
/// * `fee_tier` - The fee tier in basis points (5, 30, or 100).
///
/// # Returns
/// The address of the newly deployed pool.
///
/// # Panics
/// If tokens are the same, pool already exists, or fee_tier is invalid.
pub fn create_pool(env: Env, token_a: Address, token_b: Address, fee_tier: u32) -> Address {
if token_a == token_b {
panic!("Tokens must be different");
}
// Validate fee tier - only allow 5, 30, or 100 basis points
if fee_tier != 5 && fee_tier != 30 && fee_tier != 100 {
panic!("Invalid fee tier. Only 5, 30, or 100 basis points are supported");
}
let (token_0, token_1) = Self::sort_tokens(token_a, token_b);
let mut pools: Map<(Address, Address), Pool> =
env.storage().instance().get(&DataKey::Pools).unwrap_or_else(|| Map::new(&env));
if pools.contains_key((token_0.clone(), token_1.clone())) {
panic!("Pool already exists");
}
// Get the contract code hash
let wasm_hash: BytesN<32> = env.storage().instance().get(&DataKey::PoolWasmHash).expect("Not initialized");
// Generate a deterministic salt based on the token pair
// salt = sha256(token_0 + token_1)
let mut salt_data = Bytes::new(&env);
salt_data.append(&token_0.to_xdr(&env));
salt_data.append(&token_1.to_xdr(&env));
let salt = env.crypto().sha256(&salt_data);
// Deploy the new pool contract
let pool_address = env.deployer().with_current_contract(salt).deploy(wasm_hash);
// Initialize the pool with the fee tier
let init_args = vec![&env,
env.current_contract_address().into_val(&env), // Factory as admin
token_0.clone().into_val(&env),
token_1.clone().into_val(&env),
fee_tier.into_val(&env)
];
env.invoke_contract::<()>(&pool_address, &Symbol::new(&env, "init"), init_args);
// Store the new pool
let pool = Pool {
address: pool_address.clone(),
token_a: token_0.clone(),
token_b: token_1.clone(),
fee_tier,
paused: false,
};
pools.set((token_0, token_1), pool);
env.storage().instance().set(&DataKey::Pools, &pools);
pool_address
}
/// Sets the recipient of the protocol fees.
///
/// # Arguments
/// * `env` - The Soroban environment.
/// * `fee_to` - The new address to receive fees.
pub fn set_fee_recipient(env: Env, fee_to: Address) {
let admin: Address = env.storage().instance().get(&DataKey::Admin).expect("Not initialized");
admin.require_auth();
let old_fee_to: Address = env.storage().instance().get(&DataKey::FeeTo).unwrap();
env.storage().instance().set(&DataKey::FeeTo, &fee_to);
// Emit event: ("Admin", "SetFeeTo", old_fee_to, new_fee_to)
env.events().publish(
(symbol_short!("Admin"), symbol_short!("SetFeeTo")),
(old_fee_to, fee_to)
);
}
/// Toggles the status of a specific pool.
///
/// # Arguments
/// * `env` - The Soroban environment.
/// * `token_a` - The first token of the pair.
/// * `token_b` - The second token of the pair.
/// * `status` - The new status (e.g., 0 = Paused, 1 = Active).
pub fn toggle_pool_status(env: Env, token_a: Address, token_b: Address) {
let admin: Address = env.storage().instance().get(&DataKey::Admin).expect("Not initialized");
admin.require_auth();
let sorted_tokens = Self::sort_tokens(token_a.clone(), token_b.clone());
let mut pools: Map<(Address, Address), Pool> =
env.storage().instance().get(&DataKey::Pools).unwrap_or_else(|| Map::new(&env));
let mut pool = pools.get(sorted_tokens.clone()).expect("Pool does not exist");
// Toggle the paused state
pool.paused = !pool.paused;
// Invoke the pool contract to update its internal pause state
let args = vec![&env, pool.paused.into()];
env.invoke_contract::<()>(&pool.address, &Symbol::new(&env, "set_paused"), args);
pools.set(sorted_tokens, pool.clone());
env.storage().instance().set(&DataKey::Pools, &pools);
env.events().publish(
(symbol_short!("Admin"), symbol_short!("PoolStatus"), token_a, token_b),
pool.paused
);
}
}