Changes to support memo sample

Mostly includes convenience functions for determining balance.
This commit is contained in:
Kevin Gorham 2019-04-24 02:44:51 -04:00
parent 30d36b723e
commit 7ec61df4a9
4 changed files with 45 additions and 7 deletions

View File

@ -83,7 +83,7 @@ open class MockSynchronizer(
override fun activeTransactions() = activeTransactionsChannel.openSubscription()
override fun allTransactions() = transactionsChannel.openSubscription()
override fun balance() = balanceChannel.openSubscription()
override fun balances() = balanceChannel.openSubscription()
override fun progress() = progressChannel.openSubscription()
/**
@ -109,6 +109,18 @@ open class MockSynchronizer(
*/
override fun getAddress(accountId: Int): String = mockAddress.also { twig("returning mock address $mockAddress") }
/**
* Returns the available balance by adding up all the transactions and subtracting the miner's fee.
*/
override fun getAvailableBalance(accountId: Int): Long {
if (transactions.size != 0) {
return transactions.fold(0L) { acc, tx ->
if (tx.isSend && tx.isMined) acc - tx.value else acc + tx.value
} - 10_000L // miner's fee
}
return 0L
}
/**
* Uses the [forge] to fabricate a transaction and then walk it through the transaction lifecycle in a useful way.
* This method will validate the zatoshi amount and toAddress a bit to help with UI validation.

View File

@ -152,8 +152,8 @@ class SdkSynchronizer(
/**
* A stream of balance values, delegated to the [wallet].
*/
override fun balance(): ReceiveChannel<Wallet.WalletBalance> {
return wallet.balance()
override fun balances(): ReceiveChannel<Wallet.WalletBalance> {
return wallet.balances()
}
@ -196,10 +196,18 @@ class SdkSynchronizer(
/**
* Gets the address for the given account.
*
* @param accountId the optional accountId whose address of interest. By default, the first account is used.
* @param accountId the optional accountId whose address of interest. Typically, this value is zero.
*/
override fun getAddress(accountId: Int): String = wallet.getAddress()
/**
* Gets the available balance for the given account. In most cases, the stream of balances provided by [balances]
* should be used instead of this function.
*
* @param accountId the optional accountId whose available balance is of interest. Typically, this value is zero.
*/
override fun getAvailableBalance(accountId: Int): Long = wallet.availableBalanceSnapshot(accountId)
/**
* Sends zatoshi.
*

View File

@ -43,7 +43,7 @@ interface Synchronizer {
/**
* A stream of balance values.
*/
fun balance(): ReceiveChannel<Wallet.WalletBalance>
fun balances(): ReceiveChannel<Wallet.WalletBalance>
/**
* A stream of progress values, typically corresponding to this Synchronizer downloading blocks. Typically, any non-
@ -89,10 +89,18 @@ interface Synchronizer {
/**
* Gets the address for the given account.
*
* @param accountId the optional accountId whose address of interest. By default, the first account is used.
* @param accountId the optional accountId whose address is of interest. By default, the first account is used.
*/
fun getAddress(accountId: Int = 0): String
/**
* Gets the available balance for the given account. In most cases, the stream of balances provided by [balances]
* should be used instead of this function.
*
* @param accountId the optional accountId whose balance is of interest. By default, the first account is used.
*/
fun getAvailableBalance(accountId: Int = 0): Long
/**
* Sends zatoshi.
*

View File

@ -99,10 +99,20 @@ class Wallet(
/**
* Stream of balances.
*/
fun balance(): ReceiveChannel<WalletBalance> {
fun balances(): ReceiveChannel<WalletBalance> {
return balanceChannel.openSubscription()
}
/**
* Return a quick snapshot of the available balance. In most cases, the stream of balances
* provided by [balances] should be used instead of this funciton.
*
* @param accountId the account to check for balance info. Defaults to zero.
*/
fun availableBalanceSnapshot(accountId: Int = accountIds[0]): Long {
return converter.getVerifiedBalance(dataDbPath, accountId)
}
/**
* Calculates the latest balance info and emits it into the balance channel. Defaults to the first account.
*