forked from SO4-Markets/contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
165 lines (136 loc) · 6.03 KB
/
Copy pathlib.rs
File metadata and controls
165 lines (136 loc) · 6.03 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
//! Fee handler — claims and distributes protocol fees accumulated in the pool.
//! Mirrors GMX's FeeHandler.sol.
#![no_std]
#![allow(dependency_on_unit_never_type_fallback)]
use soroban_sdk::{
contract, contracterror, contractevent, contractimpl, contracttype, panic_with_error,
Address, BytesN, Env,
};
use gmx_keys::{
roles,
claimable_fee_amount_key, claimable_funding_amount_key,
};
// ─── Storage keys ─────────────────────────────────────────────────────────────
#[contracttype]
enum InstanceKey {
Initialized,
Admin,
RoleStore,
DataStore,
}
// ─── Errors ───────────────────────────────────────────────────────────────────
#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
pub enum Error {
AlreadyInitialized = 1,
Unauthorized = 3,
NothingToClaim = 4,
}
// ─── External clients ─────────────────────────────────────────────────────────
#[allow(dead_code)]
#[soroban_sdk::contractclient(name = "RoleStoreClient")]
trait IRoleStore {
fn has_role(env: Env, account: Address, role: BytesN<32>) -> bool;
}
#[allow(dead_code)]
#[soroban_sdk::contractclient(name = "DataStoreClient")]
trait IDataStore {
fn get_u128(env: Env, key: BytesN<32>) -> u128;
fn set_u128(env: Env, caller: Address, key: BytesN<32>, value: u128) -> u128;
fn apply_delta_to_u128(env: Env, caller: Address, key: BytesN<32>, delta: i128) -> u128;
}
#[allow(dead_code)]
#[soroban_sdk::contractclient(name = "MarketTokenClient")]
trait IMarketToken {
fn withdraw_from_pool(env: Env, caller: Address, pool_token: Address, receiver: Address, amount: i128);
}
// ─── Events ───────────────────────────────────────────────────────────────────
#[contractevent(topics = ["fee_clm"])]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FeeClaimed {
pub market: Address,
pub token: Address,
pub amount: u128,
pub receiver: Address,
}
#[contractevent(topics = ["fnd_clm"])]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FundingFeeClaimed {
pub account: Address,
pub market: Address,
pub token: Address,
pub amount: u128,
}
// ─── Contract ─────────────────────────────────────────────────────────────────
#[contract]
pub struct FeeHandler;
#[contractimpl]
impl FeeHandler {
pub fn initialize(env: Env, admin: Address, role_store: Address, data_store: Address) {
admin.require_auth();
if env.storage().instance().has(&InstanceKey::Initialized) {
panic_with_error!(&env, Error::AlreadyInitialized);
}
env.storage().instance().set(&InstanceKey::Initialized, &true);
env.storage().instance().set(&InstanceKey::Admin, &admin);
env.storage().instance().set(&InstanceKey::RoleStore, &role_store);
env.storage().instance().set(&InstanceKey::DataStore, &data_store);
}
/// Return the accumulated protocol fee amount for a given market + token.
pub fn claimable_fees(env: Env, market: Address, token: Address) -> u128 {
let data_store: Address = env.storage().instance().get(&InstanceKey::DataStore).unwrap();
let key = claimable_fee_amount_key(&env, &market, &token);
DataStoreClient::new(&env, &data_store).get_u128(&key)
}
/// Sweep accumulated protocol fees for a market/token to `receiver`. FEE_KEEPER only.
pub fn claim_fees(
env: Env,
keeper: Address,
market: Address,
token: Address,
receiver: Address,
) -> u128 {
keeper.require_auth();
let role_store: Address = env.storage().instance().get(&InstanceKey::RoleStore).unwrap();
if !RoleStoreClient::new(&env, &role_store).has_role(&keeper, &roles::fee_keeper(&env)) {
panic_with_error!(&env, Error::Unauthorized);
}
let data_store: Address = env.storage().instance().get(&InstanceKey::DataStore).unwrap();
let ds = DataStoreClient::new(&env, &data_store);
let handler = env.current_contract_address();
let key = claimable_fee_amount_key(&env, &market, &token);
let amount = ds.get_u128(&key);
if amount == 0 {
panic_with_error!(&env, Error::NothingToClaim);
}
ds.set_u128(&handler, &key, &0u128);
// Transfer from market_token pool to receiver
MarketTokenClient::new(&env, &market)
.withdraw_from_pool(&handler, &token, &receiver, &(amount as i128));
env.events().publish_event(&FeeClaimed { market, token, amount, receiver });
amount
}
/// Claim funding fees earned by a position account. Anyone can call for their own account.
pub fn claim_funding_fees(
env: Env,
account: Address,
market: Address,
token: Address,
) -> u128 {
account.require_auth();
let data_store: Address = env.storage().instance().get(&InstanceKey::DataStore).unwrap();
let ds = DataStoreClient::new(&env, &data_store);
let handler = env.current_contract_address();
let key = claimable_funding_amount_key(&env, &market, &token, &account);
let amount = ds.get_u128(&key);
if amount == 0 {
return 0;
}
ds.set_u128(&handler, &key, &0u128);
MarketTokenClient::new(&env, &market)
.withdraw_from_pool(&handler, &token, &account, &(amount as i128));
env.events().publish_event(&FundingFeeClaimed { account, market, token, amount });
amount
}
}