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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,9 @@ in each participant role, self-streams, the documented precedence between
validation groups, and double-withdraw and unknown-id guards.

It also covers the storage and event behaviour described above: the order in
which each entry point moves tokens and publishes its event, the silence of a
rejected call on the event stream, `DataKey` encoding across the id range, and
which each entry point moves tokens and publishes its event, the Created,
Withdrawn, and Cancelled payload fields, the silence of a rejected call on
the event stream, `DataKey` encoding across the id range, and
the persistent-entry and instance time-to-live bumps on both sides of
`BUMP_THRESHOLD`.

Expand Down
104 changes: 103 additions & 1 deletion contracts/stream/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use soroban_sdk::{
storage::{Instance as _, Persistent as _},
Address as _, Events as _, Ledger as _,
},
token, vec, xdr, Address, Env, TryFromVal, Vec,
token, vec, xdr, Address, Env, Event, TryFromVal, Vec,
};

use crate::contract::{StreamContract, StreamContractClient};
use crate::events::{Cancelled, Created, Withdrawn};
use crate::storage::{self, DataKey, BUMP_THRESHOLD, ENTRY_TTL};
use crate::{Stream, StreamError, StreamStatus, MAX_AMOUNT};

Expand Down Expand Up @@ -164,6 +165,17 @@ impl<'a> StreamTest<'a> {
]
}

/// Assert the latest event emitted by the stream contract has exactly the
/// payload generated by the typed event wrapper.
pub fn assert_latest_stream_event<E: Event>(&self, expected: E) {
let expected = expected.to_xdr(&self.env, &self.contract.address);
let all_events = self.env.events().all();
let latest = all_events.events().last().unwrap();
assert_eq!(latest.ext, expected.ext);
assert_eq!(latest.type_, expected.type_);
assert_eq!(latest.body, expected.body);
}

/// Whether a persistent entry exists under `key`, read straight out of the
/// contract's storage rather than through an entry point. Entry points
/// answer `StreamNotFound` for both "no such key" and "key holds
Expand Down Expand Up @@ -1944,6 +1956,96 @@ fn lifecycle_events_follow_operation_order() {
assert_eq!(t.event_publishers(), t.transfer_then_announce());
}

#[test]
fn created_event_payload_matches_opened_stream() {
let t = StreamTest::setup(1_000);
t.set_time(100);

let id = t.contract.create_stream(
&t.sender,
&t.recipient,
&t.token_address,
&750,
&100,
&1_100,
&400,
);

assert_eq!(id, 0);
t.assert_latest_stream_event(Created {
sender: t.sender.clone(),
recipient: t.recipient.clone(),
id,
token: t.token_address.clone(),
total_amount: 750,
start_time: 100,
end_time: 1_100,
cliff_time: 400,
});

assert_eq!(t.token.balance(&t.sender), 250);
assert_eq!(t.token.balance(&t.contract.address), 750);

let stream = t.contract.get_stream(&id);
assert_eq!(stream.sender, t.sender);
assert_eq!(stream.recipient, t.recipient);
assert_eq!(stream.token, t.token_address);
assert_eq!(stream.total_amount, 750);
assert_eq!(stream.start_time, 100);
assert_eq!(stream.end_time, 1_100);
assert_eq!(stream.cliff_time, 400);
}

#[test]
fn withdrawn_event_payload_matches_paid_amount() {
let t = StreamTest::setup(1_000);
t.set_time(100);
let id = t.open_default_stream(1_000);

t.set_time(600);
let amount = t.contract.withdraw(&id);

assert_eq!(amount, 500);
t.assert_latest_stream_event(Withdrawn {
recipient: t.recipient.clone(),
id,
amount,
});

assert_eq!(t.token.balance(&t.recipient), 500);
assert_eq!(t.token.balance(&t.contract.address), 500);
assert_eq!(t.contract.get_stream(&id).withdrawn, 500);
}

#[test]
fn cancelled_event_payload_matches_refund_split() {
let t = StreamTest::setup(1_000);
t.set_time(100);
let id = t.open_default_stream(1_000);

t.set_time(600);
let refund = t.contract.cancel(&id);

assert_eq!(refund, 500);
t.assert_latest_stream_event(Cancelled {
sender: t.sender.clone(),
id,
recipient_amount: 500,
sender_refund: refund,
});

assert_eq!(t.token.balance(&t.sender), 500);
assert_eq!(t.token.balance(&t.recipient), 0);
assert_eq!(t.token.balance(&t.contract.address), 500);
assert_eq!(t.contract.status(&id), StreamStatus::Cancelled);

let stream = t.contract.get_stream(&id);
assert!(stream.cancelled);
assert_eq!(stream.total_amount, 500);
assert_eq!(stream.withdrawn, 0);
assert_eq!(stream.end_time, 600);
}

/// A creation rejected for an invalid participant publishes nothing: the
/// participant checks run before any token moves, so there is neither a
/// transfer event nor a `Created` event for an indexer to unwind.
Expand Down