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
15 changes: 15 additions & 0 deletions contracts/split/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ use soroban_sdk::contracterror;
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[repr(u32)]
pub enum ContractError {
NotAuthorized = 1,
InvoiceNotFound = 2,
DeadlinePassed = 3,
AlreadyFunded = 4,
InvalidAmount = 5,
InvoiceFrozen = 6,
InvalidStatus = 7,
PayerNotAllowed = 8,
FundingInsufficient = 9,
NotAuthorized = 1,
InvoiceNotFound = 2,
DeadlinePassed = 3,
Expand Down Expand Up @@ -47,6 +56,12 @@ pub enum ContractError {
InvalidRecipients = 16,
PrerequisiteNotMet = 17,
BatchLimitExceeded = 18,
/// Issue #330: Recipient has already been paid on this invoice.
RecipientAlreadyPaid = 19,
/// Issue #327: Funds are still time-locked and cannot be released yet.
FundsLockedUntil = 20,
/// Aggregate protocol statistics would overflow their storage type.
StatsOverflow = 21,
/// Issue #327: Funds are still time-locked and cannot be released yet.
FundsLockedUntil = 20,
/// Issue #330: Recipient has already been paid on this invoice.
Expand Down
86 changes: 86 additions & 0 deletions contracts/split/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,92 @@ const TOTAL_VOLUME: &str = "stats_total_volume";
const TOTAL_RECIPIENTS_PAID: &str = "stats_total_recipients_paid";
const STATS_UPDATED: &str = "StatsUpdated";

pub type Stats = (u64, i128, u64);

fn invoices_key(env: &Env) -> Symbol {
Symbol::new(env, TOTAL_INVOICES)
}

fn volume_key(env: &Env) -> Symbol {
Symbol::new(env, TOTAL_VOLUME)
}

fn recipients_paid_key(env: &Env) -> Symbol {
Symbol::new(env, TOTAL_RECIPIENTS_PAID)
}

pub fn get_stats(env: &Env) -> Stats {
let invoices = env
.storage()
.instance()
.get::<Symbol, u64>(&invoices_key(env))
.unwrap_or(0);
let volume = env
.storage()
.instance()
.get::<Symbol, i128>(&volume_key(env))
.unwrap_or(0);
let recipients_paid = env
.storage()
.instance()
.get::<Symbol, u64>(&recipients_paid_key(env))
.unwrap_or(0);

(invoices, volume, recipients_paid)
}

fn publish_updated(env: &Env, stats: Stats) {
env.events().publish(
(Symbol::new(env, STATS_UPDATED),),
(
stats.0,
stats.1,
stats.2,
env.ledger().sequence(),
),
);
}

pub fn record_invoice_created(env: &Env) -> Result<(), ContractError> {
let (invoices, volume, recipients_paid) = get_stats(env);
let updated_invoices = invoices
.checked_add(1)
.ok_or(ContractError::StatsOverflow)?;

env.storage()
.instance()
.set(&invoices_key(env), &updated_invoices);

publish_updated(env, (updated_invoices, volume, recipients_paid));
Ok(())
}

pub fn record_volume(env: &Env, amount: i128) -> Result<(), ContractError> {
let (invoices, volume, recipients_paid) = get_stats(env);
let updated_volume = volume
.checked_add(amount)
.ok_or(ContractError::StatsOverflow)?;

env.storage()
.instance()
.set(&volume_key(env), &updated_volume);

publish_updated(env, (invoices, updated_volume, recipients_paid));
Ok(())
}

pub fn record_recipients_paid(env: &Env, count: u64) -> Result<(), ContractError> {
let (invoices, volume, recipients_paid) = get_stats(env);
let updated_recipients_paid = recipients_paid
.checked_add(count)
.ok_or(ContractError::StatsOverflow)?;

env.storage()
.instance()
.set(&recipients_paid_key(env), &updated_recipients_paid);

publish_updated(env, (invoices, volume, updated_recipients_paid));
Ok(())
fn total_invoices_key(env: &Env) -> Symbol {
Symbol::new(env, TOTAL_INVOICES)
}
Expand Down
Loading