-
Notifications
You must be signed in to change notification settings - Fork 7
Sync metadata to token-2022 pt. 1 #221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
use { | ||
crate::{get_wrapped_mint_authority, mint_customizer::interface::MintCustomizer}, | ||
solana_account_info::AccountInfo, | ||
solana_cpi::{invoke, invoke_signed}, | ||
solana_cpi::invoke, | ||
solana_program_error::{ProgramError, ProgramResult}, | ||
solana_pubkey::Pubkey, | ||
spl_token_2022::{ | ||
|
@@ -14,17 +14,14 @@ use { | |
pod::PodMint, | ||
state::Mint, | ||
}, | ||
spl_token_metadata_interface::{ | ||
instruction::initialize as initialize_token_metadata, state::TokenMetadata, | ||
}, | ||
}; | ||
|
||
/// This implementation adds the `ConfidentialTransferMint` & `TokenMetadata` | ||
/// This implementation adds the `ConfidentialTransferMint` & `MetadataPointer` | ||
/// extensions by default. | ||
pub struct DefaultToken2022Customizer; | ||
|
||
impl MintCustomizer for DefaultToken2022Customizer { | ||
fn get_token_2022_mint_initialization_space() -> Result<usize, ProgramError> { | ||
fn get_token_2022_mint_space() -> Result<usize, ProgramError> { | ||
// Calculate space for all extensions that are initialized *before* the base | ||
// mint. The TokenMetadata extension is initialized *after* and its | ||
// `initialize` instruction handles its own reallocation. | ||
|
@@ -34,15 +31,7 @@ impl MintCustomizer for DefaultToken2022Customizer { | |
]) | ||
} | ||
|
||
fn get_token_2022_total_space() -> Result<usize, ProgramError> { | ||
let base_size = Self::get_token_2022_mint_initialization_space()?; | ||
let metadata_size = TokenMetadata::default().tlv_size_of()?; | ||
base_size | ||
.checked_add(metadata_size) | ||
.ok_or(ProgramError::ArithmeticOverflow) | ||
} | ||
|
||
fn pre_initialize_extensions( | ||
fn initialize_extensions( | ||
wrapped_mint_account: &AccountInfo, | ||
wrapped_token_program_account: &AccountInfo, | ||
) -> ProgramResult { | ||
|
@@ -73,42 +62,6 @@ impl MintCustomizer for DefaultToken2022Customizer { | |
Ok(()) | ||
} | ||
|
||
fn post_initialize_extensions<'a>( | ||
wrapped_mint_account: &AccountInfo<'a>, | ||
wrapped_token_program_account: &AccountInfo, | ||
wrapped_mint_authority_account: &AccountInfo<'a>, | ||
mint_authority_signer_seeds: &[&[u8]], | ||
) -> ProgramResult { | ||
// Initialize metadata ext (must be done after mint initialization) | ||
let wrapped_mint_authority = get_wrapped_mint_authority(wrapped_mint_account.key); | ||
|
||
let cpi_accounts = [ | ||
wrapped_mint_account.clone(), | ||
wrapped_mint_authority_account.clone(), | ||
wrapped_mint_account.clone(), | ||
wrapped_mint_authority_account.clone(), | ||
]; | ||
|
||
invoke_signed( | ||
&initialize_token_metadata( | ||
wrapped_token_program_account.key, | ||
wrapped_mint_account.key, | ||
&wrapped_mint_authority, | ||
wrapped_mint_account.key, | ||
&wrapped_mint_authority, | ||
// Initialized as empty, but separate instructions are available | ||
// to update these fields | ||
"".to_string(), | ||
"".to_string(), | ||
"".to_string(), | ||
), | ||
&cpi_accounts, | ||
&[mint_authority_signer_seeds], | ||
)?; | ||
Comment on lines
-92
to
-107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing this saves rent on those wrapped mints without unwrapped mint metadata |
||
|
||
Ok(()) | ||
} | ||
|
||
fn get_freeze_auth_and_decimals( | ||
unwrapped_mint_account: &AccountInfo, | ||
) -> Result<(Option<Pubkey>, u8), ProgramError> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,34 +8,17 @@ use { | |
pub trait MintCustomizer { | ||
/// Calculates the space required for a new spl-token-2022 mint | ||
/// account, including any custom extensions | ||
fn get_token_2022_mint_initialization_space() -> Result<usize, ProgramError>; | ||
|
||
/// Calculates the total space required for a new spl-token-2022 | ||
/// mint, after `post_initialize_extensions()` is called. This is useful in | ||
/// calculating rent requirements if something like `TokenMetadata` does a | ||
/// realloc after the mint is created. If not implemented, defaults to | ||
/// `get_token_2022_mint_initialization_space()` result. | ||
fn get_token_2022_total_space() -> Result<usize, ProgramError> { | ||
Self::get_token_2022_mint_initialization_space() | ||
} | ||
fn get_token_2022_mint_space() -> Result<usize, ProgramError>; | ||
|
||
/// Customizes extensions for the wrapped mint *before* the base mint is | ||
/// initialized. This is for extensions that must be initialized on an | ||
/// uninitialized mint account, like `ConfidentialTransferMint`. | ||
fn pre_initialize_extensions( | ||
wrapped_mint_account: &AccountInfo, | ||
wrapped_token_program_account: &AccountInfo, | ||
) -> ProgramResult; | ||
|
||
/// Customizes extensions for the wrapped mint *after* the base mint is | ||
/// initialized. This is for extensions that require the mint to be | ||
/// initialized, like `TokenMetadata`. | ||
fn post_initialize_extensions<'a>( | ||
wrapped_mint_account: &AccountInfo<'a>, | ||
wrapped_token_program_account: &AccountInfo, | ||
wrapped_mint_authority_account: &AccountInfo<'a>, | ||
mint_authority_signer_seeds: &[&[u8]], | ||
) -> ProgramResult; | ||
Comment on lines
-25
to
-38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplifying this interface and removing the pre-post-mint-init pattern here (given we don't do anything post init in CreateMint anymore) |
||
fn initialize_extensions( | ||
_wrapped_mint_account: &AccountInfo, | ||
_wrapped_token_program_account: &AccountInfo, | ||
) -> ProgramResult { | ||
Ok(()) | ||
} | ||
|
||
/// Customize the freeze authority and decimals for the wrapped mint | ||
fn get_freeze_auth_and_decimals( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New lint warnings require this