bitcoin-atm
bitcoin atm for pyc inc.
git clone https://9o.is/git/bitcoin-atm.git
Expenditure.scala
(1386B)
1 package inc.pyc.chimera
2 package minions
3
4 import ddb._
5 import lycia._
6 import akka.actor._
7 import akka.pattern._
8 import concurrent.Await
9 import concurrent.duration._
10
11 /**
12 * Minion that has access to the DDB Expenditure Table.
13 */
14 class ExpenditureMinion
15 extends Actor
16 with ActorLogging
17 with Minion
18 with MinionWorker
19 with DDBMinion {
20
21 def props: Props = Props[Expenditure]
22 def name: String = "Expenditure_"+util.Random.nextInt
23
24 val tasks: Receive = {
25 case audit: AuditData => calculateBuyLimit(audit)
26 case tx: CompleteTx => saveExpenditure(tx)
27 }
28
29 /**
30 * Saves completed transaction to Dynamo DB's Expenditure table.
31 * @param tx completed transaction
32 */
33 def saveExpenditure(tx: CompleteTx) = workout(_ ! tx)
34
35 /**
36 * Calculates how many bitcoins a user can buy.
37 * @param audit data about user processing transaction
38 */
39 def calculateBuyLimit(audit: AuditData) = workout {
40 worker =>
41 val f = ask(worker, Past24Hours(audit.addresses)).mapTo[DDBItems]
42 val res = Await.result(f, 3 seconds)
43 val spent = res.items flatMap {
44 item =>
45 item.attributes.
46 filter(_.name == "paid").
47 map(_.value.getN().toInt)
48 } sum
49
50 val limit = audit.userInfo.purchaseLimit getOrElse Lycia.buyLimit
51 val left = limit - spent
52
53 sender ! LeftToSpend(left, limit)
54 }
55 }