Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create AggregateFeeRateApi * Rename, add docs
- Loading branch information
1 parent
e095a1b
commit 5f4053b
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
fee-provider/src/main/scala/org/bitcoins/feeprovider/FallbackFeeRateApi.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.bitcoins.feeprovider | ||
|
||
import org.bitcoins.core.api.feeprovider.FeeRateApi | ||
import org.bitcoins.core.util.FutureUtil | ||
import org.bitcoins.core.wallet.fee.FeeUnit | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
import scala.util.control.NonFatal | ||
|
||
/** Takes multiple [[FeeRateApi FeeRateApis]] and attempts to get a fee rate from | ||
* one in order until one succeeds. | ||
*/ | ||
case class FallbackFeeRateApi(providers: Vector[FeeRateApi])(implicit | ||
ec: ExecutionContext) | ||
extends FeeRateApi { | ||
|
||
override def getFeeRate(): Future[FeeUnit] = { | ||
val init: Option[FeeUnit] = None | ||
val retOptF = FutureUtil.foldLeftAsync(init, providers) { | ||
case (ret, provider) => | ||
ret match { | ||
case Some(value) => Future.successful(value).map(Some(_)) | ||
case None => | ||
provider | ||
.getFeeRate() | ||
.map(Some(_)) | ||
.recover { case NonFatal(_) => None } | ||
} | ||
} | ||
|
||
retOptF.map { | ||
case Some(ret) => ret | ||
case None => | ||
sys.error("Failed to get fee rate from any provider") | ||
} | ||
} | ||
} |