breez_sdk_spark/issuer/
sdk.rs

1use std::sync::Arc;
2
3use spark_wallet::{SparkAddress, SparkWallet};
4
5use crate::{
6    BurnIssuerTokenRequest, CreateIssuerTokenRequest, FreezeIssuerTokenRequest,
7    FreezeIssuerTokenResponse, MintIssuerTokenRequest, Payment, SdkError, Storage, TokenBalance,
8    TokenMetadata, UnfreezeIssuerTokenRequest, UnfreezeIssuerTokenResponse,
9    utils::token::map_and_persist_token_transaction,
10};
11
12#[cfg_attr(feature = "uniffi", derive(uniffi::Object))]
13pub struct TokenIssuer {
14    spark_wallet: Arc<SparkWallet>,
15    storage: Arc<dyn Storage>,
16}
17
18impl TokenIssuer {
19    pub fn new(spark_wallet: Arc<SparkWallet>, storage: Arc<dyn Storage>) -> Self {
20        Self {
21            spark_wallet,
22            storage,
23        }
24    }
25}
26
27#[cfg_attr(feature = "uniffi", uniffi::export(async_runtime = "tokio"))]
28impl TokenIssuer {
29    /// Gets the issuer token balance
30    ///
31    /// # Returns
32    ///
33    /// Result containing either:
34    /// * `TokenBalance` - The balance of the issuer token
35    /// * `SdkError` - If there was an error during the retrieval or no issuer token exists
36    pub async fn get_issuer_token_balance(&self) -> Result<TokenBalance, SdkError> {
37        Ok(self.spark_wallet.get_issuer_token_balance().await?.into())
38    }
39
40    /// Gets the issuer token metadata
41    ///
42    /// # Returns
43    ///
44    /// Result containing either:
45    /// * `TokenMetadata` - The metadata of the issuer token
46    /// * `SdkError` - If there was an error during the retrieval or no issuer token exists
47    pub async fn get_issuer_token_metadata(&self) -> Result<TokenMetadata, SdkError> {
48        Ok(self.spark_wallet.get_issuer_token_metadata().await?.into())
49    }
50
51    /// Creates a new issuer token
52    ///
53    /// # Arguments
54    ///
55    /// * `request`: The request containing the token parameters
56    ///
57    /// # Returns
58    ///
59    /// Result containing either:
60    /// * `TokenMetadata` - The metadata of the created token
61    /// * `SdkError` - If there was an error during the token creation
62    pub async fn create_issuer_token(
63        &self,
64        request: CreateIssuerTokenRequest,
65    ) -> Result<TokenMetadata, SdkError> {
66        self.spark_wallet
67            .create_issuer_token(
68                &request.name,
69                &request.ticker,
70                request.decimals,
71                request.is_freezable,
72                request.max_supply,
73            )
74            .await?;
75        self.get_issuer_token_metadata().await
76    }
77
78    /// Mints supply for the issuer token
79    ///
80    /// # Arguments
81    ///
82    /// * `request`: The request contiaining the amount of the supply to mint
83    ///
84    /// # Returns
85    ///
86    /// Result containing either:
87    /// * `Payment` - The payment representing the minting transaction
88    /// * `SdkError` - If there was an error during the minting process
89    pub async fn mint_issuer_token(
90        &self,
91        request: MintIssuerTokenRequest,
92    ) -> Result<Payment, SdkError> {
93        let token_transaction = self.spark_wallet.mint_issuer_token(request.amount).await?;
94        map_and_persist_token_transaction(&self.spark_wallet, &self.storage, &token_transaction)
95            .await
96    }
97
98    /// Burns supply of the issuer token
99    ///
100    /// # Arguments
101    ///
102    /// * `request`: The request containing the amount of the supply to burn
103    ///
104    /// # Returns
105    ///
106    /// Result containing either:
107    /// * `Payment` - The payment representing the burn transaction
108    /// * `SdkError` - If there was an error during the burn process
109    pub async fn burn_issuer_token(
110        &self,
111        request: BurnIssuerTokenRequest,
112    ) -> Result<Payment, SdkError> {
113        let token_transaction = self
114            .spark_wallet
115            .burn_issuer_token(request.amount, None)
116            .await?;
117        map_and_persist_token_transaction(&self.spark_wallet, &self.storage, &token_transaction)
118            .await
119    }
120
121    /// Freezes tokens held at the specified address
122    ///
123    /// # Arguments
124    ///
125    /// * `request`: The request containing the spark address where the tokens to be frozen are held
126    ///
127    /// # Returns
128    ///
129    /// Result containing either:
130    /// * `FreezeIssuerTokenResponse` - The response containing details of the freeze operation
131    /// * `SdkError` - If there was an error during the freeze process
132    pub async fn freeze_issuer_token(
133        &self,
134        request: FreezeIssuerTokenRequest,
135    ) -> Result<FreezeIssuerTokenResponse, SdkError> {
136        let spark_address = request
137            .address
138            .parse::<SparkAddress>()
139            .map_err(|_| SdkError::InvalidInput("Invalid spark address".to_string()))?;
140        Ok(self
141            .spark_wallet
142            .freeze_issuer_token(&spark_address)
143            .await?
144            .into())
145    }
146
147    /// Unfreezes tokens held at the specified address
148    ///
149    /// # Arguments
150    ///
151    /// * `request`: The request containing the spark address where the tokens to be unfrozen are held
152    ///
153    /// # Returns
154    ///
155    /// Result containing either:
156    /// * `UnfreezeIssuerTokenResponse` - The response containing details of the unfreeze operation
157    /// * `SdkError` - If there was an error during the unfreeze process
158    pub async fn unfreeze_issuer_token(
159        &self,
160        request: UnfreezeIssuerTokenRequest,
161    ) -> Result<UnfreezeIssuerTokenResponse, SdkError> {
162        let spark_address = request
163            .address
164            .parse::<SparkAddress>()
165            .map_err(|_| SdkError::InvalidInput("Invalid spark address".to_string()))?;
166        Ok(self
167            .spark_wallet
168            .unfreeze_issuer_token(&spark_address)
169            .await?
170            .into())
171    }
172}