110 lines
3.4 KiB
Swift
110 lines
3.4 KiB
Swift
//
|
|
// ApplicationEnvironmentRecord.swift
|
|
// Malinka
|
|
//
|
|
// Created by Juraldinio on 10/23/22.
|
|
// Copyright © 2022 NUT.Tech. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import WalletFoundation
|
|
import WalletNetwork
|
|
|
|
final class ApplicationEnvironmentRecord {
|
|
|
|
typealias UrlPath = String
|
|
typealias Headers = [String: String]
|
|
|
|
struct Usernames {
|
|
let name: String
|
|
let urlPath: UrlPath // malinkaGraphQL
|
|
}
|
|
|
|
struct Backend {
|
|
let name: String
|
|
let urlPath: UrlPath // paycashGraphQl
|
|
let errorUrlPath: UrlPath
|
|
let webSocket: UrlPath
|
|
}
|
|
|
|
struct SmartContracts {
|
|
let name: String
|
|
let contracts: [EOSContract: String]
|
|
}
|
|
|
|
struct Other {
|
|
let name: String
|
|
let listSite: UrlPath // listFamilySite
|
|
let listGraphQL: UrlPath // graphql
|
|
let paycashSwap: UrlPath
|
|
let paycashSwapSite: UrlPath
|
|
}
|
|
|
|
enum FirebaseLifetime: Int {
|
|
case minute = 60
|
|
case fiveMinutes = 300
|
|
case hour = 3600
|
|
case month = 2592000
|
|
}
|
|
|
|
let usernames: Usernames
|
|
let headers: Headers
|
|
let backend: Backend
|
|
let smarts: SmartContracts
|
|
let other: Other
|
|
let firebaseTokenLifetime: FirebaseLifetime
|
|
|
|
var isProduction: Bool {
|
|
return self.backend.name == ApplicationEnvironment.BackendEnvironment.production.rawValue
|
|
&& self.usernames.name == ApplicationEnvironment.UsernamesEnvironment.production.rawValue
|
|
&& self.other.name == ApplicationEnvironment.OtherEnvironment.production.rawValue
|
|
&& self.smarts.name == ApplicationEnvironment.SmartsEnvironment.production.rawValue
|
|
}
|
|
|
|
private(set) var nodes = [UrlPath]()
|
|
private(set) var hyperions = [UrlPath]()
|
|
|
|
init(usernames: Usernames, backend: Backend, headers: Headers, smarts: SmartContracts, other: Other, nodes: [UrlPath], hyperions: [UrlPath], firebaseTokenLifetime: FirebaseLifetime) {
|
|
self.usernames = usernames
|
|
self.backend = backend
|
|
self.headers = headers
|
|
self.smarts = smarts
|
|
self.other = other
|
|
self.nodes = nodes
|
|
self.hyperions = hyperions
|
|
self.firebaseTokenLifetime = firebaseTokenLifetime
|
|
}
|
|
|
|
// MARK: -
|
|
|
|
var node: UrlPath? { self.nodes.first }
|
|
|
|
var hyperion: UrlPath? { self.hyperions.first }
|
|
|
|
var useScatter: Bool { false }
|
|
|
|
func update(nodes: [UrlPath]) { self.nodes = nodes.map { $0 } }
|
|
|
|
func update(hyperions: [UrlPath]) { self.hyperions = hyperions.map { $0 } }
|
|
|
|
// TODO: - NEED USE Optional!
|
|
func contract(_ name: EOSContract) -> String { self.smarts.contracts[name] ?? "" }
|
|
|
|
func contract(_ name: String) -> EOSContract? { self.smarts.contracts.filter { $0.value == name }.first?.key }
|
|
|
|
//
|
|
func networkEnvironment() throws -> NetworkEnvironment {
|
|
guard let userURL = URL(string: self.usernames.urlPath),
|
|
let backendPath = URL(string: self.backend.urlPath) else {
|
|
throw NetworkServiceError.invalidUrl
|
|
}
|
|
|
|
return CommonEnvironment(usernames: userURL,
|
|
backend: backendPath,
|
|
hyperion: { URL(string: self.hyperion ?? "")! },
|
|
node: { URL(string: self.node ?? "")! },
|
|
headers: self.headers)
|
|
}
|
|
|
|
}
|