raydium-launchlab-sdk 0.1.7

Rust SDK for Raydium LaunchLab program
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
//! Account structures for Raydium LaunchLab

use solana_program::pubkey::Pubkey;

use super::types::{AmmCreatorFeeOn, VestingSchedule};

// =============================================================================
// Account Structures with Manual Deserialization
// =============================================================================

/// Pool state account containing all pool information
#[derive(Clone, Debug)]
pub struct PoolState {
    pub epoch: u64,
    pub auth_bump: u8,
    pub status: u8,
    pub base_decimals: u8,
    pub quote_decimals: u8,
    pub migrate_type: u8,
    pub supply: u64,
    pub total_base_sell: u64,
    pub virtual_base: u64,
    pub virtual_quote: u64,
    pub real_base: u64,
    pub real_quote: u64,
    pub total_quote_fund_raising: u64,
    pub quote_protocol_fee: u64,
    pub platform_fee: u64,
    pub migrate_fee: u64,
    pub vesting_schedule: VestingSchedule,
    pub global_config: Pubkey,
    pub platform_config: Pubkey,
    pub base_mint: Pubkey,
    pub quote_mint: Pubkey,
    pub base_vault: Pubkey,
    pub quote_vault: Pubkey,
    pub creator: Pubkey,
    pub token_program_flag: u8,
    pub amm_creator_fee_on: AmmCreatorFeeOn,
    pub padding: [u8; 62],
}

impl Default for PoolState {
    fn default() -> Self {
        Self {
            epoch: 0,
            auth_bump: 0,
            status: 0,
            base_decimals: 0,
            quote_decimals: 0,
            migrate_type: 0,
            supply: 0,
            total_base_sell: 0,
            virtual_base: 0,
            virtual_quote: 0,
            real_base: 0,
            real_quote: 0,
            total_quote_fund_raising: 0,
            quote_protocol_fee: 0,
            platform_fee: 0,
            migrate_fee: 0,
            vesting_schedule: VestingSchedule::default(),
            global_config: Pubkey::default(),
            platform_config: Pubkey::default(),
            base_mint: Pubkey::default(),
            quote_mint: Pubkey::default(),
            base_vault: Pubkey::default(),
            quote_vault: Pubkey::default(),
            creator: Pubkey::default(),
            token_program_flag: 0,
            amm_creator_fee_on: AmmCreatorFeeOn::default(),
            padding: [0u8; 62],
        }
    }
}

impl PoolState {
    pub const DISCRIMINATOR: [u8; 8] = [247, 237, 227, 245, 215, 195, 222, 70];

    /// Deserialize from bytes (including discriminator check)
    pub fn try_from_bytes(data: &[u8]) -> Result<Self, std::io::Error> {
        if data.len() < 8 {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Data too short",
            ));
        }

        let (discriminator, rest) = data.split_at(8);
        if discriminator != Self::DISCRIMINATOR {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Invalid discriminator",
            ));
        }

        Self::deserialize(rest)
    }

    fn deserialize(data: &[u8]) -> Result<Self, std::io::Error> {
        let mut offset = 0;

        let epoch = read_u64(data, &mut offset)?;
        let auth_bump = read_u8(data, &mut offset)?;
        let status = read_u8(data, &mut offset)?;
        let base_decimals = read_u8(data, &mut offset)?;
        let quote_decimals = read_u8(data, &mut offset)?;
        let migrate_type = read_u8(data, &mut offset)?;
        let supply = read_u64(data, &mut offset)?;
        let total_base_sell = read_u64(data, &mut offset)?;
        let virtual_base = read_u64(data, &mut offset)?;
        let virtual_quote = read_u64(data, &mut offset)?;
        let real_base = read_u64(data, &mut offset)?;
        let real_quote = read_u64(data, &mut offset)?;
        let total_quote_fund_raising = read_u64(data, &mut offset)?;
        let quote_protocol_fee = read_u64(data, &mut offset)?;
        let platform_fee = read_u64(data, &mut offset)?;
        let migrate_fee = read_u64(data, &mut offset)?;

        // VestingSchedule
        let total_locked_amount = read_u64(data, &mut offset)?;
        let cliff_period = read_u64(data, &mut offset)?;
        let unlock_period = read_u64(data, &mut offset)?;
        let start_time = read_u64(data, &mut offset)?;
        let allocated_share_amount = read_u64(data, &mut offset)?;
        let vesting_schedule = VestingSchedule {
            total_locked_amount,
            cliff_period,
            unlock_period,
            start_time,
            allocated_share_amount,
        };

        let global_config = read_pubkey(data, &mut offset)?;
        let platform_config = read_pubkey(data, &mut offset)?;
        let base_mint = read_pubkey(data, &mut offset)?;
        let quote_mint = read_pubkey(data, &mut offset)?;
        let base_vault = read_pubkey(data, &mut offset)?;
        let quote_vault = read_pubkey(data, &mut offset)?;
        let creator = read_pubkey(data, &mut offset)?;
        let token_program_flag = read_u8(data, &mut offset)?;
        let amm_creator_fee_on_byte = read_u8(data, &mut offset)?;

        let mut padding = [0u8; 62];
        padding.copy_from_slice(&data[offset..offset + 62]);
        let _ = offset + 62; // Update for consistency even if last field

        Ok(Self {
            epoch,
            auth_bump,
            status,
            base_decimals,
            quote_decimals,
            migrate_type,
            supply,
            total_base_sell,
            virtual_base,
            virtual_quote,
            real_base,
            real_quote,
            total_quote_fund_raising,
            quote_protocol_fee,
            platform_fee,
            migrate_fee,
            vesting_schedule,
            global_config,
            platform_config,
            base_mint,
            quote_mint,
            base_vault,
            quote_vault,
            creator,
            token_program_flag,
            amm_creator_fee_on: AmmCreatorFeeOn::from_u8(amm_creator_fee_on_byte),
            padding,
        })
    }

    pub fn is_funding(&self) -> bool {
        self.status == 0
    }

    pub fn is_migrate(&self) -> bool {
        self.status == 1
    }

    pub fn is_trading(&self) -> bool {
        self.status == 2
    }

    pub fn is_base_token_2022(&self) -> bool {
        self.token_program_flag & 0x01 != 0
    }

    pub fn is_quote_token_2022(&self) -> bool {
        self.token_program_flag & 0x02 != 0
    }
}

