Skip to content

Commit 216686e

Browse files
Encapsulate request methods into APIRequest enum.
- Delete `EthereumTransaction.createRequest` - Delete `CallingBlockPolicy` enum which duplicating `BlockNumber` enum
1 parent 8207727 commit 216686e

26 files changed

+190
-280
lines changed

Sources/web3swift/API/APIMethod.swift

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ public enum APIRequest {
2222
case getNetwork
2323
case getAccounts
2424
// ??
25-
case estimateGas
26-
25+
case estimateGas(TransactionParameters, BlockNumber)
2726
case sendRawTransaction(Hash)
28-
case sendTransaction(TransactionParameters)
27+
case sendTransaction(TransactionParameters, BlockNumber)
2928
case getTransactionByHash(Hash)
3029
case getTransactionReceipt(Hash)
3130
case getLogs(EventFilterParameters)
32-
case personalSign(Address, Data)
33-
case call(TransactionParameters)
31+
case personalSign(Address, String)
32+
case call(TransactionParameters, BlockNumber)
3433
case getTransactionCount(Address, BlockNumber)
3534
case getBalance(Address, BlockNumber)
3635

@@ -88,6 +87,9 @@ public enum APIRequest {
8887
case getTxPoolInspect // No in Eth API
8988
}
9089

90+
// FIXME: This conformance should be changed to `LiteralInitiableFromString`
91+
extension Data: APIResultType { }
92+
9193
extension APIRequest {
9294
public var method: REST {
9395
switch self {
@@ -109,7 +111,21 @@ extension APIRequest {
109111
case .getTransactionReceipt: return TransactionReceipt.self
110112
case .createAccount: return EthereumAddress.self
111113
case .unlockAccount: return Bool.self
112-
default: return String.self
114+
case .getTransactionByHash: return TransactionDetails.self
115+
case .sendTransaction: return Hash.self
116+
case .sendRawTransaction: return Hash.self
117+
case .estimateGas: return BigUInt.self
118+
case .call: return Data.self
119+
// FIXME: Not checked
120+
case .getNetwork: return UInt.self
121+
case .personalSign: return Data.self
122+
123+
// FIXME: Not implemented
124+
case .getLogs: return String.self
125+
case .getStorageAt: return String.self
126+
case .getTxPoolStatus: return String.self
127+
case .getTxPoolContent: return String.self
128+
case .getTxPoolInspect: return String.self
113129
}
114130
}
115131

@@ -122,14 +138,17 @@ extension APIRequest {
122138

123139
var parameters: [RequestParameter] {
124140
switch self {
125-
case .gasPrice, .blockNumber, .getNetwork, .getAccounts, .estimateGas:
141+
case .gasPrice, .blockNumber, .getNetwork, .getAccounts:
126142
return [RequestParameter]()
127143

144+
case .estimateGas(let transactionParameters, let blockNumber):
145+
return [RequestParameter.transaction(transactionParameters), RequestParameter.string(blockNumber.stringValue)]
146+
128147
case let .sendRawTransaction(hash):
129148
return [RequestParameter.string(hash)]
130149

131-
case .sendTransaction(let transactionParameters):
132-
return [RequestParameter.transaction(transactionParameters)]
150+
case .sendTransaction(let transactionParameters, let blockNumber):
151+
return [RequestParameter.transaction(transactionParameters), RequestParameter.string(blockNumber.stringValue)]
133152

134153
case .getTransactionByHash(let hash):
135154
return [RequestParameter.string(hash)]
@@ -140,12 +159,12 @@ extension APIRequest {
140159
case .getLogs(let eventFilterParameters):
141160
return [RequestParameter.eventFilter(eventFilterParameters)]
142161

143-
case .personalSign(let address, let data):
162+
case .personalSign(let address, let string):
144163
// FIXME: Add second parameter
145-
return [RequestParameter.string(address)]
164+
return [RequestParameter.string(address), RequestParameter.string(string)]
146165

147-
case .call(let transactionParameters):
148-
return [RequestParameter.transaction(transactionParameters)]
166+
case .call(let transactionParameters, let blockNumber):
167+
return [RequestParameter.transaction(transactionParameters), RequestParameter.string(blockNumber.stringValue)]
149168

150169
case .getTransactionCount(let address, let blockNumber):
151170
return [RequestParameter.string(address), RequestParameter.string(blockNumber.stringValue)]
@@ -169,10 +188,10 @@ extension APIRequest {
169188
return [RequestParameter.string(uInt.hexString), RequestParameter.string(blockNumber.stringValue), RequestParameter.doubleArray(array)]
170189

171190
case .createAccount(let string):
172-
return [RequestParameter]()
191+
return [RequestParameter.string(string)]
173192

174-
case .unlockAccount(let address, let string, let optional):
175-
return [RequestParameter]()
193+
case .unlockAccount(let address, let string, let uInt):
194+
return [RequestParameter.string(address), RequestParameter.string(string), RequestParameter.uint(uInt ?? 0)]
176195

177196
case .getTxPoolStatus:
178197
return [RequestParameter]()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// web3swift
2+
//
3+
// Created by Alex Vlasov.
4+
// Copyright © 2018 Alex Vlasov. All rights reserved.
5+
//
6+
7+
import Foundation
8+
9+
10+
extension web3.Eth {
11+
12+
// FIXME: Possible this not working at all.
13+
public func callTransaction(_ transaction: EthereumTransaction, transactionOptions: TransactionOptions?) async throws -> Data {
14+
// FIXME: Add appropriate error
15+
guard let transactionParameters = transaction.encodeAsDictionary(from: transactionOptions?.from) else { throw Web3Error.unknownError}
16+
let request: APIRequest = .call(transactionParameters, transactionOptions?.callOnBlock ?? .latest)
17+
let response: APIResponse<Data> = try await APIRequest.sendRequest(with: self.provider, for: request)
18+
return response.result
19+
}
20+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// web3swift
2+
//
3+
// Created by Alex Vlasov.
4+
// Copyright © 2018 Alex Vlasov. All rights reserved.
5+
//
6+
7+
import Foundation
8+
import BigInt
9+
10+
11+
extension web3.Eth {
12+
13+
public func estimateGas(for transaction: EthereumTransaction, transactionOptions: TransactionOptions?) async throws -> BigUInt {
14+
// FIXME: Add appropriate error
15+
guard let transactionParameters = transaction.encodeAsDictionary(from: transactionOptions?.from) else { throw Web3Error.unknownError }
16+
17+
let request: APIRequest = .estimateGas(transactionParameters, transactionOptions?.callOnBlock ?? .latest)
18+
let response: APIResponse<BigUInt> = try await APIRequest.sendRequest(with: provider, for: request)
19+
20+
if let policy = transactionOptions?.gasLimit {
21+
switch policy {
22+
case .automatic:
23+
return response.result
24+
case .limited(let limitValue):
25+
return limitValue < response.result ? limitValue: response.result
26+
case .manual(let exactValue):
27+
return exactValue
28+
case .withMargin:
29+
// MARK: - update value according margin
30+
return response.result
31+
}
32+
} else {
33+
return response.result
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)