-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpool.cairo
524 lines (494 loc) · 23 KB
/
pool.cairo
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
#[starknet::contract]
pub mod Pool {
use core::serde::Serde;
use core::num::traits::zero::Zero;
use contracts::errors::{Error, assert_with_err, OptionAuxTrait};
use contracts::pool::{interface::PoolContractInfo, IPool, PoolMemberInfo, Events};
use contracts::utils::{compute_rewards_rounded_down, compute_commission_amount_rounded_up};
use core::option::OptionTrait;
use starknet::{ContractAddress, get_caller_address, get_contract_address, get_block_timestamp};
use openzeppelin::access::accesscontrol::AccessControlComponent;
use openzeppelin::introspection::src5::SRC5Component;
use openzeppelin::token::erc20::interface::{IERC20DispatcherTrait, IERC20Dispatcher};
use contracts::staking::interface::{IStakingPoolDispatcher, IStakingPoolDispatcherTrait};
use starknet::storage::Map;
use contracts_commons::components::roles::RolesComponent;
use RolesComponent::InternalTrait as RolesInternalTrait;
use contracts_commons::components::replaceability::ReplaceabilityComponent;
use AccessControlComponent::InternalTrait as AccessControlInternalTrait;
use contracts::utils::CheckedIERC20DispatcherTrait;
component!(path: ReplaceabilityComponent, storage: replaceability, event: ReplaceabilityEvent);
component!(path: RolesComponent, storage: roles, event: RolesEvent);
component!(path: AccessControlComponent, storage: accesscontrol, event: AccessControlEvent);
component!(path: SRC5Component, storage: src5, event: SRC5Event);
#[abi(embed_v0)]
impl ReplaceabilityImpl =
ReplaceabilityComponent::ReplaceabilityImpl<ContractState>;
#[abi(embed_v0)]
impl RolesImpl = RolesComponent::RolesImpl<ContractState>;
#[derive(Debug, Drop, Serde, Copy)]
pub struct SwitchPoolData {
pub pool_member: ContractAddress,
pub reward_address: ContractAddress,
}
#[storage]
struct Storage {
#[substorage(v0)]
replaceability: ReplaceabilityComponent::Storage,
#[substorage(v0)]
accesscontrol: AccessControlComponent::Storage,
#[substorage(v0)]
src5: SRC5Component::Storage,
#[substorage(v0)]
roles: RolesComponent::Storage,
staker_address: ContractAddress,
pool_member_info: Map<ContractAddress, Option<PoolMemberInfo>>,
final_staker_index: Option<u64>,
staking_pool_dispatcher: IStakingPoolDispatcher,
erc20_dispatcher: IERC20Dispatcher,
commission: u16,
}
#[event]
#[derive(Drop, starknet::Event)]
pub enum Event {
#[flat]
ReplaceabilityEvent: ReplaceabilityComponent::Event,
#[flat]
AccessControlEvent: AccessControlComponent::Event,
#[flat]
SRC5Event: SRC5Component::Event,
#[flat]
RolesEvent: RolesComponent::Event,
PoolMemberExitIntent: Events::PoolMemberExitIntent,
DelegationPoolMemberBalanceChanged: Events::DelegationPoolMemberBalanceChanged,
PoolMemberRewardAddressChanged: Events::PoolMemberRewardAddressChanged,
FinalIndexSet: Events::FinalIndexSet,
PoolMemberRewardClaimed: Events::PoolMemberRewardClaimed,
DeletePoolMember: Events::DeletePoolMember,
NewPoolMember: Events::NewPoolMember,
}
#[constructor]
pub fn constructor(
ref self: ContractState,
staker_address: ContractAddress,
staking_contract: ContractAddress,
token_address: ContractAddress,
commission: u16
) {
self.accesscontrol.initializer();
self.roles.initializer();
self.replaceability.upgrade_delay.write(Zero::zero());
self.staker_address.write(staker_address);
self
.staking_pool_dispatcher
.write(IStakingPoolDispatcher { contract_address: staking_contract });
self.erc20_dispatcher.write(IERC20Dispatcher { contract_address: token_address });
self.commission.write(commission);
}
#[abi(embed_v0)]
impl PoolImpl of IPool<ContractState> {
fn enter_delegation_pool(
ref self: ContractState, reward_address: ContractAddress, amount: u128
) -> bool {
// This line was added to prevent the compiler from doing certain optimizations.
core::internal::revoke_ap_tracking();
self.assert_staker_is_active();
let pool_member = get_caller_address();
assert_with_err(
self.pool_member_info.read(pool_member).is_none(), Error::POOL_MEMBER_EXISTS
);
assert_with_err(amount.is_non_zero(), Error::AMOUNT_IS_ZERO);
let staker_address = self.staker_address.read();
let staking_pool_dispatcher = self.staking_pool_dispatcher.read();
let erc20_dispatcher = self.erc20_dispatcher.read();
let self_contract = get_contract_address();
erc20_dispatcher
.checked_transfer_from(
sender: pool_member, recipient: self_contract, amount: amount.into()
);
erc20_dispatcher
.approve(spender: staking_pool_dispatcher.contract_address, amount: amount.into());
let (_, updated_index) = staking_pool_dispatcher
.add_stake_from_pool(:staker_address, :amount);
self
.pool_member_info
.write(
pool_member,
Option::Some(
PoolMemberInfo {
reward_address: reward_address,
amount: amount,
index: updated_index,
unclaimed_rewards: Zero::zero(),
unpool_time: Option::None,
unpool_amount: Zero::zero(),
}
)
);
self
.emit(
Events::NewPoolMember { pool_member, staker_address, reward_address, amount }
);
self
.emit(
Events::DelegationPoolMemberBalanceChanged {
pool_member, old_delegated_stake: Zero::zero(), new_delegated_stake: amount
}
);
true
}
fn add_to_delegation_pool(
ref self: ContractState, pool_member: ContractAddress, amount: u128
) -> u128 {
self.assert_staker_is_active();
let mut pool_member_info = self.get_pool_member_info(:pool_member);
// This line was added to prevent the compiler from doing certain optimizations.
core::internal::revoke_ap_tracking();
let caller_address = get_caller_address();
assert_with_err(
caller_address == pool_member || caller_address == pool_member_info.reward_address,
Error::CALLER_CANNOT_ADD_TO_POOL
);
let staking_pool_dispatcher = self.staking_pool_dispatcher.read();
let erc20_dispatcher = self.erc20_dispatcher.read();
let self_contract = get_contract_address();
erc20_dispatcher
.checked_transfer_from(
sender: pool_member, recipient: self_contract, amount: amount.into()
);
erc20_dispatcher
.approve(spender: staking_pool_dispatcher.contract_address, amount: amount.into());
let (_, updated_index) = staking_pool_dispatcher
.add_stake_from_pool(staker_address: self.staker_address.read(), :amount);
self.calculate_rewards(ref :pool_member_info, :updated_index);
let old_delegated_stake = pool_member_info.amount;
pool_member_info.amount += amount;
self.pool_member_info.write(pool_member, Option::Some(pool_member_info));
self
.emit(
Events::DelegationPoolMemberBalanceChanged {
pool_member,
old_delegated_stake,
new_delegated_stake: pool_member_info.amount
}
);
pool_member_info.amount
}
fn exit_delegation_pool_intent(ref self: ContractState, amount: u128) {
let pool_member = get_caller_address();
let mut pool_member_info = self.get_pool_member_info(:pool_member);
self.update_index_and_calculate_rewards(ref :pool_member_info);
let total_amount = pool_member_info.amount + pool_member_info.unpool_amount;
assert_with_err(amount <= total_amount, Error::AMOUNT_TOO_HIGH);
let unpool_time = self.undelegate_from_staking_contract_intent(:pool_member, :amount);
if amount.is_zero() {
pool_member_info.unpool_time = Option::None;
} else {
pool_member_info.unpool_time = Option::Some(unpool_time);
}
pool_member_info.unpool_amount = amount;
pool_member_info.amount = total_amount - amount;
self.pool_member_info.write(pool_member, Option::Some(pool_member_info));
self
.emit(
Events::PoolMemberExitIntent {
pool_member, exit_timestamp: unpool_time, amount
}
);
}
fn exit_delegation_pool_action(
ref self: ContractState, pool_member: ContractAddress
) -> u128 {
let mut pool_member_info = self.get_pool_member_info(:pool_member);
let unpool_time = pool_member_info
.unpool_time
.expect_with_err(Error::MISSING_UNDELEGATE_INTENT);
assert_with_err(
get_block_timestamp() >= unpool_time, Error::INTENT_WINDOW_NOT_FINISHED
);
// Clear intent and receive funds from staking contract if needed.
let staking_pool_dispatcher = self.staking_pool_dispatcher.read();
staking_pool_dispatcher
.remove_from_delegation_pool_action(identifier: pool_member.into());
let erc20_dispatcher = self.erc20_dispatcher.read();
// Claim rewards.
self
.send_rewards_to_pool_member(
:pool_member,
reward_address: pool_member_info.reward_address,
amount: pool_member_info.unclaimed_rewards,
:erc20_dispatcher
);
// Transfer delegated amount to the pool member.
let unpool_amount = pool_member_info.unpool_amount;
pool_member_info.unpool_amount = Zero::zero();
erc20_dispatcher.checked_transfer(recipient: pool_member, amount: unpool_amount.into());
if pool_member_info.amount.is_zero() {
self.remove_pool_member(:pool_member);
} else {
pool_member_info.unpool_time = Option::None;
self.pool_member_info.write(pool_member, Option::Some(pool_member_info));
}
unpool_amount
}
fn claim_rewards(ref self: ContractState, pool_member: ContractAddress) -> u128 {
let mut pool_member_info = self.get_pool_member_info(:pool_member);
let caller_address = get_caller_address();
let reward_address = pool_member_info.reward_address;
assert_with_err(
caller_address == pool_member || caller_address == reward_address,
Error::POOL_CLAIM_REWARDS_FROM_UNAUTHORIZED_ADDRESS
);
self.update_index_and_calculate_rewards(ref :pool_member_info);
let rewards = pool_member_info.unclaimed_rewards;
let erc20_dispatcher = self.erc20_dispatcher.read();
self
.send_rewards_to_pool_member(
:pool_member, :reward_address, amount: rewards, :erc20_dispatcher
);
pool_member_info.unclaimed_rewards = Zero::zero();
self.pool_member_info.write(pool_member, Option::Some(pool_member_info));
rewards
}
fn switch_delegation_pool(
ref self: ContractState,
to_staker: ContractAddress,
to_pool: ContractAddress,
amount: u128
) -> u128 {
assert_with_err(amount.is_non_zero(), Error::AMOUNT_IS_ZERO);
let pool_member = get_caller_address();
let mut pool_member_info = self.get_pool_member_info(:pool_member);
assert_with_err(
pool_member_info.unpool_time.is_some(), Error::MISSING_UNDELEGATE_INTENT
);
assert_with_err(pool_member_info.unpool_amount >= amount, Error::AMOUNT_TOO_HIGH);
let switch_pool_data = SwitchPoolData {
pool_member, reward_address: pool_member_info.reward_address
};
let mut serialized_data = array![];
switch_pool_data.serialize(ref output: serialized_data);
pool_member_info.unpool_amount -= amount;
if pool_member_info.unpool_amount.is_zero() && pool_member_info.amount.is_zero() {
// Claim rewards.
let erc20_dispatcher = self.erc20_dispatcher.read();
erc20_dispatcher
.checked_transfer(
recipient: pool_member_info.reward_address,
amount: pool_member_info.unclaimed_rewards.into()
);
self.remove_pool_member(:pool_member);
} else {
// One of pool_member_info.unpool_amount or pool_member_info.amount is non-zero.
if pool_member_info.unpool_amount.is_zero() {
pool_member_info.unpool_time = Option::None;
}
self.pool_member_info.write(pool_member, Option::Some(pool_member_info));
}
// TODO: emit event
self
.staking_pool_dispatcher
.read()
.switch_staking_delegation_pool(
:to_staker,
:to_pool,
switched_amount: amount,
data: serialized_data.span(),
identifier: pool_member.into()
);
pool_member_info.unpool_amount
}
fn enter_delegation_pool_from_staking_contract(
ref self: ContractState, amount: u128, index: u64, data: Span<felt252>
) -> bool {
assert_with_err(amount.is_non_zero(), Error::AMOUNT_IS_ZERO);
assert_with_err(
get_caller_address() == self.staking_pool_dispatcher.read().contract_address,
Error::CALLER_IS_NOT_STAKING_CONTRACT
);
let mut serialized = data;
let switch_pool_data: SwitchPoolData = Serde::deserialize(ref :serialized)
.expect_with_err(Error::SWITCH_POOL_DATA_DESERIALIZATION_FAILED);
let pool_member = switch_pool_data.pool_member;
let pool_member_info = match self.pool_member_info.read(pool_member) {
Option::Some(mut pool_member_info) => {
assert_with_err(
pool_member_info.reward_address == switch_pool_data.reward_address,
Error::REWARD_ADDRESS_MISMATCH
);
self.calculate_rewards(ref :pool_member_info, updated_index: index);
pool_member_info.amount += amount;
pool_member_info
},
Option::None => {
PoolMemberInfo {
reward_address: switch_pool_data.reward_address,
amount,
index,
unclaimed_rewards: Zero::zero(),
unpool_time: Option::None,
unpool_amount: Zero::zero(),
}
}
};
self.pool_member_info.write(pool_member, Option::Some(pool_member_info));
self
.emit(
Events::DelegationPoolMemberBalanceChanged {
pool_member,
old_delegated_stake: pool_member_info.amount - amount,
new_delegated_stake: pool_member_info.amount
}
);
true
}
fn set_final_staker_index(ref self: ContractState, final_staker_index: u64) {
let staking_contract = get_caller_address();
assert_with_err(
staking_contract == self.staking_pool_dispatcher.read().contract_address,
Error::CALLER_IS_NOT_STAKING_CONTRACT
);
assert_with_err(
self.final_staker_index.read().is_none(), Error::FINAL_STAKER_INDEX_ALREADY_SET
);
self.final_staker_index.write(Option::Some(final_staker_index));
self
.emit(
Events::FinalIndexSet {
staker_address: self.staker_address.read(), final_staker_index
}
);
}
fn change_reward_address(ref self: ContractState, reward_address: ContractAddress) -> bool {
let pool_member = get_caller_address();
let mut pool_member_info = self.get_pool_member_info(:pool_member);
let old_address = pool_member_info.reward_address;
pool_member_info.reward_address = reward_address;
self.pool_member_info.write(pool_member, Option::Some(pool_member_info));
self
.emit(
Events::PoolMemberRewardAddressChanged {
pool_member, new_address: reward_address, old_address
}
);
true
}
fn state_of(self: @ContractState, pool_member: ContractAddress) -> PoolMemberInfo {
self.get_pool_member_info(:pool_member)
}
fn contract_parameters(self: @ContractState) -> PoolContractInfo {
PoolContractInfo {
staker_address: self.staker_address.read(),
final_staker_index: self.final_staker_index.read(),
staking_contract: self.staking_pool_dispatcher.read().contract_address,
token_address: self.erc20_dispatcher.read().contract_address,
commission: self.commission.read(),
}
}
fn update_commission(ref self: ContractState, commission: u16) -> bool {
assert_with_err(
get_caller_address() == self.staking_pool_dispatcher.read().contract_address,
Error::CALLER_IS_NOT_STAKING_CONTRACT
);
assert_with_err(
commission <= self.commission.read(), Error::CANNOT_INCREASE_COMMISSION
);
self.commission.write(commission);
true
}
}
#[generate_trait]
pub(crate) impl InternalPoolFunctions of InternalPoolFunctionsTrait {
fn get_pool_member_info(
self: @ContractState, pool_member: ContractAddress
) -> PoolMemberInfo {
self
.pool_member_info
.read(pool_member)
.expect_with_err(Error::POOL_MEMBER_DOES_NOT_EXIST)
}
fn remove_pool_member(ref self: ContractState, pool_member: ContractAddress) {
let pool_member_info = self.get_pool_member_info(:pool_member);
self.pool_member_info.write(pool_member, Option::None);
self
.emit(
Events::DeletePoolMember {
pool_member, reward_address: pool_member_info.reward_address
}
);
}
fn receive_index_and_funds_from_staker(self: @ContractState) -> u64 {
if let Option::Some(final_index) = self.final_staker_index.read() {
// If the staker is inactive, the staker already pushed index and funds.
return final_index;
}
let staking_pool_dispatcher = self.staking_pool_dispatcher.read();
staking_pool_dispatcher.claim_delegation_pool_rewards(self.staker_address.read())
}
/// Calculates the rewards for a pool member.
///
/// The caller for this function should validate that the pool member exists.
///
/// rewards formula:
/// $$ rewards = (staker\_index-pooler\_index) * pooler\_amount $$
///
/// Fields that are changed in pool_member_info:
/// - unclaimed_rewards
/// - index
fn calculate_rewards(
ref self: ContractState, ref pool_member_info: PoolMemberInfo, updated_index: u64
) -> bool {
let interest: u64 = updated_index - pool_member_info.index;
pool_member_info.index = updated_index;
let rewards_including_commission = compute_rewards_rounded_down(
amount: pool_member_info.amount, :interest
);
let commission_amount = compute_commission_amount_rounded_up(
:rewards_including_commission, commission: self.commission.read()
);
let rewards = rewards_including_commission - commission_amount;
pool_member_info.unclaimed_rewards += rewards;
true
}
fn update_index_and_calculate_rewards(
ref self: ContractState, ref pool_member_info: PoolMemberInfo
) -> bool {
let updated_index = self.receive_index_and_funds_from_staker();
self.calculate_rewards(ref :pool_member_info, :updated_index)
}
fn assert_staker_is_active(self: @ContractState) {
assert_with_err(self.final_staker_index.read().is_none(), Error::STAKER_INACTIVE);
}
fn is_staker_active(self: @ContractState) -> bool {
self.final_staker_index.read().is_none()
}
fn undelegate_from_staking_contract_intent(
self: @ContractState, pool_member: ContractAddress, amount: u128
) -> u64 {
if !self.is_staker_active() {
// Don't allow intent if an intent is already in progress and the staker is erased.
assert_with_err(
self.get_pool_member_info(:pool_member).unpool_time.is_none(),
Error::UNDELEGATE_IN_PROGRESS
);
return get_block_timestamp();
}
let staking_pool_dispatcher = self.staking_pool_dispatcher.read();
let staker_address = self.staker_address.read();
staking_pool_dispatcher
.remove_from_delegation_pool_intent(
:staker_address, identifier: pool_member.into(), :amount
)
}
fn send_rewards_to_pool_member(
ref self: ContractState,
pool_member: ContractAddress,
reward_address: ContractAddress,
amount: u128,
erc20_dispatcher: IERC20Dispatcher
) {
erc20_dispatcher.checked_transfer(recipient: reward_address, amount: amount.into());
self.emit(Events::PoolMemberRewardClaimed { pool_member, reward_address, amount });
}
}
}