-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add convenience function for programs to get minimum delegation (#24175)
- Loading branch information
1 parent
b4b2689
commit e146e86
Showing
9 changed files
with
125 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "solana-bpf-rust-get-minimum-delegation" | ||
version = "1.11.0" | ||
description = "Solana BPF test program written in Rust" | ||
authors = ["Solana Maintainers <[email protected]>"] | ||
repository = "https://github.com/solana-labs/solana" | ||
license = "Apache-2.0" | ||
homepage = "https://solana.com/" | ||
documentation = "https://docs.rs/solana-bpf-rust-get-minimum-delegation" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
solana-program = { path = "../../../../sdk/program", version = "=1.11.0" } | ||
|
||
[lib] | ||
crate-type = ["cdylib"] | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//! Example/test program to get the minimum stake delegation via the helper function | ||
#![allow(unreachable_code)] | ||
|
||
extern crate solana_program; | ||
use solana_program::{ | ||
account_info::AccountInfo, entrypoint::ProgramResult, msg, pubkey::Pubkey, stake, | ||
}; | ||
|
||
solana_program::entrypoint!(process_instruction); | ||
#[allow(clippy::unnecessary_wraps)] | ||
fn process_instruction( | ||
_program_id: &Pubkey, | ||
_accounts: &[AccountInfo], | ||
_instruction_data: &[u8], | ||
) -> ProgramResult { | ||
let minimum_delegation = stake::tools::get_minimum_delegation()?; | ||
msg!( | ||
"The minimum stake delegation is {} lamports", | ||
minimum_delegation | ||
); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//! Utility functions | ||
use crate::program_error::ProgramError; | ||
|
||
/// Helper function for programs to call [`GetMinimumDelegation`] and then fetch the return data | ||
/// | ||
/// This fn handles performing the CPI to call the [`GetMinimumDelegation`] function, and then | ||
/// calls [`get_return_data()`] to fetch the return data. | ||
/// | ||
/// [`GetMinimumDelegation`]: super::instruction::StakeInstruction::GetMinimumDelegation | ||
/// [`get_return_data()`]: crate::program::get_return_data | ||
pub fn get_minimum_delegation() -> Result<u64, ProgramError> { | ||
let instruction = super::instruction::get_minimum_delegation(); | ||
crate::program::invoke_unchecked(&instruction, &[])?; | ||
get_minimum_delegation_return_data() | ||
} | ||
|
||
/// Helper function for programs to get the return data after calling [`GetMinimumDelegation`] | ||
/// | ||
/// This fn handles calling [`get_return_data()`], ensures the result is from the correct | ||
/// program, and returns the correct type. | ||
/// | ||
/// [`GetMinimumDelegation`]: super::instruction::StakeInstruction::GetMinimumDelegation | ||
/// [`get_return_data()`]: crate::program::get_return_data | ||
fn get_minimum_delegation_return_data() -> Result<u64, ProgramError> { | ||
crate::program::get_return_data() | ||
.ok_or(ProgramError::InvalidInstructionData) | ||
.and_then(|(program_id, return_data)| { | ||
(program_id == super::program::id()) | ||
.then(|| return_data) | ||
.ok_or(ProgramError::IncorrectProgramId) | ||
}) | ||
.and_then(|return_data| { | ||
return_data | ||
.try_into() | ||
.or(Err(ProgramError::InvalidInstructionData)) | ||
}) | ||
.map(u64::from_le_bytes) | ||
} |