Skip to content

Commit e6ff991

Browse files
chore: added docs; new test for ABI.Element.Function;
1 parent 084a2cb commit e6ff991

File tree

4 files changed

+68
-5
lines changed

4 files changed

+68
-5
lines changed

Sources/Web3Core/EthereumABI/ABIParameterTypes.swift

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,45 +168,79 @@ extension ABI.Element.ParameterType: Equatable {
168168
}
169169

170170
extension ABI.Element.Function {
171+
/// String representation of a function, e.g. `transfer(address,uint256)`.
171172
public var signature: String {
172173
return "\(name ?? "")(\(inputs.map { $0.type.abiRepresentation }.joined(separator: ",")))"
173174
}
174175

176+
/// Function selector, e.g. `"cafe1234"`. Without hex prefix `0x`.
177+
@available(*, deprecated, renamed: "selector", message: "Please, use 'selector' property instead.")
175178
public var methodString: String {
179+
return selector
180+
}
181+
182+
/// Function selector, e.g. `"cafe1234"`. Without hex prefix `0x`.
183+
public var selector: String {
176184
return String(signature.sha3(.keccak256).prefix(8))
177185
}
178186

187+
/// Function selector (e.g. `0xcafe1234`) but as raw bytes.
188+
@available(*, deprecated, renamed: "selectorEncoded", message: "Please, use 'selectorEncoded' property instead.")
179189
public var methodEncoding: Data {
180-
return signature.data(using: .ascii)!.sha3(.keccak256)[0...3]
190+
return selectorEncoded
191+
}
192+
193+
/// Function selector (e.g. `0xcafe1234`) but as raw bytes.
194+
public var selectorEncoded: Data {
195+
return Data.fromHex(selector)!
181196
}
182197
}
183198

184199
// MARK: - Event topic
185200
extension ABI.Element.Event {
201+
/// String representation of an event, e.g. `ContractCreated(address)`.
186202
public var signature: String {
187203
return "\(name)(\(inputs.map { $0.type.abiRepresentation }.joined(separator: ",")))"
188204
}
189205

206+
/// Hashed signature of an event, e.g. `0xcf78cf0d6f3d8371e1075c69c492ab4ec5d8cf23a1a239b6a51a1d00be7ca312`.
190207
public var topic: Data {
191208
return signature.data(using: .ascii)!.sha3(.keccak256)
192209
}
193210
}
194211

195212
extension ABI.Element.EthError {
213+
/// String representation of an error, e.g. `TrasferFailed(address)`.
196214
public var signature: String {
197215
return "\(name)(\(inputs.map { $0.type.abiRepresentation }.joined(separator: ",")))"
198216
}
199217

218+
/// Error selector, e.g. `"cafe1234"`. Without hex prefix `0x`.
219+
@available(*, deprecated, renamed: "selector", message: "Please, use 'selector' property instead.")
200220
public var methodString: String {
221+
return selector
222+
}
223+
224+
/// Error selector, e.g. `"cafe1234"`. Without hex prefix `0x`.
225+
public var selector: String {
201226
return String(signature.sha3(.keccak256).prefix(8))
202227
}
203228

229+
/// Error selector (e.g. `0xcafe1234`) but as raw bytes.
230+
@available(*, deprecated, renamed: "selectorEncoded", message: "Please, use 'selectorEncoded' property instead.")
204231
public var methodEncoding: Data {
205-
return signature.data(using: .ascii)!.sha3(.keccak256)[0...3]
232+
return selectorEncoded
233+
}
234+
235+
/// Error selector (e.g. `0xcafe1234`) but as raw bytes.
236+
public var selectorEncoded: Data {
237+
return Data.fromHex(selector)!
206238
}
207239
}
208240

209241
extension ABI.Element.ParameterType: ABIEncoding {
242+
243+
/// Returns a valid solidity type like `address`, `uint128` or any other built-in type from Solidity.
210244
public var abiRepresentation: String {
211245
switch self {
212246
case .uint(let bits):

Sources/Web3Core/EthereumNetwork/Request/APIRequest+Methods.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func spelunkData(value: Any?) -> (message: String, data: String)? {
7676

7777
extension APIRequest {
7878
public static func sendRequest<Result>(with provider: Web3Provider, for call: APIRequest) async throws -> APIResponse<Result> {
79-
try await send(call.call, parameter: call.parameters, with: provider)
79+
try await send(call.call, parameters: call.parameters, with: provider)
8080
}
8181

8282
static func setupRequest(for body: RequestBody, with provider: Web3Provider) -> URLRequest {
@@ -106,7 +106,7 @@ extension APIRequest {
106106
}
107107

108108
/// Checks if `Result` type can be initialized from HEX-encoded bytes.
109-
/// If it can - we attempt initializing a value of `Result` type.
109+
/// If it can - we attempt initializing a value of `Result` type.
110110
if let LiteralType = Result.self as? LiteralInitiableFromString.Type {
111111
guard let responseAsString = try? JSONDecoder().decode(APIResponse<String>.self, from: data) else { throw Web3Error.dataError }
112112
guard let literalValue = LiteralType.init(from: responseAsString.result) else { throw Web3Error.dataError }

Sources/web3swift/Web3/Web3+HttpProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class Web3HttpProvider: Web3Provider {
3434
network = net
3535
} else {
3636
/// chain id could be a hex string or an int value.
37-
let response: String = try await APIRequest.send(APIRequest.getNetwork.call, parameter: [], with: self).result
37+
let response: String = try await APIRequest.send(APIRequest.getNetwork.call, parameters: [], with: self).result
3838
let result: UInt
3939
if response.hasHexPrefix() {
4040
guard let num = BigUInt(response, radix: 16) else {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// ABIElementsTests.swift
3+
//
4+
//
5+
// Created by JeneaVranceanu on 09.01.2024.
6+
//
7+
8+
import Foundation
9+
import XCTest
10+
@testable import web3swift
11+
@testable import Web3Core
12+
13+
class ABIElementsTests: XCTestCase {
14+
15+
func testABIElementFunction() {
16+
let test1Function = ABI.Element.Function(name: "Test1",
17+
inputs: [],
18+
outputs: [],
19+
constant: true,
20+
payable: false)
21+
22+
XCTAssertEqual(test1Function.name, "Test1")
23+
XCTAssertEqual(test1Function.selector, String("Test1()".sha3(.keccak256).prefix(8)))
24+
XCTAssertEqual(test1Function.selectorEncoded,
25+
Data.fromHex("Test1()".sha3(.keccak256))?.prefix(4))
26+
27+
}
28+
29+
}

0 commit comments

Comments
 (0)