Merge pull request #3236 from AlphaWallet/respect-hardcoded-block-window-supported-by-each-chain-while-fetching-events-for-activities

Respect hardcoded block window for each chain while fetching events for discovering activities
pull/3240/head
Hwee-Boon Yar 3 years ago committed by GitHub
commit 937f775d18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      AlphaWallet/Settings/Types/RPCServers.swift
  2. 9
      AlphaWallet/TokenScriptClient/Coordinators/EventSourceCoordinatorForActivities.swift
  3. 17
      AlphaWallet/Tokens/Logic/Erc1155TokenIdsFetcher.swift

@ -772,6 +772,21 @@ enum RPCServer: Hashable, CaseIterable {
}
}
func makeMaximumToBlockForEvents(fromBlockNumber: UInt64) -> EventFilter.Block {
switch self {
case .binance_smart_chain, .binance_smart_chain_testnet, .heco, .heco_testnet:
//These do not allow range more than 5000
return .blockNumber(fromBlockNumber + 4990)
case .optimistic:
//These not allow range more than 10000
return .blockNumber(fromBlockNumber + 9999)
case .polygon, .mumbai_testnet, .cronosTestnet:
//These not allow range more than 100000
return .blockNumber(fromBlockNumber + 99990)
case .main, .kovan, .ropsten, .rinkeby, .poa, .classic, .callisto, .xDai, .goerli, .artis_sigma1, .artis_tau1, .fantom, .fantom_testnet, .avalanche, .avalanche_testnet, .optimisticKovan, .sokol, .custom:
return .latest
}
}
}
// swiftlint:enable type_body_length

@ -117,14 +117,7 @@ extension EventSourceCoordinatorForActivities.functional {
}).map(on: queue, { fromBlock -> EventFilter in
let parameterFilters = filterParam.map { $0?.filter }
let addresses = [EthereumAddress(address: eventOrigin.contract)]
let toBlock: EventFilter.Block
if server == .binance_smart_chain || server == .binance_smart_chain_testnet || server == .heco {
//NOTE: binance_smart_chain does not allow range more than 5000
toBlock = .blockNumber(fromBlock.1 + 4000)
} else {
toBlock = .latest
}
let toBlock = server.makeMaximumToBlockForEvents(fromBlockNumber: fromBlock.1)
return EventFilter(fromBlock: fromBlock.0, toBlock: toBlock, addresses: addresses, parameterFilters: parameterFilters)
}).then(on: queue, { eventFilter in
getEventLogs(withServer: server, contract: eventOrigin.contract, eventName: eventOrigin.eventName, abiString: eventOrigin.eventAbiString, filter: eventFilter, queue: queue)

@ -64,22 +64,7 @@ class Erc1155TokenIdsFetcher {
//Should really be -1 instead 0, but so we don't fight with the type system (negative) and doesn't matter in practice being off by 1 at the start
let fromPreviousRead: Erc1155TokenIds = readJson() ?? .init(tokens: .init(), lastBlockNumber: 0)
let fromBlockNumber = fromPreviousRead.lastBlockNumber + 1
let toBlock: EventFilter.Block
switch server {
case .binance_smart_chain, .binance_smart_chain_testnet, .heco, .heco_testnet:
//These do not allow range more than 5000
toBlock = .blockNumber(UInt64(fromBlockNumber) + 4990)
case .optimistic:
//These not allow range more than 10000
toBlock = .blockNumber(UInt64(fromBlockNumber) + 9999)
case .polygon, .mumbai_testnet, .cronosTestnet:
//These not allow range more than 100000
toBlock = .blockNumber(UInt64(fromBlockNumber) + 99990)
case .main, .kovan, .ropsten, .rinkeby, .poa, .classic, .callisto, .xDai, .goerli, .artis_sigma1, .artis_tau1, .fantom, .fantom_testnet, .avalanche, .avalanche_testnet, .optimisticKovan, .sokol, .custom:
toBlock = .latest
}
let toBlock = server.makeMaximumToBlockForEvents(fromBlockNumber: UInt64(fromBlockNumber))
return firstly {
functional.fetchEvents(forAddress: address, server: server, fromBlock: .blockNumber(UInt64(fromBlockNumber)), toBlock: toBlock)
}.map { fetched -> Erc1155TokenIds in

Loading…
Cancel
Save