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
use std::convert::TryInto;
use crate::{
constants::{CANCELLATION_DELEGATED_AMOUNT_NOT_ENOUGH, CANCELLATION_DELEGATION_REVOKED, CANCELLATION_INSUFFICIENT_AMOUNT},
error::ErrorCode,
state::*,
};
use anchor_lang::prelude::*;
use anchor_spl::{
mint,
token::{self, Mint, Token, TokenAccount},
};
#[derive(Accounts)]
pub struct TriggerPayment<'info> {
#[account(mut)]
pub authority: Signer<'info>,
#[account(
mut,
constraint = subscriber_payment_account.mint == mint.key() @ ErrorCode::InvalidMint
)]
pub subscriber_payment_account: Box<Account<'info, TokenAccount>>,
#[account(
mut,
seeds = [b"protocol_signer"],
bump = protocol_signer.bump
)]
pub protocol_signer: Box<Account<'info, ProtocolSigner>>,
#[account(
mut,
has_one = subscription_plan,
has_one = subscriber,
constraint = subscription.has_already_been_initialized @ErrorCode::SubscriberNotInitialized,
constraint = subscription.is_active @ ErrorCode::SubscriptionNotSubscribed,
)]
pub subscription: Box<Account<'info, Subscription>>,
#[account(
constraint = subscriber.has_already_been_initialized @ ErrorCode::SubscriberNotInitialized,
has_one = subscriber_payment_account
)]
pub subscriber: Box<Account<'info, Subscriber>>,
#[account(
mut,
constraint = subscription_plan_payment_account.mint == mint.key() @ ErrorCode::InvalidMint
)]
pub subscription_plan_payment_account: Box<Account<'info, TokenAccount>>,
#[account(
constraint = subscription_plan.has_already_been_initialized @ErrorCode::SubscriptionPlanNotInitialized,
constraint = subscription_plan.is_active @ ErrorCode::SubscriptionPlanInactive,
has_one = subscription_plan_payment_account
)]
pub subscription_plan: Box<Account<'info, SubscriptionPlan>>,
#[account(
seeds = [b"node", authority.key().as_ref()],
bump = node.bump,
has_one = authority,
has_one = node_payment_account,
has_one = node_payment_wallet,
constraint = node.is_registered @ErrorCode::NodeNotRegistered
)]
pub node: Box<Account<'info, Node>>,
#[account(
mut,
associated_token::mint = mint,
associated_token::authority = node_payment_wallet,
)]
pub node_payment_account: Box<Account<'info, TokenAccount>>,
pub node_payment_wallet: UncheckedAccount<'info>,
pub mint: Box<Account<'info, Mint>>,
pub token_program: Program<'info, Token>,
pub clock: Sysvar<'info, Clock>,
}
pub fn handler(ctx: Context<TriggerPayment>) -> Result<()> {
let subscription_plan = &ctx.accounts.subscription_plan;
let subscription = &mut ctx.accounts.subscription;
let subscriber_payment_account = &mut ctx.accounts.subscriber_payment_account;
let clock = &ctx.accounts.clock;
let current_time = clock.unix_timestamp;
require!(
subscription.next_payment_timestamp < current_time,
ErrorCode::SubscriptionNextPaymentTimestampNotReached
);
let balance_of_user = token::accessor::amount(&subscriber_payment_account.to_account_info())?;
let required_balance = subscription_plan.amount;
let mut cancel_subscription = false;
if !(balance_of_user >= required_balance.try_into().unwrap()) {
cancel_subscription = true;
subscription.cancellation_reason = CANCELLATION_INSUFFICIENT_AMOUNT;
}
match subscriber_payment_account.delegate {
anchor_lang::solana_program::program_option::COption::None => {
cancel_subscription = true;
subscription.cancellation_reason = CANCELLATION_DELEGATION_REVOKED;
}
anchor_lang::solana_program::program_option::COption::Some(delegated_account) => {
if !delegated_account.eq(&ctx.accounts.protocol_signer.key()) {
cancel_subscription = true;
subscription.cancellation_reason = CANCELLATION_DELEGATION_REVOKED;
}
let delegated_amount: i64 = subscriber_payment_account
.delegated_amount
.try_into()
.unwrap();
if delegated_amount < subscription_plan.amount {
cancel_subscription = true;
subscription.cancellation_reason = CANCELLATION_DELEGATED_AMOUNT_NOT_ENOUGH;
}
}
}
if cancel_subscription {
subscription.is_active = false;
subscription.is_cancelled = true;
return Ok(());
}
let percentage_for_node: i64 = subscription_plan.fee_percentage.into();
let bump = vec![ctx.accounts.protocol_signer.bump];
let inner_seeds = vec![b"protocol_signer".as_ref(), bump.as_ref()];
let signer_seeds = vec![&inner_seeds[..]];
anchor_spl::token::transfer(
CpiContext::new_with_signer(
ctx.accounts.token_program.to_account_info(),
anchor_spl::token::Transfer {
from: ctx
.accounts
.subscriber_payment_account
.to_account_info()
.clone(),
to: ctx
.accounts
.subscription_plan_payment_account
.to_account_info(),
authority: ctx.accounts.protocol_signer.to_account_info().clone(),
},
&signer_seeds,
),
(subscription_plan.amount * (100 - percentage_for_node) / 100) as u64,
)?;
anchor_spl::token::transfer(
CpiContext::new_with_signer(
ctx.accounts.token_program.to_account_info(),
anchor_spl::token::Transfer {
from: ctx
.accounts
.subscriber_payment_account
.to_account_info()
.clone(),
to: ctx.accounts.node_payment_account.to_account_info(),
authority: ctx.accounts.protocol_signer.to_account_info().clone(),
},
&signer_seeds,
),
(subscription_plan.amount * (percentage_for_node) / 100) as u64,
)?;
subscription.is_active = true;
subscription.is_cancelled = false;
subscription.last_payment_timestamp = current_time;
subscription.next_payment_timestamp = current_time + subscription_plan.frequency;
Ok(())
}