Skip to content

Commit

Permalink
Merge pull request #15 from blend-capital/better-events
Browse files Browse the repository at this point in the history
chore: use strongly typed events and improve auction events
  • Loading branch information
mootz12 authored Dec 17, 2024
2 parents f7e0fe8 + bab2ce0 commit 5e436be
Show file tree
Hide file tree
Showing 19 changed files with 778 additions and 319 deletions.
40 changes: 12 additions & 28 deletions backstop/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use crate::{
dependencies::EmitterClient,
emissions,
errors::BackstopError,
events::BackstopEvents,
storage,
};
use soroban_sdk::{
contract, contractclient, contractimpl, panic_with_error, Address, Env, Symbol, Vec,
};
use soroban_sdk::{contract, contractclient, contractimpl, panic_with_error, Address, Env, Vec};

/// ### Backstop
///
Expand Down Expand Up @@ -218,10 +217,7 @@ impl Backstop for BackstopContract {

let to_mint = backstop::execute_deposit(&e, &from, &pool_address, amount);

e.events().publish(
(Symbol::new(&e, "deposit"), pool_address, from),
(amount, to_mint),
);
BackstopEvents::deposit(&e, pool_address, from, amount, to_mint);
to_mint
}

Expand All @@ -231,10 +227,7 @@ impl Backstop for BackstopContract {

let to_queue = backstop::execute_queue_withdrawal(&e, &from, &pool_address, amount);

e.events().publish(
(Symbol::new(&e, "queue_withdrawal"), pool_address, from),
(amount, to_queue.exp),
);
BackstopEvents::queue_withdrawal(&e, pool_address, from, amount, to_queue.exp);
to_queue
}

Expand All @@ -244,10 +237,7 @@ impl Backstop for BackstopContract {

backstop::execute_dequeue_withdrawal(&e, &from, &pool_address, amount);

e.events().publish(
(Symbol::new(&e, "dequeue_withdrawal"), pool_address, from),
amount,
);
BackstopEvents::dequeue_withdrawal(&e, pool_address, from, amount);
}

