Skip to content

Commit

Permalink
new event parser, streamlined events API, support of ABI v2 (no speci…
Browse files Browse the repository at this point in the history
…fic tests yet)
  • Loading branch information
Alex Vlasov committed Apr 4, 2018
1 parent 9bdf597 commit a9e5f2c
Show file tree
Hide file tree
Showing 14 changed files with 224 additions and 198 deletions.
4 changes: 2 additions & 2 deletions Podfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
def import_pods
pod 'Alamofire', '~> 4.5'
pod 'Alamofire', '~> 4.7'
pod 'Alamofire-Synchronous', '~> 4.0'
pod 'BigInt', '~> 3.0.1'
pod 'BigInt', '~> 3.0.2'
pod 'CryptoSwift'
pod 'Result', '~> 3.0.0'
pod 'libsodium'
Expand Down
8 changes: 4 additions & 4 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
PODS:
- Alamofire (4.7.0)
- Alamofire (4.7.1)
- Alamofire-Synchronous (4.0.0):
- Alamofire (~> 4.0)
- BigInt (3.0.1):
- SipHash (~> 1.2)
- CryptoSwift (0.8.3)
- CryptoSwift (0.9.0)
- libsodium (1.0.12)
- Result (3.0.0)
- secp256k1_ios (0.1.2)
Expand All @@ -29,10 +29,10 @@ CHECKOUT OPTIONS:
:git: https://github.com/shamatar/secp256k1_ios.git

SPEC CHECKSUMS:
Alamofire: 907e0a98eb68cdb7f9d1f541a563d6ac5dc77b25
Alamofire: 68d7d521118d49c615a8d2214d87cdf525599d30
Alamofire-Synchronous: eedf1e6e961c3795a63c74990b3f7d9fbfac7e50
BigInt: 8e8a52161c745cd3ab78e3dc346a9fbee51e6cf6
CryptoSwift: 033efc3523865d19cc6ab6612fa17a62613b5dfa
CryptoSwift: bca8c5b653dcc2d9734409242a070ff53bafac86
libsodium: 9a8faa5ef2fa0d2d57bd7f7d79bf8fb7c1a9f0ea
Result: 1b3e431f37cbcd3ad89c6aa9ab0ae55515fae3b6
secp256k1_ios: 168c6c49ed9771db51875f5255c8f9847e4cf554
Expand Down
12 changes: 5 additions & 7 deletions web3swift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "web3swift"
s.version = "0.3.6"
s.version = "0.4.0"
s.summary = "Web3 implementation in vanilla Swift for iOS ans macOS"

