Skip to content

Commit 4e8d174

Browse files
fixed estimate gas problem
1 parent 50893a0 commit 4e8d174

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

web3swift/Transaction/Classes/EthereumTransaction.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,15 @@ public struct EthereumTransaction: CustomStringConvertible {
253253
if transactionOptions != nil, transactionOptions!.value != nil {
254254
tx.value = transactionOptions!.value!
255255
}
256+
// MARK: - Fixing estimate gas problem: gas price param shouldn't be nil
257+
if transactionOptions != nil, let gasPricePolicy = transactionOptions!.gasPrice {
258+
switch gasPricePolicy {
259+
case .manual(let value):
260+
tx.gasPrice = value
261+
default:
262+
tx.gasPrice = BigUInt(1) // 1 wei to fix wrong estimating gas problem
263+
}
264+
}
256265
guard var txParams = tx.encodeAsDictionary(from: from) else {return nil}
257266
if method == .estimateGas || transactionOptions?.gasLimit == nil {
258267
txParams.gas = nil

web3swift/Web3/Classes/Web3+ReadingTransaction.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public class ReadTransaction {
7171
optionsForGasEstimation.from = mergedOptions.from
7272
optionsForGasEstimation.to = mergedOptions.to
7373
optionsForGasEstimation.value = mergedOptions.value
74+
optionsForGasEstimation.gasPrice = mergedOptions.gasPrice
7475
optionsForGasEstimation.callOnBlock = mergedOptions.callOnBlock
7576
let promise = self.web3.eth.estimateGasPromise(assembledTransaction, transactionOptions: optionsForGasEstimation)
7677
promise.done(on: queue) {(estimate: BigUInt) in

web3swiftTests/web3swift_promises_Tests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,27 @@ class web3swift_promises_Tests: XCTestCase {
5555
}
5656
}
5757

58+
func testEstimateGasFixPromise() {
59+
do {
60+
let web3 = Web3.InfuraMainnetWeb3()
61+
let sendToAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")
62+
let tempKeystore = try! EthereumKeystoreV3(password: "")
63+
let keystoreManager = KeystoreManager([tempKeystore!])
64+
web3.addKeystoreManager(keystoreManager)
65+
let contract = web3.contract(Web3.Utils.coldWalletABI, at: sendToAddress, abiVersion: 2)
66+
guard let writeTX = contract?.write("fallback") else {return XCTFail()}
67+
writeTX.transactionOptions.from = tempKeystore!.addresses?.first
68+
writeTX.transactionOptions.value = BigUInt("1.0", .eth)
69+
writeTX.transactionOptions.gasPrice = .manual(BigUInt(100000000))
70+
let esimate = try writeTX.estimateGasPromise().wait()
71+
print(esimate)
72+
XCTAssert(esimate == 21000)
73+
} catch{
74+
print(error)
75+
XCTFail()
76+
}
77+
}
78+
5879
// func testSendETHPromise() {
5980
// do {
6081
// guard let keystoreData = getKeystoreData() else {return}

0 commit comments

Comments
 (0)