Skip to content

Commit acd66fe

Browse files
committed
resolve reviews suggestions
1 parent 20bfa7a commit acd66fe

File tree

5 files changed

+45
-13
lines changed

5 files changed

+45
-13
lines changed

Sources/Web3Core/Contract/ContractProtocol.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ extension DefaultContractProtocol {
321321
}
322322

323323
guard let function = methods[method]?.first else {
324-
throw Web3Error.inputError(desc: "Function method does not exist.")
324+
throw Web3Error.inputError(desc: "Make sure ABI you use contains '\(method)' method.")
325325
}
326326

327327
switch data.count % 32 {
@@ -343,7 +343,7 @@ extension DefaultContractProtocol {
343343
throw Web3Error.inputError(desc: "Signature matches \(customError.errorDeclaration) but failed to be decoded.")
344344
}
345345
} else {
346-
throw Web3Error.inputError(desc: "Found no matched error")
346+
throw Web3Error.inputError(desc: "Make sure ABI you use contains error that can match signature: 0x\(selector.toHexString())")
347347
}
348348
default:
349349
throw Web3Error.inputError(desc: "Invalid data count")

Sources/Web3Core/EthereumABI/ABIElements.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ extension ABI.Element.EthError {
240240
}
241241

242242
/// Decodes `revert(string)` and `require(expression, string)` calls.
243-
/// These calls are decomposed as `Error(string` error.
243+
/// These calls are decomposed as `Error(string)` error.
244244
public static func decodeStringError(_ data: Data) -> String? {
245245
let decoded = ABIDecoder.decode(types: [.init(name: "", type: .string)], data: data)
246246
return decoded?.first as? String
@@ -411,6 +411,7 @@ extension ABI.Element.Function {
411411
/// // "_parsingError" is optional and is present only if decoding of custom error arguments failed
412412
/// "_parsingError": "Data matches MyCustomError(uint256, address senderAddress) but failed to be decoded."]
413413
/// ```
414+
@available(*, deprecated, message: "Use `ABI.Element.EthError.decodeEthError(_:)` instead")
414415
public func decodeErrorResponse(_ data: Data, errors: [String: ABI.Element.EthError]? = nil) -> [String: Any]? {
415416
/// If data is empty and outputs are expected it is treated as a `require(expression)` or `revert()` call with no message.
416417
/// In solidity `require(false)` and `revert()` calls return empty error response.
@@ -442,7 +443,6 @@ extension ABI.Element.Function {
442443
let errors = errors,
443444
let customError = errors[data[data.startIndex ..< data.startIndex + 4].toHexString().stripHexPrefix()] {
444445
var errorResponse: [String: Any] = ["_success": false, "_abortedByRevertOrRequire": true, "_error": customError.errorDeclaration]
445-
// customError.decodeEthError(data[4...])
446446

447447
if (data.count > 32 && !customError.inputs.isEmpty),
448448
let decodedInputs = ABIDecoder.decode(types: customError.inputs, data: Data(data[data.startIndex + 4 ..< data.startIndex + data.count])) {

Sources/web3swift/Web3/Web3+HttpProvider.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,15 @@ public class Web3HttpProvider: Web3Provider {
3737
let response: String = try await APIRequest.send(APIRequest.getNetwork.call, parameter: [], with: self).result
3838
let result: UInt
3939
if response.hasHexPrefix() {
40-
result = UInt(BigUInt(response, radix: 16) ?? Networks.Mainnet.chainID)
40+
guard let num = BigUInt(response, radix: 16) else {
41+
throw Web3Error.processingError(desc: "Get network successed but can't be parsed to a valid chain id")
42+
}
43+
result = UInt(num)
4144
} else {
42-
result = UInt(response) ?? UInt(Networks.Mainnet.chainID)
45+
guard let num = UInt(response) else {
46+
throw Web3Error.processingError(desc: "Get network successed but can't be parsed to a valid chain id")
47+
}
48+
result = num
4349
}
4450
self.network = Networks.fromInt(result)
4551
}

Tests/web3swiftTests/remoteTests/InfuraTests.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,6 @@ import Web3Core
1010

1111
// MARK: Works only with network connection
1212
class InfuraTests: XCTestCase {
13-
14-
func testGetNetwork() async throws {
15-
let requestURLstring = "https://" + Networks.Mainnet.name + Constants.infuraHttpScheme + Constants.infuraToken
16-
let web3 = try await Web3HttpProvider(url: URL(string: requestURLstring)!, network: nil)
17-
XCTAssertEqual(web3.network?.chainID, 1)
18-
}
19-
2013
func testGetBalance() async throws {
2114
let web3 = try await Web3.InfuraMainnetWeb3(accessToken: Constants.infuraToken)
2215
let address = EthereumAddress("0xd61b5ca425F8C8775882d4defefC68A6979DBbce")!
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Web3HttpProviderTests.swift
3+
//
4+
//
5+
// Created by liugang zhang on 2023/9/2.
6+
//
7+
8+
import XCTest
9+
import Web3Core
10+
11+
@testable import web3swift
12+
13+
final class Web3HttpProviderTests: XCTestCase {
14+
15+
/// if one of these rpc server lose efficacy, find a substitution from https://chainlist.org/
16+
func testGetNetwork() async throws {
17+
let requestURLstring = "https://" + Networks.Mainnet.name + Constants.infuraHttpScheme + Constants.infuraToken
18+
var web3 = try await Web3HttpProvider(url: URL(string: requestURLstring)!, network: nil)
19+
XCTAssertEqual(web3.network?.chainID, 1)
20+
21+
web3 = try await Web3HttpProvider(url: URL(string: "https://arbitrum-one.publicnode.com")!, network: nil)
22+
XCTAssertEqual(web3.network?.chainID, 42161)
23+
24+
web3 = try await Web3HttpProvider(url: URL(string: "https://rpc.ankr.com/bsc")!, network: nil)
25+
XCTAssertEqual(web3.network?.chainID, 56)
26+
27+
web3 = try await Web3HttpProvider(url: URL(string: "https://rpc-mainnet.maticvigil.com/")!, network: nil)
28+
XCTAssertEqual(web3.network?.chainID, 137)
29+
30+
web3 = try await Web3HttpProvider(url: URL(string: "https://optimism.gateway.tenderly.co")!, network: nil)
31+
XCTAssertEqual(web3.network?.chainID, 10)
32+
}
33+
}

0 commit comments

Comments
 (0)