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

Commit

Permalink
feat: expose orders api methods
Browse files Browse the repository at this point in the history
- Get details of an order with the given ID
- Get a list of orders

Create and cancel orders had already been exposed.
  • Loading branch information
CassiusPacheco committed Nov 15, 2022
1 parent d13e62b commit ccf8273
Show file tree
Hide file tree
Showing 8 changed files with 355 additions and 7 deletions.
138 changes: 137 additions & 1 deletion Sources/ImmutableXCore/ImmutableX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public struct ImmutableX {
private let balancesAPI: BalancesAPI.Type
private let mintsAPI: MintsAPI.Type
private let withdrawalAPI: WithdrawalsAPI.Type
private let ordersAPI: OrdersAPI.Type

/// Internal init method that includes dependencies. For the public facing API use ``initialize(base:logLevel:)``
/// instead.
Expand All @@ -63,7 +64,8 @@ public struct ImmutableX {
projectsAPI: ProjectsAPI.Type = ProjectsAPI.self,
balancesAPI: BalancesAPI.Type = BalancesAPI.self,
mintsAPI: MintsAPI.Type = MintsAPI.self,
withdrawalAPI: WithdrawalsAPI.Type = WithdrawalsAPI.self
withdrawalAPI: WithdrawalsAPI.Type = WithdrawalsAPI.self,
ordersAPI: OrdersAPI.Type = OrdersAPI.self
) {
self.base = base
self.logLevel = logLevel
Expand All @@ -81,6 +83,7 @@ public struct ImmutableX {
self.balancesAPI = balancesAPI
self.mintsAPI = mintsAPI
self.withdrawalAPI = withdrawalAPI
self.ordersAPI = ordersAPI
}

/// Initializes the SDK with the given ``base`` and ``logLevel`` by assigning a shared instance accessible via
Expand Down Expand Up @@ -650,4 +653,137 @@ public struct ImmutableX {
)
}
}

/// Get details of an order with the given ID
///
/// - Parameters:
/// - id: Order ID
/// - includeFees: Set flag to true to include fee body for the order (optional)
/// - auxiliaryFeePercentages: Comma separated string of fee percentages that are to be paired with
/// auxiliary_fee_recipients (optional)
/// - auxiliaryFeeRecipients: Comma separated string of fee recipients that are to be paired with
/// auxiliary_fee_percentages (optional)
/// - Returns: ``Order``
/// - Throws: A variation of ``ImmutableXError``
public func getOrder(
id: String,
includeFees: Bool? = nil,
auxiliaryFeePercentages: String? = nil,
auxiliaryFeeRecipients: String? = nil
) async throws -> Order {
try await APIErrorMapper.map(caller: "Get Order") {
try await self.ordersAPI.getOrder(
id: id,
includeFees: includeFees,
auxiliaryFeePercentages: auxiliaryFeePercentages,
auxiliaryFeeRecipients: auxiliaryFeeRecipients
)
}
}

