Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

adding exchange state machine class #179

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package tbdex.sdk.protocol

/**
* InvalidNextMessageException
*
* @param message the exception message detailing the error
*/
class InvalidNextMessageException(message: String? = null)
: RuntimeException(message)

/**
* ExchangeIdNotFoundException
*
* @param message the exception message detailing the error
*/
class ExchangeIdNotFoundException(message: String? = null)
: RuntimeException(message)

/**
* UnknownMessageKindException
*
* @param message the exception message detailing the error
*/
class UnknownMessageKindException(message: String? = null)
: RuntimeException(message)
83 changes: 83 additions & 0 deletions protocol/src/main/kotlin/tbdex/sdk/protocol/Exchange.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package tbdex.sdk.protocol

import tbdex.sdk.protocol.models.Close
import tbdex.sdk.protocol.models.Message
import tbdex.sdk.protocol.models.MessageKind
import tbdex.sdk.protocol.models.Order
import tbdex.sdk.protocol.models.OrderStatus
import tbdex.sdk.protocol.models.Quote
import tbdex.sdk.protocol.models.Rfq

class Exchange(
var rfq: Rfq? = null,
var quote: Quote? = null,
var order: Order? = null,
var orderStatus: MutableList<OrderStatus> = mutableListOf(),
var close: Close? = null
) {

fun addMessages(messages: List<Message>) {

val sortedMessages = messages.sortedBy { it.metadata.createdAt }

for (message in sortedMessages) {
this.addNextMessage(message)
}
}

fun addNextMessage(message: Message) {
if (!this.isValidNext(message.metadata.kind)) {
throw InvalidNextMessageException(
"Could not add message ${message.metadata.id} to Exchange because" +
"${message.metadata.kind} is not a valid next message for this Exchange"
)
}

if (this.exchangeId() != null &&
message.metadata.exchangeId.toString() != this.exchangeId()) {
throw ExchangeIdNotFoundException(
"Could not add message ${message.metadata.id} to Exchange because" +
"it does not have matching exchange id ${this.exchangeId()}"
)
}

when (message.metadata.kind) {
MessageKind.rfq -> this.rfq = message as Rfq
MessageKind.quote -> this.quote = message as Quote
MessageKind.order -> this.order = message as Order
MessageKind.orderstatus -> {
this.orderStatus.add(message as OrderStatus)
}

MessageKind.close -> this.close = message as Close
else -> throw UnknownMessageKindException("Unrecognized message kind")
}

}

fun isValidNext(kind: MessageKind): Boolean {
val validNext = this.latestMessage()?.validNext ?: setOf(MessageKind.rfq)
return validNext.contains(kind)
}

fun latestMessage(): Message? {
return when {
this.close != null -> this.close
this.orderStatus.isNotEmpty() -> this.orderStatus.last()
this.order != null -> this.order
this.quote != null -> this.quote
this.rfq != null -> this.rfq
else -> null
}
}

fun exchangeId(): String? {
return this.rfq?.metadata?.exchangeId?.toString()
}

fun messages(): List<Message> {
return listOfNotNull(this.rfq, this.quote, this.order, this.close) +
(this.orderStatus)
}

}
Loading