case class ExternalPayment sender String receiver String amount Long d

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
case class ExternalPayment(sender: String, receiver: String, amount: Long, deadline: DateTime)
case class InternalPayment(receiver: String, amount: Long)
case class ScheduledPayments(linkedPayments: Map[ExternalPayment, Option[InternalPayment]]) extends SmartEscrow {
override val initState = new InitState {
override val name: String = "Init"
}
override val currentState = initState
val intermediateStates = linkedPayments.map { case (ep, _) =>
new State {
override val name = s"${ep.receiver}-got-${ep.amount}-from-${ep.sender}"
}
}.toSeq.dropRight(1)
val done = new FinishState {
override val name: String = "AllIsPaid"
}
override val finishStates = Seq(done)
override val transitions: Map[Clause, Out] = {
def slidingPairs[T](s: Seq[T]): Seq[(T, T)] = s.length match {
case 0 => Seq()
case 1 => throw new IllegalStateException("no sliding pairs could be exctracted for 1 elem")
case _ => (0 to s.length - 2).toSeq.map(start => s(start) -> s(start + 1))
}
val sp = slidingPairs(Seq(initState) ++ intermediateStates ++ Seq(done))
//todo: deadline
linkedPayments.zip(sp).map{case ((ep,ip),(is,fs)) =>
val qc = ep match {
case ExternalPayment(sndr,rcvr,amt,deadline) if sndr.startsWith("NXT") && rcvr.startsWith("NXT") =>
QueryAndCheck(NxtPaymentQuery(Convert.parseAccountId(sndr), Convert.parseAccountId(rcvr)),
NotLessThan(amt))
case ExternalPayment(sndr,rcvr,amt,deadline) if !sndr.startsWith("NXT") && !rcvr.startsWith("NXT") =>
QueryAndCheck(BitcoinPaymentQuery(sndr, rcvr), NotLessThan(amt))
case _ => throw new IllegalArgumentException("Mix of BTC & NXT addresses")
}
val act = ip match{
case Some(InternalPayment(rcvr, amt)) if rcvr.startsWith("NXT") =>
SendNxt(Convert.parseAccountId(rcvr), amt)
}
(is, qc) -> (act,fs)
}
}
}