Skip to content
Merged
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
283 changes: 282 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ pub enum RevoraError {
InvalidPeriodId = 22,
/// Deposit would exceed the offering's supply cap (#96).
SupplyCapExceeded = 23,
/// Current ledger timestamp is outside configured reporting window.
ReportingWindowClosed = 24,
/// Current ledger timestamp is outside configured claiming window.
ClaimWindowClosed = 25,
}

// ── Event symbols ────────────────────────────────────────────
Expand Down Expand Up @@ -151,6 +155,15 @@ const EVENT_SUPPLY_CAP_REACHED: Symbol = symbol_short!("cap_reach");
const EVENT_INV_CONSTRAINTS: Symbol = symbol_short!("inv_cfg");
/// Emitted when per-offering or platform per-asset fee is set (#98).
const EVENT_FEE_CONFIG: Symbol = symbol_short!("fee_cfg");
const EVENT_INDEXED_V2: Symbol = symbol_short!("ev_idx2");
const EVENT_TYPE_OFFER: Symbol = symbol_short!("offer");
const EVENT_TYPE_REV_INIT: Symbol = symbol_short!("rv_init");
const EVENT_TYPE_REV_OVR: Symbol = symbol_short!("rv_ovr");
const EVENT_TYPE_REV_REJ: Symbol = symbol_short!("rv_rej");
const EVENT_TYPE_REV_REP: Symbol = symbol_short!("rv_rep");
const EVENT_TYPE_CLAIM: Symbol = symbol_short!("claim");
const EVENT_REPORT_WINDOW_SET: Symbol = symbol_short!("rep_win");
const EVENT_CLAIM_WINDOW_SET: Symbol = symbol_short!("clm_win");

const BPS_DENOMINATOR: i128 = 10_000;

Expand Down Expand Up @@ -241,6 +254,33 @@ pub struct SimulateDistributionResult {
pub payouts: Vec<(Address, i128)>,
}

/// Versioned structured topic payload for indexers.
#[contracttype]
#[derive(Clone, Debug, PartialEq)]
pub struct EventIndexTopicV2 {
pub version: u32,
pub event_type: Symbol,
pub issuer: Address,
pub namespace: Symbol,
pub token: Address,
/// 0 when the event is not period-scoped.
pub period_id: u64,
}

#[contracttype]
#[derive(Clone, Debug, PartialEq)]
pub struct AccessWindow {
pub start_timestamp: u64,
pub end_timestamp: u64,
}

#[contracttype]
#[derive(Clone, Debug, PartialEq)]
pub enum WindowDataKey {
Report(OfferingId),
Claim(OfferingId),
}

/// Defines how fractional shares are handled during distribution calculations.
#[contracttype]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -393,6 +433,38 @@ impl RevoraRevenueShare {
Ok(())
}

fn validate_window(window: &AccessWindow) -> Result<(), RevoraError> {
if window.start_timestamp > window.end_timestamp {
return Err(RevoraError::LimitReached);
}
Ok(())
}

fn is_window_open(env: &Env, window: &AccessWindow) -> bool {
let now = env.ledger().timestamp();
now >= window.start_timestamp && now <= window.end_timestamp
}

fn require_report_window_open(env: &Env, offering_id: &OfferingId) -> Result<(), RevoraError> {
let key = WindowDataKey::Report(offering_id.clone());
if let Some(window) = env.storage().persistent().get::<WindowDataKey, AccessWindow>(&key) {
if !Self::is_window_open(env, &window) {
return Err(RevoraError::ReportingWindowClosed);
}
}
Ok(())
}

fn require_claim_window_open(env: &Env, offering_id: &OfferingId) -> Result<(), RevoraError> {
let key = WindowDataKey::Claim(offering_id.clone());
if let Some(window) = env.storage().persistent().get::<WindowDataKey, AccessWindow>(&key) {
if !Self::is_window_open(env, &window) {
return Err(RevoraError::ClaimWindowClosed);
}
}
Ok(())
}


