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
use Pubkey;
use *;
// #[repr(C)]
// #[derive(Clone, Copy, Debug, Pod, Zeroable)]
// pub struct UpdateEvolver {
// pub new_evolver: Pubkey,
// }
// ===== PAYMENT-BASED REWARDS SYSTEM INSTRUCTIONS =====
/// Claim instruction data for Dual Merkle Tree system with optional social rewards.
///
/// This instruction allows users to claim rewards based on their activity contributions
/// for a specific epoch using the Dual Merkle Tree verification system. The claim is verified
/// using both a payment merkle proof and a seal merkle proof, with the payment root embedded
/// in the seal proof data for explicit verification. Optionally, users can also claim social
/// marketing rewards for the same epoch if they have verified social media engagement.
///
/// ## Dual Merkle Verification
/// 1. **Payment Proof**: Verifies user's activity within the epoch
/// 2. **Seal Proof**: Verifies the payment merkle root is authentic (from oracle)
/// 3. **Payment Root**: Embedded in seal proof data for explicit verification
/// 4. **Dual Verification**: Both proofs must pass for a valid claim
///
/// ## Social Rewards Support
/// - **Payment-First**: Social rewards require payment activity verification
/// - **Oracle Verification**: Social posts must be verified by oracle signature
/// - **Optional**: Users can choose payment-only or payment+social claims
/// - **Unified**: Single instruction supports both reward types
///
/// ## Data Structure
/// The instruction uses a hybrid approach with fixed-size fields followed by variable-length data:
///
/// ```rust
/// // Fixed-size struct (23 bytes)
/// struct Claim {
/// epoch: [u8; 8], // 8 bytes - Epoch number for the claim
/// payment_proof_length: u8, // 1 byte - Length of payment merkle path
/// seal_proof_length: u8, // 1 byte - Length of seal merkle path
/// participant_type: u8, // 1 byte - 0 for customer, 1 for merchant
/// has_social: u8, // 1 byte - 0=payment only, 1=has social data
/// _padding: [u8; 4], // 4 bytes - Padding for alignment
/// }
///
/// // Variable-size additional data (appended after fixed struct)
/// // participant_data: DailyParticipantData // Variable size - Actual participant data for verification
/// // payment_proof: Vec<[u8; 32]> // payment_proof_length * 32 bytes
/// // payment_indices: Vec<u8> // payment_proof_length * 1 byte
/// // seal_proof: Vec<[u8; 32]> // seal_proof_length * 32 bytes
/// // seal_indices: Vec<u8> // seal_proof_length * 1 byte
/// // payment_root: [u8; 32] // Payment root (embedded in seal proof data)
/// // epoch_data: EpochClaimData // Epoch-specific data for accurate historical reward calculation
/// // social_data: SocialClaimData // Optional: only if has_social = 1
/// ```
///
/// ## Security Features
/// - **Payment Proof**: Ensures claim data integrity within an epoch
/// - **Seal Proof**: Ensures payment merkle root is authentic (from oracle)
/// - **Payment Root**: Explicitly provided for verification
/// - **Dual Verification**: Both proofs must pass for a valid claim
/// - **Oracle Verification**: Social posts verified by authorized oracle
/// - **Unlimited History**: No storage limits for historical claims
///
/// ## On-Chain Reward Calculation
/// The reward amount is calculated on-chain using the transparent formula:
/// payment_reward = (activity_count * customer_reward_pool) / total_customer_activity
/// social_reward = base_reward_per_post * (1 + engagement_score / 1000)
///
/// Where:
/// - `activity_count` is extracted from the payment merkle proof
/// - `customer_reward_pool` and `total_customer_activity` are read from the Snapshot
/// - `base_reward_per_post` and engagement data come from social verification
///
/// ## Historical Claims Support
/// For historical epoch claims, the instruction includes epoch-specific data:
/// - **Current Epoch**: Uses on-chain snapshot data for reward calculation
/// - **Historical Epochs**: Uses epoch-specific data embedded in the instruction
/// - **No On-Chain Storage**: Historical data comes with the claim instruction
/// - **Accurate Rewards**: Users get correct rewards for historical activity
///
/// This enables the Dual Merkle Tree's promise of unlimited claim history without
/// requiring expensive on-chain storage of historical snapshots.
///
/// ## Off-Chain Development
/// When building claim instructions off-chain:
/// 1. Set `payment_proof_length` and `seal_proof_length` to actual path lengths
/// 2. Set `has_social` to 1 if social data is included, 0 otherwise
/// 3. Append payment proof data after the fixed struct
/// 4. Append payment indices data (1 = right, 0 = left)
/// 5. Append seal proof data
/// 6. Append seal indices data (1 = right, 0 = left)
/// 7. Append payment root data (32 bytes)
/// 8. Append epoch-specific data (EpochClaimData) for accurate historical calculations
/// 9. If has_social = 1, append SocialClaimData
/// 10. Verify total instruction size doesn't exceed Solana limits
///
/// See `DUAL_MERKLE_TREE_DESIGN.md` for detailed implementation examples.
/// BatchClaim instruction data for Dual Merkle Tree system.
///
/// This instruction allows users to claim rewards for multiple epochs in a single transaction
/// using the Dual Merkle Tree verification system. Each epoch requires both payment and seal
/// merkle proofs for complete security verification.
///
/// ## Dual Merkle Verification for Batch Claims
/// For each epoch in the batch:
/// 1. **Payment Proof**: Verifies user's activity within the epoch
/// 2. **Seal Proof**: Verifies the payment merkle root is authentic (from oracle)
/// 3. **Dual Verification**: Both proofs must pass for each epoch
///
/// ## Data Structure
/// The instruction uses a hybrid approach supporting different claim types:
///
/// ```rust
/// // Fixed-size struct (25 bytes)
/// struct BatchClaim {
/// start_epoch: [u8; 8], // 8 bytes - Start epoch for range claims
/// end_epoch: [u8; 8], // 8 bytes - End epoch for range claims
/// participant_type: u8, // 1 byte - 0 for customer, 1 for merchant
/// claim_type: u8, // 1 byte - 0=range, 1=vector, 2=date_range
/// epoch_count: u8, // 1 byte - Number of epochs for vector claims
/// max_batch_size: u8, // 1 byte - Maximum epochs to process
/// _padding: [u8; 4], // 4 bytes - Padding for alignment
/// }
///
/// // Variable-size additional data (appended after fixed struct)
/// // For vector claims: Vec<u64> of epochs
/// // For each epoch: payment_proof + payment_indices + payment_root + seal_proof + seal_indices + epoch_data
/// // Social flags array: 1 byte per epoch (0=no social, 1=has social)
/// // Social data array: SocialClaimData for epochs with social claims
/// ```
///
/// ## Claim Types
/// - **Range Claims (0)**: Claim all epochs from start_epoch to end_epoch (inclusive)
/// - **Vector Claims (1)**: Claim specific epochs listed in variable data
/// - **Date Range Claims (2)**: Convert date range to epoch range automatically
///
/// ## Benefits
/// - **Gas Efficiency**: Single transaction for multiple epochs
/// - **User Experience**: Natural date-based interface
/// - **Flexibility**: Support for both contiguous and non-contiguous epochs
/// - **Security**: Each epoch validated independently with dual merkle proofs
/// - **Unified**: Supports both payment and social rewards in single batch
/// - **Payment-First**: All social claims require payment activity
/// - **Unlimited History**: No storage limits for historical claims
///
/// ## Historical Claims Support
/// For historical epoch claims, each epoch includes epoch-specific data:
/// - **Current Epochs**: Use on-chain snapshot data for reward calculation
/// - **Historical Epochs**: Use epoch-specific data embedded in the instruction
/// - **No On-Chain Storage**: Historical data comes with the batch claim instruction
/// - **Accurate Rewards**: Users get correct rewards for historical activity across all epochs
///
/// This enables the Dual Merkle Tree's promise of unlimited claim history without
/// requiring expensive on-chain storage of historical snapshots.
///
/// ## Error Handling
/// - **No Activity**: Skip epochs with no user activity
/// - **Invalid Proofs**: Fail on security-critical errors (payment or seal)
/// - **Batch Limits**: Respect max_batch_size to prevent gas limit issues
/// - **Pool Limits**: Validate against both payment and social pool limits
/// Update community targets, activity limits, and claim rewards threshold.
/// This instruction allows the oracle to update community health targets, activity limits,
/// and claim rewards threshold based on real-world community performance and operational needs.
///
/// ## Data Structure
/// Fixed-size struct (28 bytes) containing all configurable parameters:
///
/// ```rust
/// struct UpdateTargets {
/// // Claim Rewards Threshold
/// claim_rewards_threshold: [u8; 8], // u64
///
/// // Community Health Targets
/// target_weekly_users: [u8; 4], // u32
/// target_weekly_activity: [u8; 4], // u32
/// target_retention_rate: [u8; 2], // u16
///
/// // Activity Limits
/// max_customer_activity_per_epoch: [u8; 4], // u32
/// max_merchant_activity_per_epoch: [u8; 4], // u32
/// activity_cap_enabled: u8, // u8
/// claim_cap_enabled: u8, // u8
///
/// // Padding
/// _padding: [u8; 4], // 4 bytes padding
/// }
/// ```
///
/// ## Parameters
/// - `target_weekly_users`: Target weekly active users (default: 10,000)
/// - `target_weekly_activity`: Target weekly activity count (default: 50,000)
/// - `target_retention_rate`: Target retention rate in basis points (default: 7,000 = 70%)
/// - `max_customer_activity_per_epoch`: Max customer activities per epoch (default: 5)
/// - `max_merchant_activity_per_epoch`: Max merchant activities per epoch (default: 50)
/// - `activity_cap_enabled`: Whether activity capping is enabled (default: 1 = enabled)
/// - `claim_cap_enabled`: Whether claim capping is enabled (default: 1 = enabled)
/// - `claim_rewards_threshold`: Risk threshold for rewards claim requests (default: BASE_DAILY_REWARDS)
///
/// ## Authority
/// - **Oracle Authority**: Only the oracle can update these parameters
/// - **Rationale**: Oracle has the data to make informed decisions about community health
///
/// ## Benefits
/// - **Operational Flexibility**: Adjust parameters based on real community performance
/// - **Anti-Gaming**: Update activity limits to prevent new gaming strategies
/// - **Community Adaptation**: Adjust targets as community grows and evolves
/// - **Economic Balance**: Fine-tune parameters for sustainable tokenomics
/// - **Risk Management**: Adjust claim rewards threshold based on market conditions
/*
/// ## Off-Chain Development
/// When building batch claim instructions off-chain:
/// 1. Set claim parameters (participant_type, claim_type, start/end epochs, etc.)
/// 2. For each epoch in the batch:
/// - Generate payment proof and indices
/// - Generate seal proof and indices
/// - Include epoch-specific data (EpochClaimData) for accurate historical calculations
/// 3. Append social flags array (1 byte per epoch)
/// 4. Append social data array for epochs with social claims
/// 5. Verify total instruction size doesn't exceed Solana limits
///
/// ## Epoch Data Requirements
/// - **Current Epoch Claims**: Can omit epoch data (uses on-chain snapshot)
/// - **Historical Epoch Claims**: Must include epoch-specific data for each epoch
/// - **Mixed Batches**: Include epoch data only for historical epochs
/// - **Validation**: Ensure epoch data matches the specific epoch being claimed
*/
instruction!;
instruction!;
instruction!;
instruction!;
instruction!;
instruction!;
instruction!;
instruction!;
instruction!;
instruction!;