s.description = <<-DESC
Expand All @@ -14,9 +14,7 @@ s.source = { :git => 'https://github.com/bankex/web3swift.git', :tag =
s.social_media_url = 'https://twitter.com/shamatar'

s.pod_target_xcconfig = {
'SWIFT_VERSION' => '4.0',
'ARCHS' => '${ARCHS_STANDARD_64_BIT}',
'VALID_ARCHS' => '${ARCHS_STANDARD_64_BIT}'
'SWIFT_VERSION' => '4.0'
}

s.module_name = 'web3swift'
Expand All @@ -26,11 +24,11 @@ s.source_files = "web3swift/**/*.{h,swift}",
s.public_header_files = "web3swift/**/*.{h}"


s.dependency 'Alamofire', '~> 4.6'
s.dependency 'Alamofire', '~> 4.7'
s.dependency 'Alamofire-Synchronous', '~> 4.0'
s.dependency 'BigInt', '~> 3.0.1'
s.dependency 'BigInt', '~> 3.0.2'
s.dependency 'Result', '~> 3.0.0'
s.dependency 'CryptoSwift', '~> 0.8.3'
s.dependency 'CryptoSwift', '~> 0.9.0'
s.dependency 'libsodium', '~> 1.0.12'
s.dependency 'secp256k1_ios', '~> 0.1.2'

Expand Down
2 changes: 1 addition & 1 deletion web3swift/ABIv2/Classes/ABIv2Decoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ extension ABIv2Decoder {
var eventContent = [String: Any]()
eventContent["name"]=event.name
let logs = eventLog.topics
var dataForProcessing = eventLog.data
let dataForProcessing = eventLog.data
if (logs.count == 1 && event.inputs.count > 0) {
return nil
}
Expand Down
15 changes: 3 additions & 12 deletions web3swift/ABIv2/Classes/ABIv2Elements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,10 @@ extension ABIv2.Element {
}
}

extension ABIv2.Element {
extension ABIv2.Element.Event {
func decodeReturnedLogs(_ eventLog: EventLog) -> [String:Any]? {
switch self {
case .constructor(_):
return nil
case .event(let event):
guard let eventContent = ABIv2Decoder.decodeLog(event: event, eventLog: eventLog) else {return nil}
return eventContent
case .fallback(_):
return nil
case .function(_):
return nil
}
guard let eventContent = ABIv2Decoder.decodeLog(event: self, eventLog: eventLog) else {return nil}
return eventContent
}
}

Expand Down
4 changes: 4 additions & 0 deletions web3swift/Contract/Classes/Contract.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,8 @@ public struct Contract:ContractProtocol {
guard case .function(_) = function else {return nil}
return function.decodeReturnData(data)
}

public func testBloomForEventPrecence(eventName: String, bloom: EthereumBloomFilter) -> Bool? {
return false
}
}
39 changes: 30 additions & 9 deletions web3swift/Contract/Classes/ContractABIv2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ public struct ContractV2:ContractProtocol {
}
return toReturn
}
public var events: [String: ABIv2.Element] {
var toReturn = [String: ABIv2.Element]()
public var events: [String: ABIv2.Element.Event] {
var toReturn = [String: ABIv2.Element.Event]()
for m in self._abi {
switch m {
case .event(let event):
let name = event.name
toReturn[name] = m
toReturn[name] = event
default:
continue
}
Expand Down Expand Up @@ -133,15 +133,36 @@ public struct ContractV2:ContractProtocol {
}

public func parseEvent(_ eventLog: EventLog) -> (eventName:String?, eventData:[String:Any]?) {
// for (eName, ev) in self.events {
// let parsed = ev.decodeReturnedLogs(eventLog)
// if parsed != nil {
// return (eName, parsed!)
// }
// }
for (eName, ev) in self.events {
if (!ev.anonymous) {
if eventLog.topics[0] != ev.topic {
continue
}
else {
let parsed = ev.decodeReturnedLogs(eventLog)
if parsed != nil {
return (eName, parsed!)
}
}
} else {
let parsed = ev.decodeReturnedLogs(eventLog)
if parsed != nil {
return (eName, parsed!)
}
}
}
return (nil, nil)
}

public func testBloomForEventPrecence(eventName: String, bloom: EthereumBloomFilter) -> Bool? {
guard let event = events[eventName] else {return nil}
if event.anonymous {
return true
}
let eventOfSuchTypeIsPresent = bloom.test(topic: event.topic)
return eventOfSuchTypeIsPresent
}

public func decodeReturnData(_ method:String, data: Data) -> [String:Any]? {
if method == "fallback" {
return [String:Any]()
Expand Down
3 changes: 2 additions & 1 deletion web3swift/Contract/Classes/ContractProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public protocol ContractProtocol {
init?(_ abiString: String, at: EthereumAddress?)
func decodeReturnData(_ method:String, data: Data) -> [String:Any]?
func parseEvent(_ eventLog: EventLog) -> (eventName:String?, eventData:[String:Any]?)
// optional func createEventFilter(eventName:String, filter: EventFilter?)
func testBloomForEventPrecence(eventName: String, bloom: EthereumBloomFilter) -> Bool?
// func createEventFilter(eventName:String, filter: EventFilter?)
}

public struct EventFilter {
Expand Down
6 changes: 5 additions & 1 deletion web3swift/KeystoreManager/Classes/EthereumKeystoreV3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ public class EthereumKeystoreV3: AbstractKeystore {
self.init(jsonData)
}

public init?(_ jsonData: Data) {
public convenience init?(_ jsonData: Data) {
guard let keystoreParams = try? JSONDecoder().decode(KeystoreParamsV3.self, from: jsonData) else {return nil}
self.init(keystoreParams)
}

public init?(_ keystoreParams: KeystoreParamsV3) {
if (keystoreParams.version != 3) {return nil}
if (keystoreParams.crypto.version != nil && keystoreParams.crypto.version != "1") {return nil}
self.keystoreParams = keystoreParams
Expand Down
12 changes: 6 additions & 6 deletions web3swift/Web3/Classes/Web3+Contract.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import BigInt

extension web3 {

public func contract(_ abiString: String, at: EthereumAddress? = nil, abiVersion: Int = 1) -> web3contract? {
public func contract(_ abiString: String, at: EthereumAddress? = nil, abiVersion: Int = 2) -> web3contract? {
return web3contract(web3: self, abiString: abiString, at: at, options: self.options, abiVersion: abiVersion)
}

Expand All @@ -21,7 +21,7 @@ extension web3 {
var web3 : web3
public var options: Web3Options? = nil

public init?(web3 web3Instance:web3, abiString: String, at: EthereumAddress? = nil, options: Web3Options? = nil, abiVersion: Int = 1) {
public init?(web3 web3Instance:web3, abiString: String, at: EthereumAddress? = nil, options: Web3Options? = nil, abiVersion: Int = 2) {
self.web3 = web3Instance
self.options = web3.options
switch abiVersion {
Expand All @@ -42,9 +42,6 @@ extension web3 {
contract.address = addr
}
self.options = mergedOptions
if contract.address == nil {
return nil
}
}

public func method(_ method:String = "fallback", parameters: [AnyObject] = [AnyObject](), extraData: Data = Data(), options: Web3Options?) -> TransactionIntermediate? {
Expand All @@ -60,7 +57,10 @@ extension web3 {
return self.contract.parseEvent(eventLog)
}


public func createEventParser(_ eventName:String, filter:EventFilter?) -> EventParserProtocol? {
let parser = EventParser(web3: self.web3, eventName: eventName, contract: self.contract, filter: filter)
return parser
}

}
}
Loading

0 comments on commit a9e5f2c

Please sign in to comment.