Skip to content
Open
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
1 change: 1 addition & 0 deletions src/base.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod errors;
pub mod helpers;
pub mod types;
3 changes: 3 additions & 0 deletions src/base/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@ pub mod Errors {

/// Thrown when wrong recipient or delegate
pub const WRONG_RECIPIENT_OR_DELEGATE: felt252 = 'WRONG_RECIPIENT_OR_DELEGATE';

/// @notice Thrown when trying to withdraw an amount greater than the withdrawable amount.
pub const OVERDRAW: felt252 = 'Error: Overdraw';
}

25 changes: 25 additions & 0 deletions src/base/helpers.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub mod Helpers {
use core::num::traits::Pow;

/// @dev Descales the provided `amount` from 18 decimals fixed-point number to token's decimals
/// number.
pub fn descale_amount(amount: u256, decimals: u8) -> u256 {
if decimals == 18 {
return amount;
}

let scale_factor = 10_u256.pow(18 - decimals.into());
return amount / scale_factor;
}

/// @dev Scales the provided `amount` from token's decimals number to 18 decimals fixed-point
/// number.
pub fn scale_amount(amount: u256, decimals: u8) -> u256 {
if decimals == 18 {
return amount;
}

let scale_factor = 10_u256.pow(18 - decimals.into());
return amount * scale_factor;
}
}
5 changes: 3 additions & 2 deletions src/base/types.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ pub struct Stream {
pub balance: u256,
pub status: StreamStatus,
pub rate_per_second: UFixedPoint123x128,
pub last_update_time: u64,
pub snapshot_debt_scaled: u256,
pub snapshot_time: u64,
pub transferable: bool,
}

Expand Down Expand Up @@ -45,7 +46,7 @@ pub struct WeightedDistribution {
}

/// @notice Enum representing the possible states of a stream
#[derive(Drop, Serde, starknet::Store, PartialEq)]
#[derive(Drop, Serde, starknet::Store, PartialEq, Debug)]
#[allow(starknet::store_no_default_variant)]
pub enum StreamStatus {
Active, // Stream is actively streaming tokens
Expand Down
32 changes: 27 additions & 5 deletions src/interfaces/IPaymentStream.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use fp::UFixedPoint123x128;
use starknet::ContractAddress;
use crate::base::types::{ProtocolMetrics, Stream, StreamMetrics};
use crate::base::types::{ProtocolMetrics, Stream, StreamMetrics, StreamStatus};

/// @title IPaymentStream
/// @notice Creates and manages payment streams with linear streaming functions.
Expand Down Expand Up @@ -192,10 +192,6 @@ pub trait IPaymentStream<TContractState> {
/// @param transferable Boolean indicating if the stream can be transferred
fn set_transferability(ref self: TContractState, stream_id: u256, transferable: bool);

/// @notice Checks if the stream is transferable
/// @param stream_id The ID of the stream to check
/// @return Boolean indicating if the stream is transferable
fn is_transferable(self: @TContractState, stream_id: u256) -> bool;
/// @notice Gets the protocol fee of the token
/// @param token The ContractAddress of the token
/// @return u256 The fee of the token
Expand Down Expand Up @@ -310,6 +306,32 @@ pub trait IPaymentStream<TContractState> {
/// @return Boolean indicating if the refund and pause operation was successful
fn refund_and_pause(ref self: TContractState, stream_id: u256, amount: u256) -> bool;

/// @notice Returns the stream's status.
/// @dev Reverts if `stream_id` references a null stream.
/// Integrators should exercise caution when depending on the return value of this function as
/// streams can be paused and resumed at any moment.
/// @param stream_id The stream ID for the query.
fn status_of(self: @TContractState, stream_id: u256) -> StreamStatus;

/// @notice Returns the amount of debt accrued since the snapshot time until now, denoted as a
/// fixed-point number where 1e18 is 1 token.
/// @dev Reverts if `stream_id` references a null stream.
/// @param stream_id The stream ID for the query.
fn ongoing_debt_scaled_of(self: @TContractState, stream_id: u256) -> u256;

/// @notice Calculates the amount that the recipient can withdraw from the stream, denoted in
/// token decimals.
/// @dev Reverts if `stream_id` references a null stream.
/// @param stream_id The stream ID for the query.
/// @return withdrawableAmount The amount that the recipient can withdraw.
fn covered_debt_of(self: @TContractState, stream_id: u256) -> u128;

/// @notice Returns the amount of debt not covered by the stream balance, denoted in token's
/// decimals.
/// @dev Reverts if `stream_id` references a null stream.
/// @param stream_id The stream ID for the query.
fn uncovered_debt_of(self: @TContractState, stream_id: u256) -> u256;

/// @notice Retrieves the sum of balances of all streams
/// @param token The ERC-20 token to query
/// @return The aggregated balance across all streams
Expand Down
Loading
Loading