Skip to content

Commit

Permalink
Merge pull request #1880 from gnosis/gh-1868/support-fixed-gas-prices…
Browse files Browse the repository at this point in the history
…-based-on-chain

gh-1868 Save gas price sources to the database and use when estimatin…
  • Loading branch information
Dmitry Bespalov authored Feb 1, 2022
2 parents f05e64d + d6f32f4 commit fb4c482
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
54 changes: 54 additions & 0 deletions Multisig/Logic/Models/Chain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,60 @@ extension Chain {

l2 = chainInfo.l2
features = chainInfo.features
gasPrice = chainInfo.gasPrice
}

var gasPrice: [SCGModels.GasPrice] {
get {
guard let sources = gasPriceSource else { return [] }
return sources.compactMap { element -> SCGModels.GasPrice? in
guard let source = element as? ChainGasPriceSource else { return nil }
switch source.sourceType {
case "ORACLE":
guard let uri = source.uri,
let gasParameter = source.gasParameter,
let gweiFactor = source.gweiFactor
else {
return nil
}
return .oracle(SCGModels.GasPriceOracle(uri: uri, gasParameter: gasParameter, gweiFactor: gweiFactor))

case "FIXED":
guard let weiValue = source.weiValue else { return nil }
return .fixed(SCGModels.GasPriceFixed(weiValue: weiValue))

default:
return .unknown
}
}
}
set {
let newSources = newValue.compactMap { gasPrice -> ChainGasPriceSource? in
guard let context = self.managedObjectContext else { return nil }

let value = ChainGasPriceSource(context: context)

switch gasPrice {
case .oracle(let oracle):
value.sourceType = "ORACLE"
value.uri = oracle.uri
value.gasParameter = oracle.gasParameter
value.gweiFactor = oracle.gweiFactor

case .fixed(let fixed):
value.sourceType = "FIXED"
value.weiValue = fixed.weiValue

case .unknown:
value.sourceType = "UNKNOWN"
}
return value
}
if let existing = gasPriceSource {
removeFromGasPriceSource(existing)
}
addToGasPriceSource(NSOrderedSet(array: newSources))
}
}

var features: [String]? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ class TransactionEstimationController {
typealias EstimateCompletion = (Result<(gas: Result<Sol.UInt64, Error>, transactionCount: Result<Sol.UInt64, Error>, gasPrice: Result<Sol.UInt256, Error>), Error>) -> Void

func estimateTransactionWithRpc(tx: EthTransaction, completion: @escaping EstimateCompletion) -> URLSessionTask? {
// check if we have hint from the chain configuration about the gas price. For now support only fixed.
// find the first 'fixed' gas price
var fixedGasPrice: Sol.UInt256? = nil

for case SCGModels.GasPrice.fixed(let fixed) in chain.gasPrice where Sol.UInt256(fixed.weiValue) != nil {
fixedGasPrice = Sol.UInt256(fixed.weiValue)!
break
}

// remove the fee because we want to estimate it.
var tx = tx
tx.removeFee()
Expand Down Expand Up @@ -108,7 +117,7 @@ class TransactionEstimationController {
result(request: getEstimateRequest, method: getEstimateLegacy, responses: responses).map(\.storage)
: result(request: getEstimateRequest, method: getEstimateNew, responses: responses).map(\.storage)
let txCountResult = result(request: getTransactionCountRequest, method: getTransactionCount, responses: responses).map(\.storage)
let priceResult = result(request: getPriceRequest, method: getPrice, responses: responses).map(\.storage)
let priceResult = fixedGasPrice.map { .success($0) } ?? result(request: getPriceRequest, method: getPrice, responses: responses).map(\.storage)

dispatchOnMainThread(completion(.success((gasResult, txCountResult, priceResult))))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="19574" systemVersion="21A559" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="19574" systemVersion="21C52" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="AddressBookEntry" representedClassName="AddressBookEntry" syncable="YES" codeGenerationType="class">
<attribute name="additionDate" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="address" optional="YES" attributeType="String"/>
Expand Down Expand Up @@ -52,6 +52,7 @@
<attribute name="rpcUrl" attributeType="URI"/>
<attribute name="rpcUrlAuthentication" attributeType="String" defaultValueString="API_KEY_PATH"/>
<attribute name="shortName" optional="YES" attributeType="String"/>
<relationship name="gasPriceSource" optional="YES" toMany="YES" deletionRule="Nullify" ordered="YES" destinationEntity="ChainGasPriceSource" inverseName="chain" inverseEntity="ChainGasPriceSource"/>
<relationship name="nativeCurrency" maxCount="1" deletionRule="Cascade" destinationEntity="ChainToken" inverseName="chain" inverseEntity="ChainToken"/>
<relationship name="safe" toMany="YES" deletionRule="Cascade" destinationEntity="Safe" inverseName="chain" inverseEntity="Safe"/>
<relationship name="theme" maxCount="1" deletionRule="Cascade" destinationEntity="ChainTheme" inverseName="chain" inverseEntity="ChainTheme"/>
Expand All @@ -61,6 +62,14 @@
</uniquenessConstraint>
</uniquenessConstraints>
</entity>
<entity name="ChainGasPriceSource" representedClassName="ChainGasPriceSource" syncable="YES" codeGenerationType="class">
<attribute name="gasParameter" optional="YES" attributeType="String"/>
<attribute name="gweiFactor" optional="YES" attributeType="String"/>
<attribute name="sourceType" optional="YES" attributeType="String"/>
<attribute name="uri" optional="YES" attributeType="String"/>
<attribute name="weiValue" optional="YES" attributeType="String"/>
<relationship name="chain" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Chain" inverseName="gasPriceSource" inverseEntity="Chain"/>
</entity>
<entity name="ChainTheme" representedClassName="ChainTheme" syncable="YES" codeGenerationType="class">
<attribute name="backgroundColor" attributeType="String"/>
<attribute name="textColor" attributeType="String"/>
Expand Down Expand Up @@ -110,14 +119,15 @@
<element name="AddressBookEntry" positionX="54" positionY="135" width="128" height="89"/>
<element name="AppSettings" positionX="54" positionY="18" width="128" height="284"/>
<element name="AppUser" positionX="54" positionY="63" width="128" height="89"/>
<element name="Chain" positionX="54" positionY="117" width="128" height="224"/>
<element name="CDEthTransaction" positionX="54" positionY="144" width="128" height="149"/>
<element name="Chain" positionX="54" positionY="117" width="128" height="239"/>
<element name="ChainTheme" positionX="72" positionY="135" width="128" height="74"/>
<element name="ChainToken" positionX="63" positionY="126" width="128" height="104"/>
<element name="KeyInfo" positionX="63" positionY="72" width="128" height="119"/>
<element name="Safe" positionX="-63" positionY="-18" width="128" height="134"/>
<element name="Selection" positionX="165.51171875" positionY="-15.453125" width="128" height="58"/>
<element name="WCKeySession" positionX="54" positionY="135" width="128" height="89"/>
<element name="WCSession" positionX="54" positionY="18" width="128" height="119"/>
<element name="CDEthTransaction" positionX="54" positionY="144" width="128" height="149"/>
<element name="ChainGasPriceSource" positionX="54" positionY="144" width="128" height="119"/>
</elements>
</model>

0 comments on commit fb4c482

Please sign in to comment.