From 0ea7aab98737db96d53f3b04b7bf464cd03a41d4 Mon Sep 17 00:00:00 2001 From: nikkybel Date: Sun, 26 Apr 2026 17:26:56 +0100 Subject: [PATCH] feat: implement Event Schema for Indexer Compatibility --- contracts/raffle/src/events.rs | 50 ++++++++++++++++++++++++---- contracts/raffle/src/instance/mod.rs | 12 +++++-- contracts/raffle/src/lib.rs | 36 +++++++++++++++++++- 3 files changed, 88 insertions(+), 10 deletions(-) diff --git a/contracts/raffle/src/events.rs b/contracts/raffle/src/events.rs index 605c467..a9cfe62 100644 --- a/contracts/raffle/src/events.rs +++ b/contracts/raffle/src/events.rs @@ -6,7 +6,7 @@ use crate::instance::{CancelReason, RaffleStatus, RandomnessSource}; // LIFECYCLE EVENTS // ============================================================================ -/// Emitted when a new raffle is initialized +/// Emitted when a new raffle instance is initialized #[derive(Clone)] #[contracttype] pub struct RaffleCreated { @@ -19,6 +19,7 @@ pub struct RaffleCreated { pub prizes: Vec, pub description: String, pub randomness_source: RandomnessSource, + pub timestamp: u64, } /// Emitted when the creator deposits the prize pool @@ -31,13 +32,14 @@ pub struct PrizeDeposited { pub timestamp: u64, } -/// Emitted when a user purchases one or more tickets +/// Emitted when a user purchases a ticket #[derive(Clone)] #[contracttype] pub struct TicketPurchased { pub buyer: Address, pub ticket_ids: Vec, pub quantity: u32, + pub ticket_price: i128, pub total_paid: i128, pub timestamp: u64, } @@ -79,13 +81,14 @@ pub struct RaffleFinalized { pub finalized_at: u64, } -/// Emitted when a raffle is cancelled by the creator +/// Emitted when a raffle is cancelled #[derive(Clone)] #[contracttype] pub struct RaffleCancelled { pub creator: Address, pub reason: CancelReason, pub tickets_sold: u32, + pub prize_refunded: bool, pub timestamp: u64, } @@ -94,7 +97,7 @@ pub struct RaffleCancelled { #[contracttype] pub struct TicketRefunded { pub buyer: Address, - pub ticket_id: u32, + pub ticket_number: u32, pub amount: i128, pub timestamp: u64, } @@ -105,6 +108,7 @@ pub struct TicketRefunded { pub struct PrizeClaimed { pub winner: Address, pub tier_index: u32, + pub payment_token: Address, pub gross_amount: i128, pub net_amount: i128, pub platform_fee: i128, @@ -116,6 +120,7 @@ pub struct PrizeClaimed { #[contracttype] pub struct BuybackAndBurnExecuted { pub router: Address, + pub payment_token: Address, pub tikka_token: Address, pub amount_in: i128, pub amount_out: i128, @@ -156,7 +161,7 @@ pub struct TreasuryUpdated { pub timestamp: u64, } -/// Emitted when accumulated fees are withdrawn +/// Emitted when accumulated fees are withdrawn to the treasury #[derive(Clone)] #[contracttype] pub struct FeesWithdrawn { @@ -200,11 +205,44 @@ pub struct AdminTransferAccepted { pub timestamp: u64, } +// ============================================================================ +// FACTORY EVENTS +// ============================================================================ + +/// Emitted when the factory is initialized +#[derive(Clone)] +#[contracttype] +pub struct FactoryInitialized { + pub admin: Address, + pub protocol_fee_bp: u32, + pub treasury: Address, + pub timestamp: u64, +} + +/// Emitted when a new raffle instance is deployed by the factory +#[derive(Clone)] +#[contracttype] +pub struct RaffleDeployed { + pub raffle_address: Address, + pub creator: Address, + pub timestamp: u64, +} + +/// Emitted when the factory protocol fee or treasury is updated via set_config +#[derive(Clone)] +#[contracttype] +pub struct FactoryConfigUpdated { + pub protocol_fee_bp: u32, + pub treasury: Address, + pub updated_by: Address, + pub timestamp: u64, +} + // ============================================================================ // INTERNAL STATE CHANGE EVENT // ============================================================================ -/// Emitted when raffle status changes +/// Emitted on every raffle status transition #[derive(Clone)] #[contracttype] pub struct StatusChanged { diff --git a/contracts/raffle/src/instance/mod.rs b/contracts/raffle/src/instance/mod.rs index 4697d0a..8b96858 100644 --- a/contracts/raffle/src/instance/mod.rs +++ b/contracts/raffle/src/instance/mod.rs @@ -7,7 +7,7 @@ use crate::types::{effective_limit, PageResult_Tickets, PaginationParams}; use crate::events::{ DrawTriggered, PrizeClaimed, PrizeDeposited, RaffleCancelled, RaffleCreated, RaffleFinalized, - RandomnessReceived, RandomnessRequested, StatusChanged, TicketPurchased, + RandomnessReceived, RandomnessRequested, StatusChanged, TicketPurchased, TicketRefunded, }; // --- External Contract Traits --- @@ -362,6 +362,7 @@ impl Contract { prizes: config.prizes, description: config.description, randomness_source: config.randomness_source, + timestamp: env.ledger().timestamp(), }, ); @@ -494,6 +495,7 @@ impl Contract { buyer, ticket_ids, quantity: 1u32, + ticket_price: raffle.ticket_price, total_paid: raffle.ticket_price, timestamp, }, @@ -785,6 +787,7 @@ impl Contract { "buyback_and_burn_executed", crate::events::BuybackAndBurnExecuted { router: router.clone(), + payment_token: raffle.payment_token.clone(), tikka_token: tikka.clone(), amount_in: platform_fee, amount_out, @@ -800,6 +803,7 @@ impl Contract { "buyback_and_burn_executed", crate::events::BuybackAndBurnExecuted { router: contract_address.clone(), + payment_token: raffle.payment_token.clone(), tikka_token: tikka.clone(), amount_in: platform_fee, amount_out: platform_fee, @@ -828,6 +832,7 @@ impl Contract { PrizeClaimed { winner: winner.clone(), tier_index, + payment_token: raffle.payment_token.clone(), gross_amount: tier_prize_amount, net_amount, platform_fee, @@ -912,6 +917,7 @@ impl Contract { creator: raffle.creator.clone(), reason, tickets_sold: raffle.tickets_sold, + prize_refunded: should_refund_prize, timestamp: env.ledger().timestamp(), }, ); @@ -979,9 +985,9 @@ impl Contract { publish_event( &env, "ticket_refunded", - crate::events::TicketRefunded { + TicketRefunded { buyer: ticket.owner.clone(), - ticket_id, + ticket_number: ticket_id, amount: raffle.ticket_price, timestamp: env.ledger().timestamp(), }, diff --git a/contracts/raffle/src/lib.rs b/contracts/raffle/src/lib.rs index 800d568..f21fe7c 100644 --- a/contracts/raffle/src/lib.rs +++ b/contracts/raffle/src/lib.rs @@ -99,6 +99,18 @@ impl RaffleFactory { env.storage() .persistent() .set(&DataKey::Treasury, &treasury); + + publish_factory_event( + &env, + "factory_initialized", + events::FactoryInitialized { + admin, + protocol_fee_bp, + treasury, + timestamp: env.ledger().timestamp(), + }, + ); + Ok(()) } @@ -107,13 +119,25 @@ impl RaffleFactory { protocol_fee_bp: u32, treasury: Address, ) -> Result<(), ContractError> { - require_factory_admin(&env)?; + let admin = require_factory_admin(&env)?; env.storage() .persistent() .set(&DataKey::ProtocolFeeBP, &protocol_fee_bp); env.storage() .persistent() .set(&DataKey::Treasury, &treasury); + + publish_factory_event( + &env, + "factory_config_updated", + events::FactoryConfigUpdated { + protocol_fee_bp, + treasury, + updated_by: admin, + timestamp: env.ledger().timestamp(), + }, + ); + Ok(()) } @@ -168,6 +192,16 @@ impl RaffleFactory { .persistent() .set(&DataKey::RaffleInstances, &instances); + publish_factory_event( + &env, + "raffle_deployed", + events::RaffleDeployed { + raffle_address: raffle_address.clone(), + creator, + timestamp: env.ledger().timestamp(), + }, + ); + Ok(raffle_address) }