pump_rust_client/
accounts.rs1use anchor_lang::AccountDeserialize;
4
5use crate::{
6 errors::{PumpClientError, Result},
7 state::{
8 BondingCurve, FeeConfig, Global, GlobalVolumeAccumulator, SharingConfig,
9 UserVolumeAccumulator,
10 },
11};
12
13pub fn decode<T: AccountDeserialize>(data: &[u8]) -> Result<T> {
17 T::try_deserialize(&mut &data[..]).map_err(PumpClientError::from)
18}
19
20pub fn decode_global(data: &[u8]) -> Result<Global> {
25 decode::<Global>(data)
26}
27
28pub fn decode_fee_config(data: &[u8]) -> Result<FeeConfig> {
29 decode::<FeeConfig>(data)
30}
31
32pub fn decode_bonding_curve(data: &[u8]) -> Result<BondingCurve> {
33 decode::<BondingCurve>(data)
34}
35
36pub fn decode_bonding_curve_nullable(data: &[u8]) -> Option<BondingCurve> {
37 decode::<BondingCurve>(data).ok()
38}
39
40pub fn decode_global_volume_accumulator(data: &[u8]) -> Result<GlobalVolumeAccumulator> {
41 decode::<GlobalVolumeAccumulator>(data)
42}
43
44pub fn decode_user_volume_accumulator(data: &[u8]) -> Result<UserVolumeAccumulator> {
45 decode::<UserVolumeAccumulator>(data)
46}
47
48pub fn decode_user_volume_accumulator_nullable(data: &[u8]) -> Option<UserVolumeAccumulator> {
49 decode::<UserVolumeAccumulator>(data).ok()
50}
51
52pub fn decode_sharing_config(data: &[u8]) -> Result<SharingConfig> {
53 decode::<SharingConfig>(data)
54}
55
56pub mod pump_amm {
61 use super::{decode, Result};
62 use crate::state::pump_amm::{
63 GlobalConfig, GlobalVolumeAccumulator, Pool, UserVolumeAccumulator,
64 };
65
66 pub fn decode_global_config(data: &[u8]) -> Result<GlobalConfig> {
67 decode::<GlobalConfig>(data)
68 }
69
70 pub fn decode_global_volume_accumulator(data: &[u8]) -> Result<GlobalVolumeAccumulator> {
71 decode::<GlobalVolumeAccumulator>(data)
72 }
73
74 pub fn decode_pool(data: &[u8]) -> Result<Pool> {
75 decode::<Pool>(data)
76 }
77
78 pub fn decode_user_volume_accumulator(data: &[u8]) -> Result<UserVolumeAccumulator> {
79 decode::<UserVolumeAccumulator>(data)
80 }
81}
82
83pub mod pump_agent_payments {
88 use super::{decode, Result};
89 use crate::state::pump_agent_payments::GlobalConfig;
90
91 pub fn decode_global_config(data: &[u8]) -> Result<GlobalConfig> {
92 decode::<GlobalConfig>(data)
93 }
94}
95
96#[cfg(test)]
97mod tests {
98 use super::*;
99 use anchor_lang::AccountSerialize;
100 use solana_program::pubkey::Pubkey;
101
102 use crate::{
103 constants,
104 state::{BondingCurve, BondingCurveFromIdl},
105 };
106
107 fn fake_pubkey(seed: u8) -> Pubkey {
108 Pubkey::new_from_array([seed; 32])
109 }
110
111 #[test]
112 fn bonding_curve_round_trip() {
113 let original = BondingCurve::new(BondingCurveFromIdl {
114 virtual_token_reserves: 1_000_000_000,
115 virtual_quote_reserves: 30_000_000_000,
116 real_token_reserves: 800_000_000,
117 real_quote_reserves: 0,
118 token_total_supply: 1_000_000_000_000,
119 complete: false,
120 creator: fake_pubkey(7),
121 is_mayhem_mode: true,
122 is_cashback_coin: false,
123 quote_mint: constants::NATIVE_MINT,
124 });
125
126 let mut buf = Vec::new();
127 original.try_serialize(&mut buf).expect("serialize");
128
129 let decoded: BondingCurve = decode(&buf).expect("decode");
130 assert_eq!(
131 decoded.virtual_token_reserves,
132 original.virtual_token_reserves
133 );
134 assert_eq!(decoded.creator, original.creator);
135 assert_eq!(decoded.is_mayhem_mode, original.is_mayhem_mode);
136 assert_eq!(decoded.quote_mint, original.quote_mint);
137 }
138}