Skip to content

Commit 97d65e4

Browse files
committed
Support optimistic loading
1 parent 34213b2 commit 97d65e4

1 file changed

Lines changed: 24 additions & 12 deletions

File tree

bundler/bundler.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from web3.exceptions import TimeExhausted
2+
13
from .models import Bundle
24
from .serializers import OperationSerialzer
35

@@ -174,7 +176,8 @@ def eth_sendUserOperation(request) -> Result:
174176
gasEstimation = transactionTemplate.estimate_gas()
175177
except Exception as inst:
176178
print('\033[91m' + "Bundle operation failed (Gas estimation reverted): " + str(inst) + '\033[39m')
177-
return Error(2, "Bundle operation failed", data=str(inst))
179+
return Error(2, "failed-to-submit", data={"status": "failed-to-submit", "txHash": None})
180+
178181
gasFees = getGasFees()
179182

180183
txnDict = {
@@ -184,7 +187,7 @@ def eth_sendUserOperation(request) -> Result:
184187
'gas': math.ceil(gasEstimation * 1.4),
185188
}
186189

187-
if(env('isGanache') == "True"): #as ganache evm doesn't support maxFeePerGas & maxPriorityFeePerGas
190+
if env('isGanache') == "True": #as ganache evm doesn't support maxFeePerGas & maxPriorityFeePerGas
188191
txnDict.update({
189192
'gasPrice': math.ceil(float(gasFees["medium"]["suggestedMaxFeePerGas"]))
190193
})
@@ -199,32 +202,41 @@ def eth_sendUserOperation(request) -> Result:
199202
sign_store_txn = w3.eth.account.sign_transaction(
200203
transaction, private_key=env('bundler_pk')
201204
)
202-
205+
tx_hash = ""
203206
try:
204207
send_tx = w3.eth.send_raw_transaction(sign_store_txn.rawTransaction)
205-
tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx)
206-
tx_hash = tx_receipt['transactionHash'].hex()
207-
print('\033[92m' + "Bundle Sent - with state : " + str(tx_receipt['status']) + '\033[39m')
208-
print('\033[92m' + "Transaction hash : " + str(tx_hash) + '\033[39m')
209-
bundle.status='Successful'
208+
tx_hash = str(send_tx.hex())
209+
tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx, timeout=3)
210+
print('\033[92m' + "Bundle Sent (" + tx_hash + ") - with state: " + str(tx_receipt['status']) + '\033[39m')
211+
bundle.status = 'Successful'
210212
bundle.save()
211213

212214
for operation in operations:
213215
operation.bundle = bundle
214216
operation.save()
215217

216-
return Success("Success : " + str(tx_receipt))
218+
return Success({"status": "success", "txHash": tx_hash})
219+
except TimeExhausted as inst:
220+
print('\033[92m' + "Bundle operation timeout: " + str(inst) + '\033[39m')
221+
222+
bundle.status = 'Pending'
223+
bundle.save()
224+
225+
for operation in operations:
226+
operation.bundle = bundle
227+
operation.save()
217228

229+
return Success({"status": "pending", "txHash": tx_hash})
218230
except Exception as inst:
219-
print('\033[91m' + "Bundle operation failed : " + str(inst) + '\033[39m')
220-
bundle.status='Failure'
231+
print('\033[91m' + "Bundle operation failed: " + str(inst) + '\033[39m')
232+
bundle.status = 'Failure'
221233
bundle.save()
222234

223235
for operation in operations:
224236
operation.bundle = bundle
225237
operation.save()
226238

227-
return Error(2, "Bundle operation failed", data=str(inst))
239+
return Error(2, "failed", data={"status": "failed", "txHash": tx_hash})
228240

229241
@method
230242
def eth_getRequestIds(request) -> Result:

0 commit comments

Comments
 (0)