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.
57 lines
1.3 KiB
57 lines
1.3 KiB
// Copyright SIX DAY LLC. All rights reserved.
|
|
|
|
import Foundation
|
|
import APIKit
|
|
import Result
|
|
|
|
enum TransactionError: Error {
|
|
case failedToFetch
|
|
}
|
|
|
|
enum TokenError: Error {
|
|
case failedToFetch
|
|
}
|
|
|
|
protocol TransactionDataStoreDelegate: class {
|
|
func didUpdate(result: Result<TransactionsViewModel, TransactionError>)
|
|
}
|
|
|
|
class TransactionDataStore {
|
|
|
|
var viewModel: TransactionsViewModel {
|
|
return .init(transactions: transactions)
|
|
}
|
|
weak var delegate: TransactionDataStoreDelegate?
|
|
|
|
let account: Account
|
|
var transactions: [Transaction] = []
|
|
var tokens: [Token] = []
|
|
init(account: Account) {
|
|
self.account = account
|
|
}
|
|
|
|
func fetch() {
|
|
fetchTransactions()
|
|
}
|
|
|
|
func update(transactions: [Transaction]) {
|
|
self.transactions = transactions
|
|
delegate?.didUpdate(result: .success(viewModel))
|
|
}
|
|
|
|
func update(tokens: [Token]) {
|
|
self.tokens = tokens
|
|
}
|
|
|
|
func fetchTransactions() {
|
|
let request = FetchTransactionsRequest(address: account.address.address)
|
|
Session.send(request) { result in
|
|
switch result {
|
|
case .success(let response):
|
|
self.update(transactions: response)
|
|
case .failure:
|
|
self.delegate?.didUpdate(result: .failure(TransactionError.failedToFetch))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|