blockchainethereumblockchain-walleterc20erc721walletxdaidappdecentralizederc1155erc875iosswifttokens
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.3 KiB
41 lines
1.3 KiB
// Copyright SIX DAY LLC. All rights reserved.
|
|
|
|
import Foundation
|
|
|
|
struct Token {
|
|
let address: Address
|
|
let name: String
|
|
let symbol: String
|
|
let totalSupply: String
|
|
let balance: Double
|
|
let decimals: Int
|
|
}
|
|
|
|
extension Token {
|
|
private static let numberFormatter: NumberFormatter = {
|
|
let numberFormatter = NumberFormatter()
|
|
numberFormatter.minimumFractionDigits = 3
|
|
numberFormatter.maximumFractionDigits = 3
|
|
return numberFormatter
|
|
}()
|
|
|
|
var amount: String {
|
|
let res = pow(10.0, Double(decimals))
|
|
let double = NSNumber(value: balance / res)
|
|
return Token.numberFormatter.string(from: double)!
|
|
}
|
|
}
|
|
|
|
extension Token {
|
|
static func from(address: String, json: [String: AnyObject]) -> Token {
|
|
let tokenInfo = json["tokenInfo"] as? [String: AnyObject] ?? [:]
|
|
return Token(
|
|
address: Address(address: tokenInfo["address"] as? String ?? ""),
|
|
name: tokenInfo["name"] as? String ?? "",
|
|
symbol: tokenInfo["symbol"] as? String ?? "",
|
|
totalSupply: tokenInfo["symbol"] as? String ?? "",
|
|
balance: json["balance"] as? Double ?? 0,
|
|
decimals: tokenInfo["decimals"] as? Int ?? Int(tokenInfo["decimals"] as? String ?? "") ?? 0
|
|
)
|
|
}
|
|
}
|
|
|