fn withdraw(e: Env, from: Address, pool_address: Address, amount: i128) -> i128 {
Expand All @@ -256,10 +246,7 @@ impl Backstop for BackstopContract {

let to_withdraw = backstop::execute_withdraw(&e, &from, &pool_address, amount);

e.events().publish(
(Symbol::new(&e, "withdraw"), pool_address, from),
(amount, to_withdraw),
);
BackstopEvents::withdraw(&e, pool_address, from, amount, to_withdraw);
to_withdraw
}

Expand All @@ -281,16 +268,14 @@ impl Backstop for BackstopContract {
storage::extend_instance(&e);
let new_tokens_emitted = emissions::gulp_emissions(&e);

e.events()
.publish((Symbol::new(&e, "gulp_emissions"),), new_tokens_emitted);
BackstopEvents::gulp_emissions(&e, new_tokens_emitted);
}

fn add_reward(e: Env, to_add: Address, to_remove: Address) {
storage::extend_instance(&e);
emissions::add_to_reward_zone(&e, to_add.clone(), to_remove.clone());

e.events()
.publish((Symbol::new(&e, "rw_zone"),), (to_add, to_remove));
BackstopEvents::rw_zone(&e, to_add, to_remove);
}

fn gulp_pool_emissions(e: Env, pool_address: Address) -> i128 {
Expand All @@ -305,7 +290,7 @@ impl Backstop for BackstopContract {

let amount = emissions::execute_claim(&e, &from, &pool_addresses, &to);

e.events().publish((Symbol::new(&e, "claim"), from), amount);
BackstopEvents::claim(&e, from, amount);
amount
}

Expand All @@ -321,8 +306,7 @@ impl Backstop for BackstopContract {

backstop::execute_draw(&e, &pool_address, amount, &to);

e.events()
.publish((Symbol::new(&e, "draw"), pool_address), (to, amount));
BackstopEvents::draw(&e, pool_address, to, amount);
}

fn donate(e: Env, from: Address, pool_address: Address, amount: i128) {
Expand All @@ -331,8 +315,8 @@ impl Backstop for BackstopContract {
pool_address.require_auth();

backstop::execute_donate(&e, &from, &pool_address, amount);
e.events()
.publish((Symbol::new(&e, "donate"), pool_address, from), amount);

BackstopEvents::donate(&e, pool_address, from, amount);
}

fn update_tkn_val(e: Env) -> (i128, i128) {
Expand Down
8 changes: 3 additions & 5 deletions backstop/src/emissions/claim.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{dependencies::CometClient, errors::BackstopError, storage};
use crate::{dependencies::CometClient, errors::BackstopError, events::BackstopEvents, storage};
use soroban_fixed_point_math::FixedPoint;
use soroban_sdk::{
auth::{ContractContext, InvokerContractAuthEntry, SubContractInvocation},
Expand Down Expand Up @@ -67,10 +67,8 @@ pub fn execute_claim(e: &Env, from: &Address, pool_addresses: &Vec<Address>, to:

storage::set_pool_balance(e, &pool_id, &pool_balance);
storage::set_user_balance(e, &pool_id, to, &user_balance);
e.events().publish(
(Symbol::new(&e, "deposit"), pool_id, to),
(deposit_amount, to_mint),
);

BackstopEvents::deposit(e, pool_id, to.clone(), deposit_amount, to_mint);
}
}

Expand Down
143 changes: 143 additions & 0 deletions backstop/src/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
use soroban_sdk::{Address, Env, Symbol};

pub struct BackstopEvents {}

impl BackstopEvents {
/// Emitted when tokens are deposited into a backstop
///
/// - topics - `["deposit", pool_address: Address, from: Address]`
/// - data - `[tokens_in: i128, backstop_shares_minted: i128]`
///
/// ### Arguments
/// * `pool_address` - The address of the pool
/// * `from` - The address of the user depositing tokens
/// * `tokens_in` - The amount of tokens sent to the backstop
/// * `backstop_shares_minted` - The amount of backstop shares minted
pub fn deposit(
e: &Env,
pool_address: Address,
from: Address,
tokens_in: i128,
backstop_shares_minted: i128,
) {
let topics = (Symbol::new(e, "deposit"), pool_address, from);
e.events()
.publish(topics, (tokens_in, backstop_shares_minted));
}

/// Emitted when a withdrawal is queued
///
/// - topics - `["queue_withdrawal", pool_address: Address, from: Address]`
/// - data - `[amount: i128, expiration: u64]`
///
/// ### Arguments
/// * `pool_address` - The address of the pool
/// * `from` - The address of the user queuing the withdrawal
/// * `amount` - The amount of shares being queued for withdrawal
/// * `expiration` - The expiration timestamp of the withdrawal request
pub fn queue_withdrawal(
e: &Env,
pool_address: Address,
from: Address,
amount: i128,
expiration: u64,
) {
let topics = (Symbol::new(e, "queue_withdrawal"), pool_address, from);
e.events().publish(topics, (amount, expiration));
}

/// Emitted when a withdrawal is dequeued
///
/// - topics - `["dequeue_withdrawal", pool_address: Address, from: Address]`
/// - data - `[amount: i128]`
///
/// ### Arguments
/// * `pool_address` - The address of the pool
/// * `from` - The address of the user dequeuing the withdrawal
/// * `amount` - The amount of shares being dequeued
pub fn dequeue_withdrawal(e: &Env, pool_address: Address, from: Address, amount: i128) {
let topics = (Symbol::new(e, "dequeue_withdrawal"), pool_address, from);
e.events().publish(topics, amount);
}

/// Emitted when tokens are withdrawn from the backstop
///
/// - topics - `["withdraw", pool_address: Address, from: Address]`
/// - data - `[amount: i128, tokens_out: i128]`
///
/// ### Arguments
/// * `pool_address` - The address of the pool
/// * `from` - The address of the user withdrawing tokens
/// * `amount` - The amount of backstop shares being burned
/// * `tokens_out` - The amount of tokens being withdrawn
pub fn withdraw(e: &Env, pool_address: Address, from: Address, amount: i128, tokens_out: i128) {
let topics = (Symbol::new(e, "withdraw"), pool_address, from);
e.events().publish(topics, (amount, tokens_out));
}

/// Emitted when new emissions are gulped
///
/// - topics - `["gulp_emissions"]`
/// - data - `[new_tokens_emitted: i128]`
///
/// ### Arguments
/// * `new_tokens_emitted` - The amount of new tokens emitted
pub fn gulp_emissions(e: &Env, new_tokens_emitted: i128) {
let topics = (Symbol::new(e, "gulp_emissions"),);
e.events().publish(topics, new_tokens_emitted);
}

/// Emitted when the reward zone is updated
///
/// - topics - `["rw_zone"]`
/// - data - `[to_add: Address, to_remove: Address]`
///
/// ### Arguments
/// * `to_add` - The address to add to the reward zone
/// * `to_remove` - The address to remove from the reward zone
pub fn rw_zone(e: &Env, to_add: Address, to_remove: Address) {
let topics = (Symbol::new(e, "rw_zone"),);
e.events().publish(topics, (to_add, to_remove));
}

/// Emitted when emissions are claimed
///
/// - topics - `["claim", from: Address]`
/// - data - `[amount: i128]`
///
/// ### Arguments
/// * `from` - The address of the user claiming emissions
/// * `amount` - The amount of emissions claimed
pub fn claim(e: &Env, from: Address, amount: i128) {
let topics = (Symbol::new(e, "claim"), from);
e.events().publish(topics, amount);
}

/// Emitted when tokens are drawn from the backstop
///
/// - topics - `["draw", pool_address: Address]`
/// - data - `[to: Address, amount: i128]`
///
/// ### Arguments
/// * `pool_address` - The address of the pool
/// * `to` - The address receiving the drawn tokens
/// * `amount` - The amount of tokens drawn
pub fn draw(e: &Env, pool_address: Address, to: Address, amount: i128) {
let topics = (Symbol::new(e, "draw"), pool_address);
e.events().publish(topics, (to, amount));
}

/// Emitted when tokens are donated to the backstop
///
/// - topics - `["donate", pool_address: Address, from: Address]`
/// - data - `[amount: i128]`
///
/// ### Arguments
/// * `pool_address` - The address of the pool
/// * `from` - The address of the donor
/// * `amount` - The amount of tokens donated
pub fn donate(e: &Env, pool_address: Address, from: Address, amount: i128) {
let topics = (Symbol::new(e, "donate"), pool_address, from);
e.events().publish(topics, amount);
}
}
1 change: 1 addition & 0 deletions backstop/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod contract;
mod dependencies;
mod emissions;
mod errors;
mod events;
mod storage;
mod testutils;

Expand Down
19 changes: 7 additions & 12 deletions emitter/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{backstop_manager, emitter, errors::EmitterError, storage};
use soroban_sdk::{
contract, contractclient, contractimpl, panic_with_error, Address, Env, Symbol, Vec,
};
use crate::{backstop_manager, emitter, errors::EmitterError, events::EmitterEvents, storage};
use soroban_sdk::{contract, contractclient, contractimpl, panic_with_error, Address, Env, Vec};

/// ### Emitter
///
Expand Down Expand Up @@ -94,10 +92,7 @@ impl Emitter for EmitterContract {

let distribution_amount = emitter::execute_distribute(&e, &backstop_address);

e.events().publish(
(Symbol::new(&e, "distribute"),),
(backstop_address, distribution_amount),
);
EmitterEvents::distribute(&e, backstop_address, distribution_amount);
distribution_amount
}

Expand All @@ -114,7 +109,7 @@ impl Emitter for EmitterContract {
let swap =
backstop_manager::execute_queue_swap_backstop(&e, &new_backstop, &new_backstop_token);

e.events().publish((Symbol::new(&e, "q_swap"),), swap);
EmitterEvents::q_swap(&e, swap);
}

fn get_queued_swap(e: Env) -> Option<backstop_manager::Swap> {
Expand All @@ -125,20 +120,20 @@ impl Emitter for EmitterContract {
storage::extend_instance(&e);
let swap = backstop_manager::execute_cancel_swap_backstop(&e);

e.events().publish((Symbol::new(&e, "del_swap"),), swap);
EmitterEvents::del_swap(&e, swap);
}

fn swap_backstop(e: Env) {
storage::extend_instance(&e);
let swap = backstop_manager::execute_swap_backstop(&e);

e.events().publish((Symbol::new(&e, "swap"),), swap);
EmitterEvents::swap(&e, swap);
}

fn drop(e: Env, list: Vec<(Address, i128)>) {
storage::extend_instance(&e);
emitter::execute_drop(&e, &list);

e.events().publish((Symbol::new(&e, "drop"),), list);
EmitterEvents::drop(&e, list);
}
}
Loading

0 comments on commit 5e436be

Please sign in to comment.