From 42243da30960bd72e49b2d4b1c024c4462440e8c Mon Sep 17 00:00:00 2001 From: Ivan Grachyov Date: Thu, 12 Aug 2021 14:37:51 +0300 Subject: [PATCH] Refactor chains --- Encrypted Ink/Ethereum/EthereumChain.swift | 20 +++++++++++++++++++ Encrypted Ink/Ethereum/Transaction.swift | 23 ++++------------------ 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/Encrypted Ink/Ethereum/EthereumChain.swift b/Encrypted Ink/Ethereum/EthereumChain.swift index 218d9b08..797f4923 100644 --- a/Encrypted Ink/Ethereum/EthereumChain.swift +++ b/Encrypted Ink/Ethereum/EthereumChain.swift @@ -25,6 +25,26 @@ enum EthereumChain: Int { } } + var symbol: String { + switch self { + case .main, .arbitrum, .optimism: + return "ETH" + case .binance: + return "BNB" + case .polygon: + return "MATIC" + } + } + + var hasUSDPrice: Bool { + switch self { + case .main, .arbitrum, .optimism: + return true + case .binance, .polygon: + return false + } + } + var nodeURLString: String { switch self { case .main: return "https://eth-mainnet.alchemyapi.io/v2/" + Secrets.alchemy diff --git a/Encrypted Ink/Ethereum/Transaction.swift b/Encrypted Ink/Ethereum/Transaction.swift index 9df8594c..17516f1f 100644 --- a/Encrypted Ink/Ethereum/Transaction.swift +++ b/Encrypted Ink/Ethereum/Transaction.swift @@ -32,38 +32,23 @@ struct Transaction { } func description(chain: EthereumChain, ethPrice: Double?) -> String { - let symbol: String - let showUSDPrice: Bool - - switch chain { - case .main, .arbitrum, .optimism: - symbol = "ETH" - showUSDPrice = true - case .polygon: - symbol = "MATIC" - showUSDPrice = false - case .binance: - symbol = "BNB" - showUSDPrice = false - } - let fee: String if let gasPrice = gasPrice, let gas = gas, let a = try? EthNumber(hex: gasPrice).value().toNormalizedDecimal(power: 18), let b = try? EthNumber(hex: gas).value().toDecimal() { let c = NSDecimalNumber(decimal: a).multiplying(by: NSDecimalNumber(decimal: b)) - let costString = showUSDPrice ? cost(value: c, price: ethPrice) : "" - fee = c.stringValue.prefix(7) + " \(symbol)" + costString + let costString = chain.hasUSDPrice ? cost(value: c, price: ethPrice) : "" + fee = c.stringValue.prefix(7) + " \(chain.symbol)" + costString } else { fee = "Calculating…" } var result = [String]() if let decimal = try? weiAmount.value().toNormalizedDecimal(power: 18) { let decimalNumber = NSDecimalNumber(decimal: decimal) - let costString = showUSDPrice ? cost(value: decimalNumber, price: ethPrice) : "" + let costString = chain.hasUSDPrice ? cost(value: decimalNumber, price: ethPrice) : "" if let value = ethString(decimalNumber: decimalNumber) { - result.append("\(value) \(symbol)" + costString) + result.append("\(value) \(chain.symbol)" + costString) } } result.append("Fee: " + fee)