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/EtherClient/ChainState.swift

60 lines
1.3 KiB

// Copyright SIX DAY LLC. All rights reserved.
import Foundation
import JSONRPCKit
import APIKit
class ChainState {
struct Keys {
static let latestBlock = "chainID"
}
let config: Config
private var latestBlockKey: String {
return "\(config.chainID)-" + Keys.latestBlock
}
var latestBlock: Int {
get {
return defaults.integer(forKey: latestBlockKey)
}
set {
defaults.set(newValue, forKey: latestBlockKey)
}
}
let defaults: UserDefaults
var updateLatestBlock: Timer?
init(
config: Config = Config()
) {
self.config = config
self.defaults = config.defaults
self.updateLatestBlock = Timer.scheduledTimer(timeInterval: 6, target: self, selector: #selector(fetch), userInfo: nil, repeats: true)
}
func start() {
fetch()
}
func stop() {
updateLatestBlock?.invalidate()
updateLatestBlock = nil
}
@objc func fetch() {
let request = EtherServiceRequest(batch: BatchFactory().create(BlockNumberRequest()))
Session.send(request) { result in
switch result {
case .success(let number):
self.latestBlock = number
case .failure: break
}
}
}
}