-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
token 2022: add
InitializeGroup
instruction from SPL Token Group in…
…terface
- Loading branch information
1 parent
82f3418
commit dbd9ef8
Showing
6 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,11 @@ | ||
use { | ||
crate::extension::{Extension, ExtensionType}, | ||
spl_token_group_interface::state::TokenGroup, | ||
}; | ||
|
||
/// Instruction processor for the TokenGroup extensions | ||
pub mod processor; | ||
|
||
impl Extension for TokenGroup { | ||
const TYPE: ExtensionType = ExtensionType::TokenGroup; | ||
} |
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,87 @@ | ||
//! Token-group processor | ||
use { | ||
crate::{ | ||
check_program_account, | ||
error::TokenError, | ||
extension::StateWithExtensions, | ||
state::Mint, | ||
}, | ||
solana_program::{ | ||
account_info::{next_account_info, AccountInfo}, | ||
entrypoint::ProgramResult, | ||
msg, | ||
program_error::ProgramError, | ||
program_option::COption, | ||
pubkey::Pubkey, | ||
}, | ||
spl_token_group_interface::{ | ||
instruction::{ | ||
InitializeGroup, TokenGroupInstruction, | ||
}, | ||
state::TokenGroup, | ||
}, | ||
spl_type_length_value::state::TlvStateMut, | ||
}; | ||
|
||
/// Processes a [InitializeGroup](enum.TokenGroupInstruction.html) instruction. | ||
pub fn process_initialize_group( | ||
_program_id: &Pubkey, | ||
accounts: &[AccountInfo], | ||
data: InitializeGroup, | ||
) -> ProgramResult { | ||
let account_info_iter = &mut accounts.iter(); | ||
|
||
let group_info = next_account_info(account_info_iter)?; | ||
let mint_info = next_account_info(account_info_iter)?; | ||
let mint_authority_info = next_account_info(account_info_iter)?; | ||
|
||
// check that the mint and group accounts are the same, since the group | ||
// extension should only describe itself | ||
if group_info.key != mint_info.key { | ||
msg!("Group configurations for a mint must be initialized in the mint itself."); | ||
return Err(TokenError::MintMismatch.into()); | ||
} | ||
|
||
// scope the mint authority check, since the mint is in the same account! | ||
{ | ||
// This check isn't really needed since we'll be writing into the account, | ||
// but auditors like it | ||
check_program_account(mint_info.owner)?; | ||
let mint_data = mint_info.try_borrow_data()?; | ||
let mint = StateWithExtensions::<Mint>::unpack(&mint_data)?; | ||
|
||
if !mint_authority_info.is_signer { | ||
return Err(ProgramError::MissingRequiredSignature); | ||
} | ||
if mint.base.mint_authority.as_ref() != COption::Some(mint_authority_info.key) { | ||
return Err(TokenError::IncorrectMintAuthority.into()); | ||
} | ||
} | ||
|
||
// Allocate a TLV entry for the space and write it in | ||
// Assumes that there's enough SOL for the new rent-exemption | ||
let mut buffer = group_info.try_borrow_mut_data()?; | ||
let mut state = TlvStateMut::unpack(&mut buffer)?; | ||
let (group, _) = state.init_value::<TokenGroup>(false)?; | ||
*group = TokenGroup::new(data.update_authority, data.max_size.into()); | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Processes an [Instruction](enum.Instruction.html). | ||
pub fn process_instruction( | ||
program_id: &Pubkey, | ||
accounts: &[AccountInfo], | ||
instruction: TokenGroupInstruction, | ||
) -> ProgramResult { | ||
match instruction { | ||
TokenGroupInstruction::InitializeGroup(data) => { | ||
msg!("TokenGroupInstruction: InitializeGroup"); | ||
process_initialize_group(program_id, accounts, data) | ||
} | ||
_ => { | ||
Err(ProgramError::InvalidInstructionData) | ||
} | ||
} | ||
} |
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