/// Get a list of orders
///
/// - Parameters:
/// - pageSize: Page size of the result (optional)
/// - cursor: Cursor (optional)
/// - orderBy: Property to sort by (optional)
/// - direction: Direction to sort (asc/desc) (optional)
/// - user: Ethereum address of the user who submitted this order (optional)
/// - status: Status of this order (optional)
/// - minTimestamp: Minimum created at timestamp for this order, in ISO 8601 UTC format.
/// Example: '2022-05-27T00:10:22Z' (optional)
/// - maxTimestamp: Maximum created at timestamp for this order, in ISO 8601 UTC format.
/// Example: '2022-05-27T00:10:22Z' (optional)
/// - updatedMinTimestamp: Minimum updated at timestamp for this order, in ISO 8601 UTC format.
/// Example: '2022-05-27T00:10:22Z' (optional)
/// - updatedMaxTimestamp: Maximum updated at timestamp for this order, in ISO 8601 UTC format.
/// Example: '2022-05-27T00:10:22Z' (optional)
/// - buyTokenType: Token type of the asset this order buys (optional)
/// - buyTokenId: ERC721 Token ID of the asset this order buys (optional)
/// - buyAssetId: Internal IMX ID of the asset this order buys (optional)
/// - buyTokenAddress: Token address of the asset this order buys (optional)
/// - buyTokenName: Token name of the asset this order buys (optional)
/// - buyMinQuantity: Min quantity for the asset this order buys (optional)
/// - buyMaxQuantity: Max quantity for the asset this order buys (optional)
/// - buyMetadata: JSON-encoded metadata filters for the asset this order buys (optional)
/// - sellTokenType: Token type of the asset this order sells (optional)
/// - sellTokenId: ERC721 Token ID of the asset this order sells (optional)
/// - sellAssetId: Internal IMX ID of the asset this order sells (optional)
/// - sellTokenAddress: Token address of the asset this order sells (optional)
/// - sellTokenName: Token name of the asset this order sells (optional)
/// - sellMinQuantity: Min quantity for the asset this order sells (optional)
/// - sellMaxQuantity: Max quantity for the asset this order sells (optional)
/// - sellMetadata: JSON-encoded metadata filters for the asset this order sells (optional)
/// - auxiliaryFeePercentages: Comma separated string of fee percentages that are to be paired with
/// auxiliary_fee_recipients (optional)
/// - auxiliaryFeeRecipients: Comma separated string of fee recipients that are to be paired with
/// auxiliary_fee_percentages (optional)
/// - includeFees: Set flag to true to include fee object for orders (optional)
/// - Returns: ``ListOrdersResponse``
/// - Throws: A variation of ``ImmutableXError``
public func listOrders(
pageSize: Int? = nil,
cursor: String? = nil,
orderBy: ListOrdersOrderBy? = nil,
direction: String? = nil,
user: String? = nil,
status: ListOrdersStatus? = nil,
minTimestamp: String? = nil,
maxTimestamp: String? = nil,
updatedMinTimestamp: String? = nil,
updatedMaxTimestamp: String? = nil,
buyTokenType: String? = nil,
buyTokenId: String? = nil,
buyAssetId: String? = nil,
buyTokenAddress: String? = nil,
buyTokenName: String? = nil,
buyMinQuantity: String? = nil,
buyMaxQuantity: String? = nil,
buyMetadata: String? = nil,
sellTokenType: String? = nil,
sellTokenId: String? = nil,
sellAssetId: String? = nil,
sellTokenAddress: String? = nil,
sellTokenName: String? = nil,
sellMinQuantity: String? = nil,
sellMaxQuantity: String? = nil,
sellMetadata: String? = nil,
auxiliaryFeePercentages: String? = nil,
auxiliaryFeeRecipients: String? = nil,
includeFees: Bool? = nil
) async throws -> ListOrdersResponse {
try await APIErrorMapper.map(caller: "List Orders") {
try await self.ordersAPI.listOrders(
pageSize: pageSize,
cursor: cursor,
orderBy: orderBy?.asApiArgument,
direction: direction,
user: user,
status: status?.asApiArgument,
minTimestamp: minTimestamp,
maxTimestamp: maxTimestamp,
updatedMinTimestamp: updatedMinTimestamp,
updatedMaxTimestamp: updatedMaxTimestamp,
buyTokenType: buyTokenType,
buyTokenId: buyTokenId,
buyAssetId: buyAssetId,
buyTokenAddress: buyTokenAddress,
buyTokenName: buyTokenName,
buyMinQuantity: buyMinQuantity,
buyMaxQuantity: buyMaxQuantity,
buyMetadata: buyMetadata,
sellTokenType: sellTokenType,
sellTokenId: sellTokenId,
sellAssetId: sellAssetId,
sellTokenAddress: sellTokenAddress,
sellTokenName: sellTokenName,
sellMinQuantity: sellMinQuantity,
sellMaxQuantity: sellMaxQuantity,
sellMetadata: sellMetadata,
auxiliaryFeePercentages: auxiliaryFeePercentages,
auxiliaryFeeRecipients: auxiliaryFeeRecipients,
includeFees: includeFees
)
}
}
}
30 changes: 30 additions & 0 deletions Sources/ImmutableXCore/Model/ListOrdersOrderBy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Foundation

// swiftlint:disable:next line_length
/// Ordering options available for ``ImmutableX/listOrders(pageSize:cursor:orderBy:direction:user:status:minTimestamp:maxTimestamp:updatedMinTimestamp:updatedMaxTimestamp:buyTokenType:buyTokenId:buyAssetId:buyTokenAddress:buyTokenName:buyMinQuantity:buyMaxQuantity:buyMetadata:sellTokenType:sellTokenId:sellAssetId:sellTokenAddress:sellTokenName:sellMinQuantity:sellMaxQuantity:sellMetadata:auxiliaryFeePercentages:auxiliaryFeeRecipients:includeFees:)``
public enum ListOrdersOrderBy: String, CaseIterable {
case createdAt = "created_at"
case expiredAt = "expired_at"
case sellQuantity = "sell_quantity"
case buyQuantity = "buy_quantity"
case buyQuantityWithFees = "buy_quantity_with_fees"
case updatedAt = "updated_at"

/// Converts this enum to the type expected by the generated API code
internal var asApiArgument: OrdersAPI.OrderBy_listOrders {
switch self {
case .createdAt:
return .createdAt
case .expiredAt:
return .expiredAt
case .sellQuantity:
return .sellQuantity
case .buyQuantity:
return .buyQuantity
case .buyQuantityWithFees:
return .buyQuantityWithFees
case .updatedAt:
return .updatedAt
}
}
}
27 changes: 27 additions & 0 deletions Sources/ImmutableXCore/Model/ListOrdersStatus.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Foundation

