-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathwithdraw_nonce_account.rs
85 lines (74 loc) · 2.35 KB
/
withdraw_nonce_account.rs
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
use {
pinocchio::{
account_info::AccountInfo,
instruction::{AccountMeta, Instruction, Signer},
ProgramResult,
},
pinocchio_cpi::invoke_signed,
};
/// Withdraw funds from a nonce account.
///
/// The `u64` parameter is the lamports to withdraw, which must leave the
/// account balance above the rent exempt reserve or at zero.
///
/// ### Accounts:
/// 0. `[WRITE]` Nonce account
/// 1. `[WRITE]` Recipient account
/// 2. `[]` RecentBlockhashes sysvar
/// 3. `[]` Rent sysvar
/// 4. `[SIGNER]` Nonce authority
pub struct WithdrawNonceAccount<'a> {
/// Nonce account.
pub account: &'a AccountInfo,
/// Recipient account.
pub recipient: &'a AccountInfo,
/// RecentBlockhashes sysvar.
pub recent_blockhashes_sysvar: &'a AccountInfo,
/// Rent sysvar.
pub rent_sysvar: &'a AccountInfo,
/// Nonce authority.
pub authority: &'a AccountInfo,
/// Lamports to withdraw.
///
/// The account balance muat be left above the rent exempt reserve
/// or at zero.
pub lamports: u64,
}
impl WithdrawNonceAccount<'_> {
#[inline(always)]
pub fn invoke(&self) -> ProgramResult {
self.invoke_signed(&[])
}
pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
// account metadata
let account_metas: [AccountMeta; 5] = [
AccountMeta::writable(self.account.key()),
AccountMeta::writable(self.recipient.key()),
AccountMeta::readonly(self.recent_blockhashes_sysvar.key()),
AccountMeta::readonly(self.rent_sysvar.key()),
AccountMeta::readonly_signer(self.authority.key()),
];
// instruction data
// - [0..4 ]: instruction discriminator
// - [4..12]: lamports
let mut instruction_data = [0; 12];
instruction_data[0] = 5;
instruction_data[4..12].copy_from_slice(&self.lamports.to_le_bytes());
let instruction = Instruction {
program_id: &crate::ID,
accounts: &account_metas,
data: &instruction_data,
};
invoke_signed(
&instruction,
&[
self.account,
self.recipient,
self.recent_blockhashes_sysvar,
self.rent_sysvar,
self.authority,
],
signers,
)
}
}