Add cancel on error during filling state

When calling fillSlot, any transaction errors that occur (possibly during estimate gas) will cause that tx to be replaced with a cancellation transaction (a 0-valued tx to ourselves).
This commit is contained in:
Eric 2023-09-12 12:35:03 +10:00
parent c068e7425b
commit 58a97856b4
No known key found for this signature in database
4 changed files with 25 additions and 3 deletions

View File

@ -110,7 +110,14 @@ method getActiveSlot*(market: OnChainMarket,
return none Slot
raise e
method fillSlot(market: OnChainMarket,
proc cancelTransaction(market: OnChainMarket, nonce: UInt256) {.async.} =
let address = await market.getSigner()
let cancelTx = Transaction(to: address, value: 0.u256, nonce: some nonce)
let populated = await market.signer.populateTransaction(cancelTx)
trace "cancelling transaction to prevent stuck transactions", nonce
discard market.signer.sendTransaction(populated)
method fillSlot*(market: OnChainMarket,
requestId: RequestId,
slotIndex: UInt256,
proof: seq[byte],

View File

@ -76,6 +76,17 @@ method getActiveSlot*(
raiseAssert("not implemented")
method cancelTransaction(market: Market, nonce: UInt256) {.base, async.} =
raiseAssert("not implemented")
template cancelOnError*(market: Market, body) =
try:
body
except JsonRpcProviderError as e:
if e.nonce.isSome:
# send a 0-valued transaction with the errored nonce to prevent stuck txs
await market.cancelTransaction(!e.nonce)
method fillSlot*(market: Market,
requestId: RequestId,
slotIndex: UInt256,

View File

@ -36,5 +36,6 @@ method run(state: SaleFilling, machine: Machine): Future[?State] {.async.} =
raiseAssert("no slot index assigned")
debug "Filling slot", requestId = $data.requestId, slotIndex
await market.fillSlot(data.requestId, slotIndex, state.proof, collateral)
debug "Waiting for slot filled event...", requestId = $data.requestId, slotIndex
market.cancelOnError:
await market.fillSlot(data.requestId, slotIndex, state.proof, collateral)
debug "Waiting for slot filled event...", requestId = $data.requestId, slotIndex

View File

@ -205,6 +205,9 @@ proc emitRequestFailed*(market: MockMarket, requestId: RequestId) =
subscription.requestId.isNone:
subscription.callback(requestId)
proc cancelTransaction(market: Market, nonce: UInt256) {.async.} =
discard
proc fillSlot*(market: MockMarket,
requestId: RequestId,
slotIndex: UInt256,