bitcoin-atm
bitcoin atm for pyc inc.
git clone https://9o.is/git/bitcoin-atm.git
Transaction.scala
(1216B)
1 package inc.pyc.chimera
2 package ddb
3
4 import akka.actor.Actor
5
6 /**
7 * Save transactions for historical reasons.
8 * Primary Key is the txid.
9 */
10 class Transaction extends Actor with DDB {
11
12 def receive = {
13 case tx: CompleteTx =>
14 table.put(
15 tx.txid,
16 "date" -> tx.dateISO8601,
17 "address" -> tx.address,
18 "price" -> tx.price.price,
19 "percent" -> tx.price.percentage,
20 "currency" -> tx.currency.toString,
21 "bills" -> encodeBills(tx.bills),
22 "chimera" -> tx.chimera,
23 "bitcoin" -> tx.bitcoins,
24 "ticker" -> tx.ticker.toString)
25 }
26
27 /*
28 * Converts a list of bills that may have repeated values
29 * to a list of '{bill}x{amount}'
30 */
31 private def encodeBills(bills: List[Int]): List[String] = {
32 bills.groupBy(_.toString)
33 .mapValues(_.size).map(m => m._1+"x"+m._2).toList
34 }
35
36 /*
37 * Converts an encoded list of '{bill}x{amount}' to list
38 * of bills that may have repeated values.
39 */
40 private def decodeBills(bills: List[String]): List[Int] = {
41 bills.map(_.split("x")).flatMap {
42 a => (0 until a(1).toInt) map (i => a(0).toInt)
43 }
44 }
45 }