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.
Move load and save to trait and check key
- Loading branch information
Showing
5 changed files
with
132 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,35 @@ | ||
pub trait DataBlob { | ||
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; | ||
fn key() -> Key; | ||
|
||
fn load(account: &AccountInfo, offset: usize) -> Result<Self, ProgramError> { | ||
let key = Key::from_u8((*account.data).borrow()[offset]) | ||
.ok_or(MplAssetError::DeserializationError)?; | ||
|
||
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() | ||
}) | ||
} | ||
} |