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

Commit

Permalink
feat: expose getUser method
Browse files Browse the repository at this point in the history
Get User is now part of the ImmutableX interface.
  • Loading branch information
CassiusPacheco committed Nov 9, 2022
1 parent 0d182ec commit aa05b7f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

for added features.
- `getUser(ethAddress:)` has been added to ImmutableX instance.

### Changed

Expand Down
16 changes: 15 additions & 1 deletion Sources/ImmutableXCore/ImmutableX.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public struct ImmutableX {
private let transferWorkflow: TransferWorkflow.Type
private let registerWorkflow: RegisterWorkflow.Type
private let buyCryptoWorkflow: BuyCryptoWorkflow.Type
private let usersAPI: UsersAPI.Type

/// Internal init method that includes dependencies. For the public facing API use ``initialize(base:logLevel:)``
/// instead.
Expand All @@ -45,7 +46,8 @@ public struct ImmutableX {
cancelOrderWorkflow: CancelOrderWorkflow.Type = CancelOrderWorkflow.self,
transferWorkflow: TransferWorkflow.Type = TransferWorkflow.self,
registerWorkflow: RegisterWorkflow.Type = RegisterWorkflow.self,
buyCryptoWorkflow: BuyCryptoWorkflow.Type = BuyCryptoWorkflow.self
buyCryptoWorkflow: BuyCryptoWorkflow.Type = BuyCryptoWorkflow.self,
usersAPI: UsersAPI.Type = UsersAPI.self
) {
self.base = base
self.logLevel = logLevel
Expand All @@ -55,6 +57,7 @@ public struct ImmutableX {
self.transferWorkflow = transferWorkflow
self.registerWorkflow = registerWorkflow
self.buyCryptoWorkflow = buyCryptoWorkflow
self.usersAPI = usersAPI
}

/// Initializes the SDK with the given ``base`` and ``logLevel`` by assigning a shared instance accessible via
Expand Down Expand Up @@ -157,6 +160,17 @@ public struct ImmutableX {
_ = try await registerWorkflow.registerOffchain(signer: signer, starkSigner: starkSigner)
}

/// Get stark keys for a registered user
///
/// - Parameter ethAddress: User ETH address
/// - Returns: ``GetUsersApiResponse``
/// - Throws: A variation of ``ImmutableXError``
public func getUser(ethAddress: String) async throws -> GetUsersApiResponse {
try await APIErrorMapper.map(caller: "Get Users") {
try await self.usersAPI.getUsers(user: ethAddress)
}
}

/// Gets a URL to MoonPay that provides a service for buying crypto directly on Immutable in exchange for fiat.
///
/// - Parameters:
Expand Down
35 changes: 33 additions & 2 deletions Tests/ImmutableXCoreTests/ImmutableXTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@ final class ImmutableXTests: XCTestCase {
let transferWorkflowMock = TransferWorkflowMock.self
let registerWorkflowMock = RegisterWorkflowMock.self
let buyCryptoWorkflowMock = BuyCryptoWorkflowMock.self

lazy var core = ImmutableX(buyWorkflow: buyWorkflow, sellWorkflow: sellWorkflow, cancelOrderWorkflow: cancelOrderWorkflow, transferWorkflow: transferWorkflowMock, registerWorkflow: registerWorkflowMock, buyCryptoWorkflow: buyCryptoWorkflowMock)
let usersAPIMock = UsersAPIMock.self

lazy var core = ImmutableX(
buyWorkflow: buyWorkflow,
sellWorkflow: sellWorkflow,
cancelOrderWorkflow: cancelOrderWorkflow,
transferWorkflow: transferWorkflowMock,
registerWorkflow: registerWorkflowMock,
buyCryptoWorkflow: buyCryptoWorkflowMock,
usersAPI: usersAPIMock
)

override func setUp() {
super.setUp()
Expand All @@ -19,6 +28,7 @@ final class ImmutableXTests: XCTestCase {
transferWorkflowMock.resetMock()
registerWorkflowMock.resetMock()
buyCryptoWorkflowMock.resetMock()
usersAPIMock.resetMock()
ImmutableX.initialize()

let buyCompanion = BuyWorkflowCompanion()
Expand All @@ -44,6 +54,10 @@ final class ImmutableXTests: XCTestCase {
let buyCryptoCompanion = BuyCryptoWorkflowCompanion()
buyCryptoCompanion.returnValue = "expected url"
buyCryptoWorkflowMock.mock(buyCryptoCompanion)

let usersCompanion = UsersAPIMockGetUsersCompanion()
usersCompanion.returnValue = GetUsersApiResponse(accounts: ["some key"])
usersAPIMock.mock(usersCompanion)
}

func testSdkVersion() {
Expand Down Expand Up @@ -162,4 +176,21 @@ final class ImmutableXTests: XCTestCase {
_ = try await self.core.buyCryptoURL(signer: SignerMock())
}
}

// MARK: - Get User

func testGetUserSuccess() async throws {
let response = try await core.getUser(ethAddress: "address")
XCTAssertEqual(response, GetUsersApiResponse(accounts: ["some key"]))
}

func testGetUserFailure() async {
let usersCompanion = UsersAPIMockGetUsersCompanion()
usersCompanion.throwableError = DummyError.something
usersAPIMock.mock(usersCompanion)

await XCTAssertThrowsErrorAsync {
_ = try await core.getUser(ethAddress: "address")
}
}
}

0 comments on commit aa05b7f

Please sign in to comment.