Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoints for fetching ETF data #98

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Sources/MarketKit/Classes/Kit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,16 @@ public extension Kit {
)
}

// Etf

func etfs(currencyCode: String) async throws -> [Etf] {
try await hsProvider.etfs(currencyCode: currencyCode)
}

func etfPoints(currencyCode: String) async throws -> [EtfPoint] {
try await hsProvider.etfPoints(currencyCode: currencyCode)
}

// Pro Data

func analytics(coinUid: String, currencyCode: String) async throws -> Analytics {
Expand Down
49 changes: 49 additions & 0 deletions Sources/MarketKit/Classes/Models/Etf.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Foundation
import ObjectMapper

public struct Etf: ImmutableMappable {
public let ticker: String
public let name: String
public let date: Date?
public let totalAssets: Decimal?
public let totalInflow: Decimal?
public let inflows: [HsTimePeriod: Decimal]

public init(map: Map) throws {
ticker = try map.value("ticker")
name = try map.value("name")
date = try? map.value("date", using: Self.dateTransform)
totalAssets = try? map.value("total_assets", using: Transform.stringToDecimalTransform)
totalInflow = try? map.value("total_inflow", using: Transform.stringToDecimalTransform)

var inflows = [HsTimePeriod: Decimal]()

inflows[.day1] = try? map.value("inflow_1d", using: Transform.stringToDecimalTransform)
inflows[.week1] = try? map.value("inflow_1w", using: Transform.stringToDecimalTransform)
inflows[.month1] = try? map.value("inflow_1m", using: Transform.stringToDecimalTransform)
inflows[.month3] = try? map.value("inflow_3m", using: Transform.stringToDecimalTransform)

self.inflows = inflows
}
}

extension Etf {
private static let dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT")!
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
return dateFormatter
}()

static let dateTransform: TransformOf<Date, String> = TransformOf(
fromJSON: { string -> Date? in
guard let string else { return nil }
return dateFormatter.date(from: string)
},
toJSON: { (value: Date?) in
guard let value else { return nil }
return dateFormatter.string(from: value)
}
)
}
16 changes: 16 additions & 0 deletions Sources/MarketKit/Classes/Models/EtfPoint.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Foundation
import ObjectMapper

public struct EtfPoint: ImmutableMappable {
public let date: Date
public let totalAssets: Decimal
public let totalInflow: Decimal
public let dailyInflow: Decimal

public init(map: Map) throws {
date = try map.value("date", using: Etf.dateTransform)
totalAssets = try map.value("total_assets", using: Transform.stringToDecimalTransform)
totalInflow = try map.value("total_inflow", using: Transform.stringToDecimalTransform)
dailyInflow = try map.value("daily_inflow", using: Transform.stringToDecimalTransform)
}
}
18 changes: 18 additions & 0 deletions Sources/MarketKit/Classes/Providers/HsProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,24 @@ extension HsProvider {
return try await networkManager.fetch(url: "\(baseUrl)/v1/top-platforms/\(platform)/market_chart", method: .get, parameters: parameters, headers: headers)
}

// Etf

func etfs(currencyCode: String) async throws -> [Etf] {
let parameters: Parameters = [
"currency": currencyCode.lowercased(),
]

return try await networkManager.fetch(url: "\(baseUrl)/v1/etfs", method: .get, parameters: parameters, headers: headers)
}

func etfPoints(currencyCode: String) async throws -> [EtfPoint] {
let parameters: Parameters = [
"currency": currencyCode.lowercased(),
]

return try await networkManager.fetch(url: "\(baseUrl)/v1/etfs/total", method: .get, parameters: parameters, headers: headers)
}

// Pro Charts

private func proData<T: ImmutableMappable>(path: String, currencyCode: String, timePeriod: HsTimePeriod) async throws -> [T] {
Expand Down