tokenary/Encrypted Ink/Ethereum/Transaction.swift

58 lines
1.7 KiB
Swift
Raw Normal View History

2021-06-20 16:17:11 +03:00
// Copyright © 2021 Encrypted Ink. All rights reserved.
import Foundation
2021-06-27 14:13:34 +03:00
import Web3Swift
2021-06-20 16:17:11 +03:00
struct Transaction {
let from: String
let to: String
2021-06-27 14:13:34 +03:00
var nonce: String?
var gasPrice: String?
var gas: String?
2021-06-20 16:17:11 +03:00
let value: String?
let data: String
2021-06-27 14:13:34 +03:00
var weiAmount: EthNumber {
if let value = value {
return EthNumber(hex: value)
} else {
return EthNumber(value: 0)
}
}
var hasFee: Bool {
return gas != nil && gasPrice != nil
}
var meta: String {
let value = ethString(hex: try? weiAmount.value().toHexString())
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))
fee = c.stringValue.prefix(8) + " ETH"
} else {
fee = "Calculating…"
2021-06-27 14:13:34 +03:00
}
var result = [String]()
if let value = value {
result.append("\(value) ETH")
}
result.append("Fee: " + fee)
result.append("Data: " + data)
return result.joined(separator: "\n\n")
}
private func ethString(hex: String?) -> String? {
guard let hex = hex, let decimal = try? EthNumber(hex: hex).value().toNormalizedDecimal(power: 18) else { return nil }
let formatter = NumberFormatter()
formatter.decimalSeparator = "."
formatter.maximumFractionDigits = 12
return formatter.string(from: NSDecimalNumber(decimal: decimal))
}
2021-06-20 16:17:11 +03:00
}