-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.rs
711 lines (608 loc) · 21.1 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
use anchor_lang::prelude::*;
use anchor_spl::token::{Token, TokenAccount, Transfer as SplTransfer};
use anchor_spl::associated_token::get_associated_token_address;
declare_id!("AnPhQYFcJEPBG2JTrvaNne85rXufC1Q97bu29YaWvKDs");
const MIN_SUBSCRIPTION_PERIOD: i64 = 86400; // 1 day in seconds
const MAX_QUALITY_RECORDS: usize = 10;
#[program]
pub mod subscription_manager {
use anchor_spl::token;
use super::*;
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
let state = &mut ctx.accounts.state;
state.owner = ctx.accounts.owner.key();
state.nft_program_id = ctx.accounts.nft_program.key();
state.payment_spl_token = ctx.accounts.payment_spl_token.key();
state.fee_per_day = 1;
state.collector_fee = 1;
Ok(())
}
// Subscribe function
pub fn subscribe(
ctx: Context<Subscribe>,
recipient: String,
end_time: i64,
) -> Result<()> {
let state = &ctx.accounts.state;
let subscriber = &ctx.accounts.subscriber;
let data_provider = &ctx.accounts.data_provider;
let dp_token_account = &ctx.accounts.nft_token_account;
let dp_payment_to_ata = &ctx.accounts.data_provider_payment_ata;
let owner_payment_to_ata = &ctx.accounts.owner_payment_ata;
let subscriber_payment_from_ata = &ctx.accounts.subscriber_payment_ata;
// Validate NFT ownership
let expected_token_account = get_associated_token_address(
&data_provider.key(),
&state.nft_program_id,
);
require!(
dp_token_account.key() == expected_token_account,
SubscriptionError::InvalidTokenAccount
);
require!(
dp_token_account.owner == data_provider.key(),
SubscriptionError::InvalidNFTHolder
);
require!(
dp_token_account.amount > 0,
SubscriptionError::InvalidNFTHolder
);
// Validate subscription period
let current_time = Clock::get()?.unix_timestamp;
require!(
end_time >= current_time + MIN_SUBSCRIPTION_PERIOD,
SubscriptionError::PeriodTooShort
);
// Calculate fees
let duration = end_time - current_time;
let provider_fee = (duration * state.fee_per_day as i64) / MIN_SUBSCRIPTION_PERIOD;
// Transfer Fees
let cpi_accounts_to_owner = SplTransfer {
from: subscriber_payment_from_ata.to_account_info().clone(),
to: owner_payment_to_ata.to_account_info().clone(),
authority: subscriber.to_account_info().clone(),
};
let cpi_accounts_to_provider = SplTransfer {
from: subscriber_payment_from_ata.to_account_info().clone(),
to: dp_payment_to_ata.to_account_info().clone(),
authority: subscriber.to_account_info().clone(),
};
let cpi_program = ctx.accounts.token_program.to_account_info();
// Transfer to owner
token::transfer(
CpiContext::new(cpi_program.clone(), cpi_accounts_to_owner),
state.collector_fee,
)?;
token::transfer(
CpiContext::new(cpi_program, cpi_accounts_to_provider),
provider_fee as u64,
)?;
// Create subscription
let subscription = &mut ctx.accounts.subscription;
subscription.end_time = end_time;
subscription.recipient = recipient.clone();
// Add to subscribers list
let subscribers_list = &mut ctx.accounts.subscribers_list;
subscribers_list.subscribers.push(subscriber.key());
emit!(SubscriptionCreatedEvent {
data_provider: data_provider.key(),
subscriber: subscriber.key(),
recipient: recipient.clone(),
end_time,
timestamp: current_time,
});
Ok(())
}
pub fn renew_subscription(
ctx: Context<RenewSubscription>,
new_recipient: String,
new_end_time: i64,
quality: u8,
) -> Result<()> {
// Validate quality rating
require!(quality <= 100, SubscriptionError::QualityOutOfRange);
let subscription = &mut ctx.accounts.subscription;
let state = &ctx.accounts.state;
let current_time = Clock::get()?.unix_timestamp;
// Verify subscription exists (non-zero end time indicates existence)
require!(subscription.end_time > 0, SubscriptionError::SubscriptionNotFound);
// Calculate renewal time
let renewal_time = if subscription.end_time > current_time {
subscription.end_time
} else {
current_time
};
// Validate new subscription period
require!(
new_end_time >= renewal_time + MIN_SUBSCRIPTION_PERIOD,
SubscriptionError::PeriodTooShort
);
// Calculate fees
let extended_duration = new_end_time - renewal_time;
let additional_fee = (extended_duration * state.fee_per_day as i64) / MIN_SUBSCRIPTION_PERIOD;
// Transfer fees
let transfer_to_owner = anchor_lang::system_program::Transfer {
from: ctx.accounts.subscriber.to_account_info(),
to: ctx.accounts.owner.to_account_info(),
};
let transfer_to_provider = anchor_lang::system_program::Transfer {
from: ctx.accounts.subscriber.to_account_info(),
to: ctx.accounts.data_provider.to_account_info(),
};
anchor_lang::system_program::transfer(
CpiContext::new(
ctx.accounts.system_program.to_account_info(),
transfer_to_owner,
),
state.collector_fee,
)?;
anchor_lang::system_program::transfer(
CpiContext::new(
ctx.accounts.system_program.to_account_info(),
transfer_to_provider,
),
additional_fee as u64,
)?;
// Store quality rating
let current_index = ctx.accounts.quality_info.current_index as usize;
let subscriber_key = ctx.accounts.subscriber.key();
ctx.accounts.quality_info.qualities[current_index] = QualityRecord {
provider: subscriber_key,
quality: quality
};
ctx.accounts.quality_info.current_index = ((current_index as u8) + 1) % MAX_QUALITY_RECORDS as u8;
// Update subscription
subscription.end_time = new_end_time;
subscription.recipient = new_recipient.clone();
emit!(SubscriptionRenewedEvent {
data_provider: ctx.accounts.data_provider.key(),
subscriber: ctx.accounts.subscriber.key(),
new_recipient,
new_end_time,
timestamp: current_time,
});
Ok(())
}
pub fn cancel_subscription(
ctx: Context<CancelSubscription>,
quality: u8,
) -> Result<()> {
// Validate quality rating
require!(quality <= 100, SubscriptionError::QualityOutOfRange);
let subscription = &mut ctx.accounts.subscription;
let current_time = Clock::get()?.unix_timestamp;
// Verify subscription exists and hasn't ended
require!(subscription.end_time > 0, SubscriptionError::SubscriptionNotFound);
require!(
current_time < subscription.end_time,
SubscriptionError::SubscriptionAlreadyEnded
);
// Store quality rating
let quality_info = &mut ctx.accounts.quality_info;
let current_idx = quality_info.current_index as usize;
quality_info.qualities[current_idx] = QualityRecord {
provider: ctx.accounts.subscriber.key(),
quality: quality
};
quality_info.current_index = (current_idx as u8 + 1) % MAX_QUALITY_RECORDS as u8;
// Clear subscription
subscription.recipient = String::new();
subscription.end_time = 0;
emit!(SubscriptionCancelledEvent {
data_provider: ctx.accounts.data_provider.key(),
subscriber: ctx.accounts.subscriber.key(),
});
Ok(())
}
pub fn end_subscription(
ctx: Context<EndSubscription>,
quality: u8,
) -> Result<()> {
// Validate quality rating
require!(quality <= 100, SubscriptionError::QualityOutOfRange);
let subscription = &mut ctx.accounts.subscription;
let current_time = Clock::get()?.unix_timestamp;
// Verify subscription exists
require!(subscription.end_time > 0, SubscriptionError::SubscriptionNotFound);
// Verify subscription has ended
require!(
current_time >= subscription.end_time,
SubscriptionError::ActiveSubscription
);
// Store quality rating
let quality_info = &mut ctx.accounts.quality_info;
let current_idx = quality_info.current_index as usize;
quality_info.qualities[current_idx] = QualityRecord {
provider: ctx.accounts.subscriber.key(),
quality: quality
};
quality_info.current_index = (current_idx as u8 + 1) % MAX_QUALITY_RECORDS as u8;
// Clear subscription
subscription.recipient = String::new();
subscription.end_time = 0;
emit!(SubscriptionEndedEvent {
data_provider: ctx.accounts.data_provider.key(),
subscriber: ctx.accounts.subscriber.key(),
});
Ok(())
}
pub fn set_fee_per_day(
ctx: Context<AdminFunction>,
new_fee: u64
) -> Result<()> {
// Get mutable reference to state account
let state = &mut ctx.accounts.state;
// Update the fee
state.fee_per_day = new_fee;
// Emit the event
emit!(FeePerDayUpdatedEvent {
new_fee_per_day: new_fee,
});
Ok(())
}
pub fn set_collector_fee(
ctx: Context<AdminFunction>,
new_fee: u64
) -> Result<()> {
// Get mutable reference to state account
let state = &mut ctx.accounts.state;
// Update the fee
state.collector_fee = new_fee;
// Emit the event
emit!(CollectorFeeUpdatedEvent {
new_collector_fee: new_fee,
});
Ok(())
}
// Helper function to store quality data
pub fn store_data_quality(
ctx: Context<StoreQuality>,
quality: u8
) -> Result<()> {
// Validate quality rating
require!(quality <= 100, SubscriptionError::QualityOutOfRange);
// Get mutable reference to quality info account
let quality_info = &mut ctx.accounts.quality_info;
// Store the current index in a local variable
let current_idx = quality_info.current_index as usize;
// Store the new quality rating
quality_info.qualities[current_idx] = QualityRecord {
provider: ctx.accounts.subscriber.key(),
quality
};
// Update the current index using modulo for circular buffer
quality_info.current_index =
(current_idx as u8 + 1) % MAX_QUALITY_RECORDS as u8;
// Emit the quality provided event
emit!(QualityProvidedEvent {
data_provider: ctx.accounts.data_provider.key(),
subscriber: ctx.accounts.subscriber.key(),
quality,
});
Ok(())
}
pub fn get_subscribers(ctx: Context<GetSubscribers>) -> Result<Vec<Pubkey>> {
Ok(ctx.accounts.subscribers_list.subscribers.clone())
}
pub fn initialize_quality_info(ctx: Context<InitializeQualityInfo>) -> Result<()> {
let quality_info = &mut ctx.accounts.quality_info;
quality_info.current_index = 0;
quality_info.subscriber = Pubkey::default();
quality_info.quality = 0;
// Initialize the qualities vector with default values
quality_info.qualities = vec![
QualityRecord {
provider: Pubkey::default(),
quality: 0
}; // (Pubkey, quality) default pair
MAX_QUALITY_RECORDS // Create MAX_QUALITY_RECORDS number of entries
];
Ok(())
}
}
// Account structures
#[account]
pub struct State {
pub owner: Pubkey,
pub nft_program_id: Pubkey,
pub payment_spl_token: Pubkey,
pub fee_per_day: u64,
pub collector_fee: u64,
}
impl State {
pub const SIZE: usize = 32 + 32 + 8 + 8; // 2 Pubkeys (32 bytes each) + 2 u64s (8 bytes each)
}
#[account]
pub struct Subscription {
pub end_time: i64,
pub recipient: String,
}
impl Subscription {
pub const SIZE: usize = 8 + 32; // i64 (8 bytes) + String (estimated 32 bytes)
}
#[account]
pub struct SubscribersList {
pub subscribers: Vec<Pubkey>,
}
impl SubscribersList {
pub const SIZE: usize = 4 + (32 * 10); // Vec length (4 bytes) + space for 10 Pubkeys (32 bytes each)
}
#[account]
#[derive(Default)]
pub struct QualityInfo {
pub subscriber: Pubkey, // 32 bytes
pub quality: u8, // 1 byte
pub current_index: u8, // 1 byte
pub qualities: Vec<QualityRecord> // Using our new struct instead of tuple
}
impl QualityInfo {
pub const MAX_QUALITY_RECORDS: usize = 10; // Define max records for space calculation
pub const SIZE: usize = 8 + // discriminator
32 + // subscriber pubkey
1 + // quality
1 + // current_index
4 + // vec length
(Self::MAX_QUALITY_RECORDS * (32 + 1)); // qualities array size
}
// Context structs for instructions
#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(
init,
payer = owner,
space = 8 + State::SIZE,
seeds = [b"storage"],
bump
)]
pub state: Account<'info, State>,
#[account(mut)]
pub owner: Signer<'info>,
/// CHECK: This is the NFT program ID
pub nft_program: UncheckedAccount<'info>,
/// CHECK: SPL token account for payment
pub payment_spl_token: UncheckedAccount<'info>,
pub system_program: Program<'info, System>,
}
#[derive(Accounts)]
pub struct Subscribe<'info> {
#[account(mut)]
pub state: Account<'info, State>,
#[account(mut)]
pub subscriber: Signer<'info>,
/// CHECK: Data provider account
#[account(mut)]
pub data_provider: UncheckedAccount<'info>,
#[account(
init,
payer = subscriber,
space = 8 + Subscription::SIZE,
seeds = [b"subscription", subscriber.key().as_ref(), data_provider.key().as_ref()],
bump
)]
pub subscription: Account<'info, Subscription>,
#[account(
init_if_needed,
payer = subscriber,
space = 8 + SubscribersList::SIZE,
seeds = [b"subscribers", data_provider.key().as_ref()],
bump
)]
pub subscribers_list: Account<'info, SubscribersList>,
/// CHECK: Owner account from state
#[account(mut, constraint = owner.key() == state.owner)]
pub owner: UncheckedAccount<'info>,
#[account(mut)]
pub data_provider_payment_ata: Account<'info, TokenAccount>,
#[account(mut)]
pub subscriber_payment_ata: Account<'info, TokenAccount>,
#[account(mut)]
pub owner_payment_ata: Account<'info, TokenAccount>,
pub system_program: Program<'info, System>,
// Add token program and token account validations for NFT check
pub token_program: Program<'info, Token>,
pub nft_token_account: Account<'info, TokenAccount>,
}
#[derive(Accounts)]
pub struct RenewSubscription<'info> {
#[account(mut)]
pub state: Account<'info, State>,
#[account(mut)]
pub subscriber: Signer<'info>,
/// CHECK: Data provider account
#[account(mut)]
pub data_provider: UncheckedAccount<'info>,
#[account(
mut,
seeds = [b"subscription", subscriber.key().as_ref(), data_provider.key().as_ref()],
bump
)]
pub subscription: Account<'info, Subscription>,
#[account(
mut,
seeds = [b"quality", data_provider.key().as_ref()],
bump
)]
pub quality_info: Account<'info, QualityInfo>,
/// CHECK: Owner account from state
#[account(mut, constraint = owner.key() == state.owner)]
pub owner: UncheckedAccount<'info>,
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
pub nft_token_account: Account<'info, TokenAccount>,
}
#[derive(Accounts)]
pub struct CancelSubscription<'info> {
#[account(mut)]
pub subscriber: Signer<'info>,
/// CHECK: Data provider account
pub data_provider: UncheckedAccount<'info>,
#[account(
mut,
seeds = [b"subscription", subscriber.key().as_ref(), data_provider.key().as_ref()],
bump
)]
pub subscription: Account<'info, Subscription>,
#[account(
mut,
seeds = [b"quality", data_provider.key().as_ref()],
bump
)]
pub quality_info: Account<'info, QualityInfo>,
pub token_program: Program<'info, Token>,
pub nft_token_account: Account<'info, TokenAccount>,
}
#[derive(Accounts)]
pub struct EndSubscription<'info> {
#[account(mut)]
pub subscriber: Signer<'info>,
/// CHECK: Data provider account
pub data_provider: UncheckedAccount<'info>,
#[account(
mut,
seeds = [b"subscription", subscriber.key().as_ref(), data_provider.key().as_ref()],
bump
)]
pub subscription: Account<'info, Subscription>,
#[account(
mut,
seeds = [b"quality", data_provider.key().as_ref()],
bump
)]
pub quality_info: Account<'info, QualityInfo>,
}
#[derive(Accounts)]
pub struct GetSubscribers<'info> {
/// CHECK: Data provider account
pub data_provider: UncheckedAccount<'info>,
#[account(
seeds = [b"subscribers", data_provider.key().as_ref()],
bump
)]
pub subscribers_list: Account<'info, SubscribersList>,
}
// Context struct for admin functions
#[derive(Accounts)]
pub struct AdminFunction<'info> {
#[account(
mut,
constraint = state.owner == owner.key() @ SubscriptionError::NotOwner
)]
pub state: Account<'info, State>,
#[account(
signer,
constraint = owner.key() == state.owner @ SubscriptionError::NotOwner
)]
pub owner: Signer<'info>,
}
#[derive(Accounts)]
pub struct StoreDataQuality<'info> {
#[account(mut)]
pub subscriber: Signer<'info>,
/// CHECK: Data provider account
pub data_provider: AccountInfo<'info>,
#[account(mut, seeds = [b"quality", data_provider.key().as_ref()], bump)]
pub quality_info: Account<'info, QualityInfo>,
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
}
// Context struct for storing quality data
#[derive(Accounts)]
pub struct StoreQuality<'info> {
#[account(mut)]
pub subscriber: Signer<'info>,
/// CHECK: Data provider account
pub data_provider: UncheckedAccount<'info>,
#[account(
mut,
seeds = [b"quality", data_provider.key().as_ref()],
bump
)]
pub quality_info: Account<'info, QualityInfo>,
}
#[derive(Accounts)]
pub struct InitializeQualityInfo<'info> {
#[account(
init,
payer = payer,
// Calculate space for: discriminator + subscriber pubkey + quality + current_index + qualities vec
space = 8 + 32 + 1 + 1 + 4 + (MAX_QUALITY_RECORDS * (32 + 1)),
seeds = [b"quality", data_provider.key().as_ref()],
bump
)]
pub quality_info: Account<'info, QualityInfo>,
/// CHECK: Data provider account is just used as a seed for PDA
pub data_provider: UncheckedAccount<'info>,
#[account(mut)]
pub payer: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Default)]
pub struct QualityRecord {
pub provider: Pubkey,
pub quality: u8,
}
// Events
#[event]
pub struct SubscriptionCreatedEvent {
pub data_provider: Pubkey,
pub subscriber: Pubkey,
pub recipient: String,
pub end_time: i64,
pub timestamp: i64,
}
#[event]
pub struct SubscriptionRenewedEvent {
pub data_provider: Pubkey,
pub subscriber: Pubkey,
pub new_recipient: String,
pub new_end_time: i64,
pub timestamp: i64,
}
#[event]
pub struct SubscriptionCancelledEvent {
pub data_provider: Pubkey,
pub subscriber: Pubkey,
}
#[event]
pub struct SubscriptionEndedEvent {
pub data_provider: Pubkey,
pub subscriber: Pubkey,
}
#[event]
pub struct FeePerDayUpdatedEvent {
pub new_fee_per_day: u64,
}
#[event]
pub struct CollectorFeeUpdatedEvent {
pub new_collector_fee: u64,
}
#[event]
pub struct QualityProvidedEvent {
pub data_provider: Pubkey,
pub subscriber: Pubkey,
pub quality: u8,
}
// Error definitions
#[error_code]
pub enum SubscriptionError {
#[msg("Subscription period is too short")]
PeriodTooShort,
#[msg("Already subscribed")]
AlreadySubscribed,
#[msg("Insufficient payment")]
InsufficientPayment,
#[msg("Invalid Token Account")]
InvalidTokenAccount,
#[msg("Invalid NFT holder")]
InvalidNFTHolder,
#[msg("Subscription not found")]
SubscriptionNotFound,
#[msg("Quality out of range")]
QualityOutOfRange,
#[msg("Subscription has already ended")]
SubscriptionAlreadyEnded,
#[msg("Subscription is still active")]
ActiveSubscription,
#[msg("Not the contract owner")]
NotOwner,
}