bitcoin-client
bitcoin client library for price ticker and wallet
git clone https://9o.is/git/bitcoin-client.git
PriceTicker.scala
(1098B)
1 package inc.pyc.bitcoin
2
3 import service._
4 import dispatch.Req
5 import akka.actor._
6
7 /**
8 * A Bitcoin service with buy/sell prices.
9 */
10 sealed trait PriceTicker
11
12 private[bitcoin] trait HttpPriceTicker extends HttpService with PriceTicker {
13 this: Actor with ActorLogging =>
14
15 /**
16 * Price ticker API
17 */
18 protected val ticker_api: Req
19
20 /**
21 * Gets the buy price
22 */
23 protected def buyPrice: String
24
25
26 val priceTicker: Receive = {
27 case Tick =>
28 val price = Price(buyPrice)
29 sender ! price
30 }
31 }
32
33 /**
34 * Command to update the price per bitcoin.
35 * Sent frequently to update the price in the UI.
36 */
37 case object Tick
38
39 /**
40 * Price per bitcoin.
41 * @param price price of one bitcoin
42 */
43 case class Price(price: Double, percentage: Double = 0) {
44 def format: String = "%,1.2f" format priceWithPercentage
45
46 /**
47 * Formats the over market price with percentage.
48 */
49 def priceWithPercentage: Double = {
50 val ans = price + (price * (percentage * 0.01))
51 math ceil (ans * 100) / 100
52 }
53 }
54
55 object Price {
56 def apply(s: String): Price = Price(s.toDouble)
57 }