Skip to content

Commit bf0c0da

Browse files
authored
Merge branch 'main' into 980-add-initialize-admin-tests
2 parents 0b88955 + 93f7a79 commit bf0c0da

5 files changed

Lines changed: 447 additions & 67 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 0.1.0 (2026-06-26)
1+
# 0.1.0 (2026-06-27)
22

33

44
### Bug Fixes
@@ -356,6 +356,7 @@
356356
* **subscriptions:** GET fan–creator subscription state with auth ([2fec38f](https://github.com/MyFanss/MyFans/commit/2fec38f7d73e046ce497399ec412220b12fd8044))
357357
* **subscriptions:** map query status string to SubscriptionStatus enum ([0661159](https://github.com/MyFanss/MyFans/commit/0661159312d2cc5051c8c91e5b9a8b398c24b314))
358358
* **subscriptions:** track worst-case simulation resource fees ([bc60859](https://github.com/MyFanss/MyFans/commit/bc60859f713b928c27bae13c1ca43ab3b3319f63))
359+
* **test-consumer:** add admin authorization with unauthorized caller tests ([b939826](https://github.com/MyFanss/MyFans/commit/b93982696ed600f3e7c1d759d61a73a50158ef0d)), closes [#981](https://github.com/MyFanss/MyFans/issues/981)
359360
* treasury deposit event ([d474a33](https://github.com/MyFanss/MyFans/commit/d474a3395761d872b51735bf3d783ac9246d3c35))
360361
* Treasury-min-balance guard ([5e0bcf1](https://github.com/MyFanss/MyFans/commit/5e0bcf1a114cf65c4f232375fbdb43e40089f427))
361362
* **treasury:** add minimum balance or emergency pause protection ([#851](https://github.com/MyFanss/MyFans/issues/851)) ([81cfbfc](https://github.com/MyFanss/MyFans/commit/81cfbfc570c50ca3a047c726cc41cb87ea839de5))
Lines changed: 136 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# MyFans Shared Library
22

3-
Shared types and enums for MyFans Soroban contracts.
3+
Shared types, enums, and error codes for MyFans Soroban contracts. This library provides common definitions used across all MyFans smart contracts, ensuring type safety and consistency.
44

5-
## Types
5+
## Public API
66

7-
### SubscriptionStatus
7+
### Enums
8+
9+
#### SubscriptionStatus
810

911
Represents the lifecycle status of a subscription.
1012

@@ -17,9 +19,15 @@ pub enum SubscriptionStatus {
1719
}
1820
```
1921

20-
### ContentType
22+
**Properties:**
23+
- Soroban-compatible (decorated with `#[contracttype]`)
24+
- Copy, Clone, Debug, Eq, PartialEq
25+
- Serializable/deserializable
26+
- Represented as `u32` for efficient storage
2127

22-
Represents content access type.
28+
#### ContentType
29+
30+
Represents content access type classification.
2331

2432
```rust
2533
pub enum ContentType {
@@ -28,7 +36,89 @@ pub enum ContentType {
2836
}
2937
```
3038

31-
## Usage
39+
**Properties:**
40+
- Soroban-compatible (decorated with `#[contracttype]`)
41+
- Copy, Clone, Debug, Eq, PartialEq
42+
- Serializable/deserializable
43+
- Represented as `u32` for efficient storage
44+
45+
#### MyfansError
46+
47+
Shared error enum across all MyFans contracts. Error codes are preserved exactly for test snapshot compatibility.
48+
49+
```rust
50+
pub enum MyfansError {
51+
// Common init/admin errors
52+
AlreadyInitialized = 1,
53+
NotInitialized = 2,
54+
NotAuthorized = 3,
55+
// Balance/transfer errors
56+
InsufficientBalance = 4,
57+
// Fee/config errors
58+
InvalidFeeBps = 5,
59+
// Spam/security
60+
RateLimited = 6,
61+
AlreadyRegistered = 7,
62+
NotLiked = 8,
63+
// State control
64+
Paused = 9,
65+
// content-access specific
66+
ContentPriceNotSet = 101,
67+
// subscription specific
68+
SubscriptionNotFound = 102,
69+
SubscriptionExpired = 103,
70+
AdminNotInitialized = 104,
71+
// treasury specific
72+
NegativeMinBalance = 105,
73+
MinBalanceViolation = 106,
74+
}
75+
```
76+
77+
**Properties:**
78+
- Decorated with `#[contracterror]` for Soroban error handling
79+
- Copy, Clone, Debug, Eq, PartialEq
80+
- Numeric discriminants are stable and fixed for compatibility
81+
82+
### Modules
83+
84+
#### error_codes
85+
86+
Provides stable numeric error code constants for each contract, enabling clients (TypeScript SDK, backend, frontend) to inspect Soroban error results without hard-coding magic numbers.
87+
88+
**Usage:**
89+
```rust
90+
use myfans_lib::error_codes::subscription as sub_err;
91+
92+
// Check whether a Soroban invocation failed with SubscriptionNotFound:
93+
// if result.err() == Some(sub_err::SUBSCRIPTION_NOT_FOUND) { ... }
94+
```
95+
96+
**Available sub-modules:**
97+
- `subscription` - Error codes for the subscription contract
98+
- `content_access` - Error codes for the content-access contract
99+
- `content_likes` - Error codes for the content-likes contract
100+
- `creator_registry` - Error codes for the creator-registry contract
101+
- `treasury` - Error codes for the treasury contract
102+
- `earnings` - Error codes for the earnings contract
103+
- `creator_earnings` - Error codes for the creator-earnings contract
104+
- `creator_deposits` - Error codes for the creator-deposits contract
105+
- `myfans_contract` - Error codes for the main myfans-contract
106+
107+
**Note:** Error codes in each sub-module **must** match the `#[contracterror]` discriminants in the corresponding contract's `Error` enum. The `test-consumer` contract enforces this invariant through tests.
108+
109+
#### test_fixtures
110+
111+
Provides shared test fixtures for cross-contract integration tests. Only available when the `testutils` feature is enabled.
112+
113+
**Usage:**
114+
```rust
115+
[dev-dependencies]
116+
myfans-lib = { path = "../myfans-lib", features = ["testutils"] }
117+
```
118+
119+
## Getting Started
120+
121+
### Installation
32122

33123
Add to your contract's `Cargo.toml`:
34124

@@ -37,22 +127,45 @@ Add to your contract's `Cargo.toml`:
37127
myfans-lib = { path = "../myfans-lib" }
38128
```
39129

40-
Import in your contract:
130+
### Basic Example
41131

42132
```rust
43-
use myfans_lib::{SubscriptionStatus, ContentType};
133+
use myfans_lib::{SubscriptionStatus, ContentType, MyfansError};
134+
use soroban_sdk::{contract, contractimpl, Env};
44135

45136
#[contract]
46137
pub struct SubscriptionContract;
47138

48139
#[contractimpl]
49140
impl SubscriptionContract {
50-
pub fn create_subscription(env: Env) -> SubscriptionStatus {
51-
SubscriptionStatus::Pending
141+
/// Check if a subscription is active
142+
pub fn is_active(_env: Env, status: SubscriptionStatus) -> bool {
143+
status == SubscriptionStatus::Active
52144
}
53145

54-
pub fn get_content_type(env: Env) -> ContentType {
55-
ContentType::Paid
146+
/// Get the numeric discriminant of a content type
147+
pub fn content_code(_env: Env, ct: ContentType) -> u32 {
148+
ct as u32
149+
}
150+
151+
/// Handle errors by their numeric codes
152+
pub fn error_code(_env: Env, err: MyfansError) -> u32 {
153+
err as u32
154+
}
155+
}
156+
```
157+
158+
### Error Handling Example
159+
160+
```rust
161+
use myfans_lib::error_codes::subscription as sub_err;
162+
163+
fn handle_error(code: u32) {
164+
match code {
165+
sub_err::SUBSCRIPTION_NOT_FOUND => println!("Subscription not found"),
166+
sub_err::SUBSCRIPTION_EXPIRED => println!("Subscription expired"),
167+
sub_err::ALREADY_INITIALIZED => println!("Already initialized"),
168+
_ => println!("Unknown error"),
56169
}
57170
}
58171
```
@@ -65,16 +178,19 @@ Run tests:
65178
cargo test
66179
```
67180

68-
All enums are:
69-
- Soroban-compatible (using `#[contracttype]`)
70-
- Serializable/deserializable
71-
- Copy, Clone, Debug, Eq, PartialEq
72-
- Represented as u32 for efficient storage
181+
All types are thoroughly tested for:
182+
- Soroban SDK compatibility
183+
- Serialization/deserialization
184+
- Cross-contract type safety
185+
- Error code stability
73186

74187
## Features
75188

189+
- ✅ Shared type definitions across all MyFans contracts
190+
- ✅ Stable error codes for client integration
76191
- ✅ Soroban SDK integration
77-
-Serialization/deserialization support
192+
-Full serialization/deserialization support
78193
- ✅ Type-safe enum values
79-
- ✅ Comprehensive tests
80-
- ✅ Ready for cross-contract usage
194+
- ✅ Comprehensive test coverage
195+
- ✅ Cross-contract compatibility
196+
- ✅ Zero-runtime overhead (compile-time only)

0 commit comments

Comments
 (0)