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.
59 lines
1.3 KiB
59 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
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|