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:
Ali Behjati 2022-09-30 20:53:02 +00:00 committed by GitHub
parent 0dbd0b6628
commit 97617cb5ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 4 deletions

View File

@ -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),
}
}
}

View File

@ -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 =