-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Reverse Swap notification #953
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,11 @@ package breez_sdk_notification.job | |
import android.content.Context | ||
import breez_sdk.BlockingBreezServices | ||
import breez_sdk.BreezEvent | ||
import breez_sdk.ReverseSwapStatus | ||
import breez_sdk_notification.Constants.DEFAULT_SWAP_TX_CONFIRMED_NOTIFICATION_FAILURE_TEXT | ||
import breez_sdk_notification.Constants.DEFAULT_SWAP_TX_CONFIRMED_NOTIFICATION_FAILURE_TITLE | ||
import breez_sdk_notification.Constants.DEFAULT_SWAP_TX_CONFIRMED_NOTIFICATION_TITLE | ||
import breez_sdk_notification.Constants.NOTIFICATION_CHANNEL_SWAP_TX_CONFIRMED | ||
import breez_sdk_notification.Constants.NOTIFICATION_CHANNEL_ADDRESS_TXS_CONFIRMED | ||
import breez_sdk_notification.Constants.SWAP_TX_CONFIRMED_NOTIFICATION_FAILURE_TEXT | ||
import breez_sdk_notification.Constants.SWAP_TX_CONFIRMED_NOTIFICATION_FAILURE_TITLE | ||
import breez_sdk_notification.Constants.SWAP_TX_CONFIRMED_NOTIFICATION_TITLE | ||
|
@@ -22,38 +23,63 @@ data class AddressTxsConfirmedRequest( | |
val address: String, | ||
) | ||
|
||
class RedeemSwapJob( | ||
class ConfirmTransactionJob( | ||
private val context: Context, | ||
private val fgService: SdkForegroundService, | ||
private val payload: String, | ||
private val logger: ServiceLogger, | ||
private var bitcoinAddress: String? = null, | ||
) : Job { | ||
companion object { | ||
private const val TAG = "RedeemSwapJob" | ||
private const val TAG = "ConfirmTransactionJob" | ||
} | ||
|
||
override fun start(breezSDK: BlockingBreezServices) { | ||
try { | ||
val request = Json.decodeFromString(AddressTxsConfirmedRequest.serializer(), payload) | ||
this.bitcoinAddress = request.address | ||
breezSDK.redeemSwap(request.address) | ||
logger.log(TAG, "Found swap for ${request.address}", "INFO") | ||
} catch (e: Exception) { | ||
logger.log(TAG, "Failed to manually redeem swap notification: ${e.message}", "WARN") | ||
notifyFailure() | ||
logger.log(TAG, "Failed to decode payload: ${e.message}", "WARN") | ||
} | ||
|
||
this.bitcoinAddress?.let {address -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be nice to be able to recognize whether it is a swap or reverse swap without trying both. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I thought that too, but the What if the application or NDS is already using the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good argument. I am ok then to leave this inexpensive check. |
||
try { | ||
breezSDK.redeemSwap(address) | ||
logger.log(TAG, "Found swap for $address", "INFO") | ||
return | ||
} catch (e: Exception) { | ||
logger.log(TAG, "Failed to redeem swap: ${e.message}", "WARN") | ||
} | ||
|
||
try { | ||
breezSDK.processReverseSwap(address) | ||
logger.log(TAG, "Found reverse swap for $address", "INFO") | ||
return | ||
} catch (e: Exception) { | ||
logger.log(TAG, "Failed to process reverse swap: ${e.message}", "WARN") | ||
} | ||
} | ||
|
||
fgService.onFinished(this) | ||
} | ||
|
||
override fun onEvent(e: BreezEvent) { | ||
this.bitcoinAddress?.let {address -> | ||
when (e) { | ||
is BreezEvent.ReverseSwapUpdated -> { | ||
val revSwapInfo = e.details | ||
logger.log(TAG, "Received reverse swap updated event: ${revSwapInfo.id} current address: $address status: ${revSwapInfo.status}", "TRACE") | ||
if (revSwapInfo.status == ReverseSwapStatus.COMPLETED_SEEN || revSwapInfo.status == ReverseSwapStatus.COMPLETED_CONFIRMED) { | ||
notifySuccess(address) | ||
} | ||
} | ||
|
||
is BreezEvent.SwapUpdated -> { | ||
val swapInfo = e.details | ||
logger.log(TAG, "Received swap updated event: ${swapInfo.bitcoinAddress} current address: $address status: ${swapInfo.status}", "TRACE") | ||
if (swapInfo.bitcoinAddress == address) { | ||
if (swapInfo.paidMsat.toLong() > 0) { | ||
notifySuccessAndShutdown(address) | ||
notifySuccess(address) | ||
} | ||
} | ||
} | ||
|
@@ -67,11 +93,11 @@ class RedeemSwapJob( | |
notifyFailure() | ||
} | ||
|
||
private fun notifySuccessAndShutdown(address: String) { | ||
logger.log(TAG, "Swap address $address redeemed successfully", "INFO") | ||
private fun notifySuccess(address: String) { | ||
logger.log(TAG, "Address $address processed successfully", "INFO") | ||
notifyChannel( | ||
context, | ||
NOTIFICATION_CHANNEL_SWAP_TX_CONFIRMED, | ||
NOTIFICATION_CHANNEL_ADDRESS_TXS_CONFIRMED, | ||
getString( | ||
context, | ||
SWAP_TX_CONFIRMED_NOTIFICATION_TITLE, | ||
|
@@ -83,10 +109,10 @@ class RedeemSwapJob( | |
|
||
private fun notifyFailure() { | ||
this.bitcoinAddress?.let{address -> | ||
logger.log(TAG, "Swap address $address not redeemed", "INFO") | ||
logger.log(TAG, "Address $address processing failed", "INFO") | ||
notifyChannel( | ||
context, | ||
NOTIFICATION_CHANNEL_SWAP_TX_CONFIRMED, | ||
NOTIFICATION_CHANNEL_ADDRESS_TXS_CONFIRMED, | ||
getString( | ||
context, | ||
SWAP_TX_CONFIRMED_NOTIFICATION_FAILURE_TITLE, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we rename the notification channel & notification channel group variable as well?