Skip to content

Commit 412972b

Browse files
Update for network layer:
- `GetCode` - `GetTransactionCount` - `GetTransactionReceipt` - `CreateAccount` - `UnlockAccount`
1 parent 7c5126a commit 412972b

8 files changed

+32
-62
lines changed

Sources/web3swift/API/HexDecodableProtocols.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ extension Array: APIResultType where Element: APIResultType { }
1313

1414
extension String: APIResultType { }
1515

16+
extension Bool: APIResultType { }
17+
1618
/// This is utility protocol for decoding API Responses
1719
///
1820
/// You better not use it in any other part of a bit of code except `APIResponse<T>` decoding.

Sources/web3swift/API/Web3+APIMethod.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public enum APIRequest {
2727
case sendRawTransaction(Hash)
2828
case sendTransaction(TransactionParameters)
2929
case getTransactionByHash(Hash)
30-
case getTransactionReceipt(Receipt)
30+
case getTransactionReceipt(Hash)
3131
case getLogs(EventFilterParameters)
3232
case personalSign(Address, Data)
3333
case call(TransactionParameters)
@@ -105,6 +105,10 @@ extension APIRequest {
105105
case .gasPrice: return BigUInt.self
106106
case .feeHistory: return Web3.Oracle.FeeHistory.self
107107
case .getTransactionCount: return UInt.self
108+
case .getCode: return Hash.self
109+
case .getTransactionReceipt: return TransactionReceipt.self
110+
case .createAccount: return EthereumAddress.self
111+
case .unlockAccount: return Bool.self
108112
default: return String.self
109113
}
110114
}
@@ -225,6 +229,8 @@ extension APIRequest {
225229
guard let httpResponse = response as? HTTPURLResponse,
226230
200 ..< 400 ~= httpResponse.statusCode else { throw Web3Error.connectionError }
227231

232+
/// This bit of code is purposed to work with literal types that comes in Response in hexString type.
233+
/// Currently it's just any kind of Integers like `(U)Int`, `Big(U)Int`.
228234
if Result.self == UInt.self || Result.self == Int.self || Result.self == BigInt.self || Result.self == BigUInt.self {
229235
/// This types for sure conformed with `LiteralInitiableFromString`
230236
// FIXME: Make appropriate error

Sources/web3swift/Promises/Promise+Web3+Eth+GetBlockByHash.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import BigInt
1010

1111
extension web3.Eth {
1212
public func block(by hash: Data, fullTransactions: Bool = false) async throws -> Block {
13-
try await block(by: hashString.toHexString().addHexPrefix(), fullTransactions: fullTransactions)
13+
try await block(by: hash.toHexString().addHexPrefix(), fullTransactions: fullTransactions)
1414
}
1515

1616
public func block(by hash: String, fullTransactions: Bool = false) async throws -> Block {

Sources/web3swift/Promises/Promise+Web3+Eth+GetCode.swift

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,13 @@ import Foundation
1010
import BigInt
1111

1212
extension web3.Eth {
13-
public func code(for address: EthereumAddress, onBlock: String = "latest") async throws -> String {
14-
let addr = address.address
15-
return try await code(for : addr, onBlock: onBlock)
13+
public func code(for address: EthereumAddress, onBlock: BlockNumber) async throws -> Hash {
14+
try await code(for : address.address, onBlock: onBlock)
1615
}
17-
public func code(for address: String, onBlock: String = "latest") async throws -> String {
18-
let request = JSONRPCRequestFabric.prepareRequest(.getCode, parameters: [address.lowercased(), onBlock])
19-
let response = try await web3.dispatch(request)
20-
21-
guard let value: String = response.getValue() else {
22-
if response.error != nil {
23-
throw Web3Error.nodeError(desc: response.error!.message)
24-
}
25-
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
26-
}
27-
return value
28-
16+
17+
public func code(for address: Address, onBlock: BlockNumber) async throws -> Hash {
18+
let requestCall: APIRequest = .getCode(address, onBlock)
19+
let response: APIResponse<Hash> = try await APIRequest.sendRequest(with: self.provider, for: requestCall)
20+
return response.result
2921
}
3022
}

Sources/web3swift/Promises/Promise+Web3+Eth+GetTransactionCount.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extension web3.Eth {
1414

1515
public func getTransactionCount(address: Address, onBlock: BlockNumber) async throws -> UInt {
1616
let requestCall: APIRequest = .getTransactionCount(address, onBlock)
17-
let response: APIResponse<UInt> = APIRequest.sendRequest(with: self.provider, for: requestCall)
17+
let response: APIResponse<UInt> = try await APIRequest.sendRequest(with: self.provider, for: requestCall)
1818
return response.result
1919
}
2020
}

Sources/web3swift/Promises/Promise+Web3+Eth+GetTransactionReceipt.swift

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,16 @@
77
import Foundation
88
import BigInt
99

10+
extension TransactionReceipt: APIResultType { }
1011

1112
extension web3.Eth {
1213
public func transactionReceipt(_ txhash: Data) async throws -> TransactionReceipt {
13-
let hashString = txhash.toHexString().addHexPrefix()
14-
return try await self.transactionReceipt(hashString)
14+
try await self.transactionReceipt(txhash.toHexString().addHexPrefix())
1515
}
1616

17-
public func transactionReceipt(_ txhash: String) async throws -> TransactionReceipt {
18-
let request = JSONRPCRequestFabric.prepareRequest(.getTransactionReceipt, parameters: [txhash])
19-
let response = try await web3.dispatch(request)
20-
21-
guard let value: TransactionReceipt = response.getValue() else {
22-
if response.error != nil {
23-
throw Web3Error.nodeError(desc: response.error!.message)
24-
}
25-
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
26-
}
27-
return value
28-
17+
public func transactionReceipt(_ txhash: Hash) async throws -> TransactionReceipt {
18+
let requestCall: APIRequest = .getTransactionReceipt(txhash)
19+
let response: APIResponse<TransactionReceipt> = try await APIRequest.sendRequest(with: self.provider, for: requestCall)
20+
return response.result
2921
}
3022
}

Sources/web3swift/Promises/Promise+Web3+Personal+CreateAccount.swift

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,12 @@ import BigInt
1010

1111
extension web3.Personal {
1212
public func createAccount(password: String = "web3swift") async throws -> EthereumAddress {
13-
1413
guard self.web3.provider.attachedKeystoreManager == nil else {
1514
throw Web3Error.inputError(desc: "Creating account in a local keystore with this method is not supported")
1615
}
1716

18-
let request = JSONRPCRequestFabric.prepareRequest(.createAccount, parameters: [password])
19-
let response = try await self.web3.dispatch(request)
20-
21-
guard let value: EthereumAddress = response.getValue() else {
22-
if response.error != nil {
23-
throw Web3Error.nodeError(desc: response.error!.message)
24-
}
25-
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
26-
}
27-
return value
28-
17+
let requestCall: APIRequest = .createAccount(password)
18+
let response: APIResponse<EthereumAddress> = try await APIRequest.sendRequest(with: self.provider, for: requestCall)
19+
return response.result
2920
}
3021
}

Sources/web3swift/Promises/Promise+Web3+Personal+UnlockAccount.swift

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,16 @@ import BigInt
1010

1111
extension web3.Personal {
1212
public func unlock(account: EthereumAddress, password: String = "web3swift", seconds: UInt = 300) async throws -> Bool {
13-
let addr = account.address
14-
return try await unlock(account: addr, password: password, seconds: seconds)
13+
try await unlock(account: account.address, password: password, seconds: seconds)
1514
}
1615

17-
public func unlock(account: String, password: String = "web3swift", seconds: UInt = 300) async throws -> Bool {
18-
16+
public func unlock(account: Address, password: String = "web3swift", seconds: UInt = 300) async throws -> Bool {
1917
guard self.web3.provider.attachedKeystoreManager == nil else {
2018
throw Web3Error.inputError(desc: "Can not unlock a local keystore")
2119
}
2220

23-
let parameters: [APIRequestParameterType] = [account.lowercased(), password, seconds]
24-
25-
let request = JSONRPCRequestFabric.prepareRequest(.unlockAccount, parameters: parameters)
26-
27-
let response = try await self.web3.dispatch(request)
28-
29-
guard let value: Bool = response.getValue() else {
30-
if response.error != nil {
31-
throw Web3Error.nodeError(desc: response.error!.message)
32-
}
33-
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
34-
}
35-
return value
36-
21+
let requestCall: APIRequest = .unlockAccount(account, password, seconds)
22+
let response: APIResponse<Bool> = try await APIRequest.sendRequest(with: self.provider, for: requestCall)
23+
return response.result
3724
}
3825
}

0 commit comments

Comments
 (0)