light-token-interface 0.5.0

Light Protocol token instruction data types.
Documentation
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
#![cfg(feature = "test-only")]
//! Tests token solana account - spl token account layout compatibility
//!
//! Tests:
//! 1. test_compressed_token_equivalent_to_pod_account
//! 2. test_compressed_token_with_pausable_extension
//! 3. test_account_type_compatibility_with_spl_parsing

use light_compressed_account::Pubkey;
use light_token_interface::state::{
    extensions::ExtensionStructConfig,
    token::{Token, TokenConfig, ZToken, ZTokenMut, BASE_TOKEN_ACCOUNT_SIZE},
    ACCOUNT_TYPE_TOKEN_ACCOUNT,
};
use light_zero_copy::traits::{ZeroCopyAt, ZeroCopyAtMut, ZeroCopyNew};
use rand::Rng;
use spl_pod::{bytemuck::pod_from_bytes, primitives::PodU64, solana_program_option::COption};
use spl_token_2022::{
    extension::{BaseStateWithExtensions, PodStateWithExtensions, StateWithExtensions},
    pod::PodAccount,
    solana_program::program_pack::Pack,
    state::{Account, AccountState},
};

fn default_config() -> TokenConfig {
    TokenConfig {
        mint: Pubkey::default(),
        owner: Pubkey::default(),
        state: 1,
        extensions: None,
    }
}

/// Generate random token account data using SPL Token's pack method
/// Creates a buffer large enough for the full Token meta struct
fn generate_random_token_account_data(rng: &mut impl Rng) -> Vec<u8> {
    let account = Account {
        mint: solana_pubkey::Pubkey::new_from_array(rng.gen::<[u8; 32]>()),
        owner: solana_pubkey::Pubkey::new_from_array(rng.gen::<[u8; 32]>()),
        amount: rng.gen::<u64>(),
        delegate: if rng.gen_bool(0.3) {
            COption::Some(solana_pubkey::Pubkey::new_from_array(rng.gen::<[u8; 32]>()))
        } else {
            COption::None
        },
        state: if rng.gen_bool(0.9) {
            AccountState::Initialized
        } else {
            AccountState::Frozen
        },
        is_native: if rng.gen_bool(0.2) {
            COption::Some(rng.gen_range(1_000_000..=10_000_000u64))
        } else {
            COption::None
        },
        delegated_amount: rng.gen::<u64>(),
        close_authority: if rng.gen_bool(0.25) {
            COption::Some(solana_pubkey::Pubkey::new_from_array(rng.gen::<[u8; 32]>()))
        } else {
            COption::None
        },
    };
    println!("Expected Account: {:?}", account);

    // Create buffer for SPL-compatible token account (165 bytes, no extensions)
    let mut account_data = vec![0u8; BASE_TOKEN_ACCOUNT_SIZE as usize];
    Account::pack(account, &mut account_data[..Account::LEN]).unwrap();
    // Note: No account_type byte for pure SPL accounts without extensions
    account_data
}

/// Compare all fields between our Token zero-copy implementation and Pod account
fn compare_compressed_token_with_pod_account(
    compressed_token: &ZToken,
    pod_account: &PodAccount,
) -> bool {
    // Extensions should be None for basic SPL Token accounts
    if compressed_token.extensions.is_some() {
        return false;
    }

    // Compare mint
    if compressed_token.mint.to_bytes() != pod_account.mint.to_bytes() {
        println!(
            "Mint mismatch: compressed={:?}, pod={:?}",
            compressed_token.mint.to_bytes(),
            pod_account.mint.to_bytes()
        );
        return false;
    }

    // Compare owner
    if compressed_token.owner.to_bytes() != pod_account.owner.to_bytes() {
        return false;
    }

    // Compare amount
    if u64::from(compressed_token.amount) != u64::from(pod_account.amount) {
        return false;
    }

    // Compare delegate using getter
    let pod_delegate_option: Option<Pubkey> = if pod_account.delegate.is_some() {
        Some(
            pod_account
                .delegate
                .unwrap_or(solana_pubkey::Pubkey::default())
                .to_bytes()
                .into(),
        )
    } else {
        None
    };
    match (compressed_token.delegate(), pod_delegate_option) {
        (Some(compressed_delegate), Some(pod_delegate)) => {
            if compressed_delegate.to_bytes() != pod_delegate.to_bytes() {
                return false;
            }
        }
        (None, None) => {}
        _ => return false,
    }

    // Compare state
    if compressed_token.state != pod_account.state {
        return false;
    }

    // Compare is_native using getter
    let pod_native_option: Option<u64> = if pod_account.is_native.is_some() {
        Some(u64::from(
            pod_account.is_native.unwrap_or(PodU64::default()),
        ))
    } else {
        None
    };
    match (compressed_token.is_native_value(), pod_native_option) {
        (Some(compressed_native), Some(pod_native)) => {
            if compressed_native != pod_native {
                return false;
            }
        }
        (None, None) => {}
        _ => return false,
    }

    // Compare delegated_amount
    if u64::from(compressed_token.delegated_amount) != u64::from(pod_account.delegated_amount) {
        return false;
    }

    // Compare close_authority using getter
    let pod_close_option: Option<Pubkey> = if pod_account.close_authority.is_some() {
        Some(
            pod_account
                .close_authority
                .unwrap_or(solana_pubkey::Pubkey::default())
                .to_bytes()
                .into(),
        )
    } else {
        None
    };
    match (compressed_token.close_authority(), pod_close_option) {
        (Some(compressed_close), Some(pod_close)) => {
            if compressed_close.to_bytes() != pod_close.to_bytes() {
                return false;
            }
        }
        (None, None) => {}
        _ => return false,
    }

    true
}

