diff --git a/src/base/events.cairo b/src/base/events.cairo new file mode 100644 index 0000000..352db76 --- /dev/null +++ b/src/base/events.cairo @@ -0,0 +1,271 @@ +use starknet::{ + ContractAddress +}; +use crate::base::types::{Permissions,VerificationType,RefundRequestReason,ContentUpdateType}; +#[derive(Drop, starknet::Event)] +pub struct TokenBoundAccountCreated { + pub id: u256, +} + + +#[derive(Drop, starknet::Event)] +pub struct EmergencyPaused { + pub paused_by: ContractAddress, + pub timestamp: u64, +} + +#[derive(Drop, starknet::Event)] +pub struct EmergencyUnpause { + pub unpaused_by: ContractAddress, + pub timestamp: u64, +} + +#[derive(Drop, starknet::Event)] +pub struct UserUpdated { + pub user_id: u256, +} + +#[derive(Drop, starknet::Event)] +pub struct SubscriptionCreated { + pub user_id: u256, + pub end_date: u64, + pub amount: u256, +} + +#[derive(Drop, starknet::Event)] +pub struct SubscriptionRenewed { + pub user: ContractAddress, + pub subscription_id: u256, + pub new_end_time: u64, +} + +#[derive(Drop, starknet::Event)] +pub struct ReceiptGenerated { + pub receipt_id: u256, +} + +#[derive(Drop, starknet::Event)] +pub struct SubscriptionCancelled { + pub user: ContractAddress, + pub subscription_id: u256, +} +#[derive(Drop, starknet::Event)] +pub struct AccessVerified { + pub user_id: u256, + pub content_id: felt252, + pub has_access: bool, +} + +#[derive(Drop, starknet::Event)] +pub struct UserCreated { + pub id: u256, +} + +#[derive(Drop, starknet::Event)] +pub struct PaymentProcessed { + pub payment_id: u256, + pub subscription_id: u256, + pub subscriber: ContractAddress, + pub amount: u256, + pub timestamp: u64, +} + +#[derive(Drop, starknet::Event)] +pub struct RecurringPaymentProcessed { + pub payment_id: u256, + pub subscription_id: u256, + pub subscriber: ContractAddress, + pub amount: u256, + pub timestamp: u64, +} + +#[derive(Drop, starknet::Event)] +pub struct PaymentVerified { + pub payment_id: u256, + pub subscription_id: u256, + pub timestamp: u64, +} + +#[derive(Drop, starknet::Event)] +pub struct RefundProcessed { + pub payment_id: u256, + pub subscription_id: u256, + pub amount: u256, + pub timestamp: u64, +} + +// Permission-related events +#[derive(Drop, starknet::Event)] +pub struct PermissionGranted { + pub account_id: u256, + pub operator: ContractAddress, + pub permissions: Permissions, +} + +#[derive(Drop, starknet::Event)] +pub struct PermissionRevoked { + pub account_id: u256, + pub operator: ContractAddress, +} + +#[derive(Drop, starknet::Event)] +pub struct PermissionModified { + pub account_id: u256, + pub permissions: Permissions, +} + +// NEW - Delegation-related events +#[derive(Drop, starknet::Event)] +pub struct DelegationCreated { + pub delegator: ContractAddress, + pub delegate: ContractAddress, + pub permissions: u64, + pub expiration: u64, + pub max_actions: u64, +} + +#[derive(Drop, starknet::Event)] +pub struct DelegationRevoked { + pub delegator: ContractAddress, + pub delegate: ContractAddress, +} + +#[derive(Drop, starknet::Event)] +pub struct DelegationUsed { + pub delegator: ContractAddress, + pub delegate: ContractAddress, + pub permission: u64, + pub remaining_actions: u64, +} + +#[derive(Drop, starknet::Event)] +pub struct DelegationExpired { + pub delegator: ContractAddress, + pub delegate: ContractAddress, +} + +#[derive(Drop, starknet::Event)] +pub struct ContentRegistered { + pub content_id: felt252, + pub creator: ContractAddress, +} + +#[derive(Drop, starknet::Event)] +pub struct UserVerificationStatusChanged { + #[key] + pub user: ContractAddress, + pub verification_type: VerificationType, + pub is_verified: bool, + pub timestamp: u64, +} + +#[derive(Drop, starknet::Event)] +pub struct ContentPermissionsGranted { + #[key] + pub content_id: felt252, + #[key] + pub user: ContractAddress, + pub permissions: Permissions, + pub timestamp: u64, +} + +#[derive(Drop, starknet::Event)] +pub struct ContentPurchased { + pub purchase_id: u256, + pub content_id: felt252, + pub buyer: ContractAddress, + pub price: u256, + pub timestamp: u64, +} + +#[derive(Drop, starknet::Event)] +pub struct PurchaseStatusUpdated { + pub purchase_id: u256, + pub new_status: u8, // Using u8 for status code instead of PurchaseStatus enum + pub timestamp: u64, +} + +// Payout and Refunds +#[derive(Drop, starknet::Event)] +pub struct PayoutExecuted { + pub recipients: Array, + pub timestamp: u64, + pub amount_paid: u256, +} + +#[derive(Drop, starknet::Event)] +pub struct PayoutScheduleSet { + pub start_time: u64, + pub setter: ContractAddress, + pub interval: u64, +} + +#[derive(Drop, starknet::Event)] +pub struct RefundRequested { + pub user: ContractAddress, + pub content_id: felt252, + pub purchase_id: u256, + pub reason: RefundRequestReason, +} + +#[derive(Drop, starknet::Event)] +pub struct RefundApproved { + pub approver: ContractAddress, + pub user_id: u256, + pub content_id: felt252, + pub refund_id: u64, +} + +#[derive(Drop, starknet::Event)] +pub struct RefundDeclined { + pub decliner: ContractAddress, + pub user_id: u256, + pub content_id: felt252, + pub refund_id: u64, +} + +#[derive(Drop, starknet::Event)] +pub struct RefundTimedOut { + pub user_id: u256, + pub content_id: felt252, + pub refund_id: u64, +} + +#[derive(Drop, starknet::Event)] +pub struct RefundPaid { + pub refund_id: u64, + pub content_id: felt252, + pub purchase_id: u256, + pub executor: ContractAddress, + pub user_id: u256, +} + +#[derive(Drop, starknet::Event)] +pub struct PlatformFeeChanged { + pub new_fee: u256, + pub timestamp: u64, +} + +#[derive(Drop, starknet::Event)] +pub struct RefundWindowChanged { + pub new_window: u64, + pub timestamp: u64, +} + +#[derive(Drop, starknet::Event)] +pub struct ContentUpdated { + pub content_id: felt252, + pub updater: ContractAddress, + pub version: u64, + pub update_type: ContentUpdateType, + pub timestamp: u64, +} + +#[derive(Drop, starknet::Event)] +pub struct ContentUpdateHistoryRecorded { + pub content_id: felt252, + pub version: u64, + pub updater: ContractAddress, + pub update_type: ContentUpdateType, + pub timestamp: u64, +} diff --git a/src/base/types.cairo b/src/base/types.cairo index 8768731..a78c07b 100644 --- a/src/base/types.cairo +++ b/src/base/types.cairo @@ -225,3 +225,118 @@ pub struct Receipt { pub issued_at: u64, pub transaction_hash: felt252, } + + +#[derive(Copy, Drop, Serde, starknet::Store, PartialEq, Debug)] +pub enum ContentType { + #[default] + Text, + Video, + Image, + // Any other content type +} + +#[derive(Copy, Drop, Serde, starknet::Store, PartialEq, Debug)] +pub enum Category { + Software, + #[default] + Education, + Literature, + Art, +} + +#[derive(Copy, Drop, Serde, starknet::Store, PartialEq, Debug)] +pub enum SubscriptionStatus { + Active, + #[default] + Inactive, + Cancelled, +} + +#[derive(Copy, Drop, Serde, starknet::Store, Debug)] +pub struct ContentMetadata { + pub content_id: felt252, + pub title: felt252, + pub description: felt252, + pub content_type: ContentType, + pub creator: ContractAddress, + pub category: Category, + pub last_updated: u64, + pub version: u64, +} + +#[derive(Copy, Drop, Serde, starknet::Store, Debug)] +pub struct ContentUpdateHistory { + pub content_id: felt252, + pub version: u64, + pub updater: ContractAddress, + pub timestamp: u64, + pub update_type: ContentUpdateType, + pub previous_title: felt252, + pub previous_description: felt252, + pub previous_content_type: ContentType, + pub previous_category: Category, + pub new_title: felt252, + pub new_description: felt252, + pub new_content_type: ContentType, + pub new_category: Category, +} + +#[derive(Copy, Drop, Serde, starknet::Store, PartialEq, Debug)] +pub enum ContentUpdateType { + #[default] + Full, + Partial, + Title, + Description, + ContentType, + Category, +} + +#[derive(Drop, Serde, starknet::Store, Clone)] +pub struct Subscription { + pub id: u256, + pub subscriber: ContractAddress, + pub plan_id: u256, + pub amount: u256, + pub start_date: u64, + pub end_date: u64, + pub is_active: bool, + pub last_payment_date: u64, + pub subscription_type: PlanType, + pub status: SubscriptionStatus, +} + +#[derive(Drop, Serde, starknet::Store, Clone, PartialEq)] +pub enum PlanType { + #[default] + MONTHLY, + YEARLY, + TRIAL, +} + +#[derive(Copy, Drop, Serde, starknet::Store, Debug)] +pub struct AccessCache { + pub user_id: u256, + pub content_id: felt252, + pub has_access: bool, + pub timestamp: u64, + pub expiry: u64, +} +#[derive(Copy, Drop, Serde, starknet::Store, Debug)] +pub struct ContentAccess { + pub content_id: felt252, + pub access_type: AccessType, + pub requires_subscription: bool, + pub is_premium: bool, +} + +#[derive(Copy, Drop, Serde, starknet::Store, Debug)] +pub struct Payment { + pub id: u256, + pub subscription_id: u256, + pub amount: u256, + pub timestamp: u64, + pub is_verified: bool, + pub is_refunded: bool, +} diff --git a/src/chainlib/ChainLib.cairo b/src/chainlib/ChainLib.cairo index 5ccd54b..6ca22a1 100644 --- a/src/chainlib/ChainLib.cairo +++ b/src/chainlib/ChainLib.cairo @@ -14,141 +14,27 @@ pub mod ChainLib { get_contract_address, }; use crate::base::errors::{payment_errors, permission_errors}; + use crate::base::events::{ + AccessVerified, ContentPermissionsGranted, ContentPurchased, ContentRegistered, + ContentUpdateHistoryRecorded, ContentUpdated, DelegationCreated, DelegationExpired, + DelegationRevoked, DelegationUsed, EmergencyPaused, EmergencyUnpause, PaymentProcessed, + PaymentVerified, PayoutExecuted, PayoutScheduleSet, PermissionGranted, PermissionModified, + PermissionRevoked, PlatformFeeChanged, PurchaseStatusUpdated, ReceiptGenerated, + RecurringPaymentProcessed, RefundApproved, RefundDeclined, RefundPaid, RefundProcessed, + RefundRequested, RefundTimedOut, RefundWindowChanged, SubscriptionCancelled, + SubscriptionCreated, SubscriptionRenewed, TokenBoundAccountCreated, UserCreated, + UserUpdated, UserVerificationStatusChanged, + }; use crate::base::types::{ - AccessRule, AccessType, Payout, PayoutSchedule, PayoutStatus, Permissions, Purchase, - PurchaseStatus, Rank, Receipt, ReceiptStatus, Refund, RefundRequestReason, RefundStatus, - Role, Status, TokenBoundAccount, User, VerificationRequirement, VerificationType, - delegation_flags, permission_flags, + AccessCache, AccessRule, AccessType, ContentAccess, ContentMetadata, ContentType, + ContentUpdateHistory, ContentUpdateType, Payment, Payout, PayoutSchedule, PayoutStatus, + Permissions, PlanType, Purchase, PurchaseStatus, Rank, Receipt, ReceiptStatus, Refund, + RefundRequestReason, RefundStatus, Role, Status, Subscription, SubscriptionStatus, + TokenBoundAccount, User, VerificationRequirement, VerificationType, delegation_flags, + permission_flags, Category,DelegationInfo }; use crate::interfaces::IChainLib::IChainLib; - // Define delegation-specific structures and constants - - #[derive(Copy, Drop, Serde, starknet::Store, Debug)] - pub struct DelegationInfo { - pub delegator: ContractAddress, // The account owner who created the delegation - pub delegate: ContractAddress, // The account that receives delegated permissions - pub permissions: u64, // Delegated permissions as bit flags - pub expiration: u64, // Timestamp when delegation expires (0 = no expiration) - pub max_actions: u64, // Maximum number of actions allowed (0 = unlimited) - pub action_count: u64, // Current number of actions performed - pub active: bool // Whether this delegation is active - } - - #[derive(Copy, Drop, Serde, starknet::Store, PartialEq, Debug)] - pub enum ContentType { - #[default] - Text, - Video, - Image, - // Any other content type - } - - #[derive(Copy, Drop, Serde, starknet::Store, PartialEq, Debug)] - pub enum Category { - Software, - #[default] - Education, - Literature, - Art, - } - - #[derive(Copy, Drop, Serde, starknet::Store, PartialEq, Debug)] - pub enum SubscriptionStatus { - Active, - #[default] - Inactive, - Cancelled, - } - - #[derive(Copy, Drop, Serde, starknet::Store, Debug)] - pub struct ContentMetadata { - pub content_id: felt252, - pub title: felt252, - pub description: felt252, - pub content_type: ContentType, - pub creator: ContractAddress, - pub category: Category, - pub last_updated: u64, - pub version: u64, - } - - #[derive(Copy, Drop, Serde, starknet::Store, Debug)] - pub struct ContentUpdateHistory { - pub content_id: felt252, - pub version: u64, - pub updater: ContractAddress, - pub timestamp: u64, - pub update_type: ContentUpdateType, - pub previous_title: felt252, - pub previous_description: felt252, - pub previous_content_type: ContentType, - pub previous_category: Category, - pub new_title: felt252, - pub new_description: felt252, - pub new_content_type: ContentType, - pub new_category: Category, - } - - #[derive(Copy, Drop, Serde, starknet::Store, PartialEq, Debug)] - pub enum ContentUpdateType { - #[default] - Full, - Partial, - Title, - Description, - ContentType, - Category, - } - - #[derive(Drop, Serde, starknet::Store, Clone)] - pub struct Subscription { - pub id: u256, - pub subscriber: ContractAddress, - pub plan_id: u256, - pub amount: u256, - pub start_date: u64, - pub end_date: u64, - pub is_active: bool, - pub last_payment_date: u64, - pub subscription_type: PlanType, - pub status: SubscriptionStatus, - } - - #[derive(Drop, Serde, starknet::Store, Clone, PartialEq)] - pub enum PlanType { - #[default] - MONTHLY, - YEARLY, - TRIAL, - } - - #[derive(Copy, Drop, Serde, starknet::Store, Debug)] - pub struct AccessCache { - pub user_id: u256, - pub content_id: felt252, - pub has_access: bool, - pub timestamp: u64, - pub expiry: u64, - } - #[derive(Copy, Drop, Serde, starknet::Store, Debug)] - pub struct ContentAccess { - pub content_id: felt252, - pub access_type: AccessType, - pub requires_subscription: bool, - pub is_premium: bool, - } - - #[derive(Copy, Drop, Serde, starknet::Store, Debug)] - pub struct Payment { - pub id: u256, - pub subscription_id: u256, - pub amount: u256, - pub timestamp: u64, - pub is_verified: bool, - pub is_refunded: bool, - } - #[storage] struct Storage { admin: ContractAddress, // Address of the contract admin @@ -343,273 +229,6 @@ pub mod ChainLib { ContentUpdateHistoryRecorded: ContentUpdateHistoryRecorded, } - #[derive(Drop, starknet::Event)] - pub struct TokenBoundAccountCreated { - pub id: u256, - } - - - #[derive(Drop, starknet::Event)] - pub struct EmergencyPaused { - pub paused_by: ContractAddress, - pub timestamp: u64, - } - - #[derive(Drop, starknet::Event)] - pub struct EmergencyUnpause { - pub unpaused_by: ContractAddress, - pub timestamp: u64, - } - - #[derive(Drop, starknet::Event)] - pub struct UserUpdated { - pub user_id: u256, - } - - #[derive(Drop, starknet::Event)] - pub struct SubscriptionCreated { - pub user_id: u256, - pub end_date: u64, - pub amount: u256, - } - - #[derive(Drop, starknet::Event)] - struct SubscriptionRenewed { - user: ContractAddress, - subscription_id: u256, - new_end_time: u64, - } - - #[derive(Drop, starknet::Event)] - struct ReceiptGenerated { - receipt_id: u256, - } - - #[derive(Drop, starknet::Event)] - struct SubscriptionCancelled { - user: ContractAddress, - subscription_id: u256, - } - #[derive(Drop, starknet::Event)] - pub struct AccessVerified { - pub user_id: u256, - pub content_id: felt252, - pub has_access: bool, - } - - #[derive(Drop, starknet::Event)] - pub struct UserCreated { - pub id: u256, - } - - #[derive(Drop, starknet::Event)] - pub struct PaymentProcessed { - pub payment_id: u256, - pub subscription_id: u256, - pub subscriber: ContractAddress, - pub amount: u256, - pub timestamp: u64, - } - - #[derive(Drop, starknet::Event)] - pub struct RecurringPaymentProcessed { - pub payment_id: u256, - pub subscription_id: u256, - pub subscriber: ContractAddress, - pub amount: u256, - pub timestamp: u64, - } - - #[derive(Drop, starknet::Event)] - pub struct PaymentVerified { - pub payment_id: u256, - pub subscription_id: u256, - pub timestamp: u64, - } - - #[derive(Drop, starknet::Event)] - pub struct RefundProcessed { - pub payment_id: u256, - pub subscription_id: u256, - pub amount: u256, - pub timestamp: u64, - } - - // Permission-related events - #[derive(Drop, starknet::Event)] - pub struct PermissionGranted { - pub account_id: u256, - pub operator: ContractAddress, - pub permissions: Permissions, - } - - #[derive(Drop, starknet::Event)] - pub struct PermissionRevoked { - pub account_id: u256, - pub operator: ContractAddress, - } - - #[derive(Drop, starknet::Event)] - pub struct PermissionModified { - pub account_id: u256, - pub permissions: Permissions, - } - - // NEW - Delegation-related events - #[derive(Drop, starknet::Event)] - pub struct DelegationCreated { - pub delegator: ContractAddress, - pub delegate: ContractAddress, - pub permissions: u64, - pub expiration: u64, - pub max_actions: u64, - } - - #[derive(Drop, starknet::Event)] - pub struct DelegationRevoked { - pub delegator: ContractAddress, - pub delegate: ContractAddress, - } - - #[derive(Drop, starknet::Event)] - pub struct DelegationUsed { - pub delegator: ContractAddress, - pub delegate: ContractAddress, - pub permission: u64, - pub remaining_actions: u64, - } - - #[derive(Drop, starknet::Event)] - pub struct DelegationExpired { - pub delegator: ContractAddress, - pub delegate: ContractAddress, - } - - #[derive(Drop, starknet::Event)] - pub struct ContentRegistered { - pub content_id: felt252, - pub creator: ContractAddress, - } - - #[derive(Drop, starknet::Event)] - pub struct UserVerificationStatusChanged { - #[key] - pub user: ContractAddress, - pub verification_type: VerificationType, - pub is_verified: bool, - pub timestamp: u64, - } - - #[derive(Drop, starknet::Event)] - pub struct ContentPermissionsGranted { - #[key] - pub content_id: felt252, - #[key] - pub user: ContractAddress, - pub permissions: Permissions, - pub timestamp: u64, - } - - #[derive(Drop, starknet::Event)] - pub struct ContentPurchased { - pub purchase_id: u256, - pub content_id: felt252, - pub buyer: ContractAddress, - pub price: u256, - pub timestamp: u64, - } - - #[derive(Drop, starknet::Event)] - pub struct PurchaseStatusUpdated { - pub purchase_id: u256, - pub new_status: u8, // Using u8 for status code instead of PurchaseStatus enum - pub timestamp: u64, - } - - // Payout and Refunds - #[derive(Drop, starknet::Event)] - pub struct PayoutExecuted { - pub recipients: Array, - pub timestamp: u64, - pub amount_paid: u256, - } - - #[derive(Drop, starknet::Event)] - pub struct PayoutScheduleSet { - pub start_time: u64, - pub setter: ContractAddress, - pub interval: u64, - } - - #[derive(Drop, starknet::Event)] - pub struct RefundRequested { - pub user: ContractAddress, - pub content_id: felt252, - pub purchase_id: u256, - pub reason: RefundRequestReason, - } - - #[derive(Drop, starknet::Event)] - pub struct RefundApproved { - pub approver: ContractAddress, - pub user_id: u256, - pub content_id: felt252, - pub refund_id: u64, - } - - #[derive(Drop, starknet::Event)] - pub struct RefundDeclined { - pub decliner: ContractAddress, - pub user_id: u256, - pub content_id: felt252, - pub refund_id: u64, - } - - #[derive(Drop, starknet::Event)] - pub struct RefundTimedOut { - pub user_id: u256, - pub content_id: felt252, - pub refund_id: u64, - } - - #[derive(Drop, starknet::Event)] - pub struct RefundPaid { - pub refund_id: u64, - pub content_id: felt252, - pub purchase_id: u256, - pub executor: ContractAddress, - pub user_id: u256, - } - - #[derive(Drop, starknet::Event)] - pub struct PlatformFeeChanged { - pub new_fee: u256, - pub timestamp: u64, - } - - #[derive(Drop, starknet::Event)] - pub struct RefundWindowChanged { - pub new_window: u64, - pub timestamp: u64, - } - - #[derive(Drop, starknet::Event)] - pub struct ContentUpdated { - pub content_id: felt252, - pub updater: ContractAddress, - pub version: u64, - pub update_type: ContentUpdateType, - pub timestamp: u64, - } - - #[derive(Drop, starknet::Event)] - pub struct ContentUpdateHistoryRecorded { - pub content_id: felt252, - pub version: u64, - pub updater: ContractAddress, - pub update_type: ContentUpdateType, - pub timestamp: u64, - } #[abi(embed_v0)] impl ChainLibNetImpl of IChainLib { diff --git a/src/interfaces/IChainLib.cairo b/src/interfaces/IChainLib.cairo index 765126e..7cb980c 100644 --- a/src/interfaces/IChainLib.cairo +++ b/src/interfaces/IChainLib.cairo @@ -4,7 +4,7 @@ use crate::base::types::{ AccessRule, Permissions, Purchase, PurchaseStatus, Rank, Receipt, Refund, RefundRequestReason, Role, TokenBoundAccount, User, VerificationRequirement, VerificationType, }; -use crate::chainlib::ChainLib::ChainLib::{ +use crate::base::types::{ Category, ContentMetadata, ContentType, ContentUpdateHistory, ContentUpdateType, DelegationInfo, Payment, PlanType, Subscription, }; diff --git a/src/lib.cairo b/src/lib.cairo index cb802eb..a21e2b6 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,6 +1,7 @@ pub mod base { pub mod errors; pub mod types; + pub mod events; } pub mod chainlib { pub mod ChainLib;