Skip to content

Commit ce493d7

Browse files
committed
Update blockchain providers
1 parent 022189a commit ce493d7

File tree

3 files changed

+73
-45
lines changed

3 files changed

+73
-45
lines changed

dkg/providers/blockchain/async_blockchain.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -183,20 +183,12 @@ async def fetch_gas_price(oracle_url: str) -> Wei | None:
183183
response.raise_for_status()
184184
data: dict = await response.json()
185185

186-
gas_price = None
187-
if "fast" in data:
188-
gas_price = self.w3.to_wei(data["fast"], "gwei")
189-
elif "result" in data:
190-
gas_price = int(data["result"], 16)
186+
if "result" in data:
187+
return int(data["result"], 16)
188+
elif "average" in data:
189+
return self.w3.to_wei(data["average"], "gwei")
191190
else:
192191
return None
193-
194-
# Ensure minimum gas price (2 gwei = 2,000,000,000 wei)
195-
min_gas_price = self.w3.to_wei(2, "gwei")
196-
if gas_price < min_gas_price:
197-
return min_gas_price
198-
199-
return gas_price
200192
except Exception:
201193
return None
202194

@@ -210,16 +202,7 @@ async def fetch_gas_price(oracle_url: str) -> Wei | None:
210202
if gas_price is not None:
211203
return gas_price
212204

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(2, "gwei")
217-
if network_gas_price < min_gas_price:
218-
return min_gas_price
219-
return network_gas_price
220-
except Exception:
221-
# Final fallback: return minimum gas price
222-
return self.w3.to_wei(2, "gwei")
205+
return None
223206

224207
async def _init_contracts(self):
225208
init_tasks = []

dkg/providers/blockchain/blockchain.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -168,20 +168,12 @@ def fetch_gas_price(oracle_url: str) -> Wei | None:
168168
response.raise_for_status()
169169
data: dict = response.json()
170170

171-
gas_price = None
172-
if "fast" in data:
173-
gas_price = self.w3.to_wei(data["fast"], "gwei")
174-
elif "result" in data:
175-
gas_price = int(data["result"], 16)
171+
if "result" in data:
172+
return int(data["result"], 16)
173+
elif "average" in data:
174+
return self.w3.to_wei(data["average"], "gwei")
176175
else:
177176
return None
178-
179-
# Ensure minimum gas price (2 gwei = 2,000,000,000 wei)
180-
min_gas_price = self.w3.to_wei(2, "gwei")
181-
if gas_price < min_gas_price:
182-
return min_gas_price
183-
184-
return gas_price
185177
except Exception:
186178
return None
187179

@@ -195,16 +187,7 @@ def fetch_gas_price(oracle_url: str) -> Wei | None:
195187
if gas_price is not None:
196188
return gas_price
197189

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(2, "gwei")
202-
if network_gas_price < min_gas_price:
203-
return min_gas_price
204-
return network_gas_price
205-
except Exception:
206-
# Final fallback: return minimum gas price
207-
return self.w3.to_wei(2, "gwei")
190+
return None
208191

209192
def _init_contracts(self):
210193
for contract in self.abi.keys():

tests/patched_blockchain_provider.py

Lines changed: 63 additions & 1 deletion
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,61 @@ 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
32-
self.name = blockchain_id
44+
self.name = blockchain_id
45+
46+
def _get_network_gas_price(self):
47+
"""Override the gas price method with enhanced logic"""
48+
if self.environment == "development":
49+
return None
50+
51+
def fetch_gas_price(oracle_url: str):
52+
try:
53+
import requests
54+
response = requests.get(oracle_url)
55+
response.raise_for_status()
56+
data: dict = response.json()
57+
58+
gas_price = None
59+
if "fast" in data:
60+
gas_price = self.w3.to_wei(data["fast"], "gwei")
61+
elif "result" in data:
62+
gas_price = int(data["result"], 16)
63+
else:
64+
return None
65+
66+
# Ensure minimum gas price (2 gwei = 2,000,000,000 wei)
67+
min_gas_price = self.w3.to_wei(2, "gwei")
68+
if gas_price < min_gas_price:
69+
return min_gas_price
70+
71+
return gas_price
72+
except Exception:
73+
return None
74+
75+
oracles = self.gas_price_oracle
76+
if oracles is not None:
77+
if isinstance(oracles, str):
78+
oracles = [oracles]
79+
80+
for oracle_url in oracles:
81+
gas_price = fetch_gas_price(oracle_url)
82+
if gas_price is not None:
83+
return gas_price
84+
85+
# Fallback: use network gas price with minimum
86+
try:
87+
network_gas_price = self.w3.eth.gas_price
88+
min_gas_price = self.w3.to_wei(2, "gwei")
89+
if network_gas_price < min_gas_price:
90+
return min_gas_price
91+
return network_gas_price
92+
except Exception:
93+
# Final fallback: return minimum gas price
94+
return self.w3.to_wei(2, "gwei")

0 commit comments

Comments
 (0)