refactor: add MarketError (#670)

* Add MarketError

Add MarketError and convert all EthersErrors (ProviderError, SignerError) to MarketError

* Include token contract call in conversion of ethers error
This commit is contained in:
Eric 2024-03-21 11:30:04 +11:00 committed by GitHub
parent 916e4834d0
commit 43f0bf1c1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 165 additions and 111 deletions

View File

@ -2,9 +2,9 @@ import std/sequtils
import std/strutils import std/strutils
import std/sugar import std/sugar
import pkg/ethers import pkg/ethers
import pkg/ethers/testing
import pkg/upraises import pkg/upraises
import pkg/questionable import pkg/questionable
import ../utils/exceptions
import ../logutils import ../logutils
import ../market import ../market
import ./marketplace import ./marketplace
@ -32,147 +32,177 @@ func new*(_: type OnChainMarket, contract: Marketplace): OnChainMarket =
signer: signer, signer: signer,
) )
proc raiseMarketError(message: string) {.raises: [MarketError].} =
raise newException(MarketError, message)
template convertEthersError(body) =
try:
body
except EthersError as error:
raiseMarketError(error.msgDetail)
proc approveFunds(market: OnChainMarket, amount: UInt256) {.async.} = proc approveFunds(market: OnChainMarket, amount: UInt256) {.async.} =
debug "Approving tokens", amount debug "Approving tokens", amount
let tokenAddress = await market.contract.token() convertEthersError:
let token = Erc20Token.new(tokenAddress, market.signer) let tokenAddress = await market.contract.token()
let token = Erc20Token.new(tokenAddress, market.signer)
discard await token.increaseAllowance(market.contract.address(), amount).confirm(1) discard await token.increaseAllowance(market.contract.address(), amount).confirm(1)
method getZkeyHash*(market: OnChainMarket): Future[?string] {.async.} = method getZkeyHash*(market: OnChainMarket): Future[?string] {.async.} =
let config = await market.contract.config() let config = await market.contract.config()
return some config.proofs.zkeyHash return some config.proofs.zkeyHash
method getSigner*(market: OnChainMarket): Future[Address] {.async.} = method getSigner*(market: OnChainMarket): Future[Address] {.async.} =
return await market.signer.getAddress() convertEthersError:
return await market.signer.getAddress()
method periodicity*(market: OnChainMarket): Future[Periodicity] {.async.} = method periodicity*(market: OnChainMarket): Future[Periodicity] {.async.} =
let config = await market.contract.config() convertEthersError:
let period = config.proofs.period let config = await market.contract.config()
return Periodicity(seconds: period) let period = config.proofs.period
return Periodicity(seconds: period)
method proofTimeout*(market: OnChainMarket): Future[UInt256] {.async.} = method proofTimeout*(market: OnChainMarket): Future[UInt256] {.async.} =
let config = await market.contract.config() convertEthersError:
return config.proofs.timeout let config = await market.contract.config()
return config.proofs.timeout
method proofDowntime*(market: OnChainMarket): Future[uint8] {.async.} = method proofDowntime*(market: OnChainMarket): Future[uint8] {.async.} =
let config = await market.contract.config() convertEthersError:
return config.proofs.downtime let config = await market.contract.config()
return config.proofs.downtime
method getPointer*(market: OnChainMarket, slotId: SlotId): Future[uint8] {.async.} = method getPointer*(market: OnChainMarket, slotId: SlotId): Future[uint8] {.async.} =
let overrides = CallOverrides(blockTag: some BlockTag.pending) convertEthersError:
return await market.contract.getPointer(slotId, overrides) let overrides = CallOverrides(blockTag: some BlockTag.pending)
return await market.contract.getPointer(slotId, overrides)
method myRequests*(market: OnChainMarket): Future[seq[RequestId]] {.async.} = method myRequests*(market: OnChainMarket): Future[seq[RequestId]] {.async.} =
return await market.contract.myRequests convertEthersError:
return await market.contract.myRequests
method mySlots*(market: OnChainMarket): Future[seq[SlotId]] {.async.} = method mySlots*(market: OnChainMarket): Future[seq[SlotId]] {.async.} =
let slots = await market.contract.mySlots() convertEthersError:
debug "Fetched my slots", numSlots=len(slots) let slots = await market.contract.mySlots()
debug "Fetched my slots", numSlots=len(slots)
return slots return slots
method requestStorage(market: OnChainMarket, request: StorageRequest){.async.} = method requestStorage(market: OnChainMarket, request: StorageRequest){.async.} =
debug "Requesting storage" convertEthersError:
await market.approveFunds(request.price()) debug "Requesting storage"
await market.contract.requestStorage(request) await market.approveFunds(request.price())
await market.contract.requestStorage(request)
method getRequest(market: OnChainMarket, method getRequest(market: OnChainMarket,
id: RequestId): Future[?StorageRequest] {.async.} = id: RequestId): Future[?StorageRequest] {.async.} =
try: convertEthersError:
return some await market.contract.getRequest(id) try:
except ProviderError as e: return some await market.contract.getRequest(id)
if e.revertReason.contains("Unknown request"): except ProviderError as e:
return none StorageRequest if e.msgDetail.contains("Unknown request"):
raise e return none StorageRequest
raise e
method requestState*(market: OnChainMarket, method requestState*(market: OnChainMarket,
requestId: RequestId): Future[?RequestState] {.async.} = requestId: RequestId): Future[?RequestState] {.async.} =
try: convertEthersError:
let overrides = CallOverrides(blockTag: some BlockTag.pending) try:
return some await market.contract.requestState(requestId, overrides) let overrides = CallOverrides(blockTag: some BlockTag.pending)
except ProviderError as e: return some await market.contract.requestState(requestId, overrides)
if e.revertReason.contains("Unknown request"): except ProviderError as e:
return none RequestState if e.msgDetail.contains("Unknown request"):
raise e return none RequestState
raise e
method slotState*(market: OnChainMarket, method slotState*(market: OnChainMarket,
slotId: SlotId): Future[SlotState] {.async.} = slotId: SlotId): Future[SlotState] {.async.} =
let overrides = CallOverrides(blockTag: some BlockTag.pending) convertEthersError:
return await market.contract.slotState(slotId, overrides) let overrides = CallOverrides(blockTag: some BlockTag.pending)
return await market.contract.slotState(slotId, overrides)
method getRequestEnd*(market: OnChainMarket, method getRequestEnd*(market: OnChainMarket,
id: RequestId): Future[SecondsSince1970] {.async.} = id: RequestId): Future[SecondsSince1970] {.async.} =
return await market.contract.requestEnd(id) convertEthersError:
return await market.contract.requestEnd(id)
method getHost(market: OnChainMarket, method getHost(market: OnChainMarket,
requestId: RequestId, requestId: RequestId,
slotIndex: UInt256): Future[?Address] {.async.} = slotIndex: UInt256): Future[?Address] {.async.} =
let slotId = slotId(requestId, slotIndex) convertEthersError:
let address = await market.contract.getHost(slotId) let slotId = slotId(requestId, slotIndex)
if address != Address.default: let address = await market.contract.getHost(slotId)
return some address if address != Address.default:
else: return some address
return none Address else:
return none Address
method getActiveSlot*(market: OnChainMarket, method getActiveSlot*(market: OnChainMarket,
slotId: SlotId): Future[?Slot] {.async.} = slotId: SlotId): Future[?Slot] {.async.} =
convertEthersError:
try: try:
return some await market.contract.getActiveSlot(slotId) return some await market.contract.getActiveSlot(slotId)
except ProviderError as e: except ProviderError as e:
if e.revertReason.contains("Slot is free"): if e.msgDetail.contains("Slot is free"):
return none Slot return none Slot
raise e raise e
method fillSlot(market: OnChainMarket, method fillSlot(market: OnChainMarket,
requestId: RequestId, requestId: RequestId,
slotIndex: UInt256, slotIndex: UInt256,
proof: Groth16Proof, proof: Groth16Proof,
collateral: UInt256) {.async.} = collateral: UInt256) {.async.} =
await market.approveFunds(collateral) convertEthersError:
await market.contract.fillSlot(requestId, slotIndex, proof) await market.approveFunds(collateral)
await market.contract.fillSlot(requestId, slotIndex, proof)
method freeSlot*(market: OnChainMarket, slotId: SlotId) {.async.} = method freeSlot*(market: OnChainMarket, slotId: SlotId) {.async.} =
await market.contract.freeSlot(slotId) convertEthersError:
await market.contract.freeSlot(slotId)
method withdrawFunds(market: OnChainMarket, method withdrawFunds(market: OnChainMarket,
requestId: RequestId) {.async.} = requestId: RequestId) {.async.} =
await market.contract.withdrawFunds(requestId) convertEthersError:
await market.contract.withdrawFunds(requestId)
method isProofRequired*(market: OnChainMarket, method isProofRequired*(market: OnChainMarket,
id: SlotId): Future[bool] {.async.} = id: SlotId): Future[bool] {.async.} =
try: convertEthersError:
let overrides = CallOverrides(blockTag: some BlockTag.pending) try:
return await market.contract.isProofRequired(id, overrides) let overrides = CallOverrides(blockTag: some BlockTag.pending)
except ProviderError as e: return await market.contract.isProofRequired(id, overrides)
if e.revertReason.contains("Slot is free"): except ProviderError as e:
return false if e.msgDetail.contains("Slot is free"):
raise e return false
raise e
method willProofBeRequired*(market: OnChainMarket, method willProofBeRequired*(market: OnChainMarket,
id: SlotId): Future[bool] {.async.} = id: SlotId): Future[bool] {.async.} =
try: convertEthersError:
let overrides = CallOverrides(blockTag: some BlockTag.pending) try:
return await market.contract.willProofBeRequired(id, overrides) let overrides = CallOverrides(blockTag: some BlockTag.pending)
except ProviderError as e: return await market.contract.willProofBeRequired(id, overrides)
if e.revertReason.contains("Slot is free"): except ProviderError as e:
return false if e.msgDetail.contains("Slot is free"):
raise e return false
raise e
method getChallenge*(market: OnChainMarket, id: SlotId): Future[ProofChallenge] {.async.} = method getChallenge*(market: OnChainMarket, id: SlotId): Future[ProofChallenge] {.async.} =
let overrides = CallOverrides(blockTag: some BlockTag.pending) convertEthersError:
return await market.contract.getChallenge(id, overrides) let overrides = CallOverrides(blockTag: some BlockTag.pending)
return await market.contract.getChallenge(id, overrides)
method submitProof*(market: OnChainMarket, method submitProof*(market: OnChainMarket,
id: SlotId, id: SlotId,
proof: Groth16Proof) {.async.} = proof: Groth16Proof) {.async.} =
await market.contract.submitProof(id, proof) convertEthersError:
await market.contract.submitProof(id, proof)
method markProofAsMissing*(market: OnChainMarket, method markProofAsMissing*(market: OnChainMarket,
id: SlotId, id: SlotId,
period: Period) {.async.} = period: Period) {.async.} =
await market.contract.markProofAsMissing(id, period) convertEthersError:
await market.contract.markProofAsMissing(id, period)
method canProofBeMarkedAsMissing*( method canProofBeMarkedAsMissing*(
market: OnChainMarket, market: OnChainMarket,
@ -186,7 +216,7 @@ method canProofBeMarkedAsMissing*(
await contractWithoutSigner.markProofAsMissing(id, period, overrides) await contractWithoutSigner.markProofAsMissing(id, period, overrides)
return true return true
except EthersError as e: except EthersError as e:
trace "Proof can not be marked as missing", msg = e.msg trace "Proof cannot be marked as missing", msg = e.msg
return false return false
method subscribeRequests*(market: OnChainMarket, method subscribeRequests*(market: OnChainMarket,
@ -196,16 +226,20 @@ method subscribeRequests*(market: OnChainMarket,
callback(event.requestId, callback(event.requestId,
event.ask, event.ask,
event.expiry) event.expiry)
let subscription = await market.contract.subscribe(StorageRequested, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription) convertEthersError:
let subscription = await market.contract.subscribe(StorageRequested, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription)
method subscribeSlotFilled*(market: OnChainMarket, method subscribeSlotFilled*(market: OnChainMarket,
callback: OnSlotFilled): callback: OnSlotFilled):
Future[MarketSubscription] {.async.} = Future[MarketSubscription] {.async.} =
proc onEvent(event: SlotFilled) {.upraises:[].} = proc onEvent(event: SlotFilled) {.upraises:[].} =
callback(event.requestId, event.slotIndex) callback(event.requestId, event.slotIndex)
let subscription = await market.contract.subscribe(SlotFilled, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription) convertEthersError:
let subscription = await market.contract.subscribe(SlotFilled, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription)
method subscribeSlotFilled*(market: OnChainMarket, method subscribeSlotFilled*(market: OnChainMarket,
requestId: RequestId, requestId: RequestId,
@ -215,23 +249,29 @@ method subscribeSlotFilled*(market: OnChainMarket,
proc onSlotFilled(eventRequestId: RequestId, eventSlotIndex: UInt256) = proc onSlotFilled(eventRequestId: RequestId, eventSlotIndex: UInt256) =
if eventRequestId == requestId and eventSlotIndex == slotIndex: if eventRequestId == requestId and eventSlotIndex == slotIndex:
callback(requestId, slotIndex) callback(requestId, slotIndex)
return await market.subscribeSlotFilled(onSlotFilled)
convertEthersError:
return await market.subscribeSlotFilled(onSlotFilled)
method subscribeSlotFreed*(market: OnChainMarket, method subscribeSlotFreed*(market: OnChainMarket,
callback: OnSlotFreed): callback: OnSlotFreed):
Future[MarketSubscription] {.async.} = Future[MarketSubscription] {.async.} =
proc onEvent(event: SlotFreed) {.upraises:[].} = proc onEvent(event: SlotFreed) {.upraises:[].} =
callback(event.requestId, event.slotIndex) callback(event.requestId, event.slotIndex)
let subscription = await market.contract.subscribe(SlotFreed, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription) convertEthersError:
let subscription = await market.contract.subscribe(SlotFreed, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription)
method subscribeFulfillment(market: OnChainMarket, method subscribeFulfillment(market: OnChainMarket,
callback: OnFulfillment): callback: OnFulfillment):
Future[MarketSubscription] {.async.} = Future[MarketSubscription] {.async.} =
proc onEvent(event: RequestFulfilled) {.upraises:[].} = proc onEvent(event: RequestFulfilled) {.upraises:[].} =
callback(event.requestId) callback(event.requestId)
let subscription = await market.contract.subscribe(RequestFulfilled, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription) convertEthersError:
let subscription = await market.contract.subscribe(RequestFulfilled, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription)
method subscribeFulfillment(market: OnChainMarket, method subscribeFulfillment(market: OnChainMarket,
requestId: RequestId, requestId: RequestId,
@ -240,16 +280,20 @@ method subscribeFulfillment(market: OnChainMarket,
proc onEvent(event: RequestFulfilled) {.upraises:[].} = proc onEvent(event: RequestFulfilled) {.upraises:[].} =
if event.requestId == requestId: if event.requestId == requestId:
callback(event.requestId) callback(event.requestId)
let subscription = await market.contract.subscribe(RequestFulfilled, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription) convertEthersError:
let subscription = await market.contract.subscribe(RequestFulfilled, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription)
method subscribeRequestCancelled*(market: OnChainMarket, method subscribeRequestCancelled*(market: OnChainMarket,
callback: OnRequestCancelled): callback: OnRequestCancelled):
Future[MarketSubscription] {.async.} = Future[MarketSubscription] {.async.} =
proc onEvent(event: RequestCancelled) {.upraises:[].} = proc onEvent(event: RequestCancelled) {.upraises:[].} =
callback(event.requestId) callback(event.requestId)
let subscription = await market.contract.subscribe(RequestCancelled, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription) convertEthersError:
let subscription = await market.contract.subscribe(RequestCancelled, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription)
method subscribeRequestCancelled*(market: OnChainMarket, method subscribeRequestCancelled*(market: OnChainMarket,
requestId: RequestId, requestId: RequestId,
@ -258,16 +302,20 @@ method subscribeRequestCancelled*(market: OnChainMarket,
proc onEvent(event: RequestCancelled) {.upraises:[].} = proc onEvent(event: RequestCancelled) {.upraises:[].} =
if event.requestId == requestId: if event.requestId == requestId:
callback(event.requestId) callback(event.requestId)
let subscription = await market.contract.subscribe(RequestCancelled, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription) convertEthersError:
let subscription = await market.contract.subscribe(RequestCancelled, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription)
method subscribeRequestFailed*(market: OnChainMarket, method subscribeRequestFailed*(market: OnChainMarket,
callback: OnRequestFailed): callback: OnRequestFailed):
Future[MarketSubscription] {.async.} = Future[MarketSubscription] {.async.} =
proc onEvent(event: RequestFailed) {.upraises:[]} = proc onEvent(event: RequestFailed) {.upraises:[]} =
callback(event.requestId) callback(event.requestId)
let subscription = await market.contract.subscribe(RequestFailed, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription) convertEthersError:
let subscription = await market.contract.subscribe(RequestFailed, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription)
method subscribeRequestFailed*(market: OnChainMarket, method subscribeRequestFailed*(market: OnChainMarket,
requestId: RequestId, requestId: RequestId,
@ -276,16 +324,20 @@ method subscribeRequestFailed*(market: OnChainMarket,
proc onEvent(event: RequestFailed) {.upraises:[]} = proc onEvent(event: RequestFailed) {.upraises:[]} =
if event.requestId == requestId: if event.requestId == requestId:
callback(event.requestId) callback(event.requestId)
let subscription = await market.contract.subscribe(RequestFailed, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription) convertEthersError:
let subscription = await market.contract.subscribe(RequestFailed, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription)
method subscribeProofSubmission*(market: OnChainMarket, method subscribeProofSubmission*(market: OnChainMarket,
callback: OnProofSubmitted): callback: OnProofSubmitted):
Future[MarketSubscription] {.async.} = Future[MarketSubscription] {.async.} =
proc onEvent(event: ProofSubmitted) {.upraises: [].} = proc onEvent(event: ProofSubmitted) {.upraises: [].} =
callback(event.id) callback(event.id)
let subscription = await market.contract.subscribe(ProofSubmitted, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription) convertEthersError:
let subscription = await market.contract.subscribe(ProofSubmitted, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription)
method unsubscribe*(subscription: OnChainMarketSubscription) {.async.} = method unsubscribe*(subscription: OnChainMarketSubscription) {.async.} =
await subscription.eventSubscription.unsubscribe() await subscription.eventSubscription.unsubscribe()
@ -293,20 +345,20 @@ method unsubscribe*(subscription: OnChainMarketSubscription) {.async.} =
method queryPastStorageRequests*(market: OnChainMarket, method queryPastStorageRequests*(market: OnChainMarket,
blocksAgo: int): blocksAgo: int):
Future[seq[PastStorageRequest]] {.async.} = Future[seq[PastStorageRequest]] {.async.} =
convertEthersError:
let contract = market.contract
let provider = contract.provider
let contract = market.contract let head = await provider.getBlockNumber()
let provider = contract.provider let fromBlock = BlockTag.init(head - blocksAgo.abs.u256)
let head = await provider.getBlockNumber() let events = await contract.queryFilter(StorageRequested,
let fromBlock = BlockTag.init(head - blocksAgo.abs.u256) fromBlock,
BlockTag.latest)
let events = await contract.queryFilter(StorageRequested, return events.map(event =>
fromBlock, PastStorageRequest(
BlockTag.latest) requestId: event.requestId,
return events.map(event => ask: event.ask,
PastStorageRequest( expiry: event.expiry
requestId: event.requestId, )
ask: event.ask,
expiry: event.expiry
) )
)

View File

@ -5,6 +5,7 @@ import pkg/ethers/erc20
import ./contracts/requests import ./contracts/requests
import ./contracts/proofs import ./contracts/proofs
import ./clock import ./clock
import ./errors
import ./periods import ./periods
export chronos export chronos
@ -16,6 +17,7 @@ export periods
type type
Market* = ref object of RootObj Market* = ref object of RootObj
MarketError* = object of CodexError
Subscription* = ref object of RootObj Subscription* = ref object of RootObj
OnRequest* = proc(id: RequestId, OnRequest* = proc(id: RequestId,
ask: StorageAsk, ask: StorageAsk,

View File

@ -33,8 +33,8 @@ when codex_enable_proof_failures:
try: try:
warn "Submitting INVALID proof", period = currentPeriod, slotId = slot.id warn "Submitting INVALID proof", period = currentPeriod, slotId = slot.id
await market.submitProof(slot.id, Groth16Proof.default) await market.submitProof(slot.id, Groth16Proof.default)
except SignerError as e: except MarketError as e:
if not e.msgDetail.contains("Invalid proof"): if not e.msg.contains("Invalid proof"):
onSubmitProofError(e, currentPeriod, slot.id) onSubmitProofError(e, currentPeriod, slot.id)
except CatchableError as e: except CatchableError as e:
onSubmitProofError(e, currentPeriod, slot.id) onSubmitProofError(e, currentPeriod, slot.id)