An advanced Ethereum/EVM mobile wallet
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.
alpha-wallet-ios/Trust/Transactions/TransactionDataStore.swift

118 lines
3.6 KiB

7 years ago
// Copyright SIX DAY LLC, Inc. All rights reserved.
import Foundation
import APIKit
protocol TransactionDataStoreDelegate: class {
func didUpdate(viewModel: TransactionsViewModel)
func didFail(with error: Error, viewModel: TransactionsViewModel)
}
class TransactionDataStore {
7 years ago
var viewModel: TransactionsViewModel {
return .init(transactions: transactions)
}
weak var delegate: TransactionDataStoreDelegate?
7 years ago
let account: Account
var transactions: [Transaction] = []
init(account: Account) {
self.account = account
}
7 years ago
func fetch() {
let request = FetchTransactionsRequest(address: account.address.address)
7 years ago
Session.send(request) { result in
switch result {
case .success(let response):
self.update(transactions: response)
case .failure(let error):
self.delegate?.didFail(with: error, viewModel: self.viewModel)
}
}
}
7 years ago
func update(transactions: [Transaction]) {
self.transactions = transactions
7 years ago
delegate?.didUpdate(viewModel: viewModel)
}
}
struct FetchTransactionsRequest: APIKit.Request {
typealias Response = [Transaction]
7 years ago
let address: String
7 years ago
var baseURL: URL {
let config = Config()
return config.etherScanURL
}
7 years ago
var method: HTTPMethod {
return .get
}
7 years ago
var path: String {
return ""
}
7 years ago
var parameters: Any? {
return [
"module": "account",
"action": "txlist",
"address": address,
"startblock": "0",
"endblock": "99999999",
"sort": "asc",
"apikey": "7V8AMAVQWKNAZHZG8ARYB9SQWWKBBDA7S8",
]
}
7 years ago
func response(from object: Any, urlResponse: HTTPURLResponse) throws -> Response {
NSLog("transactions urlResponse \(urlResponse)")
if
let objectJSON = object as? [String: AnyObject],
let transactionJSON = objectJSON["result"] as? [[String: AnyObject]] {
let transactions: [Transaction] = transactionJSON.map { json in
7 years ago
let blockHash = json["blockHash"] as? String ?? ""
let blockNumber = json["blockNumber"] as? String ?? ""
let confirmation = json["confirmations"] as? String ?? ""
let cumulativeGasUsed = json["cumulativeGasUsed"] as? String ?? ""
let from = json["from"] as? String ?? ""
let to = json["to"] as? String ?? ""
let gas = json["gas"] as? String ?? ""
let gasPrice = json["gasPrice"] as? String ?? ""
let gasUsed = json["gasUsed"] as? String ?? ""
let hash = json["hash"] as? String ?? ""
let isError = Bool(json["isError"] as? String ?? "") ?? false
let timestamp = (json["timeStamp"] as? String ?? "")
7 years ago
let hex = (json["value"] as? String ?? "")
let value = BInt(hex)
7 years ago
return Transaction(
blockHash: blockHash,
blockNumber: blockNumber,
confirmations: confirmation,
cumulativeGasUsed: cumulativeGasUsed,
from: from,
to: to,
owner: address,
gas: gas,
gasPrice: gasPrice,
gasUsed: gasUsed,
hash: hash,
value: value,
timestamp: timestamp,
isError: isError
)
}
return transactions
}
return []
}
}