Skip to content

Commit

Permalink
[#1108] Getting memo for sent transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
HonzaR authored Jul 31, 2023
1 parent 1936c69 commit 0be05a1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,16 @@ interface Backend {

fun getBranchIdForHeight(height: Long): Long

/**
* @throws RuntimeException as a common indicator of the operation failure
*/
@Throws(RuntimeException::class)
suspend fun getReceivedMemoAsUtf8(idNote: Long): String?

/**
* @throws RuntimeException as a common indicator of the operation failure
*/
@Throws(RuntimeException::class)
suspend fun getSentMemoAsUtf8(idNote: Long): String?

suspend fun getVerifiedBalance(account: Int): Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,10 @@ private fun TransactionsMainContent(
Button({
val memos = synchronizer.getMemos(it)
queryScope.launch {
runCatching {
memos.toList().run {
Twig.debug {
"Transaction memos: count: $size, contains: " +
joinToString().ifEmpty {
"-"
}
}
memos.toList().run {
Twig.debug {
"Transaction memos: count: $size, contains: ${joinToString().ifEmpty { "-" }}"
}
}.onFailure {
// https://github.com/zcash/librustzcash/issues/834
Twig.error { "Failed to get memos with: $it" }
}
}
}) {
Expand Down
27 changes: 18 additions & 9 deletions sdk-lib/src/main/java/cash/z/ecc/android/sdk/SdkSynchronizer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
Expand Down Expand Up @@ -316,15 +315,25 @@ class SdkSynchronizer private constructor(

override fun getMemos(transactionOverview: TransactionOverview): Flow<String> {
return storage.getNoteIds(transactionOverview.id).map {
when (transactionOverview.isSentTransaction) {
true -> {
backend.getSentMemoAsUtf8(it)
}
false -> {
backend.getReceivedMemoAsUtf8(it)
runCatching {
when (transactionOverview.isSentTransaction) {
true -> {
backend.getSentMemoAsUtf8(it)
}
false -> {
backend.getReceivedMemoAsUtf8(it)
}
}
}
}.filterNotNull()
}.onFailure {
// https://github.com/zcash/librustzcash/issues/834
Twig.error { "Failed to get memo with: $it" }
}.onSuccess {
Twig.debug { "Transaction memo queried: $it" }
}.fold(
onSuccess = { it ?: "" },
onFailure = { "" }
)
}
}

override fun getRecipients(transactionOverview: TransactionOverview): Flow<TransactionRecipient> {
Expand Down
8 changes: 7 additions & 1 deletion sdk-lib/src/main/java/cash/z/ecc/android/sdk/Synchronizer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,13 @@ interface Synchronizer {
suspend fun quickRewind()

/**
* Returns a list of memos for a transaction.
* Returns a stream of memos for a transaction. It works for both received and sent transaction.
*
* Note that this function internally resolves any error which comes, logs it, and then transforms it to an empty
* string.
*
* @param transactionOverview For which the memos will be queried
* @return Flow of memo strings
*/
fun getMemos(transactionOverview: TransactionOverview): Flow<String>

Expand Down

0 comments on commit 0be05a1

Please sign in to comment.