Skip to content

Commit

Permalink
[stake-pool] instruction to add metadata for pool token (#3335)
Browse files Browse the repository at this point in the history
* instruction to add metadata for pool token

* add documentation for instruction accounts

* address pull request comments

* added more tests for update pool token instruction

* add check for payer signature
  • Loading branch information
betterclever committed Jul 22, 2022
1 parent 4d1f816 commit 8169e52
Show file tree
Hide file tree
Showing 9 changed files with 900 additions and 0 deletions.
67 changes: 67 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions stake-pool/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ test-bpf = []
[dependencies]
arrayref = "0.3.6"
borsh = "0.9"
mpl-token-metadata = { version = "1.3.1", features = [ "no-entrypoint" ] }
num-derive = "0.3"
num-traits = "0.2"
num_enum = "0.5.4"
Expand Down
3 changes: 3 additions & 0 deletions stake-pool/program/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ pub enum StakePoolError {
/// Too much SOL withdrawn from the stake pool's reserve account
#[error("SolWithdrawalTooLarge")]
SolWithdrawalTooLarge,
/// Provided metadata account does not match metadata account derived for pool mint
#[error("InvalidMetadataAccount")]
InvalidMetadataAccount,
}
impl From<StakePoolError> for ProgramError {
fn from(e: StakePoolError) -> Self {
Expand Down
113 changes: 113 additions & 0 deletions stake-pool/program/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Instruction types

#![allow(clippy::too_many_arguments)]

use {
crate::{
find_deposit_authority_program_address, find_stake_program_address,
Expand All @@ -9,6 +10,7 @@ use {
MAX_VALIDATORS_TO_UPDATE,
},
borsh::{BorshDeserialize, BorshSchema, BorshSerialize},
mpl_token_metadata::pda::find_metadata_account,
solana_program::{
instruction::{AccountMeta, Instruction},
pubkey::Pubkey,
Expand Down Expand Up @@ -374,6 +376,48 @@ pub enum StakePoolInstruction {
/// 11. `[]` Token program id
/// 12. `[s]` (Optional) Stake pool sol withdraw authority
WithdrawSol(u64),

/// Create token metadata for the stake-pool token in the
/// metaplex-token program
/// 0. `[]` Stake pool
/// 1. `[s]` Manager
/// 2. `[]` Stake pool withdraw authority
/// 3. `[]` Pool token mint account
/// 4. `[s, w]` Payer for creation of token metadata account
/// 5. `[w]` Token metadata account
/// 6. `[]` Metadata program id
/// 7. `[]` System program id
/// 8. `[]` Rent sysvar
CreateTokenMetadata {
#[allow(dead_code)]
/// Token name
name: String,
#[allow(dead_code)]
/// Token symbol e.g. stkSOL
symbol: String,
/// URI of the uploaded metadata of the spl-token
#[allow(dead_code)]
uri: String,
},
/// Update token metadata for the stake-pool token in the
/// metaplex-token program
///
/// 0. `[]` Stake pool
/// 1. `[s]` Manager
/// 2. `[]` Stake pool withdraw authority
/// 3. `[w]` Token metadata account
/// 4. `[]` Metadata program id
UpdateTokenMetadata {
#[allow(dead_code)]
/// Token name
name: String,
#[allow(dead_code)]
/// Token symbol e.g. stkSOL
symbol: String,
/// URI of the uploaded metadata of the spl-token
#[allow(dead_code)]
uri: String,
},
}

/// Creates an 'initialize' instruction.
Expand Down Expand Up @@ -1276,3 +1320,72 @@ pub fn set_funding_authority(
.unwrap(),
}
}

/// Creates an instruction to update metadata in the mpl token metadata program account for
/// the pool token
pub fn update_token_metadata(
program_id: &Pubkey,
stake_pool: &Pubkey,
manager: &Pubkey,
pool_mint: &Pubkey,
name: String,
symbol: String,
uri: String,
) -> Instruction {
let (stake_pool_withdraw_authority, _) =
find_withdraw_authority_program_address(program_id, stake_pool);
let (token_metadata, _) = find_metadata_account(pool_mint);

let accounts = vec![
AccountMeta::new_readonly(*stake_pool, false),
AccountMeta::new_readonly(*manager, true),
AccountMeta::new_readonly(stake_pool_withdraw_authority, false),
AccountMeta::new(token_metadata, false),
AccountMeta::new_readonly(mpl_token_metadata::id(), false),
];

Instruction {
program_id: *program_id,
accounts,
data: StakePoolInstruction::UpdateTokenMetadata { name, symbol, uri }
.try_to_vec()
.unwrap(),
}
}

/// Creates an instruction to create metadata using the mpl token metadata program for
/// the pool token
pub fn create_token_metadata(
program_id: &Pubkey,
stake_pool: &Pubkey,
manager: &Pubkey,
pool_mint: &Pubkey,
payer: &Pubkey,
name: String,
symbol: String,
uri: String,
) -> Instruction {
let (stake_pool_withdraw_authority, _) =
find_withdraw_authority_program_address(program_id, stake_pool);
let (token_metadata, _) = find_metadata_account(pool_mint);

let accounts = vec![
AccountMeta::new_readonly(*stake_pool, false),
AccountMeta::new_readonly(*manager, true),
AccountMeta::new_readonly(stake_pool_withdraw_authority, false),
AccountMeta::new_readonly(*pool_mint, false),
AccountMeta::new(*payer, true),
AccountMeta::new(token_metadata, false),
AccountMeta::new_readonly(mpl_token_metadata::id(), false),
AccountMeta::new_readonly(system_program::id(), false),
AccountMeta::new_readonly(sysvar::rent::id(), false),
];

Instruction {
program_id: *program_id,
accounts,
data: StakePoolInstruction::CreateTokenMetadata { name, symbol, uri }
.try_to_vec()
.unwrap(),
}
}
Loading

0 comments on commit 8169e52

Please sign in to comment.