mirrorworld_sdk_rust/
lib.rs

1#[macro_use]
2extern crate lazy_static;
3
4use std::fmt;
5use std::sync::Mutex;
6
7use reqwest::header::HeaderMap;
8
9pub const STAGING_REQUEST_URL: &str = "https://api-staging.mirrorworld.fun";
10pub const RELEASE_REQUEST_URL: &str = "https://api.mirrorworld.fun";
11
12pub const SSO_STAGING: &str = "https://mirrorworld-sso-staging.mirrorworld.fun";
13pub const SSO_RELEASE: &str = "https://mirrorworld-sso.mirrorworld.fun";
14
15
16pub const DEVNET_ENV: &str = "devnet";
17pub const MAINNET_ENV: &str = "mainnet";
18
19pub struct Config {
20    api_key: String,
21    auth: String,
22    net: String,
23}
24impl Config {
25    fn new() -> Config {
26        Config {
27            api_key: "".to_string(),
28            auth: "".to_string(),
29            net: "devnet".to_string(),
30        }
31    }
32}
33
34lazy_static! {
35    static ref API_KEY: Mutex<String> = Mutex::new("".to_string());
36    static ref AUTH: Mutex<String> = Mutex::new("".to_string());
37    static ref CACHE: Mutex<Config> = Mutex::new(Config::new());
38}
39
40pub fn set_config(auth_str: &str, key_str: &str) {
41    CACHE.lock().unwrap().api_key = key_str.to_string();
42    CACHE.lock().unwrap().auth = auth_str.to_string();
43}
44
45pub fn set_auth(auth_str: &str) {
46    CACHE.lock().unwrap().auth = auth_str.to_string();
47}
48pub fn set_apikey(key_str: &str) {
49    CACHE.lock().unwrap().api_key = key_str.to_string();
50}
51pub fn set_network(net: &str) {
52    CACHE.lock().unwrap().net = net.to_string();
53}
54pub fn get_network() -> String {
55    return CACHE.lock().unwrap().net.to_string();
56}
57
58pub fn get_auth() -> String {
59    return CACHE.lock().unwrap().auth.to_string();
60}
61
62pub fn get_apikey() -> String {
63    return CACHE.lock().unwrap().api_key.to_string();
64}
65
66#[derive(Copy, Clone)]
67pub enum NetEnv {
68    DEVNET,
69    MAINNET,
70}
71
72pub fn get_env_name(net: NetEnv) -> String {
73    let s = match net {
74        NetEnv::DEVNET => DEVNET_ENV.to_string(),
75        NetEnv::MAINNET => MAINNET_ENV.to_string(),
76        // _ => "".to_string(),
77    };
78    s
79}
80
81pub fn get_basic_url(net: NetEnv) -> String {
82    let s = match net {
83        NetEnv::DEVNET => STAGING_REQUEST_URL.to_string(),
84        NetEnv::MAINNET => RELEASE_REQUEST_URL.to_string(),
85        // _ => "".to_string(),
86    };
87    s
88}
89
90pub fn get_sso_basic_url(net: NetEnv) -> String {
91    let s = match net {
92        NetEnv::DEVNET => SSO_STAGING.to_string(),
93        NetEnv::MAINNET => SSO_RELEASE.to_string(),
94    };
95    s
96}
97
98pub fn get_request_header(api: String, token: String) -> HeaderMap {
99    let mut headers = HeaderMap::new();
100    headers.insert("Content-Type", "application/json".parse().unwrap());
101    headers.insert("x-api-key", api.parse().unwrap());
102    let authentication: String = "Bearer ".to_string() + &token;
103    headers.insert("Authorization", authentication.parse().unwrap());
104
105    headers
106}
107
108pub mod authentication;
109pub mod marketplace;
110pub mod wallet;
111
112pub enum ActionType {
113    MINT_NFT,
114    UPDATE_NFT,
115    TRANSFER_SOL,
116    TRANSFER_SPL_TOKEN,
117    CREATE_COLLECTION,
118    CREATE_SUB_COLLECTION,
119    LIST_NFT,
120    BUY_NFT,
121    CANCEL_LISTING,
122    UPDATE_LISTING,
123    TRANSFER_NFT,
124    INTERACTION,
125    CREATE_MARKETPLACE,
126    UPDATE_MARKETPLACE
127}
128
129
130impl fmt::Display for ActionType {
131    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
132        match self {
133            ActionType::MINT_NFT => write!(f, "mint_nft"),
134            ActionType::UPDATE_NFT => write!(f, "update_nft"),
135            ActionType::TRANSFER_SOL => write!(f, "transfer_sol"),
136            ActionType::TRANSFER_SPL_TOKEN => write!(f, "transfer_spl_token"),
137            ActionType::CREATE_COLLECTION => write!(f, "create_collection"),
138            ActionType::CREATE_SUB_COLLECTION => write!(f, "create_sub_collection"),
139            ActionType::LIST_NFT => write!(f, "list_nft"),
140            ActionType::BUY_NFT => write!(f, "buy_nft"),
141            ActionType::CANCEL_LISTING => write!(f, "cancel_listing"),
142            ActionType::UPDATE_LISTING => write!(f, "update_listing"),
143            ActionType::TRANSFER_NFT => write!(f, "transfer_nft"),
144            ActionType::INTERACTION => write!(f, "interaction"),
145            ActionType::CREATE_MARKETPLACE => write!(f, "create_marketplace"),
146            ActionType::UPDATE_MARKETPLACE => write!(f, "update_marketplace"),
147        }
148    }
149}
150