MCPcopy Create free account
hub / github.com/nifty-oss/asset / process_write

Function process_write

programs/asset/program/src/processor/write.rs:28–162  ·  view source on GitHub ↗

Writes data to an extension. ### Accounts: 0. `[writable, signer]` asset 1. `[writable, signer]` payer 2. `[optional]` system_program

(
    program_id: &Pubkey,
    ctx: Context<Write>,
    data: DataInput,
)

Source from the content-addressed store, hash-verified

26/// 1. `[writable, signer]` payer
27/// 2. `[optional]` system_program
28pub(crate) fn process_write(
29 program_id: &Pubkey,
30 ctx: Context<Write>,
31 data: DataInput,
32) -> ProgramResult {
33 // account validation
34
35 require!(
36 ctx.accounts.system_program.key() == &system_program::ID,
37 ProgramError::IncorrectProgramId,
38 "system_program"
39 );
40
41 require!(
42 ctx.accounts.payer.is_signer(),
43 ProgramError::MissingRequiredSignature,
44 "payer"
45 );
46
47 require!(
48 ctx.accounts.asset.is_signer(),
49 ProgramError::MissingRequiredSignature,
50 "asset"
51 );
52
53 require!(
54 ctx.accounts.asset.owner() == program_id,
55 ProgramError::IllegalOwner,
56 "asset"
57 );
58
59 let asset_data = ctx.accounts.asset.try_borrow_data()?;
60
61 require!(
62 asset_data.len() >= Asset::LEN,
63 AssetError::InvalidAccountLength,
64 "asset"
65 );
66
67 // make sure that the asset is not already initialized
68 require!(
69 asset_data[0] == Discriminator::Uninitialized.into(),
70 AssetError::AlreadyInitialized,
71 "asset"
72 );
73
74 // determine the account offset
75
76 let (extension, offset) =
77 Asset::last_extension(&asset_data).ok_or(AssetError::ExtensionNotFound)?;
78 let current = extension.extension_type();
79 let expected = offset.saturating_add(extension.length() as usize);
80 let boundary = extension.boundary();
81 // the length of the account cannot be larger than the current extension length,
82 // otherwise we would be writting data to an extension that does not exist
83 if expected < asset_data.len() {
84 return err!(AssetError::InvalidAccountLength);
85 }

Callers 1

process_instructionFunction · 0.85

Calls 4

boundaryMethod · 0.80
resizeFunction · 0.70
extension_typeMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected