Merge pull request #2100 from AlphaWallet/fix-gas-limit-not-set-from-node-for-token-transactions

set gas limit from node rather than hardcoding for token transactions
pull/2104/head
James Sangalli 4 years ago committed by GitHub
commit 408d8006aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      AlphaWallet/EtherClient/Requests/EstimateGasRequest.swift
  2. 27
      AlphaWallet/Transfer/Controllers/TransactionConfigurator.swift
  3. 2
      AlphaWallet/Transfer/Types/GasLimitConfiguration.swift
  4. 2
      AlphaWallet/Transfer/Types/TransactionConfiguration.swift

@ -21,7 +21,7 @@ struct EstimateGasRequest: JSONRPCKit.Request {
[
"from": from.description,
"to": to?.description ?? "",
"value": value.description.hexEncoded,
"value": "0x" + String(value, radix: 16) ?? "0x0",
"data": data.hexEncoded,
],
]

@ -85,10 +85,18 @@ class TransactionConfigurator {
return token.contractAddress
}
}()
//TODO transaction.value should only ever be the attached native currency, not the erc20 amount as that is included in the data
let value: BigInt = {
switch transaction.transferType {
case .nativeCryptocurrency, .dapp, .ERC875TokenOrder: return transaction.value
case .ERC20Token, .ERC721Token, .ERC721ForTicketToken, .ERC875Token:
return 0;
}
}()
let request = EstimateGasRequest(
from: session.account.address,
to: to,
value: transaction.value,
value: value,
data: configuration.data
)
Session.send(EtherServiceRequest(server: session.server, batch: BatchFactory().create(request))) { [weak self] result in
@ -102,12 +110,7 @@ class TransactionConfigurator {
}
return min(limit + (limit * 20 / 100), GasLimitConfiguration.maxGasLimit)
}()
strongSelf.configuration = TransactionConfiguration(
gasPrice: strongSelf.calculatedGasPrice,
gasLimit: gasLimit,
data: strongSelf.configuration.data,
nonce: strongSelf.configuration.nonce
)
strongSelf.configuration.gasLimit = gasLimit
case .failure: break
}
}
@ -157,12 +160,6 @@ class TransactionConfigurator {
// swiftlint:disable function_body_length
func load(completion: @escaping (ResultResult<Void, AnyError>.t) -> Void) {
/* the node can provide reliable gas limit estimates, this prevents running out of gas or defaulting to an
inappropriately high gas limit. This can also be an issue for native transfers which are 21k if send to an EOA
address but may be higher if sent to a contract address. */
if gasLimitNotSet {
estimateGasLimit()
}
switch transaction.transferType {
case .dapp:
estimateGasPrice()
@ -278,6 +275,10 @@ class TransactionConfigurator {
completion(.failure(AnyError(Web3Error(description: "malformed tx"))))
}
}
/* the node can provide reliable gas limit estimates, this prevents running out of gas or defaulting to an
inappropriately high gas limit. This can also be an issue for native transfers which are 21k if send to an EOA
address but may be higher if sent to a contract address. */
estimateGasLimit()
}
// swiftlint:enable function_body_length

@ -5,6 +5,6 @@ import BigInt
public struct GasLimitConfiguration {
static let defaultGasLimit = BigInt(90_000)
static let minGasLimit = BigInt(21_000) // ETH transfers are always 21k
static let minGasLimit = BigInt(21_000)
static let maxGasLimit = BigInt(1_000_000)
}

@ -5,7 +5,7 @@ import BigInt
struct TransactionConfiguration {
let gasPrice: BigInt
let gasLimit: BigInt
var gasLimit: BigInt
let data: Data
let nonce: Int?

Loading…
Cancel
Save