Files
raspberry/iOS/Wallet/Sources/Application/ApplicationEnvironment.swift
T

221 lines
8.3 KiB
Swift

//
// ApplicationEnvironment.swift
// Malinka
//
// Created by Juraldinio on 10/23/22.
// Copyright © 2022 NUT.Tech. All rights reserved.
//
import Foundation
import WalletFoundation
final class ApplicationEnvironment {
enum PayCashEnvironment: String {
case production = "paycash.prod"
case preprod = "paycash.preprod"
}
enum MalinkaEnvironment: String {
case production = "malinka.prod"
case preprod = "malinka.preprod"
}
enum SettingsBundle: String {
case fileName = "Settings"
case fileExtension = "bundle"
}
private static var instance: ApplicationEnvironment?
static func shared() -> ApplicationEnvironment {
if let instance = Self.instance {
return instance
}
let instance = ApplicationEnvironment()
Self.instance = instance
return instance
}
// MARK: - Properties
private(set) var current: ApplicationEnvironmentRecord
// MARK: - Methods
// MARK: - Private
private init() {
if !Bundle.main.url(forResource: SettingsBundle.fileName.rawValue, withExtension: SettingsBundle.fileExtension.rawValue).isExist {
ApplicationSettings.clearApiEmvironment()
}
let usernamesEnv = ApplicationSettings.apiUsernameEnvironment.orTypedCreate(MalinkaEnvironment.production).rawValue
let otherEnv = ApplicationSettings.otherEnvironment.orTypedCreate(PayCashEnvironment.production).rawValue
let backendRaw = ApplicationSettings.apiBackendEnvironment
let backendEnv: String
if let backend = backendRaw {
backendEnv = MalinkaEnvironment(rawValue: backend)?.rawValue
?? PayCashEnvironment(rawValue: backend)?.rawValue
?? PayCashEnvironment.production.rawValue
} else {
backendEnv = PayCashEnvironment.production.rawValue
}
let smartsRaw = ApplicationSettings.smartsEnvironment
let smartsEnv: String
if let smarts = smartsRaw {
smartsEnv = MalinkaEnvironment(rawValue: smarts)?.rawValue
?? PayCashEnvironment(rawValue: smarts)?.rawValue
?? PayCashEnvironment.production.rawValue
} else {
smartsEnv = PayCashEnvironment.production.rawValue
}
guard let usernames = Self.usernamesRecords().first(where: { $0.name == usernamesEnv }),
let backend = Self.backendRecords().first(where: { $0.name == backendEnv }),
let other = Self.otherRecords().first(where: { $0.name == otherEnv }),
let smarts = Self.smartsRecords().first(where: { $0.name == smartsEnv }) else {
fatalError("Environment not found")
}
self.current = ApplicationEnvironmentRecord(usernames: usernames,
backend: backend,
smarts: smarts,
other: other,
nodes: Self.eosNodes(),
hyperions: [])
}
// MARK: - Private Static
private static func usernamesRecords() -> [ApplicationEnvironmentRecord.Usernames] {
[.init(name: MalinkaEnvironment.production.rawValue,
urlPath: "https://api.malinka.life",
headers: ["User-Agent": Bundle.main.userAgent]),
.init(name: MalinkaEnvironment.preprod.rawValue,
urlPath: "https://api.preprod.malinka.life",
headers: ["User-Agent": Bundle.main.userAgent])
]
}
private static func backendRecords() -> [ApplicationEnvironmentRecord.Backend] {
[.init(name: MalinkaEnvironment.production.rawValue,
urlPath: "https://api.malinka.life",
headers: ["User-Agent": Bundle.main.userAgent]),
.init(name: MalinkaEnvironment.preprod.rawValue,
urlPath: "https://api.preprod.malinka.life",
headers: ["User-Agent": Bundle.main.userAgent]),
.init(name: PayCashEnvironment.production.rawValue,
urlPath: "https://api.paycash.online",
headers: [:]),
.init(name: PayCashEnvironment.preprod.rawValue,
urlPath: "https://api.test.paycash.online",
headers: [:])
]
}
private static func otherRecords() -> [ApplicationEnvironmentRecord.Other] {
[.init(name: PayCashEnvironment.production.rawValue,
listSite: "https://list.family/search",
listGraphQL: "https://api.list.family",
paycashSwap: "https://api.paycashswap.com",
paycashSwapSite: "https://paycashswap.com",
webSocket: "wss://api.paycash.online/ws"),
.init(name: PayCashEnvironment.preprod.rawValue,
listSite: "https://test.list.family/search",
listGraphQL: "https://api.test.list.family",
paycashSwap: "https://api.dev.paycashswap.com",
paycashSwapSite: "https://paycashswap.com",
webSocket: "wss://api.test.paycash.online/ws")
]
}
private static func smartsRecords() -> [ApplicationEnvironmentRecord.SmartContracts] {
[
.init(name: MalinkaEnvironment.production.rawValue, contracts: [
.list: "token.list",
.cash: "token.pcash",
.eosioToken: "eosio.token",
.eosio: "eosio",
.chat: "mechatmechat",
.p2p: "p2p.pcash",
.freeCPU: "malinkfreetx",
.appSettings: "appsettings1", // "eidjk2adsmmu",
.swap: "swap.pcash",
.mlnk: "pcash.mlnk"
]),
.init(name: MalinkaEnvironment.preprod.rawValue, contracts: [
.list: "jqpua4jqkqwz",
.cash: "cashescashes",
.eosioToken: "eosio.token",
.eosio: "eosio",
.chat: "mechatmechat",
.p2p: "aabw2r.pcash",
.freeCPU: "malinkfreetx",
.appSettings: "appsettings1", // "i4vkf1c32lkp",
.swap: "testnewswap1",
.mlnk: "rpspakdrh3eb"
]),
.init(name: PayCashEnvironment.production.rawValue, contracts: [
.list: "token.list",
.cash: "token.pcash",
.eosioToken: "eosio.token",
.eosio: "eosio",
.chat: "mechatmechat",
.p2p: "p2p.pcash",
.freeCPU: "freetx.pcash",
.appSettings: "appsettings1", // "eidjk2adsmmu",
.swap: "swap.pcash",
.mlnk: "pcash.mlnk"
]),
.init(name: PayCashEnvironment.preprod.rawValue, contracts: [
.list: "jqpua4jqkqwz",
.cash: "cashescashes",
.eosioToken: "eosio.token",
.eosio: "eosio",
.chat: "mechatmechat",
.p2p: "aabw2r.pcash",
.freeCPU: "yp2acwnqldng",
.appSettings: "appsettings1", // "i4vkf1c32lkp",
.swap: "testnewswap1",
.mlnk: "rpspakdrh3eb"
]),
]
}
private static func eosNodes() -> [ApplicationEnvironmentRecord.UrlPath] {
[
"https://eos.greymass.com",
"https://eos.list.family",
"https://eos.paycash.online"
]
}
static func contracts() -> [String] {
var contracts = [String]()
let smarts = Self.smartsRecords()
smarts.first(where: { $0.name == MalinkaEnvironment.production.rawValue }) >>- {
contracts.append(contentsOf: $0.contracts.values)
}
smarts.first(where: { $0.name == MalinkaEnvironment.preprod.rawValue }) >>- {
contracts.append(contentsOf: $0.contracts.values)
}
smarts.first(where: { $0.name == PayCashEnvironment.production.rawValue }) >>- {
contracts.append(contentsOf: $0.contracts.values)
}
smarts.first(where: { $0.name == PayCashEnvironment.preprod.rawValue }) >>- {
contracts.append(contentsOf: $0.contracts.values)
}
return contracts
}
}