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

Emitter: updated drop tracking #147

Merged
merged 2 commits into from
Nov 17, 2023
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
2 changes: 0 additions & 2 deletions emitter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ impl Emitter for EmitterContract {
storage::set_backstop(&e, &backstop);
storage::set_blend_id(&e, &blnd_token_id);
storage::set_last_fork(&e, 0); // We set the block 45 days in the past to allow for an immediate initial drop
storage::set_drop_status(&e, false);
// TODO: Determine if setting the last distro time here is appropriate, since it means tokens immediately start being distributed
storage::set_last_distro_time(&e, &(e.ledger().timestamp() - 7 * 24 * 60 * 60));
}

Expand Down
21 changes: 9 additions & 12 deletions emitter/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pub fn execute_swap_backstop(e: &Env, new_backstop_id: Address) {
let new_backstop_balance = backstop_token_client.balance(&new_backstop_id);
if new_backstop_balance > backstop_balance {
storage::set_backstop(e, &new_backstop_id);
storage::set_drop_status(e, false);
storage::set_last_fork(e, e.ledger().sequence());
} else {
panic_with_error!(e, EmitterError::InsufficientBackstopSize);
Expand All @@ -36,7 +35,8 @@ pub fn execute_swap_backstop(e: &Env, new_backstop_id: Address) {

/// Perform drop BLND distribution
pub fn execute_drop(e: &Env) -> Map<Address, i128> {
if storage::get_drop_status(e) {
let backstop = storage::get_backstop(e);
if storage::get_drop_status(e, &backstop) {
panic_with_error!(e, EmitterError::BadDrop);
}
if storage::get_last_fork(e) + 777600 > e.ledger().sequence() {
Expand All @@ -61,7 +61,7 @@ pub fn execute_drop(e: &Env) -> Map<Address, i128> {
for (addr, amt) in drop_list.iter() {
blnd_client.mint(&addr, &amt);
}
storage::set_drop_status(e, true);
storage::set_drop_status(e, &backstop);
drop_list
}

Expand Down Expand Up @@ -152,11 +152,11 @@ mod tests {
e.as_contract(&emitter, || {
storage::set_last_distro_time(&e, &1000);
storage::set_backstop(&e, &backstop);
storage::set_drop_status(&e, true);
storage::set_drop_status(&e, &backstop);

execute_swap_backstop(&e, new_backstop.clone());
assert_eq!(storage::get_backstop(&e), new_backstop);
assert_eq!(storage::get_drop_status(&e), false);
assert_eq!(storage::get_drop_status(&e, &new_backstop), false);
});
}

Expand Down Expand Up @@ -246,11 +246,10 @@ mod tests {
storage::set_last_distro_time(&e, &1000);
storage::set_backstop(&e, &backstop);
storage::set_blend_id(&e, &blnd_id);
storage::set_drop_status(&e, false);
storage::set_last_fork(&e, 4000000);

let list = execute_drop(&e);
assert_eq!(storage::get_drop_status(&e), true);
assert_eq!(storage::get_drop_status(&e, &backstop), true);
assert_eq!(list.len(), 2);
assert_eq!(blnd_client.balance(&frodo), 20_000_000 * SCALAR_7);
assert_eq!(blnd_client.balance(&samwise), 30_000_000 * SCALAR_7);
Expand Down Expand Up @@ -298,11 +297,11 @@ mod tests {
storage::set_last_distro_time(&e, &1000);
storage::set_backstop(&e, &backstop);
storage::set_blend_id(&e, &blnd_id);
storage::set_drop_status(&e, true);
storage::set_drop_status(&e, &backstop);
storage::set_last_fork(&e, 4000000);

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

Expand Down Expand Up @@ -347,11 +346,10 @@ mod tests {
storage::set_last_distro_time(&e, &1000);
storage::set_backstop(&e, &backstop);
storage::set_blend_id(&e, &blnd_id);
storage::set_drop_status(&e, false);
storage::set_last_fork(&e, 4000000);

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

Expand Down Expand Up @@ -398,7 +396,6 @@ mod tests {
storage::set_backstop(&e, &backstop);
storage::set_blend_id(&e, &blnd_id);
storage::set_last_fork(&e, 5000000);
storage::set_drop_status(&e, false);

execute_drop(&e);
});
Expand Down
14 changes: 7 additions & 7 deletions emitter/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub enum EmitterDataKey {
BlendLPId,
// The last timestamp distribution was ran on
LastDistro,
// The drop status for the current backstop
DropStatus,
// Stores the list of backstop addresses that have dropped
Dropped(Address),
// The last block emissions were forked
LastFork,
}
Expand Down Expand Up @@ -118,21 +118,21 @@ pub fn set_last_distro_time(e: &Env, last_distro: &u64) {
/// Get whether the emitter has performed the drop distribution or not for the current backstop
///
/// Returns true if the emitter has dropped
pub fn get_drop_status(e: &Env) -> bool {
pub fn get_drop_status(e: &Env, backstop: &Address) -> bool {
e.storage()
.instance()
.get(&EmitterDataKey::DropStatus)
.unwrap_optimized()
.get::<EmitterDataKey, bool>(&EmitterDataKey::Dropped(backstop.clone()))
.unwrap_or(false)
}

/// Set whether the emitter has performed the drop distribution or not for the current backstop
///
/// ### Arguments
/// * `new_status` - new drop status
pub fn set_drop_status(e: &Env, new_status: bool) {
pub fn set_drop_status(e: &Env, backstop: &Address) {
e.storage()
.instance()
.set::<EmitterDataKey, bool>(&EmitterDataKey::DropStatus, &new_status);
.set::<EmitterDataKey, bool>(&EmitterDataKey::Dropped(backstop.clone()), &true);
}

/// Get the last block an emission fork was executed
Expand Down