carbon_fluxbeam_decoder/accounts/
mod.rs

1use carbon_core::account::AccountDecoder;
2use carbon_core::deserialize::CarbonDeserialize;
3
4use crate::PROGRAM_ID;
5
6use super::FluxbeamDecoder;
7pub mod swap_v1;
8
9pub enum FluxbeamAccount {
10    SwapV1(swap_v1::SwapV1),
11}
12
13impl AccountDecoder<'_> for FluxbeamDecoder {
14    type AccountType = FluxbeamAccount;
15    fn decode_account(
16        &self,
17        account: &solana_account::Account,
18    ) -> Option<carbon_core::account::DecodedAccount<Self::AccountType>> {
19        if !account.owner.eq(&PROGRAM_ID) {
20            return None;
21        }
22
23        if let Some(decoded_account) = swap_v1::SwapV1::deserialize(account.data.as_slice()) {
24            return Some(carbon_core::account::DecodedAccount {
25                lamports: account.lamports,
26                data: FluxbeamAccount::SwapV1(decoded_account),
27                owner: account.owner,
28                executable: account.executable,
29                rent_epoch: account.rent_epoch,
30            });
31        }
32
33        None
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use crate::types::{CurveType, Fees, SwapCurve};
40
41    use super::*;
42
43    #[test]
44    fn test_decode_swap_account() {
45        // Arrange
46        let expected_swap_account = swap_v1::SwapV1 {
47            _padding: 0,
48            is_initialized: true,
49            bump_seed: 254,
50            token_program_id: solana_pubkey::Pubkey::from_str_const(
51                "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb",
52            ),
53            token_a: solana_pubkey::Pubkey::from_str_const(
54                "jM5cFHP9iPj9en1fJFJLfRpLt68Y81UdWfXHv9an3HK",
55            ),
56            token_b: solana_pubkey::Pubkey::from_str_const(
57                "8a4WD4hbfuPyiistrVU8qcpwMcJmf3RBuw1s1tvVYJ1Q",
58            ),
59            pool_mint: solana_pubkey::Pubkey::from_str_const(
60                "7XeJQykinTiK1EveXb9y4zodFtdtu1YwkygBmWbz1pC3",
61            ),
62            token_a_mint: solana_pubkey::Pubkey::from_str_const(
63                "So11111111111111111111111111111111111111112",
64            ),
65            token_b_mint: solana_pubkey::Pubkey::from_str_const(
66                "3YkBR2w1ttpWKzdP5XQtzXqsGFS9i1mGg9pDrqn4e9j6",
67            ),
68            pool_fee_account: solana_pubkey::Pubkey::from_str_const(
69                "396TeW1MeyQvFGgxjaxJxRFkuiir4Ye4imuxVDcqfE88",
70            ),
71            fees: Fees {
72                trade_fee_numerator: 2,
73                trade_fee_denominator: 1000,
74                owner_trade_fee_numerator: 90,
75                owner_trade_fee_denominator: 100,
76                owner_withdraw_fee_numerator: 98,
77                owner_withdraw_fee_denominator: 100,
78                host_fee_numerator: 0,
79                host_fee_denominator: 10000,
80            },
81            swap_curve: SwapCurve {
82                curve_type: CurveType::ConstantProduct,
83                calculator: [0u8; 32],
84            },
85        };
86
87        // Act
88        let decoder = FluxbeamDecoder;
89        let account = carbon_test_utils::read_account("tests/fixtures/swap_account.json")
90            .expect("read fixture");
91        let decoded_account = decoder.decode_account(&account).expect("decode fixture");
92
93        // Assert
94        match decoded_account.data {
95            FluxbeamAccount::SwapV1(swap_account) => {
96                assert_eq!(
97                    expected_swap_account.is_initialized,
98                    swap_account.is_initialized
99                );
100                assert_eq!(expected_swap_account.bump_seed, swap_account.bump_seed);
101                assert_eq!(
102                    expected_swap_account.token_program_id,
103                    swap_account.token_program_id
104                );
105                assert_eq!(expected_swap_account.token_a, swap_account.token_a);
106                assert_eq!(expected_swap_account.token_b, swap_account.token_b);
107                assert_eq!(expected_swap_account.pool_mint, swap_account.pool_mint);
108                assert_eq!(
109                    expected_swap_account.token_a_mint,
110                    swap_account.token_a_mint
111                );
112                assert_eq!(
113                    expected_swap_account.token_b_mint,
114                    swap_account.token_b_mint
115                );
116                assert_eq!(
117                    expected_swap_account.pool_fee_account,
118                    swap_account.pool_fee_account
119                );
120                assert_eq!(expected_swap_account.fees, swap_account.fees);
121                assert_eq!(expected_swap_account.swap_curve, swap_account.swap_curve);
122            }
123        }
124    }
125}