Skip to content
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
21 changes: 16 additions & 5 deletions creator-keys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2385,18 +2385,29 @@ impl CreatorKeysContract {
admin.require_auth();
validate_non_zero_address(&env, &recipient)?;

if env
let old_recipient: Option<Address> = env
.storage()
.persistent()
.get::<DataKey, Address>(&constants::storage::PROTOCOL_FEE_RECIPIENT)
.as_ref()
== Some(&recipient)
{
.get(&constants::storage::PROTOCOL_FEE_RECIPIENT);

if old_recipient.as_ref() == Some(&recipient) {
return Ok(());
}

env.storage()
.persistent()
.set(&constants::storage::PROTOCOL_FEE_RECIPIENT, &recipient);

if let Some(old) = old_recipient {
env.events().publish(
(events::PROTOCOL_FEE_RECIPIENT_UPDATED_EVENT_NAME, admin),
events::ProtocolFeeRecipientUpdatedEvent {
old_recipient: old,
new_recipient: recipient,
},
);
}

Ok(())
}

Expand Down
42 changes: 42 additions & 0 deletions creator-keys/tests/set_protocol_fee_recipient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,45 @@ fn test_set_protocol_fee_recipient_idempotent() {
"recipient unchanged after idempotent set"
);
}

#[test]
fn test_set_protocol_fee_recipient_emits_event_on_update() {
use creator_keys::events;
use soroban_sdk::{testutils::Events, IntoVal};

let env = test_env_with_auths();
let (client, _) = register_creator_keys(&env);

let admin = Address::generate(&env);
let old_recipient = Address::generate(&env);
let new_recipient = Address::generate(&env);

client.set_protocol_fee_recipient(&admin, &old_recipient);
client.set_protocol_fee_recipient(&admin, &new_recipient);

let all_events = env.events().all();
let update_events: Vec<_> = all_events
.iter()
.filter(|(_, topics, _)| {
topics
.get(events::TOPIC_EVENT_NAME_INDEX)
.map(|v| {
let sym: soroban_sdk::Symbol = v.into_val(&env);
sym == events::PROTOCOL_FEE_RECIPIENT_UPDATED_EVENT_NAME
})
.unwrap_or(false)
})
.collect();

assert_eq!(
update_events.len(),
1,
"updating fee recipient via set_protocol_fee_recipient must emit exactly one event"
);

let (_, _, data) = update_events.last().unwrap();
let payload: events::ProtocolFeeRecipientUpdatedEvent = data.into_val(&env);

assert_eq!(payload.old_recipient, old_recipient);
assert_eq!(payload.new_recipient, new_recipient);
}
Loading