Skip to content

Commit de708de

Browse files
committed
Update gas price
1 parent 33d5064 commit de708de

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

dkg/providers/blockchain/async_blockchain.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,19 @@ async def fetch_gas_price(oracle_url: str) -> Wei | None:
184184
data: dict = await response.json()
185185

186186
if "result" in data:
187-
return int(data["result"], 16)
187+
gas_price = int(data["result"], 16)
188188
elif "fast" in data:
189-
return self.w3.to_wei(data["fast"], "gwei")
189+
gas_price = self.w3.to_wei(data["fast"], "gwei")
190190
else:
191191
return None
192+
193+
# Ensure minimum gas price (1 gwei = 1,000,000,000 wei)
194+
min_gas_price = self.w3.to_wei(1, "gwei") # 1 gwei minimum
195+
if gas_price < min_gas_price:
196+
print(f"⚠️ Gas price from oracle too low ({gas_price} wei), using minimum ({min_gas_price} wei)")
197+
return min_gas_price
198+
199+
return gas_price
192200
except Exception:
193201
return None
194202

@@ -202,7 +210,17 @@ async def fetch_gas_price(oracle_url: str) -> Wei | None:
202210
if gas_price is not None:
203211
return gas_price
204212

205-
return None
213+
# Fallback: use network gas price with minimum
214+
try:
215+
network_gas_price = await self.w3.eth.gas_price
216+
min_gas_price = self.w3.to_wei(1, "gwei") # 1 gwei minimum
217+
if network_gas_price < min_gas_price:
218+
print(f"⚠️ Network gas price too low ({network_gas_price} wei), using minimum ({min_gas_price} wei)")
219+
return min_gas_price
220+
return network_gas_price
221+
except Exception:
222+
# Final fallback: return minimum gas price
223+
return self.w3.to_wei(1, "gwei")
206224

207225
async def _init_contracts(self):
208226
init_tasks = []

dkg/providers/blockchain/blockchain.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,19 @@ def fetch_gas_price(oracle_url: str) -> Wei | None:
169169
data: dict = response.json()
170170

171171
if "result" in data:
172-
return int(data["result"], 16)
172+
gas_price = int(data["result"], 16)
173173
elif "fast" in data:
174-
return self.w3.to_wei(data["fast"], "gwei")
174+
gas_price = self.w3.to_wei(data["fast"], "gwei")
175175
else:
176176
return None
177+
178+
# Ensure minimum gas price (1 gwei = 1,000,000,000 wei)
179+
min_gas_price = self.w3.to_wei(1, "gwei") # 1 gwei minimum
180+
if gas_price < min_gas_price:
181+
print(f"⚠️ Gas price from oracle too low ({gas_price} wei), using minimum ({min_gas_price} wei)")
182+
return min_gas_price
183+
184+
return gas_price
177185
except Exception:
178186
return None
179187

@@ -187,7 +195,17 @@ def fetch_gas_price(oracle_url: str) -> Wei | None:
187195
if gas_price is not None:
188196
return gas_price
189197

190-
return None
198+
# Fallback: use network gas price with minimum
199+
try:
200+
network_gas_price = self.w3.eth.gas_price
201+
min_gas_price = self.w3.to_wei(1, "gwei") # 1 gwei minimum
202+
if network_gas_price < min_gas_price:
203+
print(f"⚠️ Network gas price too low ({network_gas_price} wei), using minimum ({min_gas_price} wei)")
204+
return min_gas_price
205+
return network_gas_price
206+
except Exception:
207+
# Final fallback: return minimum gas price
208+
return self.w3.to_wei(1, "gwei")
191209

192210
def _init_contracts(self):
193211
for contract in self.abi.keys():

tests/patched_blockchain_provider.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
'otp:20430': ("NEUROWEB_TESTNET_RPC", ["testnet", "otp:20430"]),
1515
}
1616

17+
# Gas price oracles for mainnet networks
18+
GAS_PRICE_ORACLES = {
19+
'base:8453': "https://api.basescan.org/api?module=gastracker&action=gasoracle",
20+
'gnosis:100': "https://api.gnosisscan.io/api?module=gastracker&action=gasoracle",
21+
'otp:2043': "https://api.origintrail.io/api?module=gastracker&action=gasoracle",
22+
}
23+
1724
class BlockchainProvider(OriginalBlockchainProvider):
1825
def __init__(self, blockchain_id):
1926
config = BLOCKCHAIN_RPC_CONFIG.get(blockchain_id)
@@ -27,6 +34,11 @@ def __init__(self, blockchain_id):
2734
for key in path[:-1]:
2835
d = d[key]
2936
d[path[-1]]["rpc"] = rpc_url
37+
38+
# Add gas price oracle for mainnet networks
39+
if blockchain_id in GAS_PRICE_ORACLES:
40+
d[path[-1]]["gas_price_oracle"] = GAS_PRICE_ORACLES[blockchain_id]
41+
3042
super().__init__(blockchain_id)
3143
self.environment = path[0] if config else None
3244
self.name = blockchain_id

0 commit comments

Comments
 (0)