Skip to content

Commit 210b187

Browse files
authored
Merge pull request #324 from linen-app/gas_estimate_fix
Gas estimate fix
2 parents d5825d4 + 2b31b76 commit 210b187

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,22 @@ extension web3.Eth {
2424
}
2525
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
2626
}
27-
return value
27+
28+
if let policy = transactionOptions?.gasLimit {
29+
switch policy {
30+
case .automatic:
31+
return value
32+
case .limited(let limitValue):
33+
return limitValue < value ? limitValue: value
34+
case .manual(let exactValue):
35+
return exactValue
36+
case .withMargin:
37+
// MARK: - update value according margin
38+
return value
39+
}
40+
} else {
41+
return value
42+
}
2843
}
2944
} catch {
3045
let returnPromise = Promise<BigUInt>.pending()

Sources/web3swift/Web3/Web3+MutatingTransaction.swift

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,21 @@ public class WriteTransaction: ReadTransaction {
7272
optionsForGasEstimation.from = mergedOptions.from
7373
optionsForGasEstimation.to = mergedOptions.to
7474
optionsForGasEstimation.value = mergedOptions.value
75+
optionsForGasEstimation.gasLimit = mergedOptions.gasLimit
7576
optionsForGasEstimation.callOnBlock = mergedOptions.callOnBlock
76-
let gasEstimatePromise : Promise<BigUInt> = self.web3.eth.estimateGasPromise(assembledTransaction, transactionOptions: optionsForGasEstimation)
77+
78+
// assemble promise for gasLimit
79+
var gasEstimatePromise: Promise<BigUInt>? = nil
80+
guard let gasLimitPolicy = mergedOptions.gasLimit else {
81+
seal.reject(Web3Error.inputError(desc: "No gasLimit policy provided"))
82+
return
83+
}
84+
switch gasLimitPolicy {
85+
case .automatic, .withMargin, .limited:
86+
gasEstimatePromise = self.web3.eth.estimateGasPromise(assembledTransaction, transactionOptions: optionsForGasEstimation)
87+
case .manual(let gasLimit):
88+
gasEstimatePromise = Promise<BigUInt>.value(gasLimit)
89+
}
7790

7891
// assemble promise for nonce
7992
var getNoncePromise: Promise<BigUInt>?
@@ -90,9 +103,20 @@ public class WriteTransaction: ReadTransaction {
90103
getNoncePromise = Promise<BigUInt>.value(nonce)
91104
}
92105

93-
let gasPricePromise : Promise<BigUInt> = self.web3.eth.getGasPricePromise()
94-
var promisesToFulfill: [Promise<BigUInt>] = [getNoncePromise!, gasPricePromise, gasPricePromise]
95-
when(resolved: getNoncePromise!, gasEstimatePromise, gasPricePromise).map(on: queue, { (results:[PromiseResult<BigUInt>]) throws -> EthereumTransaction in
106+
// assemble promise for gasPrice
107+
var gasPricePromise: Promise<BigUInt>? = nil
108+
guard let gasPricePolicy = mergedOptions.gasPrice else {
109+
seal.reject(Web3Error.inputError(desc: "No gasPrice policy provided"))
110+
return
111+
}
112+
switch gasPricePolicy {
113+
case .automatic, .withMargin:
114+
gasPricePromise = self.web3.eth.getGasPricePromise()
115+
case .manual(let gasPrice):
116+
gasPricePromise = Promise<BigUInt>.value(gasPrice)
117+
}
118+
var promisesToFulfill: [Promise<BigUInt>] = [getNoncePromise!, gasPricePromise!, gasEstimatePromise!]
119+
when(resolved: getNoncePromise!, gasEstimatePromise!, gasPricePromise!).map(on: queue, { (results:[PromiseResult<BigUInt>]) throws -> EthereumTransaction in
96120

97121
promisesToFulfill.removeAll()
98122
guard case .fulfilled(let nonce) = results[0] else {

0 commit comments

Comments
 (0)