Solitaire: Use sysvar for rent instead of default (#1505)
* Solitaire: Use sysvar for rent instead of default Using default has some risks such as: - Network having a different values (such as Pythnet) - Network changes the value It avoids using default as fallback to have Error more explicitly avoid exposing above risks under rare conditions that the sysvar might return Err. * Update comments * Format the code
This commit is contained in:
parent
0dbd0b6628
commit
97617cb5ce
|
@ -13,6 +13,7 @@ use solana_program::{
|
|||
account_info::AccountInfo,
|
||||
pubkey::Pubkey,
|
||||
rent::Rent,
|
||||
sysvar::Sysvar as SolanaSysvar,
|
||||
};
|
||||
|
||||
use std::slice::Iter;
|
||||
|
@ -72,10 +73,15 @@ pub enum CreationLamports {
|
|||
|
||||
impl CreationLamports {
|
||||
/// Amount of lamports to be paid in account creation
|
||||
pub fn amount(self, size: usize) -> u64 {
|
||||
pub fn amount(self, size: usize) -> Result<u64> {
|
||||
match self {
|
||||
CreationLamports::Exempt => Rent::default().minimum_balance(size),
|
||||
CreationLamports::Amount(v) => v,
|
||||
// Rent::get() is used over Rent::default() here in case on-chain Rent has diverged from
|
||||
// the Solana defaults. Unlike `default()`, the `get()` call only works on-chain and in
|
||||
// Solana's test framework, it will fail in unit tests. In those environments one can use
|
||||
// the following:
|
||||
// `CreationLamports::Amount(Rent::default().minimum_balance(size))`
|
||||
CreationLamports::Exempt => Ok(Rent::get()?.minimum_balance(size)),
|
||||
CreationLamports::Amount(v) => Ok(v),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -188,7 +188,7 @@ pub fn create_account(
|
|||
owner: &Pubkey,
|
||||
seeds: IsSigned,
|
||||
) -> Result<()> {
|
||||
let target_rent = lamports.amount(size);
|
||||
let target_rent = lamports.amount(size)?;
|
||||
// top up account to target rent
|
||||
if account.lamports() < target_rent {
|
||||
let transfer_ix =
|
||||
|
|
Loading…
Reference in New Issue