// swiftlint:disable:next line_length
/// Status options available for ``ImmutableX/listOrders(pageSize:cursor:orderBy:direction:user:status:minTimestamp:maxTimestamp:updatedMinTimestamp:updatedMaxTimestamp:buyTokenType:buyTokenId:buyAssetId:buyTokenAddress:buyTokenName:buyMinQuantity:buyMaxQuantity:buyMetadata:sellTokenType:sellTokenId:sellAssetId:sellTokenAddress:sellTokenName:sellMinQuantity:sellMaxQuantity:sellMetadata:auxiliaryFeePercentages:auxiliaryFeeRecipients:includeFees:)``
public enum ListOrdersStatus: String, CaseIterable {
case active
case filled
case cancelled
case expired
case inactive

/// Converts this enum to the type expected by the generated API code
internal var asApiArgument: OrdersAPI.Status_listOrders {
switch self {
case .active:
return .active
case .filled:
return .filled
case .cancelled:
return .cancelled
case .expired:
return .expired
case .inactive:
return .inactive
}
}
}
45 changes: 44 additions & 1 deletion Tests/ImmutableXCoreTests/ImmutableXTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ final class ImmutableXTests: XCTestCase {
let balancesAPIMock = BalancesAPIMock.self
let mintsAPIMock = MintsAPIMock.self
let withdrawalsAPIMock = WithdrawalsAPIMock.self
let ordersAPIMock = OrdersAPIMock.self

lazy var core = ImmutableX(
buyWorkflow: buyWorkflow,
Expand All @@ -31,7 +32,8 @@ final class ImmutableXTests: XCTestCase {
projectsAPI: projectsAPIMock,
balancesAPI: balancesAPIMock,
mintsAPI: mintsAPIMock,
withdrawalAPI: withdrawalsAPIMock
withdrawalAPI: withdrawalsAPIMock,
ordersAPI: ordersAPIMock
)

override func setUp() {
Expand All @@ -50,6 +52,7 @@ final class ImmutableXTests: XCTestCase {
balancesAPIMock.resetMock()
mintsAPIMock.resetMock()
withdrawalsAPIMock.resetMock()
ordersAPIMock.resetMock()

ImmutableX.initialize()

Expand Down Expand Up @@ -140,6 +143,14 @@ final class ImmutableXTests: XCTestCase {
let listWithdrawalsCompanion = WithdrawalsAPIMock.ListWithdrawalsCompanion()
listWithdrawalsCompanion.returnValue = listWithdrawalsResponseStub1
withdrawalsAPIMock.mock(listWithdrawalsCompanion)

let getOrderCompanion = OrdersAPIMockGetCompanion()
getOrderCompanion.returnValue = orderActiveStub2
ordersAPIMock.mock(getOrderCompanion, id: "\(orderActiveStub2.orderId)")

let listOrderCompanion = OrdersAPIMockListOrdersCompanion()
listOrderCompanion.returnValue = listOrdersResponseStub1
ordersAPIMock.mock(listOrderCompanion)
}

func testSdkVersion() {
Expand Down Expand Up @@ -514,4 +525,36 @@ final class ImmutableXTests: XCTestCase {
_ = try await core.listWithdrawals()
}
}

// MARK: - Order

func testGetOrderSuccess() async throws {
let response = try await core.getOrder(id: "\(orderActiveStub2.orderId)")
XCTAssertEqual(response, orderActiveStub2)
}

func testGetOrderFailure() async {
let companion = OrdersAPIMockGetCompanion()
companion.throwableError = DummyError.something
ordersAPIMock.mock(companion, id: "\(orderActiveStub2.orderId)")

await XCTAssertThrowsErrorAsync {
_ = try await core.getOrder(id: "\(orderActiveStub2.orderId)")
}
}

func testListOrdersSuccess() async throws {
let response = try await core.listOrders()
XCTAssertEqual(response, listOrdersResponseStub1)
}

func testListOrdersFailure() async {
let companion = OrdersAPIMockListOrdersCompanion()
companion.throwableError = DummyError.something
ordersAPIMock.mock(companion)

await XCTAssertThrowsErrorAsync {
_ = try await core.listOrders()
}
}
}
Loading

0 comments on commit ccf8273

Please sign in to comment.