Skip to content

Commit 60c9ca7

Browse files
feat: ContractProtocol - new function to search for ABI.Element.Function called in a transaction
1 parent b9ac465 commit 60c9ca7

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Sources/Core/Contract/ContractProtocol.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ public protocol ContractProtocol {
173173
/// - Returns: `true` if event is possibly present, `false` if definitely not present and `nil` if event with given name
174174
/// is not part of the ``EthereumContract/abi``.
175175
func testBloomForEventPresence(eventName: String, bloom: EthereumBloomFilter) -> Bool?
176+
177+
/// Given the transaction data searches for a match in ``ContractProtocol/methods``.
178+
/// - Parameter data: encoded function call used in transaction data field. Must be at least 4 bytes long.
179+
/// - Returns: function decoded from the ABI of this contract or `nil` if nothing was found.
180+
func getFunctionCalled(_ data: Data) -> ABI.Element.Function?
176181
}
177182

178183
// MARK: - Overloaded ContractProtocol's functions
@@ -333,4 +338,9 @@ extension DefaultContractProtocol {
333338
guard let function = methods[methodSignature]?.first else { return nil }
334339
return function.decodeInputData(Data(data[4 ..< data.count]))
335340
}
341+
342+
public func getFunctionCalled(_ data: Data) -> ABI.Element.Function? {
343+
guard data.count >= 4 else { return nil }
344+
return methods[data[0..<4].toHexString().addHexPrefix()]?.first
345+
}
336346
}

0 commit comments

Comments
 (0)