pub struct GlobalConfig {
    pub epoch: u64,
    pub curve_type: u8,
    pub index: u16,
    pub migrate_fee: u64,
    pub trade_fee_rate: u64,
    pub max_share_fee_rate: u64,
    pub min_base_supply: u64,
    pub max_lock_rate: u64,
    pub min_base_sell_rate: u64,
    pub min_base_migrate_rate: u64,
    pub min_quote_fund_raising: u64,
    pub quote_mint: Pubkey,
    pub protocol_fee_owner: Pubkey,
    pub migrate_fee_owner: Pubkey,
    pub migrate_to_amm_wallet: Pubkey,
    pub migrate_to_cpswap_wallet: Pubkey,
    pub padding: [u64; 16],
}

impl GlobalConfig {
    pub const DISCRIMINATOR: [u8; 8] = [149, 8, 156, 202, 160, 252, 176, 217];

    pub fn try_from_bytes(data: &[u8]) -> Result<Self, std::io::Error> {
        if data.len() < 8 {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Data too short",
            ));
        }

        let (discriminator, rest) = data.split_at(8);
        if discriminator != Self::DISCRIMINATOR {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Invalid discriminator",
            ));
        }

        let mut offset = 0;
        let epoch = read_u64(rest, &mut offset)?;
        let curve_type = read_u8(rest, &mut offset)?;
        let index = read_u16(rest, &mut offset)?;
        let migrate_fee = read_u64(rest, &mut offset)?;
        let trade_fee_rate = read_u64(rest, &mut offset)?;
        let max_share_fee_rate = read_u64(rest, &mut offset)?;
        let min_base_supply = read_u64(rest, &mut offset)?;
        let max_lock_rate = read_u64(rest, &mut offset)?;
        let min_base_sell_rate = read_u64(rest, &mut offset)?;
        let min_base_migrate_rate = read_u64(rest, &mut offset)?;
        let min_quote_fund_raising = read_u64(rest, &mut offset)?;
        let quote_mint = read_pubkey(rest, &mut offset)?;
        let protocol_fee_owner = read_pubkey(rest, &mut offset)?;
        let migrate_fee_owner = read_pubkey(rest, &mut offset)?;
        let migrate_to_amm_wallet = read_pubkey(rest, &mut offset)?;
        let migrate_to_cpswap_wallet = read_pubkey(rest, &mut offset)?;

        let mut padding = [0u64; 16];
        for i in 0..16 {
            padding[i] = read_u64(rest, &mut offset)?;
        }

        Ok(Self {
            epoch,
            curve_type,
            index,
            migrate_fee,
            trade_fee_rate,
            max_share_fee_rate,
            min_base_supply,
            max_lock_rate,
            min_base_sell_rate,
            min_base_migrate_rate,
            min_quote_fund_raising,
            quote_mint,
            protocol_fee_owner,
            migrate_fee_owner,
            migrate_to_amm_wallet,
            migrate_to_cpswap_wallet,
            padding,
        })
    }
}