/// Internal helper for revenue deposits.
fn do_deposit_revenue(
Expand Down Expand Up @@ -753,6 +825,20 @@ impl RevoraRevenueShare {
(symbol_short!("offer_reg"), issuer.clone(), namespace.clone()),
(token.clone(), revenue_share_bps, payout_asset.clone()),
);
env.events().publish(
(
EVENT_INDEXED_V2,
EventIndexTopicV2 {
version: 2,
event_type: EVENT_TYPE_OFFER,
issuer: issuer.clone(),
namespace: namespace.clone(),
token: token.clone(),
period_id: 0,
},
),
(revenue_share_bps, payout_asset.clone()),
);

// Optionally emit a versioned v1 event with explicit version field
if Self::is_event_versioning_enabled(env.clone()) {
Expand Down Expand Up @@ -838,6 +924,7 @@ impl RevoraRevenueShare {
namespace: namespace.clone(),
token: token.clone(),
};
Self::require_report_window_open(&env, &offering_id)?;

if !event_only {
// Verify offering exists and issuer is current
Expand Down Expand Up @@ -909,6 +996,20 @@ impl RevoraRevenueShare {
(EVENT_REVENUE_REPORT_OVERRIDE, issuer.clone(), namespace.clone(), token.clone()),
(amount, period_id, existing_amount, blacklist.clone()),
);
env.events().publish(
(
EVENT_INDEXED_V2,
EventIndexTopicV2 {
version: 2,
event_type: EVENT_TYPE_REV_OVR,
issuer: issuer.clone(),
namespace: namespace.clone(),
token: token.clone(),
period_id,
},
),
(amount, existing_amount, payout_asset.clone()),
);

env.events().publish(
(
Expand All @@ -924,6 +1025,20 @@ impl RevoraRevenueShare {
(EVENT_REVENUE_REPORT_REJECTED, issuer.clone(), namespace.clone(), token.clone()),
(amount, period_id, existing_amount, blacklist.clone()),
);
env.events().publish(
(
EVENT_INDEXED_V2,
EventIndexTopicV2 {
version: 2,
event_type: EVENT_TYPE_REV_REJ,
issuer: issuer.clone(),
namespace: namespace.clone(),
token: token.clone(),
period_id,
},
),
(amount, existing_amount, payout_asset.clone()),
);

env.events().publish(
(
Expand All @@ -950,6 +1065,20 @@ impl RevoraRevenueShare {
(EVENT_REVENUE_REPORT_INITIAL, issuer.clone(), namespace.clone(), token.clone()),
(amount, period_id, blacklist.clone()),
);
env.events().publish(
(
EVENT_INDEXED_V2,
EventIndexTopicV2 {
version: 2,
event_type: EVENT_TYPE_REV_INIT,
issuer: issuer.clone(),
namespace: namespace.clone(),
token: token.clone(),
period_id,
},
),
(amount, payout_asset.clone()),
);

env.events().publish(
(
Expand All @@ -973,6 +1102,20 @@ impl RevoraRevenueShare {
(EVENT_REVENUE_REPORTED, issuer.clone(), namespace.clone(), token.clone()),
(amount, period_id, blacklist.clone()),
);
env.events().publish(
(
EVENT_INDEXED_V2,
EventIndexTopicV2 {
version: 2,
event_type: EVENT_TYPE_REV_REP,
issuer: issuer.clone(),
namespace: namespace.clone(),
token: token.clone(),
period_id,
},
),
(amount, payout_asset.clone(), override_existing),
);

env.events().publish(
(
Expand Down Expand Up @@ -1867,6 +2010,7 @@ impl RevoraRevenueShare {
}

let offering_id = OfferingId { issuer, namespace, token };
Self::require_claim_window_open(&env, &offering_id)?;

let count_key = DataKey::PeriodCount(offering_id.clone());
let period_count: u32 = env.storage().persistent().get(&count_key).unwrap_or(0);
Expand Down Expand Up @@ -1929,13 +2073,150 @@ impl RevoraRevenueShare {
env.storage().persistent().set(&idx_key, &last_claimed_idx);

env.events().publish(
(EVENT_CLAIM, offering_id.issuer, offering_id.namespace, offering_id.token),
(
EVENT_CLAIM,
offering_id.issuer.clone(),
offering_id.namespace.clone(),
offering_id.token.clone(),
),
(holder, total_payout, claimed_periods),
);
env.events().publish(
(
EVENT_INDEXED_V2,
EventIndexTopicV2 {
version: 2,
event_type: EVENT_TYPE_CLAIM,
issuer: offering_id.issuer,
namespace: offering_id.namespace,
token: offering_id.token,
period_id: 0,
},
),
(total_payout,),
);

Ok(total_payout)
}

/// Configure the reporting access window for an offering.
/// If unset, reporting remains always permitted.
pub fn set_report_window(
env: Env,
issuer: Address,
namespace: Symbol,
token: Address,
start_timestamp: u64,
end_timestamp: u64,
) -> Result<(), RevoraError> {
Self::require_not_frozen(&env)?;
let current_issuer = Self::get_current_issuer(
&env,
issuer.clone(),
namespace.clone(),
token.clone(),
)
.ok_or(RevoraError::OfferingNotFound)?;
if current_issuer != issuer {
return Err(RevoraError::OfferingNotFound);
}
issuer.require_auth();
let window = AccessWindow {
start_timestamp,
end_timestamp,
};
Self::validate_window(&window)?;
let offering_id = OfferingId {
issuer: issuer.clone(),
namespace: namespace.clone(),
token: token.clone(),
};
env.storage()
.persistent()
.set(&WindowDataKey::Report(offering_id), &window);
env.events().publish(
(EVENT_REPORT_WINDOW_SET, issuer, namespace, token),
(start_timestamp, end_timestamp),
);
Ok(())
}

/// Configure the claiming access window for an offering.
/// If unset, claiming remains always permitted.
pub fn set_claim_window(
env: Env,
issuer: Address,
namespace: Symbol,
token: Address,
start_timestamp: u64,
end_timestamp: u64,
) -> Result<(), RevoraError> {
Self::require_not_frozen(&env)?;
let current_issuer = Self::get_current_issuer(
&env,
issuer.clone(),
namespace.clone(),
token.clone(),
)
.ok_or(RevoraError::OfferingNotFound)?;
if current_issuer != issuer {
return Err(RevoraError::OfferingNotFound);
}
issuer.require_auth();
let window = AccessWindow {
start_timestamp,
end_timestamp,
};
Self::validate_window(&window)?;
let offering_id = OfferingId {
issuer: issuer.clone(),
namespace: namespace.clone(),
token: token.clone(),
};
env.storage()
.persistent()
.set(&WindowDataKey::Claim(offering_id), &window);
env.events().publish(
(EVENT_CLAIM_WINDOW_SET, issuer, namespace, token),
(start_timestamp, end_timestamp),
);
Ok(())
}

/// Read configured reporting window (if any) for an offering.
pub fn get_report_window(
env: Env,
issuer: Address,
namespace: Symbol,
token: Address,
) -> Option<AccessWindow> {
let offering_id = OfferingId {
issuer,
namespace,
token,
};
env.storage()
.persistent()
.get(&WindowDataKey::Report(offering_id))
}

/// Read configured claiming window (if any) for an offering.
pub fn get_claim_window(
env: Env,
issuer: Address,
namespace: Symbol,
token: Address,
) -> Option<AccessWindow> {
let offering_id = OfferingId {
issuer,
namespace,
token,
};
env.storage()
.persistent()
.get(&WindowDataKey::Claim(offering_id))
}

/// Return unclaimed period IDs for a holder on an offering.
/// Ordering: by deposit index (creation order), deterministic (#38).
pub fn get_pending_periods(env: Env, issuer: Address, namespace: Symbol, token: Address, holder: Address) -> Vec<u64> {
Expand Down
Loading