Skip to content

Commit

Permalink
lamport transfer helper
Browse files Browse the repository at this point in the history
  • Loading branch information
HardhatChad committed Oct 16, 2024
1 parent 4796ca6 commit 193dd5f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion lib/src/loaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bytemuck::Pod;
use solana_program::program_pack::Pack;
use solana_program::{account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey};

use crate::{AccountDeserialize, AccountInfoValidation, AsAccount, Discriminator};
use crate::{AccountDeserialize, AccountInfoValidation, AsAccount, Discriminator, LamportTransfer};
#[cfg(feature = "spl")]
use crate::{AccountValidation, AsSplToken};

Expand Down Expand Up @@ -104,6 +104,14 @@ impl AsAccount for AccountInfo<'_> {
}
}

impl LamportTransfer for AccountInfo<'_> {
fn transfer(&self, amount: u64, to: AccountInfo) -> Result<(), ProgramError> {
**self.lamports.borrow_mut() -= amount;
**to.lamports.borrow_mut() += amount;
Ok(())
}
}

#[cfg(feature = "spl")]
impl AsSplToken for AccountInfo<'_> {
fn as_mint(&self) -> Result<spl_token::state::Mint, ProgramError> {
Expand Down
6 changes: 5 additions & 1 deletion lib/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bytemuck::Pod;
use solana_program::{program_error::ProgramError, pubkey::Pubkey};
use solana_program::{account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey};

pub trait AccountDeserialize {
fn try_from_bytes(data: &[u8]) -> Result<&Self, ProgramError>;
Expand Down Expand Up @@ -144,6 +144,10 @@ pub trait AsSplToken {
) -> Result<spl_token::state::Account, ProgramError>;
}

pub trait LamportTransfer {
fn transfer(&self, amount: u64, to: AccountInfo) -> Result<(), ProgramError>;
}

pub trait ProgramOwner {
fn owner() -> Pubkey;
}
Expand Down

0 comments on commit 193dd5f

Please sign in to comment.