zcash-android-wallet-sdk/src/main/java/cash/z/wallet/sdk/Synchronizer.kt

181 lines
5.8 KiB
Kotlin
Raw Normal View History

2019-10-23 22:21:52 -07:00
package cash.z.wallet.sdk
2019-11-01 13:25:28 -07:00
import androidx.paging.PagedList
import cash.z.wallet.sdk.block.CompactBlockProcessor.WalletBalance
import cash.z.wallet.sdk.entity.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
/**
2019-11-01 13:25:28 -07:00
* Primary interface for interacting with the SDK. Defines the contract that specific
* implementations like [MockSynchronizer] and [SdkSynchronizer] fulfill. Given the language-level
* support for coroutines, we favor their use in the SDK and incorporate that choice into this
* contract.
*/
interface Synchronizer {
2019-07-14 15:13:12 -07:00
//
// Lifecycle
//
/**
* Starts this synchronizer within the given scope.
*
2019-11-01 13:25:28 -07:00
* @param parentScope the scope to use for this synchronizer, typically something with a
* lifecycle such as an Activity. Implementations should leverage structured concurrency and
* cancel all jobs when this scope completes.
*/
fun start(parentScope: CoroutineScope? = null): Synchronizer
/**
2019-11-01 13:25:28 -07:00
* Stop this synchronizer. Implementations should ensure that calling this method cancels all
* jobs that were created by this instance.
*/
fun stop()
2019-07-14 15:13:12 -07:00
//
2019-11-01 13:25:28 -07:00
// Flows
2019-07-14 15:13:12 -07:00
//
2019-11-01 13:25:28 -07:00
/* Status */
/**
2019-11-01 13:25:28 -07:00
* A flow of values representing the [Status] of this Synchronizer. As the status changes, a new
* value will be emitted by this flow.
*/
2019-11-01 13:25:28 -07:00
val status: Flow<Status>
/**
2019-11-01 13:25:28 -07:00
* A flow of progress values, typically corresponding to this Synchronizer downloading blocks.
* Typically, any non- zero value below 100 indicates that progress indicators can be shown and
* a value of 100 signals that progress is complete and any progress indicators can be hidden.
*/
2019-11-01 13:25:28 -07:00
val progress: Flow<Int>
/**
2019-11-01 13:25:28 -07:00
* A stream of balance values, separately reflecting both the available and total balance.
*/
2019-11-01 13:25:28 -07:00
val balances: Flow<WalletBalance>
/* Transactions */
2019-07-14 15:13:12 -07:00
/**
2019-11-01 13:25:28 -07:00
* A flow of all the outbound pending transaction that have been sent but are awaiting
* confirmations.
2019-07-14 15:13:12 -07:00
*/
val pendingTransactions: Flow<List<PendingTransaction>>
2019-07-14 15:13:12 -07:00
/**
2019-11-01 13:25:28 -07:00
* A flow of all the transactions that are on the blockchain.
2019-07-14 15:13:12 -07:00
*/
2019-11-01 13:25:28 -07:00
val clearedTransactions: Flow<PagedList<ConfirmedTransaction>>
/**
2019-11-01 13:25:28 -07:00
* A flow of all transactions related to sending funds.
*/
2019-11-01 13:25:28 -07:00
val sentTransactions: Flow<PagedList<ConfirmedTransaction>>
/**
2019-11-01 13:25:28 -07:00
* A flow of all transactions related to receiving funds.
*/
2019-11-01 13:25:28 -07:00
val receivedTransactions: Flow<PagedList<ConfirmedTransaction>>
2019-07-14 15:13:12 -07:00
//
// Operations
//
/**
2019-07-14 15:13:12 -07:00
* Gets the address for the given account.
*
2019-11-01 13:25:28 -07:00
* @param accountId the optional accountId whose address is of interest. By default, the first
* account is used.
*/
2019-07-14 15:13:12 -07:00
suspend fun getAddress(accountId: Int = 0): String
/**
* Sends zatoshi.
*
2019-11-01 13:25:28 -07:00
* @param spendingKey the key that allows spends to occur.
* @param zatoshi the amount of zatoshi to send.
* @param toAddress the recipient's address.
* @param memo the optional memo to include as part of the transaction.
* @param fromAccountId the optional account id to use. By default, the first account is used.
*/
2019-11-01 13:25:28 -07:00
fun sendToAddress(
spendingKey: String,
2019-07-14 15:13:12 -07:00
zatoshi: Long,
toAddress: String,
memo: String = "",
2019-11-01 13:25:28 -07:00
fromAccountIndex: Int = 0
): Flow<PendingTransaction>
/**
2019-11-01 13:25:28 -07:00
* Attempts to cancel a transaction that is about to be sent. Typically, cancellation is only
* an option if the transaction has not yet been submitted to the server.
*
* @param transaction the transaction to cancel.
2019-11-01 13:25:28 -07:00
* @return true when the cancellation request was successful. False when it is too late.
*/
2019-11-01 13:25:28 -07:00
suspend fun cancelSpend(transaction: PendingTransaction): Boolean
2019-07-14 15:13:12 -07:00
//
// Error Handling
//
/**
2019-11-01 13:25:28 -07:00
* Gets or sets a global error handler. This is a useful hook for handling unexpected critical
* errors.
2019-07-14 15:13:12 -07:00
*
2019-11-01 13:25:28 -07:00
* @return true when the error has been handled and the Synchronizer should attempt to continue.
* False when the error is unrecoverable and the Synchronizer should [stop].
2019-07-14 15:13:12 -07:00
*/
var onCriticalErrorHandler: ((Throwable?) -> Boolean)?
/**
2019-11-01 13:25:28 -07:00
* An error handler for exceptions during processing. For instance, a block might be missing or
* a reorg may get mishandled or the database may get corrupted.
2019-07-14 15:13:12 -07:00
*
2019-11-01 13:25:28 -07:00
* @return true when the error has been handled and the processor should attempt to continue.
* False when the error is unrecoverable and the processor should [stop].
2019-07-14 15:13:12 -07:00
*/
var onProcessorErrorHandler: ((Throwable?) -> Boolean)?
/**
2019-11-01 13:25:28 -07:00
* An error handler for exceptions while submitting transactions to lightwalletd. For instance,
* a transaction may get rejected because it would be a double-spend or the user might lose
* their cellphone signal.
2019-07-14 15:13:12 -07:00
*
2019-11-01 13:25:28 -07:00
* @return true when the error has been handled and the sender should attempt to resend. False
* when the error is unrecoverable and the sender should [stop].
2019-07-14 15:13:12 -07:00
*/
var onSubmissionErrorHandler: ((Throwable?) -> Boolean)?
enum class Status {
/**
* Indicates that [stop] has been called on this Synchronizer and it will no longer be used.
*/
STOPPED,
/**
* Indicates that this Synchronizer is disconnected from its lightwalletd server.
* When set, a UI element may want to turn red.
*/
DISCONNECTED,
/**
* Indicates that this Synchronizer is not yet synced and therefore should not broadcast
* transactions because it does not have the latest data. When set, a UI element may want
* to turn yellow.
*/
SYNCING,
/**
* Indicates that this Synchronizer is fully up to date and ready for all wallet functions.
* When set, a UI element may want to turn green.
*/
SYNCED
}
}