// // 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 UsernamesEnvironment: String { case production = "malinka.prod" case preprod = "malinka.preprod" case devFirst = "malinka.dev-1" case devSecond = "malinka.dev-2" } enum BackendEnvironment: String { case production = "malinka.prod" case preprod = "malinka.preprod" case devFirst = "malinka.dev-1" case devSecond = "malinka.dev-2" case paycashProduction = "paycash.prod" case paycashPreprod = "paycash.preprod" } enum OtherEnvironment: String { case production = "paycash.prod" case preprod = "paycash.preprod" } enum SmartsEnvironment: String, CaseIterable { case production = "malinka.prod" case preprod = "malinka.preprod" case paycashProduction = "paycash.prod" case paycashPreprod = "paycash.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.clearApiEnvironment() } let usernamesEnv = ApplicationSettings.apiUsernameEnvironment.orTypedCreate(UsernamesEnvironment.production) let otherEnv = ApplicationSettings.otherEnvironment.orTypedCreate(OtherEnvironment.production) let backendEnv = ApplicationSettings.apiBackendEnvironment.orTypedCreate(BackendEnvironment.production) let smartsEnv = ApplicationSettings.smartsEnvironment.orTypedCreate(SmartsEnvironment.production) let firebaseTokenLifetime = ApplicationSettings.firebaseTokenLifetime.orTypedCreate(ApplicationEnvironmentRecord.FirebaseLifetime.month) self.current = ApplicationEnvironmentRecord(usernames: Self.usernamesRecords(for: usernamesEnv), backend: Self.backendRecords(for: backendEnv), headers: Self.headers, smarts: Self.smartsRecords(for: smartsEnv), other: Self.otherRecords(for: otherEnv), nodes: Self.eosNodes(), hyperions: [], firebaseTokenLifetime: firebaseTokenLifetime) } // MARK: - Private Static private static func usernamesRecords(for envorinment: UsernamesEnvironment) -> ApplicationEnvironmentRecord.Usernames { switch envorinment { case .production: return .init(name: UsernamesEnvironment.production.rawValue, urlPath: "https://api.malinka.life") case .preprod: return .init(name: UsernamesEnvironment.preprod.rawValue, urlPath: "https://api.preprod.malinka.life") case .devFirst: return .init(name: UsernamesEnvironment.devFirst.rawValue, urlPath: "https://api.dev-1.malinka.life") case .devSecond: return .init(name: UsernamesEnvironment.devSecond.rawValue, urlPath: "https://api.dev-2.malinka.life") } } private static func backendRecords(for environment: BackendEnvironment) -> ApplicationEnvironmentRecord.Backend { switch environment { case .production: return .init(name: BackendEnvironment.production.rawValue, urlPath: "https://api.malinka.life", errorUrlPath: "https://api.malinka.life", webSocket: "wss://api.malinka.life/ws") case .preprod: return .init(name: BackendEnvironment.preprod.rawValue, urlPath: "https://api.preprod.malinka.life", errorUrlPath: "https://api.preprod.malinka.life", webSocket: "wss://api.preprod.malinka.life/ws") case .paycashProduction: return .init(name: BackendEnvironment.paycashProduction.rawValue, urlPath: "https://api.paycash.online", errorUrlPath: "https://api.test.paycash.online", webSocket: "wss://api.paycash.online/ws" ) case .paycashPreprod: return .init(name: BackendEnvironment.paycashPreprod.rawValue, urlPath: "https://api.test.paycash.online", errorUrlPath: "https://api.test.paycash.online", webSocket: "wss://api.test.paycash.online/ws") case .devFirst: return .init(name: BackendEnvironment.devFirst.rawValue, urlPath: "https://api.dev-1.malinka.life", errorUrlPath: "https://api.dev-1.malinka.life", webSocket: "wss://api.dev-1.malinka.life/ws") case .devSecond: return .init(name: BackendEnvironment.devSecond.rawValue, urlPath: "https://api.dev-2.malinka.life", errorUrlPath: "https://api.dev-2.malinka.life", webSocket: "wss://api.dev-2.malinka.life/ws") } } private static func otherRecords(for environment: OtherEnvironment) -> ApplicationEnvironmentRecord.Other { switch environment { case .production: return .init(name: OtherEnvironment.production.rawValue, listSite: "https://list.family/search", listGraphQL: "https://api.list.family", paycashSwap: "https://api.paycashswap.com", paycashSwapSite: "https://paycashswap.com") case .preprod: return .init(name: OtherEnvironment.preprod.rawValue, listSite: "https://test.list.family/search", listGraphQL: "https://api.test.list.family", paycashSwap: "https://api.dev.paycashswap.com", paycashSwapSite: "https://paycashswap.com") } } private static func smartsRecords(for environment: SmartsEnvironment) -> ApplicationEnvironmentRecord.SmartContracts { switch environment { case .production: return .init(name: SmartsEnvironment.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", .malinkaReceiver: "malinkawallt", .usdt: "tethertether" ]) case .preprod: return .init(name: SmartsEnvironment.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", .malinkaReceiver: "marlin.pcash", .usdt: "tethertether" ]) case .paycashProduction: return .init(name: SmartsEnvironment.paycashProduction.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", .malinkaReceiver: "pcash", .usdt: "tethertether" ]) case .paycashPreprod: return .init(name: SmartsEnvironment.paycashPreprod.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", .malinkaReceiver: "pcash", .usdt: "aabw2r.pcash" ]) } } private static func eosNodes() -> [ApplicationEnvironmentRecord.UrlPath] { [ "https://eos.greymass.com", "https://eos.list.family", "https://eos.paycash.online" ] } private static let headers = ["User-Agent": Bundle.main.userAgent] static func contracts() -> [String] { let values = SmartsEnvironment.allCases .map { Self.smartsRecords(for: $0) } .map { $0.contracts } var result = Set() values.forEach { record in record.forEach { _, value in result.insert(value) } } return result.sorted() } }