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

Commit

Permalink
feat: expose withdrawal api methods
Browse files Browse the repository at this point in the history
- Gets details of withdrawal with the given ID
- Get a list of withdrawals
  • Loading branch information
CassiusPacheco committed Nov 15, 2022
1 parent 91ee53e commit d13e62b
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 2 deletions.
85 changes: 84 additions & 1 deletion Sources/ImmutableXCore/ImmutableX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public struct ImmutableX {
private let projectsAPI: ProjectsAPI.Type
private let balancesAPI: BalancesAPI.Type
private let mintsAPI: MintsAPI.Type
private let withdrawalAPI: WithdrawalsAPI.Type

/// Internal init method that includes dependencies. For the public facing API use ``initialize(base:logLevel:)``
/// instead.
Expand All @@ -61,7 +62,8 @@ public struct ImmutableX {
collectionsAPI: CollectionsAPI.Type = CollectionsAPI.self,
projectsAPI: ProjectsAPI.Type = ProjectsAPI.self,
balancesAPI: BalancesAPI.Type = BalancesAPI.self,
mintsAPI: MintsAPI.Type = MintsAPI.self
mintsAPI: MintsAPI.Type = MintsAPI.self,
withdrawalAPI: WithdrawalsAPI.Type = WithdrawalsAPI.self
) {
self.base = base
self.logLevel = logLevel
Expand All @@ -78,6 +80,7 @@ public struct ImmutableX {
self.projectsAPI = projectsAPI
self.balancesAPI = balancesAPI
self.mintsAPI = mintsAPI
self.withdrawalAPI = withdrawalAPI
}

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

/// Gets details of withdrawal with the given ID
///
/// - Parameter id: Withdrawal ID
/// - Returns: ``Withdrawal``
/// - Throws: A variation of ``ImmutableXError``
public func getWithdrawal(id: String) async throws -> Withdrawal {
try await APIErrorMapper.map(caller: "Get Withdrawal") {
try await self.withdrawalAPI.getWithdrawal(id: id)
}
}

/// Get a list of withdrawals
///
/// - Parameters:
/// - withdrawnToWallet: Withdrawal has been transferred to user's Layer 1 wallet (optional)
/// - rollupStatus: Status of the on-chain batch confirmation for this withdrawal (optional)
/// - 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 withdrawal (optional)
/// - status: Status of this withdrawal (optional)
/// - minTimestamp: Minimum timestamp for this deposit, in ISO 8601 UTC format.
/// Example: '2022-05-27T00:10:22Z' (optional)
/// - maxTimestamp: Maximum timestamp for this deposit, in ISO 8601 UTC format.
/// Example: '2022-05-27T00:10:22Z' (optional)
/// - tokenType: Token type of the withdrawn asset (optional)
/// - tokenId: ERC721 Token ID of the minted asset (optional)
/// - assetId: Internal IMX ID of the minted asset (optional)
/// - tokenAddress: Token address of the withdrawn asset (optional)
/// - tokenName: Token name of the withdrawn asset (optional)
/// - minQuantity: Min quantity for the withdrawn asset (optional)
/// - maxQuantity: Max quantity for the withdrawn asset (optional)
/// - metadata: JSON-encoded metadata filters for the withdrawn asset (optional)
/// - Returns: ``ListWithdrawalsResponse``
/// - Throws: A variation of ``ImmutableXError``
public func listWithdrawals(
withdrawnToWallet: Bool? = nil,
rollupStatus: String? = nil,
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,
tokenAddress: String? = nil,
tokenName: String? = nil,
minQuantity: String? = nil,
maxQuantity: String? = nil,
metadata: String? = nil
) async throws -> ListWithdrawalsResponse {
try await APIErrorMapper.map(caller: "List Withdrawals") {
try await self.withdrawalAPI.listWithdrawals(
withdrawnToWallet: withdrawnToWallet,
rollupStatus: rollupStatus,
pageSize: pageSize,
cursor: cursor,
orderBy: orderBy,
direction: direction,
user: user,
status: status,
minTimestamp: minTimestamp,
maxTimestamp: maxTimestamp,
tokenType: tokenType,
tokenId: tokenId,
assetId: assetId,
tokenAddress: tokenAddress,
tokenName: tokenName,
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 @@ -15,6 +15,7 @@ final class ImmutableXTests: XCTestCase {
let projectsAPIMock = ProjectsAPIMock.self
let balancesAPIMock = BalancesAPIMock.self
let mintsAPIMock = MintsAPIMock.self
let withdrawalsAPIMock = WithdrawalsAPIMock.self

lazy var core = ImmutableX(
buyWorkflow: buyWorkflow,
Expand All @@ -29,7 +30,8 @@ final class ImmutableXTests: XCTestCase {
collectionsAPI: collectionsAPIMock,
projectsAPI: projectsAPIMock,
balancesAPI: balancesAPIMock,
mintsAPI: mintsAPIMock
mintsAPI: mintsAPIMock,
withdrawalAPI: withdrawalsAPIMock
)

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

ImmutableX.initialize()

Expand Down Expand Up @@ -129,6 +132,14 @@ final class ImmutableXTests: XCTestCase {
let listMintsCompanion = MintsAPIMock.ListMintsCompanion()
listMintsCompanion.returnValue = listMintsResponseStub1
mintsAPIMock.mock(listMintsCompanion)

let getWithdrawalCompanion = WithdrawalsAPIMock.GetWithdrawalCompanion()
getWithdrawalCompanion.returnValue = withdrawalStub1
withdrawalsAPIMock.mock(getWithdrawalCompanion)

let listWithdrawalsCompanion = WithdrawalsAPIMock.ListWithdrawalsCompanion()
listWithdrawalsCompanion.returnValue = listWithdrawalsResponseStub1
withdrawalsAPIMock.mock(listWithdrawalsCompanion)
}

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

// MARK: - Withdrawal

func testGetWithdrawalSuccess() async throws {
let response = try await core.getWithdrawal(id: "id")
XCTAssertEqual(response, withdrawalStub1)
}

func testGetWithdrawalFailure() async {
let companion = WithdrawalsAPIMock.GetWithdrawalCompanion()
companion.throwableError = DummyError.something
withdrawalsAPIMock.mock(companion)

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

func testListWithdrawalsSuccess() async throws {
let response = try await core.listWithdrawals()
XCTAssertEqual(response, listWithdrawalsResponseStub1)
}

func testListWithdrawalsFailure() async {
let companion = WithdrawalsAPIMock.ListWithdrawalsCompanion()
companion.throwableError = DummyError.something
withdrawalsAPIMock.mock(companion)

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

final class WithdrawalsAPIMock: WithdrawalsAPI {
class GetWithdrawalCompanion {
var throwableError: Error?
var callsCount = 0
var returnValue: Withdrawal!
}

class ListWithdrawalsCompanion {
var throwableError: Error?
var callsCount = 0
var returnValue: ListWithdrawalsResponse!
}

static var getWithdrawalCompanion: GetWithdrawalCompanion?
static var listWithdrawalsCompanion: ListWithdrawalsCompanion?

static func mock(_ companion: GetWithdrawalCompanion) {
getWithdrawalCompanion = companion
}

static func mock(_ companion: ListWithdrawalsCompanion) {
listWithdrawalsCompanion = companion
}

static func resetMock() {
getWithdrawalCompanion = nil
listWithdrawalsCompanion = nil
}

override class func getWithdrawal(id: String) async throws -> Withdrawal {
let companion = getWithdrawalCompanion!
companion.callsCount += 1

if let error = companion.throwableError {
throw error
}

return companion.returnValue
}

override class func listWithdrawals(
withdrawnToWallet: Bool? = nil,
rollupStatus: String? = nil,
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,
tokenAddress: String? = nil,
tokenName: String? = nil,
minQuantity: String? = nil,
maxQuantity: String? = nil,
metadata: String? = nil
) async throws -> ListWithdrawalsResponse {
let companion = listWithdrawalsCompanion!
companion.callsCount += 1

if let error = companion.throwableError {
throw error
}

return companion.returnValue
}
}
16 changes: 16 additions & 0 deletions Tests/ImmutableXCoreTests/Mocks/Stubs/Models.swift
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,19 @@ let listMintsResponseStub1 = ListMintsResponse(
remaining: 1,
result: [mintStub1]
)

let withdrawalStub1 = Withdrawal(
rollupStatus: "status",
sender: "sender",
status: "status",
timestamp: "",
token: tokenETHStub1,
transactionId: 1,
withdrawnToWallet: false
)

let listWithdrawalsResponseStub1 = ListWithdrawalsResponse(
cursor: "",
remaining: 0,
result: [withdrawalStub1]
)

0 comments on commit d13e62b

Please sign in to comment.