Skip to content

Commit fe44f97

Browse files
authored
Merge pull request #108 from BANKEX/develop
change estimate gas request and improve formatter
2 parents 2520268 + 127acb2 commit fe44f97

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

web3swift/Transaction/Classes/EthereumTransaction.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,10 @@ public struct EthereumTransaction: CustomStringConvertible {
317317
var request = JSONRPCrequest()
318318
request.method = method
319319
guard let from = options?.from else {return nil}
320-
guard let txParams = transaction.encodeAsDictionary(from: from) else {return nil}
320+
guard var txParams = transaction.encodeAsDictionary(from: from) else {return nil}
321+
if options?.gasLimit == nil {
322+
txParams.gas = nil
323+
}
321324
var params = [txParams] as Array<Encodable>
322325
if method.requiredNumOfParameters == 2 && onBlock != nil {
323326
params.append(onBlock as Encodable)

web3swift/Web3/Classes/Web3+Eth.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ extension web3.Eth {
293293
}
294294

295295
public func estimateGas(_ transaction: EthereumTransaction, options: Web3Options?, onBlock: String = "latest") -> Result<BigUInt, Web3Error> {
296-
let mergedOptions = Web3Options.merge(self.options, with: options)
296+
var mergedOptions = Web3Options.merge(self.options, with: options)
297+
mergedOptions?.gasLimit = nil // use gas limit of the previous block
297298
guard let request = EthereumTransaction.createRequest(method: JSONRPCmethod.estimateGas, transaction: transaction, onBlock: onBlock, options: mergedOptions) else {
298299
return Result.failure(Web3Error.inputError("Transaction serialization failed"))
299300
}

web3swift/Web3/Classes/Web3+Utils.swift

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ extension Web3.Utils {
169169
}
170170

171171
public static func formatToPrecision(_ bigNumber: BigUInt, numberDecimals: Int = 18, formattingDecimals: Int = 4, decimalSeparator: String = ".", fallbackToScientific: Bool = false) -> String? {
172+
if bigNumber == 0 {
173+
return "0"
174+
}
172175
let unitDecimals = numberDecimals
173176
var toDecimals = formattingDecimals
174177
if unitDecimals < toDecimals {
@@ -179,17 +182,21 @@ extension Web3.Utils {
179182
let fullRemainder = String(remainder);
180183
let fullPaddedRemainder = fullRemainder.leftPadding(toLength: unitDecimals, withPad: "0")
181184
let remainderPadded = fullPaddedRemainder[0..<toDecimals]
182-
if remainderPadded == String(repeating: "0", count: toDecimals) && quotient == 0 {
183-
var firstDigit = 0
184-
for char in fullPaddedRemainder {
185-
if (char == "0") {
186-
firstDigit = firstDigit + 1;
187-
} else {
188-
firstDigit = firstDigit + 1;
189-
break
185+
if remainderPadded == String(repeating: "0", count: toDecimals) {
186+
if quotient != 0 {
187+
return String(quotient)
188+
} else if fallbackToScientific {
189+
var firstDigit = 0
190+
for char in fullPaddedRemainder {
191+
if (char == "0") {
192+
firstDigit = firstDigit + 1;
193+
} else {
194+
firstDigit = firstDigit + 1;
195+
break
196+
}
190197
}
198+
return fullRemainder + "e-" + String(firstDigit)
191199
}
192-
return fullRemainder + "e-" + String(firstDigit)
193200
}
194201
if (toDecimals == 0) {
195202
return String(quotient)

web3swiftTests/web3swiftTests.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,6 +2213,14 @@ class web3swiftTests: XCTestCase {
22132213
let keystoreManager = KeystoreManager([tempKeystore!])
22142214
web3.addKeystoreManager(keystoreManager)
22152215
let intermediate = web3.eth.sendERC20tokensWithNaturalUnits(tokenAddress:contractAddress, from: coldWalletAddress, to: coldWalletAddress, amount: "1.0")
2216+
let gasEstimate = intermediate!.estimateGas(options: nil)
2217+
switch gasEstimate {
2218+
case .success(let result):
2219+
print(result)
2220+
case .failure(let error):
2221+
print(error)
2222+
XCTFail()
2223+
}
22162224
let bkxBalanceSend = intermediate!.call(options: nil)
22172225
switch bkxBalanceSend {
22182226
case .success(let result):
@@ -2267,6 +2275,12 @@ class web3swiftTests: XCTestCase {
22672275
XCTAssert(formatted == "-1e-18")
22682276
}
22692277

2278+
func testNumberFormattingUtil6() {
2279+
let balance = BigInt("0")!
2280+
let formatted = Web3.Utils.formatToPrecision(balance, numberDecimals: 18, formattingDecimals: 9, decimalSeparator: ",")
2281+
XCTAssert(formatted == "0")
2282+
}
2283+
22702284
func testPerformanceExample() {
22712285
// This is an example of a performance test case.
22722286
self.measure {

0 commit comments

Comments
 (0)