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

Commit

Permalink
add protocol and externalId to messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kirahsapong committed Mar 19, 2024
1 parent 5495d5c commit e776214
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 10 deletions.
18 changes: 15 additions & 3 deletions Sources/tbDEX/Protocol/Models/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ public struct Message<D: MessageData>: Codable, Equatable {
/// An ephemeral JSON object used to transmit sensitive data (e.g. PII)
public let `private`: AnyCodable?

/// Default Initializer
/// Default Initializer. `protocol` defaults to "1.0" if nil
public init(
from: String,
to: String,
exchangeID: String,
data: D
data: D,
externalID: String?,
`protocol`: String?
) {
let now = Date()
self.metadata = MessageMetadata(
Expand All @@ -34,7 +36,9 @@ public struct Message<D: MessageData>: Codable, Equatable {
from: from,
to: to,
exchangeID: exchangeID,
createdAt: now
createdAt: now,
externalID: externalID,
protocol: `protocol` ?? "1.0"
)
self.data = data
self.signature = nil
Expand Down Expand Up @@ -112,6 +116,12 @@ public struct MessageMetadata: Codable, Equatable {

/// The time at which the message was created. Can be serialized to or from JSON with `tbDEXDateFormatter`. Use `tbDEXJSONDecoder` or `tbDEXJSONEncoder`.
public let createdAt: Date

/// Arbitrary ID for the caller to associate with the message. Optional */
public let externalID: String?

/// Version of the protocol in use (x.x format). Must be consistent with all other messages in a given exchange */
public let `protocol`: String

enum CodingKeys: String, CodingKey {
case id
Expand All @@ -120,5 +130,7 @@ public struct MessageMetadata: Codable, Equatable {
case to
case exchangeID = "exchangeId"
case createdAt
case externalID = "externalId"
case `protocol`
}
}
9 changes: 7 additions & 2 deletions Sources/tbDEX/Protocol/Models/Messages/RFQ.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ public typealias RFQ = Message<RFQData>

extension RFQ {

/// Default Initializer. `protocol` defaults to "1.0" if nil
public init(
to: String,
from: String,
data: RFQData
data: RFQData,
externalID: String?,
`protocol`: String?
) {
let id = TypeID(prefix: data.kind().rawValue)!
self.metadata = MessageMetadata(
Expand All @@ -18,7 +21,9 @@ extension RFQ {
from: from,
to: to,
exchangeID: id.rawValue,
createdAt: Date()
createdAt: Date(),
externalID: externalID,
protocol: `protocol` ?? "1.0"
)
self.data = data
self.private = nil
Expand Down
4 changes: 3 additions & 1 deletion Tests/tbDEXTests/Protocol/Models/Messages/CloseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ final class CloseTests: XCTestCase {
exchangeID: "exchange_123",
data: .init(
reason: "test reason"
)
),
externalID: nil,
protocol: "1.0"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ final class OrderStatusTests: XCTestCase {
exchangeID: "exchange_123",
data: .init(
orderStatus: "test status"
)
),
externalID: nil,
protocol: nil
)
}
}
4 changes: 3 additions & 1 deletion Tests/tbDEXTests/Protocol/Models/Messages/OrderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ final class OrderTests: XCTestCase {
from: from,
to: to,
exchangeID: "exchange_123",
data: .init()
data: .init(),
externalID: nil,
protocol: nil
)
}
}
4 changes: 3 additions & 1 deletion Tests/tbDEXTests/Protocol/Models/Messages/QuoteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ final class QuoteTests: XCTestCase {
amount: "2.00",
fee: "0.50"
)
)
),
externalID: nil,
protocol: nil
)
}
}
4 changes: 3 additions & 1 deletion Tests/tbDEXTests/Protocol/Models/Messages/RFQTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ final class RFQTests: XCTestCase {
kind: "BITCOIN_ADDRESS"
),
claims: []
)
),
externalID: nil,
protocol: nil
)
}
}

0 comments on commit e776214

Please sign in to comment.