Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backstop: lowered withdrawal queue - Emitter: Removed drop delay #186

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions backstop/src/backstop/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ impl UserBalance {

// user has enough tokens to withdrawal, add Q4W
// TODO: Consider capping how many active Q4Ws a user can have
let thirty_days_in_sec = 30 * 24 * 60 * 60;
let twentyone_days_in_sec = 21 * 24 * 60 * 60;
let new_q4w = Q4W {
amount: to_q,
exp: e.ledger().timestamp() + thirty_days_in_sec,
exp: e.ledger().timestamp() + twentyone_days_in_sec,
};
self.q4w.push_back(new_q4w.clone());
}
Expand Down Expand Up @@ -168,7 +168,7 @@ mod tests {
&e,
Q4W {
amount: to_queue,
exp: 10000 + 30 * 24 * 60 * 60,
exp: 10000 + 21 * 24 * 60 * 60,
},
],
);
Expand Down Expand Up @@ -205,7 +205,7 @@ mod tests {
user.queue_shares_for_withdrawal(&e, to_queue);
cur_q4w.push_back(Q4W {
amount: to_queue,
exp: 11000000 + 30 * 24 * 60 * 60,
exp: 11000000 + 21 * 24 * 60 * 60,
});
assert_eq_vec_q4w(&user.q4w, &cur_q4w);
}
Expand Down
8 changes: 4 additions & 4 deletions backstop/src/backstop/withdrawal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ mod tests {
&e,
Q4W {
amount: 42_0000000,
exp: 10000 + 30 * 24 * 60 * 60,
exp: 10000 + 21 * 24 * 60 * 60,
},
];
assert_eq_vec_q4w(&new_user_balance.q4w, &expected_q4w);
Expand Down Expand Up @@ -235,7 +235,7 @@ mod tests {
&e,
Q4W {
amount: 35_0000000,
exp: 10000 + 30 * 24 * 60 * 60,
exp: 10000 + 21 * 24 * 60 * 60,
},
];
assert_eq_vec_q4w(&new_user_balance.q4w, &expected_q4w);
Expand Down Expand Up @@ -335,7 +335,7 @@ mod tests {
e.ledger().set(LedgerInfo {
protocol_version: 20,
sequence_number: 200,
timestamp: 10000 + 30 * 24 * 60 * 60 + 1,
timestamp: 10000 + 21 * 24 * 60 * 60 + 1,
network_id: Default::default(),
base_reserve: 10,
min_temp_entry_ttl: 10,
Expand Down Expand Up @@ -401,7 +401,7 @@ mod tests {
e.ledger().set(LedgerInfo {
protocol_version: 20,
sequence_number: 200,
timestamp: 10000 + 30 * 24 * 60 * 60 + 1,
timestamp: 10000 + 21 * 24 * 60 * 60 + 1,
network_id: Default::default(),
base_reserve: 10,
min_temp_entry_ttl: 10,
Expand Down
14 changes: 0 additions & 14 deletions emitter/src/backstop_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ pub fn execute_swap_backstop(e: &Env) -> Swap {
emitter::execute_distribute(e, &backstop);

// swap backstop and token
storage::set_last_fork(e, e.ledger().sequence());
storage::del_queued_swap(e);
storage::set_backstop(e, &swap.new_backstop);
storage::set_backstop_token(e, &swap.new_backstop_token);
Expand Down Expand Up @@ -135,14 +134,12 @@ mod tests {
storage::set_backstop(&e, &backstop);
storage::set_backstop_token(&e, &backstop_token);
storage::set_drop_status(&e, &backstop);
storage::set_last_fork(&e, 123);

execute_queue_swap_backstop(&e, &new_backstop, &new_backstop_token);

// verify no swap occurred
assert_eq!(storage::get_backstop(&e), backstop);
assert_eq!(storage::get_backstop_token(&e), backstop_token);
assert_eq!(storage::get_last_fork(&e), 123);

// verify swap is queued
let swap = storage::get_queued_swap(&e);
Expand Down Expand Up @@ -188,7 +185,6 @@ mod tests {
storage::set_backstop(&e, &backstop);
storage::set_backstop_token(&e, &backstop_token);
storage::set_drop_status(&e, &backstop);
storage::set_last_fork(&e, 123);

execute_queue_swap_backstop(&e, &new_backstop, &new_backstop_token);
assert!(false); // should panic
Expand Down Expand Up @@ -235,7 +231,6 @@ mod tests {
storage::set_backstop(&e, &backstop);
storage::set_backstop_token(&e, &backstop_token);
storage::set_drop_status(&e, &backstop);
storage::set_last_fork(&e, 123);
storage::set_queued_swap(&e, &swap);

execute_queue_swap_backstop(&e, &new_backstop, &new_backstop_token);
Expand Down Expand Up @@ -284,15 +279,13 @@ mod tests {
storage::set_backstop(&e, &backstop);
storage::set_backstop_token(&e, &backstop_token);
storage::set_drop_status(&e, &backstop);
storage::set_last_fork(&e, 123);
storage::set_queued_swap(&e, &swap);

execute_cancel_swap_backstop(&e);

// verify no swap occurred
assert_eq!(storage::get_backstop(&e), backstop);
assert_eq!(storage::get_backstop_token(&e), backstop_token);
assert_eq!(storage::get_last_fork(&e), 123);

// verify swap is removed
let swap = storage::get_queued_swap(&e);
Expand Down Expand Up @@ -340,7 +333,6 @@ mod tests {
storage::set_backstop(&e, &backstop);
storage::set_backstop_token(&e, &backstop_token);
storage::set_drop_status(&e, &backstop);
storage::set_last_fork(&e, 123);
storage::set_queued_swap(&e, &swap);

execute_cancel_swap_backstop(&e);
Expand Down Expand Up @@ -381,7 +373,6 @@ mod tests {
storage::set_backstop(&e, &backstop);
storage::set_backstop_token(&e, &backstop_token);
storage::set_drop_status(&e, &backstop);
storage::set_last_fork(&e, 123);

execute_cancel_swap_backstop(&e);
assert!(false);
Expand Down Expand Up @@ -434,15 +425,13 @@ mod tests {
storage::set_backstop_token(&e, &backstop_token);
storage::set_blnd_token(&e, &blnd_token);
storage::set_drop_status(&e, &backstop);
storage::set_last_fork(&e, 123);
storage::set_queued_swap(&e, &swap);

execute_swap_backstop(&e);

// verify swap occurred
assert_eq!(storage::get_backstop(&e), new_backstop);
assert_eq!(storage::get_backstop_token(&e), new_backstop_token);
assert_eq!(storage::get_last_fork(&e), 500);

// verify swap is removed
let swap = storage::get_queued_swap(&e);
Expand Down Expand Up @@ -499,7 +488,6 @@ mod tests {
storage::set_backstop_token(&e, &backstop_token);
storage::set_blnd_token(&e, &blnd_token);
storage::set_drop_status(&e, &backstop);
storage::set_last_fork(&e, 123);
storage::set_queued_swap(&e, &swap);

execute_swap_backstop(&e);
Expand Down Expand Up @@ -543,7 +531,6 @@ mod tests {
storage::set_backstop_token(&e, &backstop_token);
storage::set_blnd_token(&e, &blnd_token);
storage::set_drop_status(&e, &backstop);
storage::set_last_fork(&e, 123);

execute_swap_backstop(&e);
assert!(false);
Expand Down Expand Up @@ -593,7 +580,6 @@ mod tests {
storage::set_backstop_token(&e, &backstop_token);
storage::set_blnd_token(&e, &blnd_token);
storage::set_drop_status(&e, &backstop);
storage::set_last_fork(&e, 123);
storage::set_queued_swap(&e, &swap);

execute_swap_backstop(&e);
Expand Down
47 changes: 0 additions & 47 deletions emitter/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ pub fn execute_drop(e: &Env, list: &Map<Address, i128>) {
if storage::get_drop_status(e, &backstop) {
panic_with_error!(e, EmitterError::BadDrop);
}
if storage::get_last_fork(e) + 777600 > e.ledger().sequence() {
// Check that the last fork was at least 45 days ago
panic_with_error!(e, EmitterError::BadDrop);
}

let mut drop_amount = 0;
for (_, amt) in list.iter() {
Expand Down Expand Up @@ -126,7 +122,6 @@ mod tests {
storage::set_last_distro_time(&e, &backstop, 1000);
storage::set_backstop(&e, &backstop);
storage::set_blnd_token(&e, &blnd_id);
storage::set_last_fork(&e, 4000000);

execute_drop(&e, &drop_list);
assert_eq!(storage::get_drop_status(&e, &backstop), true);
Expand Down Expand Up @@ -169,7 +164,6 @@ mod tests {
storage::set_backstop(&e, &backstop);
storage::set_blnd_token(&e, &blnd_id);
storage::set_drop_status(&e, &backstop);
storage::set_last_fork(&e, 4000000);

execute_drop(&e, &drop_list);
assert_eq!(storage::get_drop_status(&e, &backstop), true);
Expand Down Expand Up @@ -209,50 +203,9 @@ mod tests {
storage::set_last_distro_time(&e, &backstop, 1000);
storage::set_backstop(&e, &backstop);
storage::set_blnd_token(&e, &blnd_id);
storage::set_last_fork(&e, 4000000);

execute_drop(&e, &drop_list);
assert_eq!(storage::get_drop_status(&e, &backstop), false);
});
}

#[test]
#[should_panic(expected = "Error(Contract, #1101)")]
fn test_drop_bad_block() {
let e = Env::default();
e.mock_all_auths();

e.ledger().set(LedgerInfo {
timestamp: 12345,
protocol_version: 20,
sequence_number: 5000000,
network_id: Default::default(),
base_reserve: 10,
min_temp_entry_ttl: 10,
min_persistent_entry_ttl: 10,
max_entry_ttl: 2000000,
});

let bombadil = Address::generate(&e);
let frodo = Address::generate(&e);
let samwise = Address::generate(&e);
let emitter = create_emitter(&e);
let backstop = Address::generate(&e);

let blnd_id = e.register_stellar_asset_contract(bombadil.clone());
let drop_list = map![
&e,
(frodo.clone(), 20_000_000 * SCALAR_7),
(samwise.clone(), 30_000_000 * SCALAR_7)
];

e.as_contract(&emitter, || {
storage::set_last_distro_time(&e, &backstop, 1000);
storage::set_backstop(&e, &backstop);
storage::set_blnd_token(&e, &blnd_id);
storage::set_last_fork(&e, 5000000);

execute_drop(&e, &drop_list);
});
}
}
21 changes: 0 additions & 21 deletions emitter/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const IS_INIT_KEY: &str = "IsInit";
const BACKSTOP_KEY: &str = "Backstop";
const BACKSTOP_TOKEN_KEY: &str = "BToken";
const BLND_TOKEN_KEY: &str = "BLNDTkn";
const LAST_FORK_KEY: &str = "LastFork";
const SWAP_KEY: &str = "Swap";

// Emitter Data Keys
Expand Down Expand Up @@ -193,23 +192,3 @@ pub fn set_drop_status(e: &Env, backstop: &Address) {
.instance()
.set::<EmitterDataKey, bool>(&EmitterDataKey::Dropped(backstop.clone()), &true);
}

/// Get the last block an emission fork was executed
///
/// Returns true if the emitter has dropped
pub fn get_last_fork(e: &Env) -> u32 {
e.storage()
.instance()
.get(&Symbol::new(e, LAST_FORK_KEY))
.unwrap_optimized()
}

/// Set whether the emitter has performed the drop distribution or not for the current backstop
///
/// ### Arguments
/// * `new_status` - new drop status
pub fn set_last_fork(e: &Env, block: u32) {
e.storage()
.instance()
.set::<Symbol, u32>(&Symbol::new(e, LAST_FORK_KEY), &block);
}
2 changes: 1 addition & 1 deletion test-suites/tests/test_backstop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ fn test_backstop() {
assert_eq!(result.amount, amount);
assert_eq!(
result.exp,
fixture.env.ledger().timestamp() + 30 * 24 * 60 * 60
fixture.env.ledger().timestamp() + 21 * 24 * 60 * 60
);
assert_eq!(bstop_token.balance(&sam), sam_bstop_token_balance);
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion test-suites/tests/test_wasm_happy_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ fn test_wasm_happy_path() {
assert_eq!(result.amount, amount);
assert_eq!(
result.exp,
fixture.env.ledger().timestamp() + 60 * 60 * 24 * 30
fixture.env.ledger().timestamp() + 60 * 60 * 24 * 21
);
assert_eq!(fixture.lp.balance(&frodo), frodo_bstop_token_balance);
assert_eq!(
Expand Down