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 required field for top coin market info endpoints #96

Merged
merged 1 commit into from
May 13, 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
5 changes: 5 additions & 0 deletions Sources/MarketKit/Classes/Kit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ public extension Kit {
return coinManager.marketInfos(rawMarketInfos: rawMarketInfos)
}

func topCoinsMarketInfos(top: Int, currencyCode: String) async throws -> [MarketInfo] {
let rawMarketInfos = try await hsProvider.topCoinsMarketInfos(top: top, currencyCode: currencyCode)
return coinManager.marketInfos(rawMarketInfos: rawMarketInfos)
}

func advancedMarketInfos(top: Int = 250, currencyCode: String) async throws -> [MarketInfo] {
let rawMarketInfos = try await hsProvider.advancedMarketInfos(top: top, currencyCode: currencyCode)
return coinManager.marketInfos(rawMarketInfos: rawMarketInfos)
Expand Down
1 change: 1 addition & 0 deletions Sources/MarketKit/Classes/Models/MarketInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public struct MarketInfo {
public let priceChange7d: Decimal?
public let priceChange14d: Decimal?
public let priceChange30d: Decimal?
public let priceChange90d: Decimal?
public let priceChange200d: Decimal?
public let priceChange1y: Decimal?
public let marketCap: Decimal?
Expand Down
3 changes: 3 additions & 0 deletions Sources/MarketKit/Classes/Models/MarketInfoRaw.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct MarketInfoRaw: ImmutableMappable {
let priceChange7d: Decimal?
let priceChange14d: Decimal?
let priceChange30d: Decimal?
let priceChange90d: Decimal?
let priceChange200d: Decimal?
let priceChange1y: Decimal?
let marketCap: Decimal?
Expand All @@ -28,6 +29,7 @@ struct MarketInfoRaw: ImmutableMappable {
priceChange7d = try? map.value("price_change_7d", using: Transform.stringToDecimalTransform)
priceChange14d = try? map.value("price_change_14d", using: Transform.stringToDecimalTransform)
priceChange30d = try? map.value("price_change_30d", using: Transform.stringToDecimalTransform)
priceChange90d = try? map.value("price_change_90d", using: Transform.stringToDecimalTransform)
priceChange200d = try? map.value("price_change_200d", using: Transform.stringToDecimalTransform)
priceChange1y = try? map.value("price_change_1y", using: Transform.stringToDecimalTransform)
marketCap = try? map.value("market_cap", using: Transform.stringToDecimalTransform)
Expand All @@ -54,6 +56,7 @@ struct MarketInfoRaw: ImmutableMappable {
priceChange7d: priceChange7d,
priceChange14d: priceChange14d,
priceChange30d: priceChange30d,
priceChange90d: priceChange90d,
priceChange200d: priceChange200d,
priceChange1y: priceChange1y,
marketCap: marketCap,
Expand Down
13 changes: 12 additions & 1 deletion Sources/MarketKit/Classes/Providers/HsProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ extension HsProvider {
return try await networkManager.fetch(url: "\(baseUrl)/v1/coins", method: .get, parameters: parameters, headers: headers)
}

func topCoinsMarketInfos(top: Int, currencyCode: String) async throws -> [MarketInfoRaw] {
let parameters: Parameters = [
"limit": top,
"fields": "price,price_change_24h,price_change_7d,price_change_30d,price_change_90d,market_cap,market_cap_rank,total_volume",
"currency": currencyCode.lowercased(),
"order_by_rank": "true",
]

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

func advancedMarketInfos(top: Int, currencyCode: String) async throws -> [MarketInfoRaw] {
let parameters: Parameters = [
"limit": top,
Expand All @@ -107,7 +118,7 @@ extension HsProvider {
func marketInfos(coinUids: [String], currencyCode: String) async throws -> [MarketInfoRaw] {
let parameters: Parameters = [
"uids": coinUids.joined(separator: ","),
"fields": "price,price_change_24h,price_change_7d,price_change_30d,market_cap,market_cap_rank,total_volume",
"fields": "price,price_change_24h,price_change_7d,price_change_30d,price_change_90d,market_cap,market_cap_rank,total_volume",
"currency": currencyCode.lowercased(),
]

Expand Down