Skip to content

Commit

Permalink
Moves cashu processing to a state class to account for errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorpamplona committed Jun 27, 2023
1 parent 9bed6fd commit 833e6bc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.vitorpamplona.amethyst.service.lnurl.LightningAddressResolver
import com.vitorpamplona.amethyst.ui.components.GenericLoadable
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
Expand All @@ -21,21 +22,25 @@ data class CashuToken(
)

class CashuProcessor {
fun parse(cashutoken: String): CashuToken {
val base64token = cashutoken.replace("cashuA", "")
val cashu = JsonParser.parseString(String(Base64.getDecoder().decode(base64token)))
val token = cashu.asJsonObject.get("token").asJsonArray[0].asJsonObject
val proofs = token["proofs"].asJsonArray
val mint = token["mint"].asString
fun parse(cashuToken: String): GenericLoadable<CashuToken> {
try {
val base64token = cashuToken.replace("cashuA", "")
val cashu = JsonParser.parseString(String(Base64.getDecoder().decode(base64token)))
val token = cashu.asJsonObject.get("token").asJsonArray[0].asJsonObject
val proofs = token["proofs"].asJsonArray
val mint = token["mint"].asString

var totalAmount = 0L
for (proof in proofs) {
totalAmount += proof.asJsonObject["amount"].asLong
}
val fees = Math.max(((totalAmount * 0.02).toInt()), 2)
val redeemInvoiceAmount = totalAmount - fees
var totalAmount = 0L
for (proof in proofs) {
totalAmount += proof.asJsonObject["amount"].asLong
}
val fees = Math.max(((totalAmount * 0.02).toInt()), 2)
val redeemInvoiceAmount = totalAmount - fees

return CashuToken(mint, totalAmount, fees, redeemInvoiceAmount, proofs)
return GenericLoadable.Loaded(CashuToken(mint, totalAmount, fees, redeemInvoiceAmount, proofs))
} catch (e: Exception) {
return GenericLoadable.Error<CashuToken>("Could not parse this cashu token")
}
}

fun melt(token: CashuToken, lud16: String, onSuccess: (String) -> Unit, onError: (String) -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import kotlinx.coroutines.launch

@Composable
fun CashuPreview(cashutoken: String, accountViewModel: AccountViewModel) {
var cachuData by remember { mutableStateOf<CashuToken?>(null) }
var cachuData by remember { mutableStateOf<GenericLoadable<CashuToken>>(GenericLoadable.Loading<CashuToken>()) }

LaunchedEffect(key1 = cashutoken) {
launch(Dispatchers.IO) {
Expand All @@ -44,13 +44,13 @@ fun CashuPreview(cashutoken: String, accountViewModel: AccountViewModel) {
}

Crossfade(targetState = cachuData) {
if (it != null) {
CashuPreview(it, accountViewModel)
} else {
Text(
when (it) {
is GenericLoadable.Loaded<CashuToken> -> CashuPreview(it.loaded, accountViewModel)
is GenericLoadable.Error<CashuToken> -> Text(
text = "$cashutoken ",
style = LocalTextStyle.current.copy(textDirection = TextDirection.Content)
)
else -> {}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.vitorpamplona.amethyst.ui.components

import androidx.compose.runtime.Immutable

@Immutable
sealed class GenericLoadable<T> {

@Immutable
class Loading<T> : GenericLoadable<T>()

@Immutable
class Loaded<T>(val loaded: T) : GenericLoadable<T>()

@Immutable
class Empty<T> : GenericLoadable<T>()

@Immutable
class Error<T>(val errorMessage: String) : GenericLoadable<T>()
}

0 comments on commit 833e6bc

Please sign in to comment.