Skip to content

Commit 635009a

Browse files
Update for network layer:
- `GetAccoung` - `GetBalance` - `GetBlockByHash` - `GetBlockByNumber`
1 parent 4e69483 commit 635009a

6 files changed

+45
-58
lines changed

Sources/web3swift/API/Web3+APIMethod.swift

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public enum APIRequest {
4444

4545
case getCode(Address, BlockNumber)
4646
case getBlockByHash(Hash, Bool)
47-
case getBlockByNumber(Hash, Bool)
47+
case getBlockByNumber(BlockNumber, Bool)
4848

4949
/// Returns fee history with a respect to given setup
5050
///
@@ -99,6 +99,9 @@ extension APIRequest {
9999
switch self {
100100
case .blockNumber: return UInt.self
101101
case .getAccounts: return [EthereumAddress].self
102+
case .getBalance: return BigUInt.self
103+
case .getBlockByHash: return Block.self
104+
case .getBlockByNumber: return Block.self
102105
case .feeHistory: return Web3.Oracle.FeeHistory.self
103106
default: return String.self
104107
}
@@ -115,43 +118,62 @@ extension APIRequest {
115118
switch self {
116119
case .gasPrice, .blockNumber, .getNetwork, .getAccounts, .estimateGas:
117120
return [RequestParameter]()
121+
118122
case let .sendRawTransaction(hash):
119123
return [RequestParameter.string(hash)]
124+
120125
case .sendTransaction(let transactionParameters):
121126
return [RequestParameter.transaction(transactionParameters)]
127+
122128
case .getTransactionByHash(let hash):
123129
return [RequestParameter.string(hash)]
130+
124131
case .getTransactionReceipt(let receipt):
125132
return [RequestParameter.string(receipt)]
133+
126134
case .getLogs(let eventFilterParameters):
127135
return [RequestParameter.eventFilter(eventFilterParameters)]
136+
128137
case .personalSign(let address, let data):
129138
// FIXME: Add second parameter
130139
return [RequestParameter.string(address)]
140+
131141
case .call(let transactionParameters):
132142
return [RequestParameter.transaction(transactionParameters)]
143+
133144
case .getTransactionCount(let address, let blockNumber):
134145
return [RequestParameter.string(address), RequestParameter.string(blockNumber.stringValue)]
146+
135147
case .getBalance(let address, let blockNumber):
136148
return [RequestParameter.string(address), RequestParameter.string(blockNumber.stringValue)]
149+
137150
case .getStorageAt(let address, let hash, let blockNumber):
138151
return [RequestParameter.string(address), RequestParameter.string(hash), RequestParameter.string(blockNumber.stringValue)]
152+
139153
case .getCode(let address, let blockNumber):
140154
return [RequestParameter.string(address), RequestParameter.string(blockNumber.stringValue)]
155+
141156
case .getBlockByHash(let hash, let bool):
142157
return [RequestParameter.string(hash), RequestParameter.bool(bool)]
143-
case .getBlockByNumber(let hash, let bool):
144-
return [RequestParameter.string(hash), RequestParameter.bool(bool)]
158+
159+
case .getBlockByNumber(let block, let bool):
160+
return [RequestParameter.string(block.stringValue), RequestParameter.bool(bool)]
161+
145162
case .feeHistory(let uInt, let blockNumber, let array):
146163
return [RequestParameter.string(uInt.hexString), RequestParameter.string(blockNumber.stringValue), RequestParameter.doubleArray(array)]
164+
147165
case .createAccount(let string):
148166
return [RequestParameter]()
167+
149168
case .unlockAccount(let address, let string, let optional):
150169
return [RequestParameter]()
170+
151171
case .getTxPoolStatus:
152172
return [RequestParameter]()
173+
153174
case .getTxPoolContent:
154175
return [RequestParameter]()
176+
155177
case .getTxPoolInspect:
156178
return [RequestParameter]()
157179
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ extension Web3.Oracle.FeeHistory: APIResultType { }
1414
extension web3.Eth {
1515
func feeHistory(blockCount: UInt, block: BlockNumber, percentiles:[Double]) async throws -> Web3.Oracle.FeeHistory {
1616
let requestCall: APIRequest = .feeHistory(blockCount, block, percentiles)
17-
1817
let response: APIResponse<Web3.Oracle.FeeHistory> = try await APIRequest.sendRequest(with: web3.provider, for: requestCall)
19-
2018
return response.result
2119
}
2220
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ extension web3.Eth {
1212
guard self.web3.provider.attachedKeystoreManager == nil else {
1313
return try self.web3.wallet.getAccounts()
1414
}
15-
16-
let requestCall: APIRequest = .getAccounts
17-
18-
let response: APIResponse<[EthereumAddress]> = try await APIRequest.sendRequest(with: web3.provider, for: requestCall)
19-
15+
let response: APIResponse<[EthereumAddress]> = try await APIRequest.sendRequest(with: web3.provider, for: .getAccounts)
2016
return response.result
2117
}
2218
}

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,13 @@ import Foundation
99
import BigInt
1010

1111
extension web3.Eth {
12-
public func getBalance(for address: EthereumAddress, onBlock: String = "latest") async throws -> BigUInt {
13-
let addr = address.address
14-
return try await getBalance(address: addr, onBlock: onBlock)
12+
public func getBalance(for address: EthereumAddress, onBlock: BlockNumber = .latest) async throws -> BigUInt {
13+
try await getBalance(for: address.address, onBlock: onBlock)
1514
}
16-
public func getBalance(address: String, onBlock: String = "latest") async throws -> BigUInt {
17-
let request = JSONRPCRequestFabric.prepareRequest(.getBalance, parameters: [address.lowercased(), onBlock])
18-
let response = try await web3.dispatch(request)
19-
20-
guard let value: BigUInt = response.getValue() else {
21-
if response.error != nil {
22-
throw Web3Error.nodeError(desc: response.error!.message)
23-
}
24-
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
25-
}
26-
return value
2715

16+
public func getBalance(for address: Address, onBlock: BlockNumber = .latest) async throws -> BigUInt {
17+
let requestCall: APIRequest = .getBalance(address, onBlock)
18+
let response: APIResponse<BigUInt> = try await APIRequest.sendRequest(with: web3.provider, for: requestCall)
19+
return response.result
2820
}
2921
}

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,14 @@ import BigInt
99

1010

1111
extension web3.Eth {
12-
public func block(for hash: Data, fullTransactions: Bool = false) async throws -> Block {
12+
public func block(by hash: Data, fullTransactions: Bool = false) async throws -> Block {
1313
let hashString = hash.toHexString().addHexPrefix()
14-
return try await block(for: hashString, fullTransactions: fullTransactions)
14+
return try await block(by: hashString, fullTransactions: fullTransactions)
1515
}
1616

17-
public func block(for hash: String, fullTransactions: Bool = false) async throws -> Block {
18-
let request = JSONRPCRequestFabric.prepareRequest(.getBlockByHash, parameters: [hash, fullTransactions])
19-
let response = try await web3.dispatch(request)
20-
21-
guard let value: Block = 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 block(by hash: String, fullTransactions: Bool = false) async throws -> Block {
18+
let requestCall: APIRequest = .getBlockByHash(hash, fullTransactions)
19+
let response: APIResponse<Block> = try await APIRequest.sendRequest(with: self.provider, for: requestCall)
20+
return response.result
2921
}
3022
}

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

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,14 @@ import BigInt
99

1010

1111
extension web3.Eth {
12-
public func blockBy(number: UInt, fullTransactions: Bool = false) async throws -> Block {
13-
let block = String(number, radix: 16).addHexPrefix()
14-
return try await blockBy(number: block, fullTransactions: fullTransactions)
12+
public func block(by number: UInt, fullTransactions: Bool = false) async throws -> Block {
13+
let blockNumber = BlockNumber.exact(number)
14+
return try await block(by: blockNumber, fullTransactions: fullTransactions)
1515
}
1616

17-
public func blockBy(number: BigUInt, fullTransactions: Bool = false) async throws -> Block {
18-
let block = String(number, radix: 16).addHexPrefix()
19-
return try await blockBy(number: block, fullTransactions: fullTransactions)
20-
}
21-
22-
public func blockBy(number: String, fullTransactions: Bool = false) async throws -> Block {
23-
let request = JSONRPCRequestFabric.prepareRequest(.getBlockByNumber, parameters: [number, fullTransactions])
24-
let response = try await web3.dispatch(request)
25-
26-
guard let value: Block = response.getValue() else {
27-
if response.error != nil {
28-
throw Web3Error.nodeError(desc: response.error!.message)
29-
}
30-
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
31-
}
32-
return value
33-
17+
public func block(by number: BlockNumber, fullTransactions: Bool = false) async throws -> Block {
18+
let requestCall: APIRequest = .getBlockByNumber(number, fullTransactions)
19+
let response: APIResponse<Block> = try await APIRequest.sendRequest(with: self.provider, for: requestCall)
20+
return response.result
3421
}
3522
}

0 commit comments

Comments
 (0)