Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gas estimate fix #324

Merged
merged 2 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Sources/web3swift/Promises/Promise+Web3+Eth+EstimateGas.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,22 @@ extension web3.Eth {
}
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
}
return value

if let policy = transactionOptions?.gasLimit {
switch policy {
case .automatic:
return value
case .limited(let limitValue):
return limitValue < value ? limitValue: value
case .manual(let exactValue):
return exactValue
case .withMargin:
// MARK: - update value according margin
return value
}
} else {
return value
}
}
} catch {
let returnPromise = Promise<BigUInt>.pending()
Expand Down
32 changes: 28 additions & 4 deletions Sources/web3swift/Web3/Web3+MutatingTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,21 @@ public class WriteTransaction: ReadTransaction {
optionsForGasEstimation.from = mergedOptions.from
optionsForGasEstimation.to = mergedOptions.to
optionsForGasEstimation.value = mergedOptions.value
optionsForGasEstimation.gasLimit = mergedOptions.gasLimit
optionsForGasEstimation.callOnBlock = mergedOptions.callOnBlock
let gasEstimatePromise : Promise<BigUInt> = self.web3.eth.estimateGasPromise(assembledTransaction, transactionOptions: optionsForGasEstimation)

// assemble promise for gasLimit
var gasEstimatePromise: Promise<BigUInt>? = nil
guard let gasLimitPolicy = mergedOptions.gasLimit else {
seal.reject(Web3Error.inputError(desc: "No gasLimit policy provided"))
return
}
switch gasLimitPolicy {
case .automatic, .withMargin, .limited:
gasEstimatePromise = self.web3.eth.estimateGasPromise(assembledTransaction, transactionOptions: optionsForGasEstimation)
case .manual(let gasLimit):
gasEstimatePromise = Promise<BigUInt>.value(gasLimit)
}

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

let gasPricePromise : Promise<BigUInt> = self.web3.eth.getGasPricePromise()
var promisesToFulfill: [Promise<BigUInt>] = [getNoncePromise!, gasPricePromise, gasPricePromise]
when(resolved: getNoncePromise!, gasEstimatePromise, gasPricePromise).map(on: queue, { (results:[PromiseResult<BigUInt>]) throws -> EthereumTransaction in
// assemble promise for gasPrice
var gasPricePromise: Promise<BigUInt>? = nil
guard let gasPricePolicy = mergedOptions.gasPrice else {
seal.reject(Web3Error.inputError(desc: "No gasPrice policy provided"))
return
}
switch gasPricePolicy {
case .automatic, .withMargin:
gasPricePromise = self.web3.eth.getGasPricePromise()
case .manual(let gasPrice):
gasPricePromise = Promise<BigUInt>.value(gasPrice)
}
var promisesToFulfill: [Promise<BigUInt>] = [getNoncePromise!, gasPricePromise!, gasEstimatePromise!]
when(resolved: getNoncePromise!, gasEstimatePromise!, gasPricePromise!).map(on: queue, { (results:[PromiseResult<BigUInt>]) throws -> EthereumTransaction in

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