Skip to content

p-token: Add withdraw_excess_lamports instruction #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Mar 11, 2025
301 changes: 301 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion interface/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,18 @@ pub enum TokenInstruction {
/// - `&str` The `ui_amount` of tokens to reformat.
UiAmountToAmount,

/// This instruction is to be used to rescue SOL sent to any `TokenProgram`
/// owned account by sending them to any other account, leaving behind only
/// lamports for rent exemption.
///
/// Accounts expected by this instruction:
///
/// 0. `[writable]` Source Account owned by the token program
/// 1. `[writable]` Destination account
/// 2. `[signer]` Authority
/// 3. `..+M` `[signer]` M signer accounts.
WithdrawExcessLamports = 38,

/// Executes a batch of instructions. The instructions to be executed are specified
/// in sequence on the instruction data. Each instruction provides:
/// - `u8`: number of accounts
Expand Down Expand Up @@ -506,7 +518,7 @@ impl TryFrom<u8> for TokenInstruction {
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
// SAFETY: `value` is guaranteed to be in the range of the enum variants.
0..=24 | 255 => Ok(unsafe { core::mem::transmute::<u8, TokenInstruction>(value) }),
0..=24 | 38 | 255 => Ok(unsafe { core::mem::transmute::<u8, TokenInstruction>(value) }),
_ => Err(ProgramError::InvalidInstructionData),
}
}
Expand Down
2 changes: 2 additions & 0 deletions p-token/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ spl-token-interface = { version = "^0", path = "../interface" }

[dev-dependencies]
assert_matches = "1.5.0"
num-traits = "0.2"
solana-program-test = "2.1"
solana-sdk = "2.1"
spl-token = { version="^4", features=["no-entrypoint"] }
spl-token-2022 = { version="^7", features=["no-entrypoint"] }
test-case = "3.3.1"
7 changes: 7 additions & 0 deletions p-token/src/entrypoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ fn inner_process_remaining_instruction(

process_ui_amount_to_amount(accounts, instruction_data)
}
// 38 - WithdrawExcessLamports
38 => {
#[cfg(feature = "logging")]
pinocchio::msg!("Instruction: WithdrawExcessLamports");

process_withdraw_excess_lamports(accounts)
}
_ => Err(ProgramError::InvalidInstructionData),
}
}
Loading