fn read_u8(data: &[u8], offset: &mut usize) -> Result<u8, std::io::Error> {
    if *offset >= data.len() {
        return Err(std::io::Error::new(
            std::io::ErrorKind::UnexpectedEof,
            "Unexpected end of data",
        ));
    }
    let value = data[*offset];
    *offset += 1;
    Ok(value)
}

fn read_u16(data: &[u8], offset: &mut usize) -> Result<u16, std::io::Error> {
    if *offset + 2 > data.len() {
        return Err(std::io::Error::new(
            std::io::ErrorKind::UnexpectedEof,
            "Unexpected end of data",
        ));
    }
    let value = u16::from_le_bytes([data[*offset], data[*offset + 1]]);
    *offset += 2;
    Ok(value)
}

fn read_u64(data: &[u8], offset: &mut usize) -> Result<u64, std::io::Error> {
    if *offset + 8 > data.len() {
        return Err(std::io::Error::new(
            std::io::ErrorKind::UnexpectedEof,
            "Unexpected end of data",
        ));
    }
    let bytes: [u8; 8] = data[*offset..*offset + 8].try_into().unwrap();
    let value = u64::from_le_bytes(bytes);
    *offset += 8;
    Ok(value)
}

fn read_pubkey(data: &[u8], offset: &mut usize) -> Result<Pubkey, std::io::Error> {
    if *offset + 32 > data.len() {
        return Err(std::io::Error::new(
            std::io::ErrorKind::UnexpectedEof,
            "Unexpected end of data",
        ));
    }
    let bytes: [u8; 32] = data[*offset..*offset + 32].try_into().unwrap();
    let pubkey = Pubkey::new_from_array(bytes);
    *offset += 32;
    Ok(pubkey)
}

pub struct PlatformConfig {
    pub epoch: u64,
    pub platform_fee_wallet: Pubkey,
    pub platform_nft_wallet: Pubkey,
    pub platform_scale: u64,
    pub creator_scale: u64,
    pub burn_scale: u64,
    pub fee_rate: u64,
    pub name: [u8; 64],
    pub web: [u8; 256],
    pub img: [u8; 256],
    pub cpswap_config: Pubkey,
    pub creator_fee_rate: u64,
    pub transfer_fee_extension_auth: Pubkey,
}

impl Default for PlatformConfig {
    fn default() -> Self {
        Self {
            epoch: 0,
            platform_fee_wallet: Pubkey::default(),
            platform_nft_wallet: Pubkey::default(),
            platform_scale: 0,
            creator_scale: 0,
            burn_scale: 0,
            fee_rate: 0,
            name: [0; 64],
            web: [0; 256],
            img: [0; 256],
            cpswap_config: Pubkey::default(),
            creator_fee_rate: 0,
            transfer_fee_extension_auth: Pubkey::default(),
        }
    }
}

impl PlatformConfig {
    pub const DISCRIMINATOR: [u8; 8] = [160, 78, 128, 0, 248, 83, 230, 160];

    pub fn try_from_bytes(data: &[u8]) -> Result<Self, std::io::Error> {
        if data.len() < 8 {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Data too short",
            ));
        }

        let (discriminator, rest) = data.split_at(8);
        if discriminator != Self::DISCRIMINATOR {
            return Err(std::io::Error::new(
                std::io::ErrorKind::InvalidData,
                "Invalid discriminator",
            ));
        }

        let mut offset = 0;
        let epoch = read_u64(rest, &mut offset)?;
        let platform_fee_wallet = read_pubkey(rest, &mut offset)?;
        let platform_nft_wallet = read_pubkey(rest, &mut offset)?;
        let platform_scale = read_u64(rest, &mut offset)?;
        let creator_scale = read_u64(rest, &mut offset)?;
        let burn_scale = read_u64(rest, &mut offset)?;
        let fee_rate = read_u64(rest, &mut offset)?;

        let mut name = [0u8; 64];
        name.copy_from_slice(&rest[offset..offset + 64]);
        offset += 64;

        let mut web = [0u8; 256];
        web.copy_from_slice(&rest[offset..offset + 256]);
        offset += 256;

        let mut img = [0u8; 256];
        img.copy_from_slice(&rest[offset..offset + 256]);
        offset += 256;

        let cpswap_config = read_pubkey(rest, &mut offset)?;
        let creator_fee_rate = read_u64(rest, &mut offset)?;
        let transfer_fee_extension_auth = read_pubkey(rest, &mut offset)?;

        Ok(Self {
            epoch,
            platform_fee_wallet,
            platform_nft_wallet,
            platform_scale,
            creator_scale,
            burn_scale,
            fee_rate,
            name,
            web,
            img,
            cpswap_config,
            creator_fee_rate,
            transfer_fee_extension_auth,
        })
    }
}