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

Commit

Permalink
feat: expose mint api methods
Browse files Browse the repository at this point in the history
- Get details of a mint with the given ID
- Get a list of mints
  • Loading branch information
CassiusPacheco committed Nov 14, 2022
1 parent b81cf42 commit 91ee53e
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 2 deletions.
79 changes: 78 additions & 1 deletion Sources/ImmutableXCore/ImmutableX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public struct ImmutableX {
private let collectionsAPI: CollectionsAPI.Type
private let projectsAPI: ProjectsAPI.Type
private let balancesAPI: BalancesAPI.Type
private let mintsAPI: MintsAPI.Type

/// Internal init method that includes dependencies. For the public facing API use ``initialize(base:logLevel:)``
/// instead.
Expand All @@ -59,7 +60,8 @@ public struct ImmutableX {
assetsAPI: AssetsAPI.Type = AssetsAPI.self,
collectionsAPI: CollectionsAPI.Type = CollectionsAPI.self,
projectsAPI: ProjectsAPI.Type = ProjectsAPI.self,
balancesAPI: BalancesAPI.Type = BalancesAPI.self
balancesAPI: BalancesAPI.Type = BalancesAPI.self,
mintsAPI: MintsAPI.Type = MintsAPI.self
) {
self.base = base
self.logLevel = logLevel
Expand All @@ -75,6 +77,7 @@ public struct ImmutableX {
self.collectionsAPI = collectionsAPI
self.projectsAPI = projectsAPI
self.balancesAPI = balancesAPI
self.mintsAPI = mintsAPI
}

/// Initializes the SDK with the given ``base`` and ``logLevel`` by assigning a shared instance accessible via
Expand Down Expand Up @@ -490,4 +493,78 @@ public struct ImmutableX {
try await self.balancesAPI.listBalances(owner: owner)
}
}

/// Get details of a mint with the given ID
///
/// - Parameter id: Mint ID. This is the transaction_id returned from listMints
/// - Returns: ``Mint``
/// - Throws: A variation of ``ImmutableXError``
public func getMint(id: String) async throws -> Mint {
try await APIErrorMapper.map(caller: "Get Mint") {
try await self.mintsAPI.getMint(id: id)
}
}

/// Get a list of mints
///
/// - 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 mint (optional)
/// - status: Status of this mint (optional)
/// - minTimestamp: Minimum timestamp for this mint, in ISO 8601 UTC format.
/// Example: '2022-05-27T00:10:22Z' (optional)
/// - maxTimestamp: Maximum timestamp for this mint, in ISO 8601 UTC format.
/// Example: '2022-05-27T00:10:22Z' (optional)
/// - tokenType: Token type of the minted asset (optional)
/// - tokenId: ERC721 Token ID of the minted asset (optional)
/// - assetId: Internal IMX ID of the minted asset (optional)
/// - tokenName: Token Name of the minted asset (optional)
/// - tokenAddress: Token address of the minted asset (optional)
/// - minQuantity: Min quantity for the minted asset (optional)
/// - maxQuantity: Max quantity for the minted asset (optional)
/// - metadata: JSON-encoded metadata filters for the minted asset (optional)
/// - Returns: ``ListMintsResponse``
/// - Throws: A variation of ``ImmutableXError``
public func listMints(
pageSize: Int? = nil,
cursor: String? = nil,
orderBy: String? = nil,
direction: String? = nil,
user: String? = nil,
status: String? = nil,
minTimestamp: String? = nil,
maxTimestamp: String? = nil,
tokenType: String? = nil,
tokenId: String? = nil,
assetId: String? = nil,
tokenName: String? = nil,
tokenAddress: String? = nil,
minQuantity: String? = nil,
maxQuantity: String? = nil,
metadata: String? = nil
) async throws -> ListMintsResponse {
try await APIErrorMapper.map(caller: "List Mints") {
try await self.mintsAPI.listMints(
pageSize: pageSize,
cursor: cursor,
orderBy: orderBy,
direction: direction,
user: user,
status: status,
minTimestamp: minTimestamp,
maxTimestamp: maxTimestamp,
tokenType: tokenType,
tokenId: tokenId,
assetId: assetId,
tokenName: tokenName,
tokenAddress: tokenAddress,
minQuantity: minQuantity,
maxQuantity: maxQuantity,
metadata: metadata
)
}
}
}
45 changes: 44 additions & 1 deletion Tests/ImmutableXCoreTests/ImmutableXTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class ImmutableXTests: XCTestCase {
let collectionsAPIMock = CollectionsAPIMock.self
let projectsAPIMock = ProjectsAPIMock.self
let balancesAPIMock = BalancesAPIMock.self
let mintsAPIMock = MintsAPIMock.self

lazy var core = ImmutableX(
buyWorkflow: buyWorkflow,
Expand All @@ -27,7 +28,8 @@ final class ImmutableXTests: XCTestCase {
assetsAPI: assetsAPIMock,
collectionsAPI: collectionsAPIMock,
projectsAPI: projectsAPIMock,
balancesAPI: balancesAPIMock
balancesAPI: balancesAPIMock,
mintsAPI: mintsAPIMock
)

override func setUp() {
Expand All @@ -44,6 +46,7 @@ final class ImmutableXTests: XCTestCase {
collectionsAPIMock.resetMock()
projectsAPIMock.resetMock()
balancesAPIMock.resetMock()
mintsAPIMock.resetMock()

ImmutableX.initialize()

Expand Down Expand Up @@ -118,6 +121,14 @@ final class ImmutableXTests: XCTestCase {
let listBalancesCompanion = BalancesAPIMock.ListBalancesCompanion()
listBalancesCompanion.returnValue = listBalancesResponseStub1
balancesAPIMock.mock(listBalancesCompanion)

let getMintCompanion = MintsAPIMock.GetMintCompanion()
getMintCompanion.returnValue = mintStub1
mintsAPIMock.mock(getMintCompanion)

let listMintsCompanion = MintsAPIMock.ListMintsCompanion()
listMintsCompanion.returnValue = listMintsResponseStub1
mintsAPIMock.mock(listMintsCompanion)
}

func testSdkVersion() {
Expand Down Expand Up @@ -428,4 +439,36 @@ final class ImmutableXTests: XCTestCase {
_ = try await core.listBalances(owner: "owner")
}
}

// MARK: - Mint

func testGetMintSuccess() async throws {
let response = try await core.getMint(id: "id")
XCTAssertEqual(response, mintStub1)
}

func testGetMintFailure() async {
let companion = MintsAPIMock.GetMintCompanion()
companion.throwableError = DummyError.something
mintsAPIMock.mock(companion)

await XCTAssertThrowsErrorAsync {
_ = try await core.getMint(id: "id")
}
}

func testListMintsSuccess() async throws {
let response = try await core.listMints()
XCTAssertEqual(response, listMintsResponseStub1)
}

func testListMintsFailure() async {
let companion = MintsAPIMock.ListMintsCompanion()
companion.throwableError = DummyError.something
mintsAPIMock.mock(companion)

await XCTAssertThrowsErrorAsync {
_ = try await core.listMints()
}
}
}
71 changes: 71 additions & 0 deletions Tests/ImmutableXCoreTests/Mocks/API/MintsAPIMock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import Foundation
@testable import ImmutableXCore

final class MintsAPIMock: MintsAPI {
class GetMintCompanion {
var throwableError: Error?
var callsCount = 0
var returnValue: Mint!
}

class ListMintsCompanion {
var throwableError: Error?
var callsCount = 0
var returnValue: ListMintsResponse!
}

static var getMintCompanion: GetMintCompanion?
static var listMintsCompanion: ListMintsCompanion?

static func mock(_ companion: GetMintCompanion) {
getMintCompanion = companion
}

static func mock(_ companion: ListMintsCompanion) {
listMintsCompanion = companion
}

static func resetMock() {
getMintCompanion = nil
listMintsCompanion = nil
}

override class func getMint(id: String) async throws -> Mint {
let companion = getMintCompanion!
companion.callsCount += 1

if let error = companion.throwableError {
throw error
}

return companion.returnValue
}

override class func listMints(
pageSize: Int? = nil,
cursor: String? = nil,
orderBy: String? = nil,
direction: String? = nil,
user: String? = nil,
status: String? = nil,
minTimestamp: String? = nil,
maxTimestamp: String? = nil,
tokenType: String? = nil,
tokenId: String? = nil,
assetId: String? = nil,
tokenName: String? = nil,
tokenAddress: String? = nil,
minQuantity: String? = nil,
maxQuantity: String? = nil,
metadata: String? = nil
) async throws -> ListMintsResponse {
let companion = listMintsCompanion!
companion.callsCount += 1

if let error = companion.throwableError {
throw error
}

return companion.returnValue
}
}
14 changes: 14 additions & 0 deletions Tests/ImmutableXCoreTests/Mocks/Stubs/Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,17 @@ let listBalancesResponseStub1 = ListBalancesResponse(
cursor: "",
result: [balanceStub1]
)

let mintStub1 = Mint(
status: "status",
timestamp: "",
token: tokenETHStub1,
transactionId: 1,
user: "user"
)

let listMintsResponseStub1 = ListMintsResponse(
cursor: "",
remaining: 1,
result: [mintStub1]
)

0 comments on commit 91ee53e

Please sign in to comment.