2019-01-02 21:31:12 -08:00
|
|
|
package cash.z.wallet.sdk.data
|
|
|
|
|
2019-07-12 01:47:17 -07:00
|
|
|
import cash.z.wallet.sdk.dao.ClearedTransaction
|
2019-02-26 14:36:08 -08:00
|
|
|
import cash.z.wallet.sdk.secure.Wallet
|
2019-02-14 13:43:06 -08:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
2019-01-02 21:31:12 -08:00
|
|
|
import kotlinx.coroutines.channels.ReceiveChannel
|
|
|
|
|
2019-03-28 23:04:25 -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.
|
|
|
|
*/
|
2019-02-14 13:43:06 -08:00
|
|
|
interface Synchronizer {
|
2019-01-02 21:31:12 -08:00
|
|
|
|
2019-02-14 13:43:06 -08:00
|
|
|
/* Lifecycle */
|
2019-03-28 23:04:25 -07:00
|
|
|
/**
|
|
|
|
* Starts this synchronizer within the given scope.
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*/
|
2019-02-14 13:43:06 -08:00
|
|
|
fun start(parentScope: CoroutineScope): Synchronizer
|
2019-03-28 23:04:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop this synchronizer.
|
|
|
|
*/
|
2019-02-14 13:43:06 -08:00
|
|
|
fun stop()
|
2019-01-23 02:45:26 -08:00
|
|
|
|
2019-03-28 23:04:25 -07:00
|
|
|
|
2019-02-14 13:43:06 -08:00
|
|
|
/* Channels */
|
|
|
|
// NOTE: each of these are expected to be a broadcast channel, such that [receive] always returns the latest value
|
2019-03-28 23:04:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A stream of all the active transactions.
|
|
|
|
*/
|
2019-02-14 13:43:06 -08:00
|
|
|
fun activeTransactions(): ReceiveChannel<Map<ActiveTransaction, TransactionState>>
|
2019-03-28 23:04:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A stream of all the wallet transactions.
|
|
|
|
*/
|
2019-07-12 01:47:17 -07:00
|
|
|
fun allTransactions(): ReceiveChannel<List<ClearedTransaction>>
|
2019-03-28 23:04:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A stream of balance values.
|
|
|
|
*/
|
2019-04-23 23:44:51 -07:00
|
|
|
fun balances(): ReceiveChannel<Wallet.WalletBalance>
|
2019-03-28 23:04:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A stream 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-02-14 13:43:06 -08:00
|
|
|
fun progress(): ReceiveChannel<Int>
|
2019-01-23 02:45:26 -08:00
|
|
|
|
2019-03-28 23:04:25 -07:00
|
|
|
|
2019-02-14 13:43:06 -08:00
|
|
|
/* Status */
|
2019-03-28 23:04:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A flag to indicate that this Synchronizer is significantly out of sync with it's server. Typically, this means
|
|
|
|
* that the balance and other data cannot be completely trusted because a significant amount of data has not been
|
|
|
|
* processed. This is intended for showing progress indicators when the user returns to the app after having not
|
|
|
|
* used it for days. Typically, this means minor sync issues should be ignored and this should be leveraged in order
|
|
|
|
* to alert a user that the balance information is stale.
|
|
|
|
*
|
|
|
|
* @return true when the local data is significantly out of sync with the remote server and the app data is stale.
|
|
|
|
*/
|
|
|
|
suspend fun isStale(): Boolean
|
|
|
|
|
2019-02-24 15:59:07 -08:00
|
|
|
/**
|
2019-03-28 23:04:25 -07:00
|
|
|
* Gets or sets a global error listener. This is a useful hook for handling unexpected critical errors.
|
2019-02-24 15:59:07 -08:00
|
|
|
*
|
|
|
|
* @return true when the error has been handled and the Synchronizer should continue. False when the error is
|
|
|
|
* unrecoverable and the Synchronizer should [stop].
|
|
|
|
*/
|
|
|
|
var onSynchronizerErrorListener: ((Throwable?) -> Boolean)?
|
2019-01-23 02:45:26 -08:00
|
|
|
|
2019-03-28 23:04:25 -07:00
|
|
|
|
2019-02-14 13:43:06 -08:00
|
|
|
/* Operations */
|
2019-03-28 23:04:25 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the address for the given account.
|
|
|
|
*
|
2019-04-23 23:44:51 -07:00
|
|
|
* @param accountId the optional accountId whose address is of interest. By default, the first account is used.
|
2019-03-28 23:04:25 -07:00
|
|
|
*/
|
|
|
|
fun getAddress(accountId: Int = 0): String
|
|
|
|
|
2019-04-23 23:44:51 -07:00
|
|
|
/**
|
2019-06-20 01:37:48 -07:00
|
|
|
* Gets the balance info for the given account. In most cases, the stream of balances provided by [balances]
|
2019-04-23 23:44:51 -07:00
|
|
|
* should be used instead of this function.
|
|
|
|
*
|
|
|
|
* @param accountId the optional accountId whose balance is of interest. By default, the first account is used.
|
2019-06-20 01:37:48 -07:00
|
|
|
* @return a wrapper around the available and total balances.
|
2019-04-23 23:44:51 -07:00
|
|
|
*/
|
2019-06-20 01:37:48 -07:00
|
|
|
suspend fun getBalance(accountId: Int = 0): Wallet.WalletBalance
|
2019-04-23 23:44:51 -07:00
|
|
|
|
2019-03-28 23:04:25 -07:00
|
|
|
/**
|
|
|
|
* Sends zatoshi.
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
suspend fun sendToAddress(zatoshi: Long, toAddress: String, memo: String = "", fromAccountId: Int = 0)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attempts to cancel a previously sent transaction. Typically, cancellation is only an option if the transaction
|
|
|
|
* has not yet been submitted to the server.
|
|
|
|
*
|
|
|
|
* @param transaction the transaction to cancel.
|
|
|
|
* @return true when the cancellation request was successful. False when it is too late to cancel.
|
|
|
|
*/
|
2019-02-14 13:43:06 -08:00
|
|
|
fun cancelSend(transaction: ActiveSendTransaction): Boolean
|
2019-01-02 21:31:12 -08:00
|
|
|
}
|