Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

[stake-pool] instruction to add metadata for pool token #3335

Merged
merged 5 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
132 changes: 132 additions & 0 deletions stake-pool/program/src/instruction.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
//! Instruction types

#![allow(clippy::too_many_arguments)]

use crate::AUTHORITY_WITHDRAW;
use mpl_token_metadata::state::PREFIX;
use solana_program::info;
use std::str::FromStr;
use {
crate::{
find_deposit_authority_program_address, find_stake_program_address,
Expand Down Expand Up @@ -374,6 +379,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. `[]` Manager
/// 2. `[]` Stake pool withdraw authority
/// 3. `[]` Pool token mint account
/// 4. `[]` Payer for creation of token metadata account
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Since it's paying, it needs to be writable and signer:

Suggested change
/// 4. `[]` Payer for creation of token metadata account
/// 4. `[s, w]` Payer for creation of token metadata account

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests pass locally without making payer a writable AccountMeta though. What could be reason behind it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question. It may be because the specified payer is the same as the fee payer in your test transactions. You can try a test with a different fee payer and see what happens! I'm surprised that it lets this through though 🤔

/// 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. `[]` 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 +1323,88 @@ 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, _) =
Pubkey::find_program_address(&[&stake_pool.to_bytes(), AUTHORITY_WITHDRAW], &program_id);

let mpl_token_metadata_program_id = mpl_token_metadata::id();
let metadata_seeds = &[
PREFIX.as_bytes(),
mpl_token_metadata_program_id.as_ref(),
pool_mint.as_ref(),
];
let (token_metadata, _) =
Pubkey::find_program_address(metadata_seeds, &mpl_token_metadata_program_id);

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_program_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, _) =
Pubkey::find_program_address(&[&stake_pool.to_bytes(), AUTHORITY_WITHDRAW], &program_id);

let mpl_token_metadata_program_id = mpl_token_metadata::id();
let metadata_seeds = &[
PREFIX.as_bytes(),
mpl_token_metadata_program_id.as_ref(),
pool_mint.as_ref(),
];
let (token_metadata, _) =
Pubkey::find_program_address(metadata_seeds, &mpl_token_metadata_program_id);

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_readonly(*payer, false),
AccountMeta::new(token_metadata, false),
AccountMeta::new_readonly(mpl_token_metadata_program_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