|
| 1 | +use generic_array_struct::generic_array_struct; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + internal_utils::const_assign_byte_arr, LIDO_STATE_ADDR, STAKE_AUTH_PDA, STAKE_PROGRAM, |
| 5 | + STSOL_MINT_ADDR, SYSTEM_PROGRAM, SYSVAR_CLOCK, TOKENKEG_PROGRAM, VALIDATOR_LIST_ADDR, |
| 6 | +}; |
| 7 | + |
| 8 | +pub const WITHDRAW_V2_IX_DISCM: u8 = 23; |
| 9 | + |
| 10 | +#[generic_array_struct(pub)] |
| 11 | +#[repr(transparent)] |
| 12 | +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)] |
| 13 | +pub struct WithdrawV2IxAccs<T> { |
| 14 | + pub lido_state: T, |
| 15 | + pub user: T, |
| 16 | + pub burn_stsol_from: T, |
| 17 | + pub stsol_mint: T, |
| 18 | + pub vote: T, |
| 19 | + |
| 20 | + /// PDA obtained using |
| 21 | + /// [`crate::Validator::withdraw_stake_acc_seeds()`] |
| 22 | + /// of `Validator` entry on the `ValidatorList` account |
| 23 | + /// for `vote = self.vote()` |
| 24 | + pub withdraw_stake_from: T, |
| 25 | + |
| 26 | + /// Must be a system account prefunded with rent-exempt lamports |
| 27 | + /// for a stake account. The program will call allocate() and assign() |
| 28 | + pub split_stake_to: T, |
| 29 | + |
| 30 | + pub stake_auth: T, |
| 31 | + pub validator_list: T, |
| 32 | + pub token_prog: T, |
| 33 | + pub sysvar_clock: T, |
| 34 | + pub system_prog: T, |
| 35 | + pub stake_prog: T, |
| 36 | +} |
| 37 | + |
| 38 | +pub type WithdrawV2IxKeysOwned = WithdrawV2IxAccs<[u8; 32]>; |
| 39 | +pub type WithdrawV2IxKeys<'a> = WithdrawV2IxAccs<&'a [u8; 32]>; |
| 40 | +pub type WithdrawV2IxAccsFlag = WithdrawV2IxAccs<bool>; |
| 41 | + |
| 42 | +pub const WITHDRAW_V2_IX_IS_WRITER: WithdrawV2IxAccsFlag = |
| 43 | + WithdrawV2IxAccsFlag::new([false; WITHDRAW_V2_IX_ACCS_LEN]) |
| 44 | + .const_with_lido_state(true) |
| 45 | + .const_with_burn_stsol_from(true) |
| 46 | + .const_with_stsol_mint(true) |
| 47 | + .const_with_withdraw_stake_from(true) |
| 48 | + .const_with_split_stake_to(true) |
| 49 | + .const_with_validator_list(true); |
| 50 | + |
| 51 | +pub const WITHDRAW_V2_IX_IS_SIGNER: WithdrawV2IxAccsFlag = |
| 52 | + WithdrawV2IxAccsFlag::new([false; WITHDRAW_V2_IX_ACCS_LEN]) |
| 53 | + .const_with_user(true) |
| 54 | + .const_with_split_stake_to(true); |
| 55 | + |
| 56 | +impl<T> WithdrawV2IxAccs<T> { |
| 57 | + /// This seems redundant because `.0` is `pub`, but this is necessary for |
| 58 | + /// nice init syntax with type aliases. |
| 59 | + /// |
| 60 | + /// With this, you can now do |
| 61 | + /// |
| 62 | + /// ``` |
| 63 | + /// use solido_legacy_core::WithdrawV2IxAccsFlag; |
| 64 | + /// let var: WithdrawV2IxAccsFlag = WithdrawV2IxAccsFlag::new(Default::default()); |
| 65 | + /// ``` |
| 66 | + /// |
| 67 | + /// instead of |
| 68 | + /// |
| 69 | + /// ``` |
| 70 | + /// use solido_legacy_core::{WithdrawV2IxAccsFlag, WithdrawV2IxAccs}; |
| 71 | + /// let var: WithdrawV2IxAccsFlag = WithdrawV2IxAccs(Default::default()); |
| 72 | + /// ``` |
| 73 | + #[inline] |
| 74 | + pub const fn new(arr: [T; WITHDRAW_V2_IX_ACCS_LEN]) -> Self { |
| 75 | + Self(arr) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl WithdrawV2IxKeysOwned { |
| 80 | + #[inline] |
| 81 | + pub fn as_borrowed(&self) -> WithdrawV2IxKeys<'_> { |
| 82 | + WithdrawV2IxKeys::new(self.0.each_ref()) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl WithdrawV2IxKeysOwned { |
| 87 | + #[inline] |
| 88 | + pub fn with_lido_consts(self) -> Self { |
| 89 | + self.as_borrowed().with_lido_consts().into_owned() |
| 90 | + } |
| 91 | + |
| 92 | + #[inline] |
| 93 | + pub fn with_sol_consts(self) -> Self { |
| 94 | + self.as_borrowed().with_sol_consts().into_owned() |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl WithdrawV2IxKeys<'_> { |
| 99 | + #[inline] |
| 100 | + pub fn into_owned(self) -> WithdrawV2IxKeysOwned { |
| 101 | + WithdrawV2IxKeysOwned::new(self.0.map(|pk| *pk)) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +impl WithdrawV2IxKeys<'_> { |
| 106 | + #[inline] |
| 107 | + pub const fn with_lido_consts(self) -> Self { |
| 108 | + self.const_with_lido_state(&LIDO_STATE_ADDR) |
| 109 | + .const_with_stsol_mint(&STSOL_MINT_ADDR) |
| 110 | + .const_with_stake_auth(&STAKE_AUTH_PDA) |
| 111 | + .const_with_validator_list(&VALIDATOR_LIST_ADDR) |
| 112 | + } |
| 113 | + |
| 114 | + #[inline] |
| 115 | + pub const fn with_sol_consts(self) -> Self { |
| 116 | + self.const_with_system_prog(&SYSTEM_PROGRAM) |
| 117 | + .const_with_stake_prog(&STAKE_PROGRAM) |
| 118 | + .const_with_sysvar_clock(&SYSVAR_CLOCK) |
| 119 | + .const_with_token_prog(&TOKENKEG_PROGRAM) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +pub const WITHDRAW_V2_IX_DATA_LEN: usize = 13; |
| 124 | + |
| 125 | +#[repr(transparent)] |
| 126 | +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)] |
| 127 | +pub struct WithdrawV2IxData([u8; WITHDRAW_V2_IX_DATA_LEN]); |
| 128 | + |
| 129 | +impl WithdrawV2IxData { |
| 130 | + #[inline] |
| 131 | + pub const fn new(amount: u64, validator_index: u32) -> Self { |
| 132 | + let mut res = [0u8; WITHDRAW_V2_IX_DATA_LEN]; |
| 133 | + const_assign_byte_arr::<WITHDRAW_V2_IX_DATA_LEN, 0, 1>(&mut res, [WITHDRAW_V2_IX_DISCM]); |
| 134 | + const_assign_byte_arr::<WITHDRAW_V2_IX_DATA_LEN, 1, 8>(&mut res, amount.to_le_bytes()); |
| 135 | + const_assign_byte_arr::<WITHDRAW_V2_IX_DATA_LEN, 9, 4>( |
| 136 | + &mut res, |
| 137 | + validator_index.to_le_bytes(), |
| 138 | + ); |
| 139 | + Self(res) |
| 140 | + } |
| 141 | + |
| 142 | + #[inline] |
| 143 | + pub const fn to_buf(self) -> [u8; WITHDRAW_V2_IX_DATA_LEN] { |
| 144 | + self.0 |
| 145 | + } |
| 146 | +} |
0 commit comments