bitcoin-atm
bitcoin atm for pyc inc.
git clone https://9o.is/git/bitcoin-atm.git
Bitcoin.scala
(3560B)
1 package inc.pyc.chimera
2
3 import lycia._
4 import minions._
5 import System._
6 import inc.pyc._
7 import bitcoin._
8 import BitcoinService._
9 import BitcoinJsonRPC._
10 import akka.actor._
11 import concurrent._
12 import duration._
13
14 /**
15 * Overlord's extension for bitcoin library.
16 */
17 trait Bitcoin extends Wallet with Ticker {
18 this: Overlord =>
19
20 /**
21 * Gets fiat valued balance in bitcoin wallet.
22 */
23 def fiatBalance: FiatBalance =
24 FiatBalance(price.priceWithPercentage * balance.remaining)
25 }
26
27
28 trait Wallet {
29 this: FSM[State, Data] =>
30
31 /**
32 * Bitcoin wallet service
33 */
34 var walletService: BitcoinService.Value = BitcoinService.withName({
35 Settings(system).walletService
36 })
37
38 /**
39 * Balance in bitcoin wallet
40 */
41 var balance: Balance = Balance(0)
42
43 /**
44 * Creates a Wallet minion.
45 */
46 def walletMinion =
47 context.actorOf(Props(classOf[WalletMinion], walletService))
48
49 /**
50 * Run this function in preStart to enable the wallet.
51 */
52 def initWallet {
53 walletService = Lycia.serviceWallet
54 self ! Lycia.percentProfit
55 walletMinion ! GetBalance
56 setTimer("wallet-balance", GetBalance, 3 minutes, true)
57 }
58
59 /**
60 * Run this function in postStop to disable the wallet.
61 */
62 def stopWallet {
63 cancelTimer("wallet-balance")
64 }
65
66 /**
67 * Handle bitcoin wallet commands.
68 */
69 def handleWallet: StateFunction = {
70 case Event(GetBalance, _) =>
71 walletMinion ! GetBalance
72 stay
73
74 case Event(newBalance: Balance, _) =>
75 balance = newBalance
76 stay
77 }
78
79 /**
80 * Validates bitcoin address in the QR code.
81 */
82 def validateQr(qrcode: QrCode) {
83 walletMinion ! qrcode
84 }
85
86 /**
87 * Sells bitcoins to the user.
88 */
89 def sell(tx: IncompleteTx) {
90 walletMinion ! tx
91 }
92 }
93
94
95 trait Ticker {
96 this: FSM[State, Data] =>
97
98 /**
99 * Bitcoin price ticker service
100 */
101 var tickerService: BitcoinService.Value = BitcoinService.withName({
102 Settings(system).tickerService
103 })
104
105 /**
106 * Price per bitcoin
107 */
108 var price: Price = Price(999999999)
109
110 /**
111 * Creates a Ticker minion.
112 */
113 def tickerMinion =
114 context.actorOf(Props(classOf[TickerMinion], tickerService))
115
116 /**
117 * Run this function in preStart to enable the ticker.
118 */
119 def initTicker {
120 tickerService = Lycia.servicePriceTicker
121 tickerMinion ! Tick
122 setTimer("ticker", Tick, 1 minute, true)
123 }
124
125 /**
126 * Run this function in postStop to disable the ticker.
127 */
128 def stopTicker {
129 cancelTimer("ticker")
130 }
131
132 /**
133 * Handle price ticker commands.
134 */
135 def handleTicker: StateFunction = {
136 case Event(Tick, _) =>
137 tickerMinion ! Tick
138 stay
139
140 case Event(GetPrice, _) =>
141 client.updatePrice(price format)
142 stay
143
144 case Event(Price(newPrice, _), _) =>
145 price = price.copy(price = newPrice)
146 client.updatePrice(price format)
147 stay
148
149 case Event(Percentage(value), _) =>
150 price = price.copy(percentage = value)
151 client.updatePrice(price format)
152 stay
153 }
154 }
155
156 /**
157 * Command to send the buy price.
158 */
159 case object GetPrice
160
161 /**
162 * Command to set a percentage price over market.
163 * If price over market is 5%, send 5, not 0.05, as the profit value.
164 * @param profit profit value
165 */
166 case class Percentage(profit: Double)