/// Compare all fields between our Token mutable zero-copy implementation and Pod account
fn compare_compressed_token_mut_with_pod_account(
    compressed_token: &ZTokenMut,
    pod_account: &PodAccount,
) -> bool {
    // Extensions should be None for basic SPL Token accounts
    if compressed_token.extensions.is_some() {
        return false;
    }

    // Compare mint
    if compressed_token.mint.to_bytes() != pod_account.mint.to_bytes() {
        println!(
            "Mint mismatch: compressed={:?}, pod={:?}",
            compressed_token.mint.to_bytes(),
            pod_account.mint.to_bytes()
        );
        return false;
    }

    // Compare owner
    if compressed_token.owner.to_bytes() != pod_account.owner.to_bytes() {
        return false;
    }

    // Compare amount
    if u64::from(compressed_token.amount) != u64::from(pod_account.amount) {
        return false;
    }

    // Compare delegate using getter
    let pod_delegate_option: Option<Pubkey> = if pod_account.delegate.is_some() {
        Some(
            pod_account
                .delegate
                .unwrap_or(solana_pubkey::Pubkey::default())
                .to_bytes()
                .into(),
        )
    } else {
        None
    };
    match (compressed_token.delegate(), pod_delegate_option) {
        (Some(compressed_delegate), Some(pod_delegate)) => {
            if compressed_delegate.to_bytes() != pod_delegate.to_bytes() {
                return false;
            }
        }
        (None, None) => {}
        _ => return false,
    }

    // Compare state
    if compressed_token.state != pod_account.state {
        println!(
            "State mismatch: compressed={}, pod={}",
            compressed_token.state, pod_account.state
        );
        return false;
    }

    // Compare is_native using getter
    let pod_native_option: Option<u64> = if pod_account.is_native.is_some() {
        Some(u64::from(
            pod_account.is_native.unwrap_or(PodU64::default()),
        ))
    } else {
        None
    };
    match (compressed_token.is_native_value(), pod_native_option) {
        (Some(compressed_native), Some(pod_native)) => {
            if compressed_native != pod_native {
                return false;
            }
        }
        (None, None) => {}
        _ => return false,
    }

    // Compare delegated_amount
    if u64::from(compressed_token.delegated_amount) != u64::from(pod_account.delegated_amount) {
        return false;
    }

    // Compare close_authority using getter
    let pod_close_option: Option<Pubkey> = if pod_account.close_authority.is_some() {
        Some(
            pod_account
                .close_authority
                .unwrap_or(solana_pubkey::Pubkey::default())
                .to_bytes()
                .into(),
        )
    } else {
        None
    };
    match (compressed_token.close_authority(), pod_close_option) {
        (Some(compressed_close), Some(pod_close)) => {
            if compressed_close.to_bytes() != pod_close.to_bytes() {
                return false;
            }
        }
        (None, None) => {}
        _ => return false,
    }

    true
}

#[test]
fn test_compressed_token_equivalent_to_pod_account() {
    let mut rng = rand::thread_rng();

    for _ in 0..10000 {
        let mut account_data = generate_random_token_account_data(&mut rng);
        let account_data_clone = account_data.clone();
        // Pod account only knows about the first 165 bytes
        let pod_account = pod_from_bytes::<PodAccount>(&account_data_clone[..165]).unwrap();

        // Test immutable version
        let (compressed_token, _) = Token::zero_copy_at(&account_data).unwrap();
        println!("Compressed Token: {:?}", compressed_token);
        println!("Pod Account: {:?}", pod_account);
        assert!(compare_compressed_token_with_pod_account(
            &compressed_token,
            pod_account
        ));
        {
            let account_data_clone = account_data.clone();
            // Pod account only knows about the first 165 bytes
            let pod_account = pod_from_bytes::<PodAccount>(&account_data_clone[..165]).unwrap();
            // Test mutable version
            let (compressed_token_mut, _) = Token::zero_copy_at_mut(&mut account_data).unwrap();
            println!("Compressed Token Mut: {:?}", compressed_token_mut);
            println!("Pod Account: {:?}", pod_account);

            assert!(compare_compressed_token_mut_with_pod_account(
                &compressed_token_mut,
                pod_account
            ));
        }
    }
}

