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
//! This code was AUTOGENERATED using the Codama library.
use carbon_core::{account_utils::next_account, deserialize::ArrangeAccounts};
/// Perform a swap in this Whirlpool
/// ### Authority
/// - "token_authority" - The authority to withdraw tokens from the input token
/// account.
///
/// ### Parameters
/// - `amount` - The amount of input or output token to swap from (depending on
/// amount_specified_is_input).
/// - `other_amount_threshold` - The maximum/minimum of input/output token to
/// swap into (depending on amount_specified_is_input).
/// - `sqrt_price_limit` - The maximum/minimum price the swap will swap to.
/// - `amount_specified_is_input` - Specifies the token the parameter
/// `amount`represents. If true, the amount represents the input token of the
/// swap.
/// - `a_to_b` - The direction of the swap. True if swapping from A to B. False
/// if swapping from B to A.
///
/// #### Special Errors
/// - `ZeroTradableAmount` - User provided parameter `amount` is 0.
/// - `InvalidSqrtPriceLimitDirection` - User provided parameter
/// `sqrt_price_limit` does not match the direction of the trade.
/// - `SqrtPriceOutOfBounds` - User provided parameter `sqrt_price_limit` is
/// over Whirlppool's max/min bounds for sqrt-price.
/// - `InvalidTickArraySequence` - User provided tick-arrays are not in
/// sequential order required to proceed in this trade direction.
/// - `TickArraySequenceInvalidIndex` - The swap loop attempted to access an
/// invalid array index during the query of the next initialized tick.
/// - `TickArrayIndexOutofBounds` - The swap loop attempted to access an invalid
/// array index during tick crossing.
/// - `LiquidityOverflow` - Liquidity value overflowed 128bits during tick
/// crossing.
/// - `InvalidTickSpacing` - The swap pool was initialized with tick-spacing of
/// 0.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, borsh::BorshSerialize, borsh::BorshDeserialize, PartialEq)]
pub struct Swap {
pub amount: u64,
pub other_amount_threshold: u64,
pub sqrt_price_limit: u128,
pub amount_specified_is_input: bool,
pub a_to_b: bool,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SwapInstructionAccounts {
pub token_program: solana_pubkey::Pubkey,
pub token_authority: solana_pubkey::Pubkey,
pub whirlpool: solana_pubkey::Pubkey,
pub token_owner_account_a: solana_pubkey::Pubkey,
pub token_vault_a: solana_pubkey::Pubkey,
pub token_owner_account_b: solana_pubkey::Pubkey,
pub token_vault_b: solana_pubkey::Pubkey,
pub tick_array0: solana_pubkey::Pubkey,
pub tick_array1: solana_pubkey::Pubkey,
pub tick_array2: solana_pubkey::Pubkey,
pub oracle: solana_pubkey::Pubkey,
pub remaining: Vec<solana_instruction::AccountMeta>,
}
impl Swap {
pub fn decode(data: &[u8]) -> Option<Self> {
if data.len() < 8 {
return None;
}
let discriminator = &data[0..8];
if discriminator != [248, 198, 158, 145, 225, 117, 135, 200] {
return None;
}
let mut data_slice = data;
data_slice = &data_slice[8..];
borsh::BorshDeserialize::deserialize(&mut data_slice).ok()
}
}
impl ArrangeAccounts for Swap {
type ArrangedAccounts = SwapInstructionAccounts;
fn arrange_accounts(
accounts: &[solana_instruction::AccountMeta],
) -> Option<Self::ArrangedAccounts> {
let mut iter = accounts.iter();
let token_program = next_account(&mut iter)?;
let token_authority = next_account(&mut iter)?;
let whirlpool = next_account(&mut iter)?;
let token_owner_account_a = next_account(&mut iter)?;
let token_vault_a = next_account(&mut iter)?;
let token_owner_account_b = next_account(&mut iter)?;
let token_vault_b = next_account(&mut iter)?;
let tick_array0 = next_account(&mut iter)?;
let tick_array1 = next_account(&mut iter)?;
let tick_array2 = next_account(&mut iter)?;
let oracle = next_account(&mut iter)?;
let remaining = iter.as_slice();
Some(SwapInstructionAccounts {
token_program,
token_authority,
whirlpool,
token_owner_account_a,
token_vault_a,
token_owner_account_b,
token_vault_b,
tick_array0,
tick_array1,
tick_array2,
oracle,
remaining: remaining.to_vec(),
})
}
}