Support gas limit estimation for contract deployment transactions

pull/3519/head
Hwee-Boon Yar 3 years ago
parent 060671dd5a
commit e2a7926a81
  1. 22
      AlphaWallet/EtherClient/Requests/EstimateGasRequest.swift
  2. 13
      AlphaWallet/Transfer/Controllers/TransactionConfigurator.swift
  3. 5
      AlphaWallet/Transfer/Types/GasLimitConfiguration.swift

@ -7,8 +7,22 @@ import BigInt
struct EstimateGasRequest: JSONRPCKit.Request {
typealias Response = String
enum TransactionType {
case normal(to: AlphaWallet.Address)
case contractDeployment
}
private var to: AlphaWallet.Address? {
switch transactionType {
case .normal(let to):
return to
case .contractDeployment:
return nil
}
}
let from: AlphaWallet.Address
let to: AlphaWallet.Address
let transactionType: TransactionType
let value: BigInt
let data: Data
@ -18,14 +32,16 @@ struct EstimateGasRequest: JSONRPCKit.Request {
var parameters: Any? {
//Explicit type declaration to speed up build time. 160msec -> <100ms, as of Xcode 11.7
let results: [[String: String]] = [
var results: [[String: String]] = [
[
"from": from.description,
"to": to.eip55String,
"value": "0x" + String(value, radix: 16),
"data": data.hexEncoded,
],
]
if let to: AlphaWallet.Address = to {
results[0]["to"] = to.eip55String
}
return results
}

@ -136,10 +136,15 @@ class TransactionConfigurator {
}
private func estimateGasLimit() {
guard let toAddress = toAddress else { return }
let transactionType: EstimateGasRequest.TransactionType
if let toAddress = toAddress {
transactionType = .normal(to: toAddress)
} else {
transactionType = .contractDeployment
}
let request = EstimateGasRequest(
from: session.account.address,
to: toAddress,
transactionType: transactionType,
value: value,
data: currentConfiguration.data
)
@ -170,9 +175,9 @@ class TransactionConfigurator {
}
self.delegate?.gasLimitEstimateUpdated(to: gasLimit, in: self)
}.catch({ e in
}.catch { e in
error(value: e, rpcServer: self.session.server)
})
}
}
private func estimateGasPrice() {

@ -6,5 +6,6 @@ import BigInt
public struct GasLimitConfiguration {
static let defaultGasLimit = BigInt(90_000)
static let minGasLimit = BigInt(21_000)
static let maxGasLimit = BigInt(1_000_000)
}
//TODO make max be 1M unless for contract deployment then bigger, maybe 2M
static let maxGasLimit = BigInt(2_000_000)
}
Loading…
Cancel
Save