I can provide you with an article on how to add metadata to SPL (Solana token) using Rust and Metaplex.
Adding metadata to Spl tokens using Rust and Metaplex
In this article, we’ll look at how to add metadata to Solana tokens using Rust and the Metaplex protocol. This is especially useful for creating non-interchangeable assets that require unique identifiers or information about their owner.
Prerequisites
Before you begin, make sure you have the following installed:
- Rust (version 1.62 or later)
- Metaplex (version 0.9.0 or land)
You can install them using Cargo, Rust’s package manager:
cargo install --dev solana-sdk
Configuring the Solana node
To use Metaplex with your Solana host, you will need to configure it. Here’s how to do it:
- Install the
solana-cli' package and create a new Solana account in the CLI:
solana login
- Create a new program using the solana-program’ package:
solana program new my_program --rust-sdk-version=1.62 --type Rust Program
Creating a new token
Let’s create a new token with some metadata:
my_token.rs
`rust
use solana_program::{
account_info::next_account_info,
entrypoint::ProgramResult,
message
};
use std::hash::{Hash, Hasher};
pub struct MyToken {
pub id: u64,
pub name: String,
pub description: String,
}
impl MyToken {
pub fn new(id: u64, name: String, description: String) -> Self {
Self { id, name, description }
}
pub fn to_account_info(&self) -> AccountInfo {
AccountInfo::new(
self.id as u128,
&self.name.to_bytes(),
&self.description.to_bytes(),
)
}
}
impl std::fmt::Display for MyToken {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{} ({})", self.id, self.name)
}
}
Creating a metadata structure
Let's create a new structure to store our token's metadata:
rust
use solana_program::{
account_info::{ next_account_info, AccountInfo },
entrypoint::ProgramResult,
message
};
use std::hash::{Hash, Hasher};
pub struct Metadata {
pub id: u64,
pub name: String,
pub description: String,
}
impl Metadata {
pub fn new(id: u64, name: String, description: String) -> Self {
Self { id, name, description }
}
pub fn to_account_info(&self) -> AccountInfo {
AccountInfo::new(
self.id as u128,
&self.name.to_bytes(),
&self.description.to_bytes(),
)
}
}
Creating a metadata structure from the Solana program
Now let's create a new program that generates metadata for our token:
`rust
use solana_program::{
account_info::next_account_info,
entrypoint::ProgramResult,
message
};
use std::hash::{Hash, Hasher};
pub struct MetadataGenerator {
pub id: u64,
}
impl MetadataGenerator {
pub fn new(id: u64) -> Self {
self { id }
}
pub fn generate_metadata(&self, metadata: &Metadata) -> ProgramResult {
let account_info = next_account_info();
let metadata_struct = Metadata::new(self.id, metadata.name.clone(), metadata.description.clone());
msg!(« Generating metadata for token {} », self.id);
// Save the metadata to the Solana registry
// For now we’ll just print them out
println!(« Generated metadata: {:?} », metadata_struct.