Skip to content

Commit

Permalink
Merge branch 'develop' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroslavyaroslav committed Nov 3, 2022
2 parents b657241 + fd78f00 commit ee7c6a3
Show file tree
Hide file tree
Showing 108 changed files with 610 additions and 622 deletions.
12 changes: 5 additions & 7 deletions Sources/Core/Contract/ContractProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ import BigInt
public protocol ContractProtocol {
/// Address of the referenced smart contract. Can be set later, e.g. if the contract is deploying and address is not yet known.
var address: EthereumAddress? {get set}

/// All ABI elements like: events, functions, constructors and errors.
var abi: [ABI.Element] {get}

Expand All @@ -81,13 +81,13 @@ public protocol ContractProtocol {
/// - and 4 bytes signature `0xffffffff` (expected to be lowercased).
/// The mapping by name (e.g. `getData`) is the one most likely expected to return arrays with
/// more than one entry due to the fact that solidity allows method overloading.
var methods: [String:[ABI.Element.Function]] {get}
var methods: [String: [ABI.Element.Function]] {get}

/// All values from ``methods``.
var allMethods: [ABI.Element.Function] {get}

/// Events filtered from ``abi`` and mapped to their unchanged ``ABI/Element/Event/name``.
var events: [String:ABI.Element.Event] {get}
var events: [String: ABI.Element.Event] {get}

/// All values from ``events``.
var allEvents: [ABI.Element.Event] {get}
Expand Down Expand Up @@ -225,7 +225,6 @@ extension DefaultContractProtocol {
let parameters = parameters,
!parameters.isEmpty {
guard constructor.inputs.count == parameters.count,
// FIXME: This should be zipped, because Arrays don't guarantee it's elements order
let encodedData = constructor.encodeParameters(parameters) else {
NSLog("Constructor encoding will fail as the number of input arguments doesn't match the number of given arguments.")
return nil
Expand Down Expand Up @@ -278,11 +277,10 @@ extension DefaultContractProtocol {

public func parseEvent(_ eventLog: EventLog) -> (eventName: String?, eventData: [String: Any]?) {
for (eName, ev) in self.events {
if (!ev.anonymous) {
if !ev.anonymous {
if eventLog.topics[0] != ev.topic {
continue
}
else {
} else {
let logTopics = eventLog.topics
let logData = eventLog.data
let parsed = ev.decodeReturnedLogs(eventLogTopics: logTopics, eventLogData: logData)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Core/Contract/EthereumContract.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import BigInt
/// constructor, errors and optional ``EthereumAddress`` that could be set later.
public class EthereumContract: DefaultContractProtocol {

public var address: EthereumAddress? = nil
public var address: EthereumAddress?

public let abi: [ABI.Element]
public let methods: [String: [ABI.Element.Function]]
Expand Down Expand Up @@ -40,7 +40,7 @@ public class EthereumContract: DefaultContractProtocol {
public convenience required init(_ abiString: String, at: EthereumAddress? = nil) throws {
let jsonData = abiString.data(using: .utf8)
let abi = try JSONDecoder().decode([ABI.Record].self, from: jsonData!)
let abiNative = try abi.map({ (record) -> ABI.Element in
let abiNative = try abi.map({ record -> ABI.Element in
return try record.parse()
})
try self.init(abi: abiNative, at: at)
Expand Down
15 changes: 7 additions & 8 deletions Sources/Core/EthereumABI/ABIDecoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public struct ABIDecoder { }

extension ABIDecoder {
public static func decode(types: [ABI.Element.InOut], data: Data) -> [AnyObject]? {
let params = types.compactMap { (el) -> ABI.Element.ParameterType in
let params = types.compactMap { el -> ABI.Element.ParameterType in
return el.type
}
return decode(types: params, data: data)
Expand Down Expand Up @@ -118,10 +118,9 @@ extension ABIDecoder {
let (v, c) = decodeSingleType(type: subType, data: dataSlice, pointer: subpointer)
guard let valueUnwrapped = v, let consumedUnwrapped = c else {break}
toReturn.append(valueUnwrapped)
if (subType.isStatic) {
if subType.isStatic {
subpointer = subpointer + consumedUnwrapped
}
else {
} else {
subpointer = consumedUnwrapped // need to go by nextElementPointer
}
}
Expand Down Expand Up @@ -224,16 +223,16 @@ extension ABIDecoder {
eventContent["name"]=event.name
let logs = eventLogTopics
let dataForProcessing = eventLogData
let indexedInputs = event.inputs.filter { (inp) -> Bool in
let indexedInputs = event.inputs.filter { inp -> Bool in
return inp.indexed
}
if (logs.count == 1 && indexedInputs.count > 0) {
if logs.count == 1 && indexedInputs.count > 0 {
return nil
}
let nonIndexedInputs = event.inputs.filter { (inp) -> Bool in
let nonIndexedInputs = event.inputs.filter { inp -> Bool in
return !inp.indexed
}
let nonIndexedTypes = nonIndexedInputs.compactMap { (inp) -> ABI.Element.ParameterType in
let nonIndexedTypes = nonIndexedInputs.compactMap { inp -> ABI.Element.ParameterType in
return inp.type
}
guard logs.count == indexedInputs.count + 1 else {return nil}
Expand Down
32 changes: 16 additions & 16 deletions Sources/Core/EthereumABI/ABIElements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ public extension ABI {
public struct EthError {
public let name: String
public let inputs: [Input]

public struct Input {
public let name: String
public let type: ParameterType

public init(name: String, type: ParameterType) {
self.name = name
self.type = type
Expand All @@ -177,15 +177,15 @@ extension ABI.Element {
switch self {
case .constructor(let constructor):
return constructor.encodeParameters(parameters)
case .event(_):
case .event:
return nil
case .fallback(_):
case .fallback:
return nil
case .function(let function):
return function.encodeParameters(parameters)
case .receive(_):
case .receive:
return nil
case .error(_):
case .error:
return nil
}
}
Expand Down Expand Up @@ -224,17 +224,17 @@ extension ABI.Element.Event {
extension ABI.Element {
public func decodeReturnData(_ data: Data) -> [String: Any]? {
switch self {
case .constructor(_):
case .constructor:
return nil
case .event(_):
case .event:
return nil
case .fallback(_):
case .fallback:
return nil
case .function(let function):
return function.decodeReturnData(data)
case .receive(_):
case .receive:
return nil
case .error(_):
case .error:
return nil
}
}
Expand All @@ -245,15 +245,15 @@ extension ABI.Element {
switch self {
case .constructor(let constructor):
return constructor.decodeInputData(data)
case .event(_):
case .event:
return nil
case .fallback(_):
case .fallback:
return nil
case .function(let function):
return function.decodeInputData(data)
case .receive(_):
case .receive:
return nil
case .error(_):
case .error:
return nil
}
}
Expand Down Expand Up @@ -347,7 +347,7 @@ extension ABI.Element.Constructor {
/// - inputs: expected input types. Order must be the same as in function declaration.
/// - Returns: decoded dictionary of input arguments mapped to their indices and arguments' names if these are not empty.
/// If decoding of at least one argument fails, `rawData` size is invalid or `methodEncoding` doesn't match - `nil` is returned.
fileprivate func decodeInputData(_ rawData: Data,
private func decodeInputData(_ rawData: Data,
methodEncoding: Data? = nil,
inputs: [ABI.Element.InOut]) -> [String: Any]? {
let data: Data
Expand Down
15 changes: 5 additions & 10 deletions Sources/Core/EthereumABI/ABIEncoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,29 +130,25 @@ extension ABIEncoder {
return nil
}


/// Encode Elements In Out
/// - Parameters:
/// - types: Contract element InOut to encode
/// - values: Contract values of a given element to encode
/// - Returns: Encoded data
public static func encode(types: [ABI.Element.InOut], values: [AnyObject]) -> Data? {
// FIXME: This should be zipped, because Arrays don't guarantee it's elements order
guard types.count == values.count else {return nil}
let params = types.compactMap { (el) -> ABI.Element.ParameterType in
let params = types.compactMap { el -> ABI.Element.ParameterType in
return el.type
}
return encode(types: params, values: values)
}


/// Encode Elements Prarmeter Type
/// - Parameters:
/// - types: Contract parameters type to encode
/// - values: Contract values of a given element to encode
/// - Returns: Encoded data
public static func encode(types: [ABI.Element.ParameterType], values: [AnyObject]) -> Data? {
// FIXME: This should be zipped, because Arrays don't guarantee it's elements order
guard types.count == values.count else {return nil}
var tails = [Data]()
var heads = [Data]()
Expand Down Expand Up @@ -192,14 +188,14 @@ extension ABIEncoder {

public static func encodeSingleType(type: ABI.Element.ParameterType, value: AnyObject) -> Data? {
switch type {
case .uint(_):
case .uint:
if let biguint = convertToBigUInt(value) {
return biguint.abiEncode(bits: 256)
}
if let bigint = convertToBigInt(value) {
return bigint.abiEncode(bits: 256)
}
case .int(_):
case .int:
if let biguint = convertToBigUInt(value) {
return biguint.abiEncode(bits: 256)
}
Expand All @@ -220,7 +216,7 @@ extension ABIEncoder {
}
case .bool:
if let bool = value as? Bool {
if (bool) {
if bool {
return BigUInt(1).abiEncode(bits: 256)
} else {
return BigUInt(0).abiEncode(bits: 256)
Expand All @@ -235,8 +231,7 @@ extension ABIEncoder {
var dataGuess: Data?
if string.hasHexPrefix() {
dataGuess = Data.fromHex(string.lowercased().stripHexPrefix())
}
else {
} else {
dataGuess = string.data(using: .utf8)
}
guard let data = dataGuess else {break}
Expand Down
14 changes: 7 additions & 7 deletions Sources/Core/EthereumABI/ABIParameterTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ extension ABI.Element {
case .dynamicBytes:
return false
case .array(type: let type, length: let length):
if (length == 0) {
if length == 0 {
return false
}
if (!type.isStatic) {
if !type.isStatic {
return false
}
return true
case .tuple(types: let types):
for t in types {
if (!t.isStatic) {
if !t.isStatic {
return false
}
}
Expand All @@ -60,7 +60,7 @@ extension ABI.Element {

var isTuple: Bool {
switch self {
case .tuple(_):
case .tuple:
return true
default:
return false
Expand Down Expand Up @@ -129,7 +129,7 @@ extension ABI.Element {
var arraySize: ABI.Element.ArraySize {
switch self {
case .array(type: _, length: let length):
if (length == 0) {
if length == 0 {
return ArraySize.dynamicSize
}
return ArraySize.staticSize(length)
Expand Down Expand Up @@ -210,7 +210,7 @@ extension ABI.Element.ParameterType: ABIEncoding {
case .function:
return "function"
case .array(type: let type, length: let length):
if (length == 0) {
if length == 0 {
return "\(type.abiRepresentation)[]"
}
return "\(type.abiRepresentation)[\(length)]"
Expand All @@ -235,7 +235,7 @@ extension ABI.Element.ParameterType: ABIValidation {
return type.isValid
case .tuple(types: let types):
for t in types {
if (!t.isValid) {
if !t.isValid {
return false
}
}
Expand Down
Loading

0 comments on commit ee7c6a3

Please sign in to comment.