Skip to main content

carbon_token_program_decoder/instructions/
amount_to_ui_amount.rs

1//! This code was AUTOGENERATED using the Codama library.
2use carbon_core::{account_utils::next_account, deserialize::ArrangeAccounts};
3/// Convert an Amount of tokens to a UiAmount `string`, using the given
4/// mint. In this version of the program, the mint can only specify the
5/// number of decimals.
6/// Fails on an invalid mint.
7/// Return data can be fetched using `sol_get_return_data` and deserialized
8/// with `String::from_utf8`.
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10#[derive(Debug, Clone, borsh::BorshSerialize, borsh::BorshDeserialize, PartialEq)]
11pub struct AmountToUiAmount {
12    /// The amount of tokens to reformat.
13    pub amount: u64,
14}
15
16#[derive(Debug, Clone, PartialEq)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18pub struct AmountToUiAmountInstructionAccounts {
19    pub mint: solana_pubkey::Pubkey,
20    pub remaining: Vec<solana_instruction::AccountMeta>,
21}
22
23impl AmountToUiAmount {
24    pub fn decode(data: &[u8]) -> Option<Self> {
25        if data.is_empty() {
26            return None;
27        }
28        let discriminator = &data[0..1];
29        if discriminator != [23] {
30            return None;
31        }
32
33        let mut data_slice = data;
34
35        data_slice = &data_slice[1..];
36
37        borsh::BorshDeserialize::deserialize(&mut data_slice).ok()
38    }
39}
40
41impl ArrangeAccounts for AmountToUiAmount {
42    type ArrangedAccounts = AmountToUiAmountInstructionAccounts;
43
44    fn arrange_accounts(
45        accounts: &[solana_instruction::AccountMeta],
46    ) -> Option<Self::ArrangedAccounts> {
47        let mut iter = accounts.iter();
48
49        let mint = next_account(&mut iter)?;
50
51        let remaining = iter.as_slice();
52
53        Some(AmountToUiAmountInstructionAccounts {
54            mint,
55            remaining: remaining.to_vec(),
56        })
57    }
58}