Skip to content

Commit 90152b1

Browse files
authored
p-token: Add withdraw_excess_lamports instruction (#36)
* Update workspace * Rename interface crate * Fix spelling * [wip]: Fix review comments * A few more fixes * Fix merge * Update workspace * Rename interface crate * Fix spelling * [wip]: Fix review comments * Add withdraw_excess_lamports instruction * Add test * Add missing safety comments * Add more tests * Fix merge * Use assert_matches
1 parent baa5918 commit 90152b1

File tree

7 files changed

+1161
-1
lines changed

7 files changed

+1161
-1
lines changed

Diff for: Cargo.lock

+301
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: interface/src/instruction.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,18 @@ pub enum TokenInstruction {
478478
/// - `&str` The `ui_amount` of tokens to reformat.
479479
UiAmountToAmount,
480480

481+
/// This instruction is to be used to rescue SOL sent to any `TokenProgram`
482+
/// owned account by sending them to any other account, leaving behind only
483+
/// lamports for rent exemption.
484+
///
485+
/// Accounts expected by this instruction:
486+
///
487+
/// 0. `[writable]` Source Account owned by the token program
488+
/// 1. `[writable]` Destination account
489+
/// 2. `[signer]` Authority
490+
/// 3. `..+M` `[signer]` M signer accounts.
491+
WithdrawExcessLamports = 38,
492+
481493
/// Executes a batch of instructions. The instructions to be executed are specified
482494
/// in sequence on the instruction data. Each instruction provides:
483495
/// - `u8`: number of accounts
@@ -506,7 +518,7 @@ impl TryFrom<u8> for TokenInstruction {
506518
fn try_from(value: u8) -> Result<Self, Self::Error> {
507519
match value {
508520
// SAFETY: `value` is guaranteed to be in the range of the enum variants.
509-
0..=24 | 255 => Ok(unsafe { core::mem::transmute::<u8, TokenInstruction>(value) }),
521+
0..=24 | 38 | 255 => Ok(unsafe { core::mem::transmute::<u8, TokenInstruction>(value) }),
510522
_ => Err(ProgramError::InvalidInstructionData),
511523
}
512524
}

Diff for: p-token/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ spl-token-interface = { version = "^0", path = "../interface" }
2121

2222
[dev-dependencies]
2323
assert_matches = "1.5.0"
24+
num-traits = "0.2"
2425
solana-program-test = "2.1"
2526
solana-sdk = "2.1"
2627
spl-token = { version="^4", features=["no-entrypoint"] }
28+
spl-token-2022 = { version="^7", features=["no-entrypoint"] }
2729
test-case = "3.3.1"

Diff for: p-token/src/entrypoint.rs

+7
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ fn inner_process_remaining_instruction(
257257

258258
process_ui_amount_to_amount(accounts, instruction_data)
259259
}
260+
// 38 - WithdrawExcessLamports
261+
38 => {
262+
#[cfg(feature = "logging")]
263+
pinocchio::msg!("Instruction: WithdrawExcessLamports");
264+
265+
process_withdraw_excess_lamports(accounts)
266+
}
260267
_ => Err(ProgramError::InvalidInstructionData),
261268
}
262269
}

0 commit comments

Comments
 (0)