Skip to content

Commit

Permalink
Merge pull request #1 from JeneaVranceanu/feat/decode-error
Browse files Browse the repository at this point in the history
fix: decode error PR update
  • Loading branch information
zhangliugang authored Nov 27, 2023
2 parents e66dd35 + 0ba7039 commit ab90bda
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Sources/Web3Core/Contract/ContractProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ extension DefaultContractProtocol {
throw Web3Error.inputError(desc: "Make sure ABI you use contains error that can match signature: 0x\(selector.toHexString())")
}
default:
throw Web3Error.inputError(desc: "Invalid data count")
throw Web3Error.inputError(desc: "Given data has invalid bytes count.")
}
}

Expand Down Expand Up @@ -395,7 +395,7 @@ extension DefaultContractProtocol {
@discardableResult
public func callStatic(_ method: String, parameters: [Any], provider: Web3Provider) async throws -> [String: Any] {
guard let address = address else {
throw Web3Error.inputError(desc: "address field is missing")
throw Web3Error.inputError(desc: "RPC failed: contract is missing an address.")
}
guard let data = self.method(method, parameters: parameters, extraData: nil) else {
throw Web3Error.dataError
Expand Down
12 changes: 11 additions & 1 deletion Sources/Web3Core/Utility/String+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,17 @@ extension String {
}

public var isHex: Bool {
stripHexPrefix().reduce(true, { $0 && $1.isHexDigit } )
var _str = self.trim()
if _str.isEmpty {
return false
}
_str = _str.stripHexPrefix()
for char in _str {
if !char.isHexDigit {
return false
}
}
return true
}

/// Splits a string into groups of `every` n characters, grouping from left-to-right by default. If `backwards` is true, right-to-left.
Expand Down
31 changes: 31 additions & 0 deletions Tests/web3swiftTests/localTests/String+ExtensionTests.swift.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// String+ExtensionTests.swift
//
// Created by JeneaVranceanu on 26.11.2023.
//

import Foundation
import XCTest

class StringExtensionsTest: XCTestCase {

func testIsHex() throws {
XCTAssertTrue("0x".isHex)
XCTAssertTrue("0xF".isHex)
XCTAssertTrue("F".isHex)
XCTAssertTrue("0xFF".isHex)
XCTAssertTrue("0x0123456789abcdefABCDEF".isHex)
XCTAssertTrue("0123456789abcdefABCDEF".isHex)
XCTAssertTrue("0123456789abcdefABCDEF ".isHex)
XCTAssertTrue(" 0123456789abcdefABCDEF ".isHex)
XCTAssertTrue(" 0123456789abcdefABCDEF".isHex)

XCTAssertFalse("".isHex)
XCTAssertFalse("-".isHex)
XCTAssertFalse("xyz".isHex)
XCTAssertFalse("0xCAFEQ".isHex)
XCTAssertFalse("R0123456789abcdefABCDEF ".isHex)
XCTAssertFalse(" R0123456789abcdefABCDEF ".isHex)
}

}

0 comments on commit ab90bda

Please sign in to comment.