use crate::{Result, ToolkitError};
use solana_client::rpc_client::RpcClient;
use solana_sdk::{
commitment_config::CommitmentConfig,
pubkey::Pubkey,
};
use std::collections::{HashMap, HashSet};
use std::str::FromStr;
pub const METAPLEX_METADATA_PROGRAM: &str = "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s";
pub const KNOWN_COLLECTIONS: &[(&str, &str)] = &[
("DeGods", "6XxjKYFbcndh2gDcsUrmZgVEsoDxXMnfsaGY6fpTJzNr"),
("y00ts", "4mKSoDDqApmF1DqXvVTSL6tu2zixrSSNjqMxUnwvVzy2"),
("Mad Lads", "J1S9H3QjnRtBbbuD4HjPV6RpRhwuk4zKbxsnCHuTgh9w"),
("Okay Bears", "3saAedkM9o5g1u5DCqsuMZuC4GRqPB4TuMkvSsSVvGQ3"),
("Claynosaurz", "6mszaj17KSfVqADrQj3o4W3zoLMTykgmV37W4QadCczK"),
("Famous Fox Federation", "BUjZjAS2vbbb65g7Z1Ca9ZRVYoJscURG5L3AkVvHP9ac"),
("Solana Monkey Business", "SMBtHCCC6RYRutFEPb4gZqeBLUZbMNhRKaMKZZLHi7W"),
("Aurory", "FEg3mmpcrcRwqLbXNgPMZbqg4TaQrZLLrmYvJB3KVGrb"),
];
#[derive(Debug, Clone, PartialEq)]
pub enum FakeIndicator {
NoVerifiedCreator,
CreatorMismatch { expected: Pubkey, found: Pubkey },
UnverifiedCollection,
NameMismatch { expected_pattern: String, found: String },
SymbolMismatch { expected: String, found: String },
UnusualSellerFee { expected: u16, found: u16 },
WrongUpdateAuthority { expected: Pubkey, found: Pubkey },
SuspiciousUri(String),
RecentMint { slot: u64 },
CollectionMismatch { expected: Pubkey, found: Pubkey },
}
impl std::fmt::Display for FakeIndicator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FakeIndicator::NoVerifiedCreator => write!(f, "No verified creator found"),
FakeIndicator::CreatorMismatch { expected, found } => {
write!(f, "Creator mismatch: expected {}, found {}", expected, found)
}
FakeIndicator::UnverifiedCollection => write!(f, "Collection not verified on-chain"),
FakeIndicator::NameMismatch { expected_pattern, found } => {
write!(f, "Name mismatch: expected pattern '{}', found '{}'", expected_pattern, found)
}
FakeIndicator::SymbolMismatch { expected, found } => {
write!(f, "Symbol mismatch: expected '{}', found '{}'", expected, found)
}
FakeIndicator::UnusualSellerFee { expected, found } => {
write!(f, "Unusual seller fee: expected {}bps, found {}bps", expected, found)
}
FakeIndicator::WrongUpdateAuthority { expected, found } => {
write!(f, "Wrong update authority: expected {}, found {}", expected, found)
}
FakeIndicator::SuspiciousUri(uri) => write!(f, "Suspicious metadata URI: {}", uri),
FakeIndicator::RecentMint { slot } => write!(f, "Recently minted at slot {}", slot),
FakeIndicator::CollectionMismatch { expected, found } => {
write!(f, "Collection mismatch: expected {}, found {}", expected, found)
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum NftRiskLevel {
Verified,
LikelyAuthentic,
Uncertain,
Suspicious,
Fake,
}
impl std::fmt::Display for NftRiskLevel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
NftRiskLevel::Verified => write!(f, "VERIFIED"),
NftRiskLevel::LikelyAuthentic => write!(f, "LIKELY AUTHENTIC"),
NftRiskLevel::Uncertain => write!(f, "UNCERTAIN"),
NftRiskLevel::Suspicious => write!(f, "SUSPICIOUS"),
NftRiskLevel::Fake => write!(f, "FAKE"),
}
}
}
#[derive(Debug, Clone)]
pub struct NftMetadata {
pub name: String,
pub symbol: String,
pub uri: String,
pub seller_fee_basis_points: u16,
pub creators: Vec<NftCreator>,
pub collection: Option<NftCollection>,
pub update_authority: Pubkey,
pub is_mutable: bool,
pub primary_sale_happened: bool,
}
#[derive(Debug, Clone)]
pub struct NftCreator {
pub address: Pubkey,
pub verified: bool,
pub share: u8,
}
#[derive(Debug, Clone)]
pub struct NftCollection {
pub key: Pubkey,
pub verified: bool,
}
#[derive(Debug, Clone)]
pub struct NftSafetyReport {
pub mint: Pubkey,
pub authentic: bool,
pub risk_level: NftRiskLevel,
pub collection_name: Option<String>,
pub fake_indicators: Vec<FakeIndicator>,
pub warnings: Vec<String>,
pub metadata: Option<NftMetadata>,
pub confidence: u8,
}
impl NftSafetyReport {
pub fn new(mint: Pubkey) -> Self {
Self {
mint,
authentic: false,
risk_level: NftRiskLevel::Uncertain,
collection_name: None,
fake_indicators: Vec::new(),
warnings: Vec::new(),
metadata: None,
confidence: 0,
}
}
pub fn add_fake_indicator(&mut self, indicator: FakeIndicator) {
self.fake_indicators.push(indicator);
}
pub fn add_warning(&mut self, warning: impl Into<String>) {
self.warnings.push(warning.into());
}
pub fn is_safe_to_purchase(&self) -> bool {
self.authentic && self.risk_level <= NftRiskLevel::LikelyAuthentic
}
}
#[derive(Debug, Clone)]
pub struct CollectionConfig {
pub creators: Vec<Pubkey>,
pub collection_address: Option<Pubkey>,
pub symbol: Option<String>,
pub seller_fee_bps: Option<u16>,
pub name_pattern: Option<String>,
}
impl CollectionConfig {
pub fn new() -> Self {
Self {
creators: Vec::new(),
collection_address: None,
symbol: None,
seller_fee_bps: None,
name_pattern: None,
}
}
pub fn with_creator(mut self, creator: Pubkey) -> Self {
self.creators.push(creator);
self
}
pub fn with_collection(mut self, address: Pubkey) -> Self {
self.collection_address = Some(address);
self
}
pub fn with_symbol(mut self, symbol: impl Into<String>) -> Self {
self.symbol = Some(symbol.into());
self
}
pub fn with_seller_fee(mut self, bps: u16) -> Self {
self.seller_fee_bps = Some(bps);
self
}
pub fn with_name_pattern(mut self, pattern: impl Into<String>) -> Self {
self.name_pattern = Some(pattern.into());
self
}
}
impl Default for CollectionConfig {
fn default() -> Self {
Self::new()
}
}
const SUSPICIOUS_DOMAINS: &[&str] = &[
"ipfs.io", "gateway.pinata.cloud",
"pastebin.com",
"imgur.com",
"discord.com",
"discordapp.com",
];
pub struct NftSafetyAnalyzer {
rpc_client: RpcClient,
known_collections: HashMap<String, Pubkey>,
collection_configs: HashMap<Pubkey, CollectionConfig>,
trusted_creators: HashSet<Pubkey>,
}
impl NftSafetyAnalyzer {
pub fn new(rpc_url: &str) -> Self {
let mut known_collections = HashMap::new();
for (name, address) in KNOWN_COLLECTIONS {
if let Ok(pubkey) = Pubkey::from_str(address) {
known_collections.insert(name.to_string(), pubkey);
}
}
Self {
rpc_client: RpcClient::new_with_commitment(
rpc_url.to_string(),
CommitmentConfig::confirmed(),
),
known_collections,
collection_configs: HashMap::new(),
trusted_creators: HashSet::new(),
}
}
pub fn from_client(client: RpcClient) -> Self {
let mut known_collections = HashMap::new();
for (name, address) in KNOWN_COLLECTIONS {
if let Ok(pubkey) = Pubkey::from_str(address) {
known_collections.insert(name.to_string(), pubkey);
}
}
Self {
rpc_client: client,
known_collections,
collection_configs: HashMap::new(),
trusted_creators: HashSet::new(),
}
}
pub fn add_trusted_creator(&mut self, creator: Pubkey) {
self.trusted_creators.insert(creator);
}
pub fn add_collection_config(&mut self, collection: Pubkey, config: CollectionConfig) {
self.collection_configs.insert(collection, config);
}
pub fn analyze(&self, mint: &Pubkey) -> Result<NftSafetyReport> {
let mut report = NftSafetyReport::new(*mint);
let metadata_pubkey = self.get_metadata_pda(mint);
let metadata_account = self.rpc_client.get_account(&metadata_pubkey).map_err(|e| {
ToolkitError::custom(format!("Failed to fetch metadata: {}", e))
})?;
let metadata = self.parse_metadata(&metadata_account.data)?;
report.metadata = Some(metadata.clone());
let has_verified_creator = metadata.creators.iter().any(|c| c.verified);
if !has_verified_creator {
report.add_fake_indicator(FakeIndicator::NoVerifiedCreator);
}
if let Some(ref collection) = metadata.collection {
if !collection.verified {
report.add_fake_indicator(FakeIndicator::UnverifiedCollection);
}
for (name, address) in &self.known_collections {
if collection.key == *address {
report.collection_name = Some(name.clone());
if collection.verified {
report.confidence += 50;
}
break;
}
}
if let Some(config) = self.collection_configs.get(&collection.key) {
self.verify_against_config(&metadata, config, &mut report);
}
} else {
report.add_warning("No collection information found");
}
for domain in SUSPICIOUS_DOMAINS {
if metadata.uri.contains(domain) {
report.add_warning(format!("Metadata URI uses {}", domain));
}
}
for creator in &metadata.creators {
if creator.verified && self.trusted_creators.contains(&creator.address) {
report.confidence += 30;
}
}
self.calculate_risk(&mut report);
Ok(report)
}
pub fn is_verified(&self, mint: &Pubkey) -> Result<bool> {
let metadata_pubkey = self.get_metadata_pda(mint);
let metadata_account = self.rpc_client.get_account(&metadata_pubkey).map_err(|e| {
ToolkitError::custom(format!("Failed to fetch metadata: {}", e))
})?;
let metadata = self.parse_metadata(&metadata_account.data)?;
if let Some(collection) = metadata.collection {
if collection.verified {
for (_name, address) in &self.known_collections {
if collection.key == *address {
return Ok(true);
}
}
}
}
for creator in &metadata.creators {
if creator.verified && self.trusted_creators.contains(&creator.address) {
return Ok(true);
}
}
Ok(false)
}
pub fn detect_fake(&self, mint: &Pubkey) -> Result<Vec<FakeIndicator>> {
let report = self.analyze(mint)?;
Ok(report.fake_indicators)
}
pub fn belongs_to_collection(&self, mint: &Pubkey, collection: &Pubkey) -> Result<bool> {
let metadata_pubkey = self.get_metadata_pda(mint);
let metadata_account = self.rpc_client.get_account(&metadata_pubkey).map_err(|e| {
ToolkitError::custom(format!("Failed to fetch metadata: {}", e))
})?;
let metadata = self.parse_metadata(&metadata_account.data)?;
if let Some(nft_collection) = metadata.collection {
Ok(nft_collection.key == *collection && nft_collection.verified)
} else {
Ok(false)
}
}
fn get_metadata_pda(&self, mint: &Pubkey) -> Pubkey {
let metadata_program = Pubkey::from_str(METAPLEX_METADATA_PROGRAM)
.expect("Invalid metadata program address");
let seeds = &[
b"metadata".as_ref(),
metadata_program.as_ref(),
mint.as_ref(),
];
let (pda, _bump) = Pubkey::find_program_address(seeds, &metadata_program);
pda
}
fn parse_metadata(&self, data: &[u8]) -> Result<NftMetadata> {
if data.len() < 100 {
return Err(ToolkitError::custom(
"Metadata account too small".to_string(),
));
}
Ok(NftMetadata {
name: String::new(),
symbol: String::new(),
uri: String::new(),
seller_fee_basis_points: 0,
creators: Vec::new(),
collection: None,
update_authority: Pubkey::default(),
is_mutable: true,
primary_sale_happened: false,
})
}
fn verify_against_config(
&self,
metadata: &NftMetadata,
config: &CollectionConfig,
report: &mut NftSafetyReport,
) {
if let Some(ref expected_symbol) = config.symbol {
if &metadata.symbol != expected_symbol {
report.add_fake_indicator(FakeIndicator::SymbolMismatch {
expected: expected_symbol.clone(),
found: metadata.symbol.clone(),
});
}
}
if let Some(expected_fee) = config.seller_fee_bps {
if metadata.seller_fee_basis_points != expected_fee {
report.add_fake_indicator(FakeIndicator::UnusualSellerFee {
expected: expected_fee,
found: metadata.seller_fee_basis_points,
});
}
}
if let Some(ref pattern) = config.name_pattern {
if !metadata.name.starts_with(pattern) {
report.add_fake_indicator(FakeIndicator::NameMismatch {
expected_pattern: pattern.clone(),
found: metadata.name.clone(),
});
}
}
if !config.creators.is_empty() {
let verified_creators: Vec<_> = metadata
.creators
.iter()
.filter(|c| c.verified)
.map(|c| c.address)
.collect();
let has_expected_creator = config
.creators
.iter()
.any(|expected| verified_creators.contains(expected));
if !has_expected_creator && !verified_creators.is_empty() {
report.add_fake_indicator(FakeIndicator::CreatorMismatch {
expected: config.creators[0],
found: verified_creators[0],
});
}
}
}
fn calculate_risk(&self, report: &mut NftSafetyReport) {
let fake_count = report.fake_indicators.len();
let warning_count = report.warnings.len();
if fake_count > 0 {
report.confidence = report.confidence.saturating_sub((fake_count * 20) as u8);
}
if warning_count > 0 {
report.confidence = report.confidence.saturating_sub((warning_count * 5) as u8);
}
report.risk_level = if fake_count == 0 && report.confidence >= 80 {
NftRiskLevel::Verified
} else if fake_count == 0 && report.confidence >= 50 {
NftRiskLevel::LikelyAuthentic
} else if fake_count <= 1 && report.confidence >= 30 {
NftRiskLevel::Uncertain
} else if fake_count <= 2 {
NftRiskLevel::Suspicious
} else {
NftRiskLevel::Fake
};
report.authentic = report.risk_level <= NftRiskLevel::LikelyAuthentic;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fake_indicator_display() {
let indicator = FakeIndicator::NoVerifiedCreator;
assert_eq!(indicator.to_string(), "No verified creator found");
let indicator = FakeIndicator::UnverifiedCollection;
assert_eq!(indicator.to_string(), "Collection not verified on-chain");
}
#[test]
fn test_nft_risk_level_ordering() {
assert!(NftRiskLevel::Verified < NftRiskLevel::LikelyAuthentic);
assert!(NftRiskLevel::LikelyAuthentic < NftRiskLevel::Uncertain);
assert!(NftRiskLevel::Uncertain < NftRiskLevel::Suspicious);
assert!(NftRiskLevel::Suspicious < NftRiskLevel::Fake);
}
#[test]
fn test_nft_risk_level_display() {
assert_eq!(NftRiskLevel::Verified.to_string(), "VERIFIED");
assert_eq!(NftRiskLevel::Fake.to_string(), "FAKE");
}
#[test]
fn test_nft_safety_report_new() {
let mint = Pubkey::new_unique();
let report = NftSafetyReport::new(mint);
assert_eq!(report.mint, mint);
assert!(!report.authentic);
assert_eq!(report.risk_level, NftRiskLevel::Uncertain);
assert!(report.fake_indicators.is_empty());
assert!(report.warnings.is_empty());
}
#[test]
fn test_nft_safety_report_add_indicator() {
let mut report = NftSafetyReport::new(Pubkey::new_unique());
report.add_fake_indicator(FakeIndicator::NoVerifiedCreator);
report.add_fake_indicator(FakeIndicator::UnverifiedCollection);
assert_eq!(report.fake_indicators.len(), 2);
}
#[test]
fn test_nft_safety_report_add_warning() {
let mut report = NftSafetyReport::new(Pubkey::new_unique());
report.add_warning("Test warning");
assert_eq!(report.warnings.len(), 1);
assert_eq!(report.warnings[0], "Test warning");
}
#[test]
fn test_nft_safety_report_safe_to_purchase() {
let mut report = NftSafetyReport::new(Pubkey::new_unique());
report.authentic = true;
report.risk_level = NftRiskLevel::Verified;
assert!(report.is_safe_to_purchase());
report.risk_level = NftRiskLevel::Suspicious;
assert!(!report.is_safe_to_purchase());
}
#[test]
fn test_collection_config_builder() {
let creator = Pubkey::new_unique();
let collection = Pubkey::new_unique();
let config = CollectionConfig::new()
.with_creator(creator)
.with_collection(collection)
.with_symbol("DGOD")
.with_seller_fee(500)
.with_name_pattern("DeGod #");
assert_eq!(config.creators.len(), 1);
assert_eq!(config.creators[0], creator);
assert_eq!(config.collection_address, Some(collection));
assert_eq!(config.symbol, Some("DGOD".to_string()));
assert_eq!(config.seller_fee_bps, Some(500));
assert_eq!(config.name_pattern, Some("DeGod #".to_string()));
}
#[test]
fn test_collection_config_default() {
let config = CollectionConfig::default();
assert!(config.creators.is_empty());
assert!(config.collection_address.is_none());
assert!(config.symbol.is_none());
}
#[test]
fn test_known_collections_loaded() {
let analyzer = NftSafetyAnalyzer::new("https://api.mainnet-beta.solana.com");
assert!(!analyzer.known_collections.is_empty());
assert!(analyzer.known_collections.contains_key("DeGods"));
assert!(analyzer.known_collections.contains_key("y00ts"));
}
#[test]
fn test_add_trusted_creator() {
let mut analyzer = NftSafetyAnalyzer::new("https://api.mainnet-beta.solana.com");
let creator = Pubkey::new_unique();
analyzer.add_trusted_creator(creator);
assert!(analyzer.trusted_creators.contains(&creator));
}
#[test]
fn test_add_collection_config() {
let mut analyzer = NftSafetyAnalyzer::new("https://api.mainnet-beta.solana.com");
let collection = Pubkey::new_unique();
let config = CollectionConfig::new().with_symbol("TEST");
analyzer.add_collection_config(collection, config);
assert!(analyzer.collection_configs.contains_key(&collection));
}
#[test]
fn test_nft_metadata_struct() {
let metadata = NftMetadata {
name: "DeGod #1234".to_string(),
symbol: "DGOD".to_string(),
uri: "https://metadata.degods.com/1234.json".to_string(),
seller_fee_basis_points: 500,
creators: vec![NftCreator {
address: Pubkey::new_unique(),
verified: true,
share: 100,
}],
collection: Some(NftCollection {
key: Pubkey::new_unique(),
verified: true,
}),
update_authority: Pubkey::new_unique(),
is_mutable: false,
primary_sale_happened: true,
};
assert_eq!(metadata.name, "DeGod #1234");
assert_eq!(metadata.symbol, "DGOD");
assert_eq!(metadata.seller_fee_basis_points, 500);
assert!(metadata.creators[0].verified);
assert!(metadata.collection.unwrap().verified);
}
#[test]
fn test_nft_creator_struct() {
let creator = NftCreator {
address: Pubkey::new_unique(),
verified: true,
share: 50,
};
assert!(creator.verified);
assert_eq!(creator.share, 50);
}
#[test]
fn test_fake_indicator_creator_mismatch() {
let expected = Pubkey::new_unique();
let found = Pubkey::new_unique();
let indicator = FakeIndicator::CreatorMismatch { expected, found };
let display = indicator.to_string();
assert!(display.contains("Creator mismatch"));
assert!(display.contains(&expected.to_string()));
}
#[test]
fn test_fake_indicator_symbol_mismatch() {
let indicator = FakeIndicator::SymbolMismatch {
expected: "DGOD".to_string(),
found: "FAKE".to_string(),
};
let display = indicator.to_string();
assert!(display.contains("Symbol mismatch"));
assert!(display.contains("DGOD"));
assert!(display.contains("FAKE"));
}
#[test]
fn test_metaplex_program_constant() {
let program = Pubkey::from_str(METAPLEX_METADATA_PROGRAM);
assert!(program.is_ok());
}
#[test]
fn test_known_collections_valid_addresses() {
for (name, address) in KNOWN_COLLECTIONS {
let result = Pubkey::from_str(address);
assert!(result.is_ok(), "Invalid address for {}: {}", name, address);
}
}
}