@@ -7,14 +7,16 @@ import com.iriswallet.data.db.HiddenAsset
77import com.iriswallet.data.db.RgbPendingAsset
88import com.iriswallet.data.retrofit.RgbAsset
99import com.iriswallet.utils.*
10+ import java.io.File
11+ import java.io.InputStream
1012import java.util.concurrent.TimeUnit
1113import org.rgbtools.RgbLibException
1214
1315object AppRepository {
1416
1517 internal var isCacheDirty: Boolean = false
1618
17- lateinit var appAssets: MutableList <AppAsset >
19+ val appAssets: MutableList <AppAsset > = mutableListOf ()
1820
1921 private var rgbPendingAssetIDs: MutableList <String > = mutableListOf ()
2022
@@ -51,9 +53,7 @@ object AppRepository {
5153
5254 val transfers = BdkRepository .listTransfers().toMutableList()
5355 val autoTXs = AppContainer .db.automaticTransactionDao().getAutomaticTransactions()
54- transfers
55- .filter { it.txid in autoTXs.map { tx -> tx.txid } }
56- .forEach { it.automatic = true }
56+ transfers.filter { it.txid in autoTXs.map { tx -> tx.txid } }.forEach { it.internal = true }
5757 bitcoinAsset.transfers = transfers
5858
5959 val balance = BdkRepository .getBalance()
@@ -129,8 +129,8 @@ object AppRepository {
129129 if (firstAppRefresh) {
130130 val changed = RgbRepository .refresh()
131131 if (! changed) return
132- val rgbAssets = RgbRepository .listAssets()
133- for (rgbAsset in rgbAssets ) {
132+ val updatedRgbAssets = RgbRepository .listAssets()
133+ for (rgbAsset in updatedRgbAssets ) {
134134 var assetToUpdate = getCachedAsset(rgbAsset.id)
135135 if (assetToUpdate == null ) {
136136 assetToUpdate = rgbAsset
@@ -169,7 +169,11 @@ object AppRepository {
169169 Log .d(TAG , " Sending funds to RGB wallet..." )
170170 try {
171171 val txid =
172- BdkRepository .sendToAddress(RgbRepository .getAddress(), AppConstants .satsForRgb)
172+ BdkRepository .sendToAddress(
173+ RgbRepository .getAddress(),
174+ AppConstants .satsForRgb,
175+ SharedPreferencesManager .feeRate.toFloat()
176+ )
173177 AppContainer .db
174178 .automaticTransactionDao()
175179 .insertAutomaticTransactions(AutomaticTransaction (txid))
@@ -235,9 +239,15 @@ object AppRepository {
235239 return Receiver (blindedData.invoice, AppConstants .rgbBlindDuration, false )
236240 }
237241
238- private fun initiateRgbTransfer (asset : AppAsset , blindedUTXO : String , amount : ULong ): String {
242+ private fun initiateRgbTransfer (
243+ asset : AppAsset ,
244+ blindedUTXO : String ,
245+ amount : ULong ,
246+ consignmentEndpoints : List <String >,
247+ feeRate : Float ,
248+ ): String {
239249 Log .d(TAG , " Initiating transfer for blinded UTXO: $blindedUTXO " )
240- val txid = RgbRepository .send(asset, blindedUTXO, amount)
250+ val txid = RgbRepository .send(asset, blindedUTXO, amount, consignmentEndpoints, feeRate )
241251 runCatching {
242252 updateRGBAssets(
243253 refresh = false ,
@@ -249,7 +259,7 @@ object AppRepository {
249259 return txid
250260 }
251261
252- fun issueRGBAsset (ticker : String , name : String , amounts : List <ULong >): AppAsset {
262+ fun issueRgb20Asset (ticker : String , name : String , amounts : List <ULong >): AppAsset {
253263 checkMaxAssets()
254264 val contract = handleMissingFunds { RgbRepository .issueAssetRgb20(ticker, name, amounts) }
255265 val asset =
@@ -264,6 +274,40 @@ object AppRepository {
264274 return asset
265275 }
266276
277+ fun issueRgb121Asset (
278+ name : String ,
279+ amounts : List <ULong >,
280+ description : String? ,
281+ fileStream : InputStream ?
282+ ): AppAsset {
283+ checkMaxAssets()
284+ val contract = handleMissingFunds {
285+ var filePath: String? = null
286+ var file: File ? = null
287+
288+ if (fileStream != null ) {
289+ file = File .createTempFile(" tmp" , null , AppContainer .appContext.cacheDir)
290+ file.writeBytes(fileStream.readBytes())
291+ fileStream.close()
292+ filePath = file.absolutePath
293+ }
294+
295+ val contract = RgbRepository .issueAssetRgb121(name, amounts, description, filePath)
296+ file?.delete()
297+ contract
298+ }
299+ val asset =
300+ AppAsset (
301+ AppAssetType .RGB121 ,
302+ contract.assetId,
303+ name,
304+ )
305+ if (contract.dataPaths.isNotEmpty()) asset.media = AppMedia (contract.dataPaths[0 ])
306+ appAssets.add(asset)
307+ updateRGBAsset(asset)
308+ return asset
309+ }
310+
267311 fun deleteRGBTransfer (asset : AppAsset , transfer : AppTransfer ): AppAsset {
268312 Log .d(TAG , " Removing transfer '$transfer '" )
269313 RgbRepository .deleteTransfer(transfer)
@@ -299,13 +343,21 @@ object AppRepository {
299343 else handleMissingFunds { startRGBReceiving(asset) }
300344 }
301345
302- fun sendAsset (asset : AppAsset , recipient : String , amount : ULong ): String {
303- return if (asset.bitcoin()) BdkRepository .sendToAddress(recipient, amount)
304- else handleMissingFunds { initiateRgbTransfer(asset, recipient, amount) }
346+ fun sendAsset (
347+ asset : AppAsset ,
348+ recipient : String ,
349+ amount : ULong ,
350+ consignmentEndpoints : List <String >,
351+ feeRate : Float ,
352+ ): String {
353+ return if (asset.bitcoin()) BdkRepository .sendToAddress(recipient, amount, feeRate)
354+ else
355+ handleMissingFunds {
356+ initiateRgbTransfer(asset, recipient, amount, consignmentEndpoints, feeRate)
357+ }
305358 }
306359
307360 fun getAssets (): List <AppAsset > {
308- appAssets = mutableListOf ()
309361 updateBitcoinAsset(refresh = false )
310362 updateRGBAssets(refresh = false )
311363
0 commit comments