Skip to content

Commit

Permalink
chore: added docs; new test for ABI.Element.Function;
Browse files Browse the repository at this point in the history
  • Loading branch information
JeneaVranceanu committed Jan 8, 2024
1 parent 084a2cb commit e6ff991
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
38 changes: 36 additions & 2 deletions Sources/Web3Core/EthereumABI/ABIParameterTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,45 +168,79 @@ extension ABI.Element.ParameterType: Equatable {
}

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

/// Function selector, e.g. `"cafe1234"`. Without hex prefix `0x`.
@available(*, deprecated, renamed: "selector", message: "Please, use 'selector' property instead.")
public var methodString: String {
return selector
}

/// Function selector, e.g. `"cafe1234"`. Without hex prefix `0x`.
public var selector: String {
return String(signature.sha3(.keccak256).prefix(8))
}

/// Function selector (e.g. `0xcafe1234`) but as raw bytes.
@available(*, deprecated, renamed: "selectorEncoded", message: "Please, use 'selectorEncoded' property instead.")
public var methodEncoding: Data {
return signature.data(using: .ascii)!.sha3(.keccak256)[0...3]
return selectorEncoded
}

/// Function selector (e.g. `0xcafe1234`) but as raw bytes.
public var selectorEncoded: Data {
return Data.fromHex(selector)!
}
}

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

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

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

/// Error selector, e.g. `"cafe1234"`. Without hex prefix `0x`.
@available(*, deprecated, renamed: "selector", message: "Please, use 'selector' property instead.")
public var methodString: String {
return selector
}

/// Error selector, e.g. `"cafe1234"`. Without hex prefix `0x`.
public var selector: String {
return String(signature.sha3(.keccak256).prefix(8))
}

/// Error selector (e.g. `0xcafe1234`) but as raw bytes.
@available(*, deprecated, renamed: "selectorEncoded", message: "Please, use 'selectorEncoded' property instead.")
public var methodEncoding: Data {
return signature.data(using: .ascii)!.sha3(.keccak256)[0...3]
return selectorEncoded
}

/// Error selector (e.g. `0xcafe1234`) but as raw bytes.
public var selectorEncoded: Data {
return Data.fromHex(selector)!
}
}

extension ABI.Element.ParameterType: ABIEncoding {

/// Returns a valid solidity type like `address`, `uint128` or any other built-in type from Solidity.
public var abiRepresentation: String {
switch self {
case .uint(let bits):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func spelunkData(value: Any?) -> (message: String, data: String)? {

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

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

/// Checks if `Result` type can be initialized from HEX-encoded bytes.
/// If it can - we attempt initializing a value of `Result` type.
/// If it can - we attempt initializing a value of `Result` type.
if let LiteralType = Result.self as? LiteralInitiableFromString.Type {
guard let responseAsString = try? JSONDecoder().decode(APIResponse<String>.self, from: data) else { throw Web3Error.dataError }
guard let literalValue = LiteralType.init(from: responseAsString.result) else { throw Web3Error.dataError }
Expand Down
2 changes: 1 addition & 1 deletion Sources/web3swift/Web3/Web3+HttpProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class Web3HttpProvider: Web3Provider {
network = net
} else {
/// chain id could be a hex string or an int value.
let response: String = try await APIRequest.send(APIRequest.getNetwork.call, parameter: [], with: self).result
let response: String = try await APIRequest.send(APIRequest.getNetwork.call, parameters: [], with: self).result
let result: UInt
if response.hasHexPrefix() {
guard let num = BigUInt(response, radix: 16) else {
Expand Down
29 changes: 29 additions & 0 deletions Tests/web3swiftTests/localTests/ABIElementsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// ABIElementsTests.swift
//
//
// Created by JeneaVranceanu on 09.01.2024.
//

import Foundation
import XCTest
@testable import web3swift
@testable import Web3Core

class ABIElementsTests: XCTestCase {

func testABIElementFunction() {
let test1Function = ABI.Element.Function(name: "Test1",
inputs: [],
outputs: [],
constant: true,
payable: false)

XCTAssertEqual(test1Function.name, "Test1")
XCTAssertEqual(test1Function.selector, String("Test1()".sha3(.keccak256).prefix(8)))
XCTAssertEqual(test1Function.selectorEncoded,
Data.fromHex("Test1()".sha3(.keccak256))?.prefix(4))

}

}

0 comments on commit e6ff991

Please sign in to comment.