#[test]
fn test_compressed_token_with_pausable_extension() {
    let config = TokenConfig {
        extensions: Some(vec![ExtensionStructConfig::PausableAccount(())]),
        ..default_config()
    };

    let required_size = Token::byte_len(&config).unwrap();
    println!("Required size for pausable extension: {}", required_size);

    // Should be more than 165 bytes due to AccountType byte and extension
    assert!(required_size > 165);

    let mut buffer = vec![0u8; required_size];
    {
        let (_, remaining_bytes) = Token::new_zero_copy(&mut buffer, config)
            .expect("Failed to initialize compressed token with pausable extension");

        assert_eq!(remaining_bytes.len(), 0);
        // Note: new_zero_copy now writes extensions directly to bytes but returns extensions: None
        // Extensions are parsed when deserializing with zero_copy_at
    }

    // Test zero-copy deserialization round-trip - extensions are parsed from bytes
    let (deserialized_token, _) =
        Token::zero_copy_at(&buffer).expect("Failed to deserialize token with pausable extension");

    assert!(deserialized_token.extensions.is_some());
    let deserialized_extensions = deserialized_token.extensions.as_ref().unwrap();
    assert_eq!(deserialized_extensions.len(), 1);

    // Test mutable deserialization with a fresh buffer
    let mut buffer_copy = buffer.clone();
    let (mutable_token, _) = Token::zero_copy_at_mut(&mut buffer_copy)
        .expect("Failed to deserialize mutable token with pausable extension");

    assert!(mutable_token.extensions.is_some());
}

#[test]
fn test_account_type_compatibility_with_spl_parsing() {
    let config = TokenConfig {
        extensions: Some(vec![ExtensionStructConfig::PausableAccount(())]),
        ..default_config()
    };

    let mut buffer = vec![0u8; Token::byte_len(&config).unwrap()];
    {
        let (mut compressed_token, _) = Token::new_zero_copy(&mut buffer, config)
            .expect("Failed to create token with extension");
        // Set state to Initialized (1) for SPL compatibility - required for SPL parsing
        compressed_token.base.state = 1;
    }

    let pod_account = pod_from_bytes::<PodAccount>(&buffer[..165])
        .expect("First 165 bytes should be valid SPL Token Account data");
    let pod_state = PodStateWithExtensions::<PodAccount>::unpack(&buffer[..165])
        .expect("Pod account with extensions should succeed.");
    let base_account = pod_state.base;
    assert_eq!(pod_account, base_account);

    // Verify AccountType byte is at position 165
    assert_eq!(buffer[165], ACCOUNT_TYPE_TOKEN_ACCOUNT);

    // Deserialize with extensions
    let token_account_data = StateWithExtensions::<Account>::unpack(&buffer)
        .unwrap()
        .base;

    // Deserialize without extensions need to truncate buffer to correct length.
    let token_account_data_no_extensions = Account::unpack(&buffer[..165]).unwrap();
    assert_eq!(token_account_data, token_account_data_no_extensions);
    let token_account_data = StateWithExtensions::<Account>::unpack(&buffer)
        .unwrap()
        .get_first_extension_type();
    println!("token_account_data {:?}", token_account_data);
}

/// Test PartialEq between ZToken and Token with Pausable extension.
#[test]
fn test_pausable_extension_partial_eq() {
    use light_token_interface::state::{
        extensions::{ExtensionStruct, PausableAccountExtension},
        token::AccountState as CtokenAccountState,
    };

    let config = TokenConfig {
        extensions: Some(vec![ExtensionStructConfig::PausableAccount(())]),
        ..default_config()
    };

    let mut buffer = vec![0u8; Token::byte_len(&config).unwrap()];
    let _ = Token::new_zero_copy(&mut buffer, config).unwrap();

    // new_zero_copy now sets fields from config
    let expected = Token {
        mint: Pubkey::default(),
        owner: Pubkey::default(),
        amount: 0,
        delegate: None,
        state: CtokenAccountState::Initialized, // state: 1 from default_config
        is_native: None,
        delegated_amount: 0,
        close_authority: None,
        account_type: ACCOUNT_TYPE_TOKEN_ACCOUNT,
        extensions: Some(vec![ExtensionStruct::PausableAccount(
            PausableAccountExtension,
        )]),
    };

    let (zctoken, _) = Token::zero_copy_at(&buffer).unwrap();
    assert_eq!(zctoken, expected);
}