Performance boost, and hitting Infura less often by batching RPC JSON calls

pull/2109/head
Hwee-Boon Yar 4 years ago
parent 79a16dffa3
commit 8f423e8ec6
  1. 51
      AlphaWallet/Tokens/Helpers/CallSmartContractFunction.swift
  2. 4
      Podfile
  3. 8
      Podfile.lock

@ -4,10 +4,40 @@ import Foundation
import PromiseKit
import web3swift
//TODO time to wrap `callSmartContract` with a class
//TODO wrap callSmartContract() and cache into a type
var smartContractCallsCache = [String: (promise: Promise<[String: Any]>, timestamp: Date)]()
fileprivate var smartContractCallsCache = [String: (promise: Promise<[String: Any]>, timestamp: Date)]()
fileprivate var web3s = [RPCServer: [TimeInterval: web3]]()
func getCachedWeb3(forServer server: RPCServer, timeout: TimeInterval) throws -> web3 {
if let result = web3s[server]?[timeout] {
return result
} else {
guard let webProvider = Web3HttpProvider(server.rpcURL, network: server.web3Network) else {
throw Web3Error(description: "Error creating web provider for: \(server.rpcURL) + \(server.web3Network)")
}
let configuration = webProvider.session.configuration
configuration.timeoutIntervalForRequest = timeout
configuration.timeoutIntervalForResource = timeout
let session = URLSession(configuration: configuration)
webProvider.session = session
let result = web3swift.web3(provider: webProvider)
if var timeoutsAndWeb3s = web3s[server] {
timeoutsAndWeb3s[timeout] = result
web3s[server] = timeoutsAndWeb3s
} else {
let timeoutsAndWeb3s: [TimeInterval: web3] = [timeout: result]
web3s[server] = timeoutsAndWeb3s
}
return result
}
}
func callSmartContract(withServer server: RPCServer, contract: AlphaWallet.Address, functionName: String, abiString: String, parameters: [AnyObject] = [AnyObject](), timeout: TimeInterval? = nil) -> Promise<[String: Any]> {
let timeout: TimeInterval = 60
//We must include the ABI string in the key because the order of elements in a dictionary when serialized in the string is not ordered. Parameters (which is ordered) should ensure it's the same function
let cacheKey = "\(contract).\(functionName) \(parameters) \(server.chainID)"
let ttlForCache: TimeInterval = 10
@ -29,19 +59,11 @@ func callSmartContract(withServer server: RPCServer, contract: AlphaWallet.Addre
}
let result: Promise<[String: Any]> = Promise { seal in
let contractAddress = EthereumAddress(address: contract)
guard let webProvider = Web3HttpProvider(server.rpcURL, network: server.web3Network) else {
throw Web3Error(description: "Error creating web provider for: \(server.rpcURL) + \(server.web3Network)")
}
if let timeout = timeout {
let configuration = webProvider.session.configuration
configuration.timeoutIntervalForRequest = timeout
configuration.timeoutIntervalForResource = timeout
let session = URLSession(configuration: configuration)
webProvider.session = session
guard let web3 = try? getCachedWeb3(forServer: server, timeout: timeout) else {
throw Web3Error(description: "Error creating web3 for: \(server.rpcURL) + \(server.web3Network)")
}
let web3 = web3swift.web3(provider: webProvider)
let contractAddress = EthereumAddress(address: contract)
guard let contractInstance = web3swift.web3.web3contract(web3: web3, abiString: abiString, at: contractAddress, options: web3.options) else {
throw Web3Error(description: "Error creating web3swift contract instance to call \(functionName)()")
@ -73,11 +95,10 @@ func getEventLogs(
let contractAddress = EthereumAddress(address: contract)
return .value(contractAddress)
}.then { contractAddress -> Promise<[EventParserResultProtocol]> in
guard let webProvider = Web3HttpProvider(server.rpcURL, network: server.web3Network) else {
return Promise(error: Web3Error(description: "Error creating web provider for: \(server.rpcURL) + \(server.web3Network)"))
guard let web3 = try? getCachedWeb3(forServer: server, timeout: 60) else {
throw Web3Error(description: "Error creating web3 for: \(server.rpcURL) + \(server.web3Network)")
}
let web3 = web3swift.web3(provider: webProvider)
guard let contractInstance = web3swift.web3.web3contract(web3: web3, abiString: abiString, at: contractAddress, options: web3.options) else {
return Promise(error: Web3Error(description: "Error creating web3swift contract instance to call \(eventName)()"))
}

@ -26,9 +26,7 @@ target 'AlphaWallet' do
pod 'TrezorCrypto', :git=>'https://github.com/AlphaWallet/trezor-crypto-ios.git', :commit => '50c16ba5527e269bbc838e80aee5bac0fe304cc7'
pod 'TrustKeystore', :git => 'https://github.com/alpha-wallet/trust-keystore.git', :commit => '37f7eaf9531cb4e33d06129543b3a56972f59d2a'
pod 'SwiftyJSON'
#pod 'web3swift', :git => 'https://github.com/alpha-wallet/web3swift.git', :commit => 'ae74a86c09dbec703e2aaf27217d7fb0722948ed'
pod 'web3swift', :git => 'https://github.com/alpha-wallet/web3swift.git', :commit => '7e2b99198acb2243b6a539cb32832a96f67c893d'
pod 'web3swift', :git => 'https://github.com/alpha-wallet/web3swift.git', :commit => 'd863662b08f112dee48538a04ab704244e9c7bae'
pod 'SAMKeychain'
pod 'PromiseKit/CorePromise'
pod 'PromiseKit/Alamofire'

@ -118,7 +118,7 @@ DEPENDENCIES:
- TrezorCrypto (from `https://github.com/AlphaWallet/trezor-crypto-ios.git`, commit `50c16ba5527e269bbc838e80aee5bac0fe304cc7`)
- TrustKeystore (from `https://github.com/alpha-wallet/trust-keystore.git`, commit `37f7eaf9531cb4e33d06129543b3a56972f59d2a`)
- TrustWalletCore
- web3swift (from `https://github.com/alpha-wallet/web3swift.git`, commit `7e2b99198acb2243b6a539cb32832a96f67c893d`)
- web3swift (from `https://github.com/alpha-wallet/web3swift.git`, commit `d863662b08f112dee48538a04ab704244e9c7bae`)
SPEC REPOS:
https://github.com/CocoaPods/Specs.git:
@ -181,7 +181,7 @@ EXTERNAL SOURCES:
:commit: 37f7eaf9531cb4e33d06129543b3a56972f59d2a
:git: https://github.com/alpha-wallet/trust-keystore.git
web3swift:
:commit: 7e2b99198acb2243b6a539cb32832a96f67c893d
:commit: d863662b08f112dee48538a04ab704244e9c7bae
:git: https://github.com/alpha-wallet/web3swift.git
CHECKOUT OPTIONS:
@ -210,7 +210,7 @@ CHECKOUT OPTIONS:
:commit: 37f7eaf9531cb4e33d06129543b3a56972f59d2a
:git: https://github.com/alpha-wallet/trust-keystore.git
web3swift:
:commit: 7e2b99198acb2243b6a539cb32832a96f67c893d
:commit: d863662b08f112dee48538a04ab704244e9c7bae
:git: https://github.com/alpha-wallet/web3swift.git
SPEC CHECKSUMS:
@ -256,6 +256,6 @@ SPEC CHECKSUMS:
TrustWalletCore: dd8c81d5958f1eb737abea1e14675efcc6a104aa
web3swift: 1597d4997976d21325882db4c523f1dfd5459406
PODFILE CHECKSUM: ec2b391a12a22f8ccec61785b00f8d8eade47186
PODFILE CHECKSUM: 2099838a6060fe419603c9225cc2d43ec0fd2117
COCOAPODS: 1.8.4

Loading…
Cancel
Save