Convert SatsPrice to a Skip multiplatform app
This commit is contained in:
48
Sources/SatsPrice/Network/CoinGeckoPriceFetcher.swift
Normal file
48
Sources/SatsPrice/Network/CoinGeckoPriceFetcher.swift
Normal file
@@ -0,0 +1,48 @@
|
||||
// This is free software: you can redistribute and/or modify it
|
||||
// under the terms of the GNU General Public License 3.0
|
||||
// as published by the Free Software Foundation https://fsf.org
|
||||
//
|
||||
// CoinGeckoPriceFetcher.swift
|
||||
// SatsPrice
|
||||
//
|
||||
// Created by Terry Yiu on 2/19/24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
private struct CoinGeckoPriceResponse: Codable {
|
||||
let bitcoin: CoinGeckoPrice
|
||||
}
|
||||
|
||||
private struct CoinGeckoPrice: Codable {
|
||||
#if !SKIP
|
||||
let usd: Decimal
|
||||
#else
|
||||
let usd: String
|
||||
#endif
|
||||
}
|
||||
|
||||
class CoinGeckoPriceFetcher : PriceFetcher {
|
||||
private static let urlString = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd&precision=18"
|
||||
|
||||
func btcToUsd() async throws -> Decimal? {
|
||||
do {
|
||||
guard let urlComponents = URLComponents(string: CoinGeckoPriceFetcher.urlString), let url = urlComponents.url else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let (data, _) = try await URLSession.shared.data(from: url, delegate: nil)
|
||||
|
||||
let priceResponse = try JSONDecoder().decode(CoinGeckoPriceResponse.self, from: data)
|
||||
let price = priceResponse.bitcoin
|
||||
|
||||
#if !SKIP
|
||||
return price.usd
|
||||
#else
|
||||
return Decimal(price.usd)
|
||||
#endif
|
||||
} catch {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
52
Sources/SatsPrice/Network/CoinbasePriceFetcher.swift
Normal file
52
Sources/SatsPrice/Network/CoinbasePriceFetcher.swift
Normal file
@@ -0,0 +1,52 @@
|
||||
// This is free software: you can redistribute and/or modify it
|
||||
// under the terms of the GNU General Public License 3.0
|
||||
// as published by the Free Software Foundation https://fsf.org
|
||||
//
|
||||
// CoinbasePriceFetcher.swift
|
||||
// SatsPrice
|
||||
//
|
||||
// Created by Terry Yiu on 2/19/24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
private struct CoinbasePriceResponse: Codable {
|
||||
let data: CoinbasePrice
|
||||
}
|
||||
|
||||
private struct CoinbasePrice: Codable {
|
||||
let amount: String
|
||||
let base: String
|
||||
let currency: String
|
||||
}
|
||||
|
||||
class CoinbasePriceFetcher : PriceFetcher {
|
||||
private static let urlString = "https://api.coinbase.com/v2/prices/BTC-USD/spot"
|
||||
private static let btc = "BTC"
|
||||
private static let usd = "USD"
|
||||
|
||||
func btcToUsd() async throws -> Decimal? {
|
||||
do {
|
||||
guard let urlComponents = URLComponents(string: CoinbasePriceFetcher.urlString), let url = urlComponents.url else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let (data, _) = try await URLSession.shared.data(from: url, delegate: nil)
|
||||
|
||||
let coinbasePriceResponse = try JSONDecoder().decode(CoinbasePriceResponse.self, from: data)
|
||||
let coinbasePrice = coinbasePriceResponse.data
|
||||
|
||||
guard coinbasePrice.base == CoinbasePriceFetcher.btc && coinbasePrice.currency == CoinbasePriceFetcher.usd else {
|
||||
return nil
|
||||
}
|
||||
|
||||
#if !SKIP
|
||||
return Decimal(string: coinbasePrice.amount)
|
||||
#else
|
||||
return Decimal(coinbasePrice.amount)
|
||||
#endif
|
||||
} catch {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Sources/SatsPrice/Network/FakePriceFetcher.swift
Normal file
20
Sources/SatsPrice/Network/FakePriceFetcher.swift
Normal file
@@ -0,0 +1,20 @@
|
||||
// This is free software: you can redistribute and/or modify it
|
||||
// under the terms of the GNU General Public License 3.0
|
||||
// as published by the Free Software Foundation https://fsf.org
|
||||
//
|
||||
// FakePriceFetcher.swift
|
||||
// SatsPrice
|
||||
//
|
||||
// Created by Terry Yiu on 2/21/24.
|
||||
//
|
||||
|
||||
#if DEBUG
|
||||
import Foundation
|
||||
|
||||
/// Fake price fetcher that returns a randomized price. Useful for development testing without requiring a network call.
|
||||
class FakePriceFetcher: PriceFetcher {
|
||||
func btcToUsd() async throws -> Decimal? {
|
||||
Decimal(Double.random(in: 10000...100000))
|
||||
}
|
||||
}
|
||||
#endif
|
||||
20
Sources/SatsPrice/Network/ManualPriceFetcher.swift
Normal file
20
Sources/SatsPrice/Network/ManualPriceFetcher.swift
Normal file
@@ -0,0 +1,20 @@
|
||||
// This is free software: you can redistribute and/or modify it
|
||||
// under the terms of the GNU General Public License 3.0
|
||||
// as published by the Free Software Foundation https://fsf.org
|
||||
//
|
||||
// ManualPriceFetcher.swift
|
||||
// SatsPrice
|
||||
//
|
||||
// Created by Terry Yiu on 8/29/24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
/// Fake price fetcher that returns a randomized price. Useful for development testing without requiring a network call.
|
||||
class ManualPriceFetcher: PriceFetcher {
|
||||
var price: Decimal = Decimal(1)
|
||||
|
||||
func btcToUsd() async throws -> Decimal? {
|
||||
return price
|
||||
}
|
||||
}
|
||||
15
Sources/SatsPrice/Network/PriceFetcher.swift
Normal file
15
Sources/SatsPrice/Network/PriceFetcher.swift
Normal file
@@ -0,0 +1,15 @@
|
||||
// This is free software: you can redistribute and/or modify it
|
||||
// under the terms of the GNU General Public License 3.0
|
||||
// as published by the Free Software Foundation https://fsf.org
|
||||
//
|
||||
// PriceFetcher.swift
|
||||
// SatsPrice
|
||||
//
|
||||
// Created by Terry Yiu on 2/19/24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol PriceFetcher {
|
||||
func btcToUsd() async throws -> Decimal?
|
||||
}
|
||||
45
Sources/SatsPrice/Network/PriceFetcherDelegator.swift
Normal file
45
Sources/SatsPrice/Network/PriceFetcherDelegator.swift
Normal file
@@ -0,0 +1,45 @@
|
||||
// This is free software: you can redistribute and/or modify it
|
||||
// under the terms of the GNU General Public License 3.0
|
||||
// as published by the Free Software Foundation https://fsf.org
|
||||
//
|
||||
// PriceFetcherDelegator.swift
|
||||
// SatsPrice
|
||||
//
|
||||
// Created by Terry Yiu on 2/20/24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class PriceFetcherDelegator: PriceFetcher {
|
||||
private let coinbasePriceFetcher = CoinbasePriceFetcher()
|
||||
private let coinGeckoPriceFetcher = CoinGeckoPriceFetcher()
|
||||
private let manualPriceFetcher = ManualPriceFetcher()
|
||||
#if DEBUG
|
||||
private let fakePriceFetcher = FakePriceFetcher()
|
||||
#endif
|
||||
|
||||
var priceSource: PriceSource
|
||||
|
||||
init(_ priceSource: PriceSource) {
|
||||
self.priceSource = priceSource
|
||||
}
|
||||
|
||||
private var delegate: PriceFetcher {
|
||||
switch priceSource {
|
||||
case .coinbase:
|
||||
coinbasePriceFetcher
|
||||
case .coingecko:
|
||||
coinGeckoPriceFetcher
|
||||
case .manual:
|
||||
manualPriceFetcher
|
||||
#if DEBUG
|
||||
case .fake:
|
||||
fakePriceFetcher
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
func btcToUsd() async throws -> Decimal? {
|
||||
return try await delegate.btcToUsd()
|
||||
}
|
||||
}
|
||||
45
Sources/SatsPrice/Network/PriceSource.swift
Normal file
45
Sources/SatsPrice/Network/PriceSource.swift
Normal file
@@ -0,0 +1,45 @@
|
||||
// This is free software: you can redistribute and/or modify it
|
||||
// under the terms of the GNU General Public License 3.0
|
||||
// as published by the Free Software Foundation https://fsf.org
|
||||
//
|
||||
// PriceSource.swift
|
||||
// SatsPrice
|
||||
//
|
||||
// Created by Terry Yiu on 2/20/24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
enum PriceSource: CaseIterable, CustomStringConvertible {
|
||||
|
||||
static var allCases: [PriceSource] {
|
||||
#if DEBUG
|
||||
[.coinbase, .coingecko, .manual, .fake]
|
||||
#else
|
||||
[.coinbase, .coingecko, .manual]
|
||||
#endif
|
||||
}
|
||||
|
||||
case coinbase
|
||||
case coingecko
|
||||
case manual
|
||||
|
||||
#if DEBUG
|
||||
case fake
|
||||
#endif
|
||||
|
||||
var description: String {
|
||||
switch self {
|
||||
case .coinbase:
|
||||
"Coinbase"
|
||||
case .coingecko:
|
||||
"CoinGecko"
|
||||
case .manual:
|
||||
"Manual"
|
||||
#if DEBUG
|
||||
case .fake:
|
||||
"Fake"
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user