bitcoin-atm

bitcoin atm for pyc inc.

git clone https://9o.is/git/bitcoin-atm.git

LocalDB.scala

(2038B)


      1 package inc.pyc.chimera
      2 package minions
      3 
      4 import model._
      5 import System._
      6 import akka.actor._
      7 import inc.pyc.bill._
      8 import acceptor._
      9 import Events._
     10 
     11 class LocalDBMinion
     12   extends Actor 
     13   with ActorLogging 
     14   with Minion {
     15   
     16   val tasks: Receive = {
     17     case tx: IncompleteTx                   => save(tx)
     18     case tx: CompleteTx                     => save(tx)
     19     case creator: CreatorTx                 => create(creator)
     20     case (cnf: Confirmed, tx: IncompleteTx) => saveBill(cnf,tx) 
     21   }
     22   
     23   /**
     24    * Saves incomplete transaction to disk.
     25    * @param tx incomplete transaction
     26    */
     27   def save(tx: IncompleteTx) {
     28     IncompleteTransaction.save(tx)
     29   }
     30   
     31   /**
     32    * Removes incomplete transaction and saves completed 
     33    * transaction to local database.
     34    * @param tx completed transaction
     35    */
     36   def save(tx: CompleteTx) {
     37     IncompleteTransaction find (tx.address) map (_.delete_!)
     38     CompletedTransaction.save(tx)
     39   }
     40   
     41   /**
     42    * Saves confirmed inserted bill to current transaction
     43    * and saves the current transaction to disk.
     44    * @param confirmed confirmed bill
     45    * @param tx transaction in process
     46    */
     47   def saveBill(confirmed: Confirmed, tx: IncompleteTx) = runTask {
     48     val newTx = tx.insertBill(confirmed.bill)
     49     IncompleteTransaction.save(newTx)
     50     sender ! newTx
     51   }
     52 
     53   /**
     54    * Creates a local, incomplete transaction.
     55    * @param creator contains info to create a transaction: audit and buy limit
     56    */
     57   def create(creator: CreatorTx) = runTask {
     58     val audit = creator.audit
     59     val toSpend = creator.limit
     60     val price = creator.price
     61     val tx =
     62       IncompleteTransaction.find(audit.address.data) map {
     63         tx =>
     64           tx.asIncompleteTx.copy(
     65             limit = toSpend.left,
     66             userInfo = audit.userInfo)
     67       } openOr {
     68         IncompleteTx(
     69           address = audit.address.data,
     70           price = price,
     71           currency = currency,
     72           bills = Nil,
     73           limit = toSpend.left,
     74           userInfo = audit.userInfo)
     75       }
     76          
     77     sender ! tx
     78   }
     79 }