Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

Commit

Permalink
Add gasPrice to request
Browse files Browse the repository at this point in the history
  • Loading branch information
ant013 committed Nov 19, 2019
1 parent b7ab480 commit ac01c20
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 15 deletions.
10 changes: 5 additions & 5 deletions Erc20Kit/Erc20Kit/Core/Erc20Kit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Erc20Kit {

private let state: KitState

init(ethereumKit: EthereumKit, transactionManager: ITransactionManager, balanceManager: IBalanceManager, gasLimit: Int = 1_000_000, state: KitState = KitState()) {
init(ethereumKit: EthereumKit, transactionManager: ITransactionManager, balanceManager: IBalanceManager, gasLimit: Int, state: KitState = KitState()) {
self.ethereumKit = ethereumKit
self.transactionManager = transactionManager
self.balanceManager = balanceManager
Expand Down Expand Up @@ -100,7 +100,7 @@ extension Erc20Kit {
state.transactionsSubject.asObservable()
}

public func estimateGas(to: String, contractAddress: String, value: String) -> Single<Int> {
public func estimateGas(to: String, contractAddress: String, value: String, gasPrice: Int?) -> Single<Int> {
guard let amountValue = BigUInt(value) else {
return Single.error(ValidationError.invalidValue)
}
Expand All @@ -109,7 +109,7 @@ extension Erc20Kit {
let toAddress = try convert(address: to)
let data = transactionManager.transactionContractData(to: toAddress, value: amountValue)

return ethereumKit.estimateGas(contractAddress: contractAddress, amount: nil, gasLimit: gasLimit, data: data)
return ethereumKit.estimateGas(contractAddress: contractAddress, amount: nil, gasLimit: gasLimit, gasPrice: gasPrice, data: data)
} catch {
return Single.error(ValidationError.invalidAddress)
}
Expand Down Expand Up @@ -151,7 +151,7 @@ extension Erc20Kit: IBalanceManagerDelegate {

extension Erc20Kit {

public static func instance(ethereumKit: EthereumKit, contractAddress: String) throws -> Erc20Kit {
public static func instance(ethereumKit: EthereumKit, contractAddress: String, gasLimit: Int = 1_000_000) throws -> Erc20Kit {
let databaseFileName = "\(ethereumKit.uniqueId)-\(contractAddress)"

guard let contractAddress = Data(hex: contractAddress) else {
Expand All @@ -167,7 +167,7 @@ extension Erc20Kit {
var transactionManager: ITransactionManager = TransactionManager(contractAddress: contractAddress, address: address, storage: storage, dataProvider: dataProvider, transactionBuilder: transactionBuilder)
var balanceManager: IBalanceManager = BalanceManager(contractAddress: contractAddress, address: address, storage: storage, dataProvider: dataProvider)

let erc20Kit = Erc20Kit(ethereumKit: ethereumKit, transactionManager: transactionManager, balanceManager: balanceManager)
let erc20Kit = Erc20Kit(ethereumKit: ethereumKit, transactionManager: transactionManager, balanceManager: balanceManager, gasLimit: gasLimit)

transactionManager.delegate = erc20Kit
balanceManager.delegate = erc20Kit
Expand Down
4 changes: 2 additions & 2 deletions EthereumKit/EthereumKit/Api/Core/ApiBlockchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ extension ApiBlockchain: IBlockchain {
}
}

func estimateGas(from: String?, contractAddress: String, amount: BigUInt?, gasLimit: Int?, data: Data?) -> Single<Int> {
rpcApiProvider.getEstimateGas(from: from, contractAddress: contractAddress, amount: amount, gasLimit: gasLimit, data: data?.toHexString())
func estimateGas(from: String?, contractAddress: String, amount: BigUInt?, gasLimit: Int?, gasPrice: Int?, data: Data?) -> Single<Int> {
rpcApiProvider.getEstimateGas(from: from, contractAddress: contractAddress, amount: amount, gasLimit: gasLimit, gasPrice: gasPrice, data: data?.toHexString())
.flatMap { (value: String) -> Single<Int> in
guard let data = Int(value.stripHexPrefix(), radix: 16) else {
return Single.error(EthereumKit.ApiError.invalidData)
Expand Down
2 changes: 1 addition & 1 deletion EthereumKit/EthereumKit/Api/Core/ApiProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ protocol IRpcApiProvider {
func getLogs(address: Data?, fromBlock: Int?, toBlock: Int?, topics: [Any?]) -> Single<[EthereumLog]>
func getStorageAt(contractAddress: String, position: String, blockNumber: Int?) -> Single<String>
func call(contractAddress: String, data: String, blockNumber: Int?) -> Single<String>
func getEstimateGas(from: String?, contractAddress: String, amount: BigUInt?, gasLimit: Int?, data: String?) -> Single<String>
func getEstimateGas(from: String?, contractAddress: String, amount: BigUInt?, gasLimit: Int?, gasPrice: Int?, data: String?) -> Single<String>
func getBlock(byNumber: Int) -> Single<Block>
}

Expand Down
5 changes: 4 additions & 1 deletion EthereumKit/EthereumKit/Api/Core/InfuraApiProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ extension InfuraApiProvider: IRpcApiProvider {
infuraStringSingle(method: "eth_call", params: [["to": contractAddress, "data": data], "latest"])
}

func getEstimateGas(from: String?, contractAddress: String, amount: BigUInt?, gasLimit: Int?, data: String?) -> Single<String> {
func getEstimateGas(from: String?, contractAddress: String, amount: BigUInt?, gasLimit: Int?, gasPrice: Int?, data: String?) -> Single<String> {
var params = [String: Any]()
if let from = from {
params["from"] = from.lowercased()
Expand All @@ -161,6 +161,9 @@ extension InfuraApiProvider: IRpcApiProvider {
if let gasLimit = gasLimit {
params["gas"] = "0x" + String(gasLimit, radix: 16).removeLeadingZeros()
}
if let gasPrice = gasPrice {
params["gas"] = "0x" + String(gasPrice, radix: 16).removeLeadingZeros()
}
params["to"] = contractAddress.lowercased()
params["data"] = data
return infuraStringSingle(method: "eth_estimateGas", params: [params])
Expand Down
4 changes: 2 additions & 2 deletions EthereumKit/EthereumKit/Core/EthereumKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ extension EthereumKit {
blockchain.call(contractAddress: contractAddress, data: data, blockHeight: blockHeight)
}

public func estimateGas(contractAddress: String, amount: BigUInt?, gasLimit: Int?, data: Data?) -> Single<Int> {
blockchain.estimateGas(from: receiveAddress, contractAddress: contractAddress, amount: amount, gasLimit: gasLimit, data: data)
public func estimateGas(contractAddress: String, amount: BigUInt?, gasLimit: Int?, gasPrice: Int?, data: Data?) -> Single<Int> {
blockchain.estimateGas(from: receiveAddress, contractAddress: contractAddress, amount: amount, gasLimit: gasLimit, gasPrice: gasPrice, data: data)
}

public func statusInfo() -> [(String, Any)] {
Expand Down
2 changes: 1 addition & 1 deletion EthereumKit/EthereumKit/Core/Protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protocol IBlockchain {
func getLogsSingle(address: Data?, topics: [Any?], fromBlock: Int, toBlock: Int, pullTimestamps: Bool) -> Single<[EthereumLog]>
func getStorageAt(contractAddress: Data, positionData: Data, blockHeight: Int) -> Single<Data>
func call(contractAddress: Data, data: Data, blockHeight: Int?) -> Single<Data>
func estimateGas(from: String?, contractAddress: String, amount: BigUInt?, gasLimit: Int?, data: Data?) -> Single<Int>
func estimateGas(from: String?, contractAddress: String, amount: BigUInt?, gasLimit: Int?, gasPrice: Int?, data: Data?) -> Single<Int>
}

protocol IBlockchainDelegate: class {
Expand Down
4 changes: 2 additions & 2 deletions EthereumKit/EthereumKit/Spv/Core/SpvBlockchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ extension SpvBlockchain: IBlockchain {
}
}

func estimateGas(from: String?, contractAddress: String, amount: BigUInt?, gasLimit: Int?, data: Data?) -> Single<Int> {
rpcApiProvider.getEstimateGas(from: from, contractAddress: contractAddress, amount: amount, gasLimit: gasLimit, data: data?.toHexString())
func estimateGas(from: String?, contractAddress: String, amount: BigUInt?, gasLimit: Int?, gasPrice: Int?, data: Data?) -> Single<Int> {
rpcApiProvider.getEstimateGas(from: from, contractAddress: contractAddress, amount: amount, gasLimit: gasLimit, gasPrice: gasPrice, data: data?.toHexString())
.flatMap { (value: String) -> Single<Int> in
guard let data = Int(value.stripHexPrefix(), radix: 16) else {
return Single.error(EthereumKit.ApiError.invalidData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ extension Erc20Adapter: IAdapter {
}

func estimatedGasLimit(to address: String, value: Decimal) -> Single<Int> {
erc20Kit.estimateGas(to: address, contractAddress: contractAddress, value: value.roundedString(decimal: decimal))
erc20Kit.estimateGas(to: address, contractAddress: contractAddress, value: value.roundedString(decimal: decimal), gasPrice: 5_000_000_000)
}

}

0 comments on commit ac01c20

Please sign in to comment.