SHARE
TWEET

DouThing

iva_kam Jun 10th, 2019 148 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. case class PlainEffect(
  2.     ledgerSequence: Long,
  3.     ledgerHash: String,
  4.     transactionHash: String,
  5.     transactionSequence: Long,
  6.     operationSequence: Long,
  7.     effectSequence: Long,
  8.     operationType: String,
  9.     effectType: String,
  10.     transactionOwner: String,
  11.     actionAccount: String,
  12.     amountPaid: Option[Long],
  13.     assetCodePaid: Option[String],
  14.     amountReceived: Option[Long],
  15.     assetReceived: Option[String],
  16.     counterAgent: Option[String],
  17.     additionalEffectProps: Option[String]
  18.   )
  19.  
  20. case class Transfer(agent: String, counterAgent: String, sendAmount: Long, sendCode: String, received: Long,
  21.     receivedCode: String)
  22.  
  23. object Balance{
  24.     def empty = Balance(mutable.Map.empty[String, (Long, Long)])
  25.   }
  26.   case class Balance(map: mutable.Map[String, (Long, Long)]){
  27.     def addSend(asset: String, amount: Long): Unit = {
  28.       val (received, send): (Long, Long) = map.getOrElse(asset, (0,0))
  29.       map.update(asset, (received, send + amount))
  30.     }
  31.  
  32.     def addReceived(asset: String, amount: Long): Unit = {
  33.       val (received, send): (Long, Long) = map.getOrElse(asset, (0,0))
  34.       map.update(asset, (received + amount, send))
  35.     }
  36.   }
  37.  
  38.   object Balances{
  39.     def empty: Balances = Balances(mutable.Map.empty[String,Balance])
  40.   }
  41.  
  42.   case class Balances(map: mutable.Map[String, Balance]) {
  43.     def addSend(account: String, asset: String, amount: Long): Unit = {
  44.       val v = map getOrElse (account, Balance.empty)
  45.       v.addSend(asset, amount)
  46.       map.update(account, v)
  47.     }
  48.  
  49.     def addReceived(account: String, asset: String, amount: Long): Unit = {
  50.       val v = map getOrElse (account, Balance.empty)
  51.       v.addReceived(asset, amount)
  52.       map.update(account, v)
  53.     }
  54.   }
  55.  
  56.  
  57.   def fromEffect(effect: PlainEffect): Option[Transfer] = ???
  58.   def someFunc(effects: Seq[PlainEffect]): Balances =
  59.     effects.flatMap { fromEffect }.foldLeft(Balances.empty) { (balances, transfer) =>
  60.       val Transfer(agent, counterAgent, sendAmount, sendCode, received, receivedCode) = transfer
  61.       balances.addReceived(agent, receivedCode, received)
  62.       balances.addReceived(counterAgent, sendCode, sendAmount)
  63.       balances
  64.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top