Skip to content

Commit

Permalink
Merge pull request #286 from horizontalsystems/remove-fee-rate-priority
Browse files Browse the repository at this point in the history
Remove fee rate priority in Bitcoin Kit
  • Loading branch information
abdrasulov authored Apr 12, 2019
2 parents e41ddc8 + e9a3f0d commit affdbed
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ class MainViewModel : ViewModel(), BitcoinKit.Listener {
}

fun send(address: String, amount: Long) {
bitcoinKit.send(address, amount, feePriority = feePriority)
val feeRate = feeRateFromPriority(feePriority)
bitcoinKit.send(address, amount, feeRate = feeRate)
}

fun fee(value: Long, address: String? = null): Long {
return bitcoinKit.fee(value, address, feePriority = feePriority)
val feeRate = feeRateFromPriority(feePriority)
return bitcoinKit.fee(value, address, feeRate = feeRate)
}

fun showDebugInfo() {
Expand Down Expand Up @@ -106,4 +108,18 @@ class MainViewModel : ViewModel(), BitcoinKit.Listener {
override fun onKitStateUpdate(bitcoinKit: BitcoinKit, state: KitState) {
this.state.postValue(state)
}

private fun feeRateFromPriority(feePriority: FeePriority): Int {
val lowPriority = 20
val mediumPriority = 42
val highPriority = 81
return when (feePriority) {
FeePriority.Lowest -> lowPriority
FeePriority.Low -> (lowPriority + mediumPriority) / 2
FeePriority.Medium -> mediumPriority
FeePriority.High -> (mediumPriority + highPriority) / 2
FeePriority.Highest -> highPriority
is FeePriority.Custom -> feePriority.feeRate.toInt()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import io.horizontalsystems.bitcoinkit.blocks.BlockSyncer
import io.horizontalsystems.bitcoinkit.blocks.Blockchain
import io.horizontalsystems.bitcoinkit.core.*
import io.horizontalsystems.bitcoinkit.managers.*
import io.horizontalsystems.bitcoinkit.models.*
import io.horizontalsystems.bitcoinkit.models.BitcoinPaymentData
import io.horizontalsystems.bitcoinkit.models.BlockInfo
import io.horizontalsystems.bitcoinkit.models.PublicKey
import io.horizontalsystems.bitcoinkit.models.TransactionInfo
import io.horizontalsystems.bitcoinkit.network.*
import io.horizontalsystems.bitcoinkit.network.peer.PeerGroup
import io.horizontalsystems.bitcoinkit.network.peer.PeerHostManager
Expand Down Expand Up @@ -46,7 +49,6 @@ class BitcoinKit(seed: ByteArray, networkType: NetworkType, walletId: String? =

private val peerGroup: PeerGroup
private val initialSyncer: InitialSyncer
private val feeRateSyncer: FeeRateSyncer
private val addressManager: AddressManager
private val addressConverter: AddressConverter
private val paymentAddressParser: PaymentAddressParser
Expand Down Expand Up @@ -108,7 +110,6 @@ class BitcoinKit(seed: ByteArray, networkType: NetworkType, walletId: String? =
val blockHashFetcher = BlockHashFetcherBCoin(addressSelector, BCoinApi(network, HttpRequester()), BlockHashFetcherHelper())
val blockDiscovery = BlockDiscoveryBatch(Wallet(hdWallet), blockHashFetcher, network.checkpointBlock.height)

feeRateSyncer = FeeRateSyncer(realmFactory, ApiFeeRate(networkType), connectionManager)
initialSyncer = InitialSyncer(realmFactory, blockDiscovery, stateManager, addressManager, peerGroup, kitStateProvider)
transactionBuilder = TransactionBuilder(realmFactory, addressConverter, hdWallet, network, addressManager, unspentOutputProvider)
transactionCreator = TransactionCreator(realmFactory, transactionBuilder, transactionProcessor, peerGroup)
Expand All @@ -119,12 +120,10 @@ class BitcoinKit(seed: ByteArray, networkType: NetworkType, walletId: String? =
//
fun start() {
initialSyncer.sync()
feeRateSyncer.start()
}

fun stop() {
initialSyncer.stop()
feeRateSyncer.stop()
dataProvider.clear()
}

Expand All @@ -143,12 +142,12 @@ class BitcoinKit(seed: ByteArray, networkType: NetworkType, walletId: String? =
return dataProvider.transactions(fromHash, limit)
}

fun fee(value: Long, address: String? = null, senderPay: Boolean = true, feePriority: FeePriority = FeePriority.Medium): Long {
return transactionBuilder.fee(value, getFeeRate(feePriority), senderPay, address)
fun fee(value: Long, address: String? = null, senderPay: Boolean = true, feeRate: Int): Long {
return transactionBuilder.fee(value, feeRate, senderPay, address)
}

fun send(address: String, value: Long, senderPay: Boolean = true, feePriority: FeePriority = FeePriority.Medium) {
transactionCreator.create(address, value, getFeeRate(feePriority), senderPay)
fun send(address: String, value: Long, senderPay: Boolean = true, feeRate: Int) {
transactionCreator.create(address, value, feeRate, senderPay)
}

fun receiveAddress(): String {
Expand Down Expand Up @@ -224,22 +223,6 @@ class BitcoinKit(seed: ByteArray, networkType: NetworkType, walletId: String? =
}
}

private fun getFeeRate(feePriority: FeePriority): Int {
val feeRate: Double = when(feePriority) {
FeePriority.Lowest -> dataProvider.feeRate.lowPriority
FeePriority.Low -> {
(dataProvider.feeRate.lowPriority + dataProvider.feeRate.mediumPriority) / 2
}
FeePriority.Medium -> dataProvider.feeRate.mediumPriority
FeePriority.High -> {
(dataProvider.feeRate.mediumPriority + dataProvider.feeRate.highPriority) / 2
}
FeePriority.Highest -> dataProvider.feeRate.highPriority
is FeePriority.Custom -> feePriority.feeRate
}
return feeRate.toInt()
}

enum class NetworkType {
MainNet,
TestNet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ class DataProvider(private val realmFactory: RealmFactory, private val listener:
var lastBlockInfo: BlockInfo?
private set

val feeRate: FeeRate
get() = realmFactory.realm.use { realm ->
realm.where(FeeRate::class.java).findAll().firstOrNull()?.let { realm.copyFromRealm(it) }
?: FeeRate.defaultFeeRate
}

init {
lastBlockInfo = realmFactory.realm.use { realm ->
realm.where(Block::class.java)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit affdbed

Please sign in to comment.