Skip to content

Commit 07d64f8

Browse files
feat: added array of errors in ContractProtocol filtered from ABI;
1 parent f5c5c6e commit 07d64f8

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

Sources/web3swift/Contract/ContractProtocol.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ public protocol ContractProtocol {
3232
/// All events from `events`.
3333
var allEvents: [ABI.Element.Event] {get}
3434

35+
/// Errors filtered from ``abi`` and mapped to their unchanged ``ABI/Element/EthError/name``.
36+
var errors: [String: ABI.Element.EthError] {get}
37+
38+
/// All values from ``errors``.
39+
var allErrors: [ABI.Element.EthError] {get}
40+
3541
/// Parsed from ABI or a default constructor with no input arguments.
3642
var constructor: ABI.Element.Constructor {get}
3743

Sources/web3swift/Contract/EthereumContract.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import Foundation
77
import BigInt
88
import Core
99

10+
/// Default representation of a smart contract. Created out of an array of ``ABI/Element`` which could be functions, events,
11+
/// constructor, errors and optional ``EthereumAddress`` that could be set later.
1012
public class EthereumContract: DefaultContractProtocol {
1113

1214
public var address: EthereumAddress? = nil
@@ -16,6 +18,8 @@ public class EthereumContract: DefaultContractProtocol {
1618
public let allMethods: [ABI.Element.Function]
1719
public let events: [String: ABI.Element.Event]
1820
public let allEvents: [ABI.Element.Event]
21+
public let errors: [String: ABI.Element.EthError]
22+
public let allErrors: [ABI.Element.EthError]
1923
public let constructor: ABI.Element.Constructor
2024

2125
public init(abi: [ABI.Element], at: EthereumAddress? = nil) throws {
@@ -30,6 +34,8 @@ public class EthereumContract: DefaultContractProtocol {
3034
events = abi.getEvents()
3135
allEvents = Array(events.values)
3236
constructor = abi.getConstructor()
37+
errors = abi.getErrors()
38+
allErrors = Array(errors.values)
3339
}
3440

3541
public convenience required init(_ abiString: String, at: EthereumAddress? = nil) throws {

Sources/web3swift/EthereumABI/ABIElements.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import BigInt
88
import Core
99

1010
public extension ABI {
11-
// JSON Decoding
1211
struct Input: Decodable {
1312
public var name: String?
1413
public var type: String

Sources/web3swift/EthereumABI/Sequence+ABIExtension.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ public extension Sequence where Element == ABI.Element {
4949
return events
5050
}
5151

52+
/// Filters out all ``ABI/Element/EthError`` types and maps them to their names
53+
/// provided in ``ABI/Element/EthError/name`` variable.
54+
/// - Returns: dictionary of all errors mapped to their names.
55+
func getErrors() -> [String: ABI.Element.EthError] {
56+
var errors = [String: ABI.Element.EthError]()
57+
for case let .error(error) in self {
58+
errors[error.name] = error
59+
}
60+
return errors
61+
}
62+
5263
/// Filters out ``ABI/Element/Constructor``.
5364
/// If there are multiple of them the first encountered will be returned and if there are none a default constructor will be returned
5465
/// that accepts no input parameters.

0 commit comments

Comments
 (0)