Skip to content

Commit e86c10b

Browse files
Merge pull request #119 from matter-labs/estimateGasFix
fixed estimate gas problem
2 parents 50893a0 + 3534e20 commit e86c10b

File tree

3 files changed

+65
-3
lines changed

3 files changed

+65
-3
lines changed

web3swift/Web3/Classes/Web3+ReadingTransaction.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ public class ReadTransaction {
7171
optionsForGasEstimation.from = mergedOptions.from
7272
optionsForGasEstimation.to = mergedOptions.to
7373
optionsForGasEstimation.value = mergedOptions.value
74+
75+
// MARK: - Fixing estimate gas problem: gas price param shouldn't be nil
76+
if let gasPricePolicy = mergedOptions.gasPrice {
77+
switch gasPricePolicy {
78+
case .manual( _):
79+
optionsForGasEstimation.gasPrice = gasPricePolicy
80+
default:
81+
optionsForGasEstimation.gasPrice = .manual(1) // 1 wei to fix wrong estimating gas problem
82+
}
83+
}
84+
7485
optionsForGasEstimation.callOnBlock = mergedOptions.callOnBlock
7586
let promise = self.web3.eth.estimateGasPromise(assembledTransaction, transactionOptions: optionsForGasEstimation)
7687
promise.done(on: queue) {(estimate: BigUInt) in

web3swift/Web3/Classes/Web3+Utils.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ extension Web3.Utils {
6666
}
6767
}
6868

69+
// Precoded API for testing estimate gas fix
70+
public static var estimateGasTestABI = """
71+
[{"constant":true,"inputs":[],"name":"a","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"foo","type":"uint256"}],"name":"test","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"b","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]
72+
"""
73+
6974
/// Precoded "cold wallet" (private key controlled) address. Basically - only a payable fallback function.
7075
public static var coldWalletABI = "[{\"payable\":true,\"type\":\"fallback\"}]"
7176

web3swiftTests/web3swift_promises_Tests.swift

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,61 @@ class web3swift_promises_Tests: XCTestCase {
4646
guard let writeTX = contract?.write("fallback") else {return XCTFail()}
4747
writeTX.transactionOptions.from = tempKeystore!.addresses?.first
4848
writeTX.transactionOptions.value = BigUInt("1.0", .eth)
49-
let esimate = try writeTX.estimateGasPromise().wait()
50-
print(esimate)
51-
XCTAssert(esimate == 21000)
49+
let estimate = try writeTX.estimateGasPromise().wait()
50+
print(estimate)
51+
XCTAssert(estimate == 21000)
5252
} catch{
5353
print(error)
5454
XCTFail()
5555
}
5656
}
5757

58+
func testEstimateGasFixPromise() {
59+
do {
60+
let web3 = Web3.InfuraMainnetWeb3()
61+
let tempKeystore = try! EthereumKeystoreV3(password: "")
62+
let keystoreManager = KeystoreManager([tempKeystore!])
63+
web3.addKeystoreManager(keystoreManager)
64+
65+
guard let contractAddress = EthereumAddress("0x28a958cD020efeA3734a0bb36DDdc5F9B872cEa8"),
66+
let contract = web3.contract(Web3.Utils.estimateGasTestABI,
67+
at: contractAddress,
68+
abiVersion: 2) else {
69+
return
70+
}
71+
72+
var options = TransactionOptions.defaultOptions
73+
let fromAddress = tempKeystore!.addresses?.first
74+
options.from = fromAddress
75+
76+
let amount1 = Web3.Utils.parseToBigUInt("0.000000000000000001", units: .eth) // 1 wei
77+
78+
guard let tx1 = contract.write("test",
79+
parameters: [amount1] as [AnyObject],
80+
extraData: Data(),
81+
transactionOptions: options) else {
82+
return
83+
}
84+
let estimate1 = try tx1.estimateGasPromise().wait()
85+
print(estimate1)
86+
87+
let amount2 = Web3.Utils.parseToBigUInt("0.00000005", units: .eth) // 50 gwei
88+
89+
guard let tx2 = contract.write("test",
90+
parameters: [amount2] as [AnyObject],
91+
extraData: Data(),
92+
transactionOptions: options) else {
93+
return
94+
}
95+
let estimate2 = try tx2.estimateGasPromise().wait()
96+
print(estimate2)
97+
XCTAssert(estimate2 - estimate1 <= 22000)
98+
} catch {
99+
print(error)
100+
XCTFail()
101+
}
102+
}
103+
58104
// func testSendETHPromise() {
59105
// do {
60106
// guard let keystoreData = getKeystoreData() else {return}

0 commit comments

Comments
 (0)