Refactor chains

This commit is contained in:
Ivan Grachyov 2021-08-12 14:37:51 +03:00
parent cdd7a7e045
commit 42243da309
2 changed files with 24 additions and 19 deletions

View File

@ -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

View File

@ -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)