generated from metaplex-foundation/solana-project-template
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
885328d
commit eae99ac
Showing
13 changed files
with
75 additions
and
132 deletions.
There are no files selected for viewing
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
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
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,44 @@ | ||
use crate::{error::MplAssetError, state::Key}; | ||
use borsh::{BorshDeserialize, BorshSerialize}; | ||
use num_traits::FromPrimitive; | ||
use solana_program::account_info::AccountInfo; | ||
use solana_program::entrypoint::ProgramResult; | ||
use solana_program::msg; | ||
use solana_program::program_error::ProgramError; | ||
|
||
pub trait DataBlob: BorshSerialize + BorshDeserialize { | ||
fn get_initial_size() -> usize; | ||
fn get_size(&self) -> usize; | ||
} | ||
|
||
pub trait SolanaAccount: BorshSerialize + BorshDeserialize { | ||
fn key() -> Key; | ||
|
||
fn load(account: &AccountInfo, offset: usize) -> Result<Self, ProgramError> { | ||
let key = load_key(account, offset)?; | ||
|
||
if key != Self::key() { | ||
return Err(MplAssetError::DeserializationError.into()); | ||
} | ||
|
||
let mut bytes: &[u8] = &(*account.data).borrow()[offset..]; | ||
Self::deserialize(&mut bytes).map_err(|error| { | ||
msg!("Error: {}", error); | ||
MplAssetError::DeserializationError.into() | ||
}) | ||
} | ||
|
||
fn save(&self, account: &AccountInfo, offset: usize) -> ProgramResult { | ||
borsh::to_writer(&mut account.data.borrow_mut()[offset..], self).map_err(|error| { | ||
msg!("Error: {}", error); | ||
MplAssetError::SerializationError.into() | ||
}) | ||
} | ||
} | ||
|
||
pub fn load_key(account: &AccountInfo, offset: usize) -> Result<Key, ProgramError> { | ||
let key = Key::from_u8((*account.data).borrow()[offset]) | ||
.ok_or(MplAssetError::DeserializationError)?; | ||
|
||
Ok(key) | ||
} |
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