mirror of
https://github.com/status-im/nim-dagger.git
synced 2025-01-30 16:36:26 +00:00
refactor: marketplace custom errors handling (#1061)
* refactor: marketplace custom errors handling * chore: update contracts repo * chore: update contracts submodule
This commit is contained in:
parent
39e8e6e6fa
commit
68ad804f9e
@ -120,10 +120,8 @@ method getRequest*(market: OnChainMarket,
|
|||||||
convertEthersError:
|
convertEthersError:
|
||||||
try:
|
try:
|
||||||
return some await market.contract.getRequest(id)
|
return some await market.contract.getRequest(id)
|
||||||
except ProviderError as e:
|
except Marketplace_UnknownRequest:
|
||||||
if e.msgDetail.contains("Unknown request"):
|
|
||||||
return none StorageRequest
|
return none StorageRequest
|
||||||
raise e
|
|
||||||
|
|
||||||
method requestState*(market: OnChainMarket,
|
method requestState*(market: OnChainMarket,
|
||||||
requestId: RequestId): Future[?RequestState] {.async.} =
|
requestId: RequestId): Future[?RequestState] {.async.} =
|
||||||
@ -131,10 +129,8 @@ method requestState*(market: OnChainMarket,
|
|||||||
try:
|
try:
|
||||||
let overrides = CallOverrides(blockTag: some BlockTag.pending)
|
let overrides = CallOverrides(blockTag: some BlockTag.pending)
|
||||||
return some await market.contract.requestState(requestId, overrides)
|
return some await market.contract.requestState(requestId, overrides)
|
||||||
except ProviderError as e:
|
except Marketplace_UnknownRequest:
|
||||||
if e.msgDetail.contains("Unknown request"):
|
|
||||||
return none RequestState
|
return none RequestState
|
||||||
raise e
|
|
||||||
|
|
||||||
method slotState*(market: OnChainMarket,
|
method slotState*(market: OnChainMarket,
|
||||||
slotId: SlotId): Future[SlotState] {.async.} =
|
slotId: SlotId): Future[SlotState] {.async.} =
|
||||||
@ -168,10 +164,8 @@ method getActiveSlot*(market: OnChainMarket,
|
|||||||
convertEthersError:
|
convertEthersError:
|
||||||
try:
|
try:
|
||||||
return some await market.contract.getActiveSlot(slotId)
|
return some await market.contract.getActiveSlot(slotId)
|
||||||
except ProviderError as e:
|
except Marketplace_SlotIsFree:
|
||||||
if e.msgDetail.contains("Slot is free"):
|
|
||||||
return none Slot
|
return none Slot
|
||||||
raise e
|
|
||||||
|
|
||||||
method fillSlot(market: OnChainMarket,
|
method fillSlot(market: OnChainMarket,
|
||||||
requestId: RequestId,
|
requestId: RequestId,
|
||||||
@ -219,10 +213,8 @@ method isProofRequired*(market: OnChainMarket,
|
|||||||
try:
|
try:
|
||||||
let overrides = CallOverrides(blockTag: some BlockTag.pending)
|
let overrides = CallOverrides(blockTag: some BlockTag.pending)
|
||||||
return await market.contract.isProofRequired(id, overrides)
|
return await market.contract.isProofRequired(id, overrides)
|
||||||
except ProviderError as e:
|
except Marketplace_SlotIsFree:
|
||||||
if e.msgDetail.contains("Slot is free"):
|
|
||||||
return false
|
return false
|
||||||
raise e
|
|
||||||
|
|
||||||
method willProofBeRequired*(market: OnChainMarket,
|
method willProofBeRequired*(market: OnChainMarket,
|
||||||
id: SlotId): Future[bool] {.async.} =
|
id: SlotId): Future[bool] {.async.} =
|
||||||
@ -230,10 +222,8 @@ method willProofBeRequired*(market: OnChainMarket,
|
|||||||
try:
|
try:
|
||||||
let overrides = CallOverrides(blockTag: some BlockTag.pending)
|
let overrides = CallOverrides(blockTag: some BlockTag.pending)
|
||||||
return await market.contract.willProofBeRequired(id, overrides)
|
return await market.contract.willProofBeRequired(id, overrides)
|
||||||
except ProviderError as e:
|
except Marketplace_SlotIsFree:
|
||||||
if e.msgDetail.contains("Slot is free"):
|
|
||||||
return false
|
return false
|
||||||
raise e
|
|
||||||
|
|
||||||
method getChallenge*(market: OnChainMarket, id: SlotId): Future[ProofChallenge] {.async.} =
|
method getChallenge*(market: OnChainMarket, id: SlotId): Future[ProofChallenge] {.async.} =
|
||||||
convertEthersError:
|
convertEthersError:
|
||||||
|
@ -17,25 +17,54 @@ export requests
|
|||||||
type
|
type
|
||||||
Marketplace* = ref object of Contract
|
Marketplace* = ref object of Contract
|
||||||
|
|
||||||
|
Marketplace_RepairRewardPercentageTooHigh* = object of SolidityError
|
||||||
|
Marketplace_SlashPercentageTooHigh* = object of SolidityError
|
||||||
|
Marketplace_MaximumSlashingTooHigh* = object of SolidityError
|
||||||
|
Marketplace_InvalidExpiry* = object of SolidityError
|
||||||
|
Marketplace_InvalidMaxSlotLoss* = object of SolidityError
|
||||||
|
Marketplace_InsufficientSlots* = object of SolidityError
|
||||||
|
Marketplace_InvalidClientAddress* = object of SolidityError
|
||||||
|
Marketplace_RequestAlreadyExists* = object of SolidityError
|
||||||
|
Marketplace_InvalidSlot* = object of SolidityError
|
||||||
|
Marketplace_SlotNotFree* = object of SolidityError
|
||||||
|
Marketplace_InvalidSlotHost* = object of SolidityError
|
||||||
|
Marketplace_AlreadyPaid* = object of SolidityError
|
||||||
|
Marketplace_TransferFailed* = object of SolidityError
|
||||||
|
Marketplace_UnknownRequest* = object of SolidityError
|
||||||
|
Marketplace_InvalidState* = object of SolidityError
|
||||||
|
Marketplace_StartNotBeforeExpiry* = object of SolidityError
|
||||||
|
Marketplace_SlotNotAcceptingProofs* = object of SolidityError
|
||||||
|
Marketplace_SlotIsFree* = object of SolidityError
|
||||||
|
Marketplace_ReservationRequired* = object of SolidityError
|
||||||
|
Marketplace_NothingToWithdraw* = object of SolidityError
|
||||||
|
Proofs_InsufficientBlockHeight* = object of SolidityError
|
||||||
|
Proofs_InvalidProof* = object of SolidityError
|
||||||
|
Proofs_ProofAlreadySubmitted* = object of SolidityError
|
||||||
|
Proofs_PeriodNotEnded* = object of SolidityError
|
||||||
|
Proofs_ValidationTimedOut* = object of SolidityError
|
||||||
|
Proofs_ProofNotMissing* = object of SolidityError
|
||||||
|
Proofs_ProofNotRequired* = object of SolidityError
|
||||||
|
Proofs_ProofAlreadyMarkedMissing* = object of SolidityError
|
||||||
|
|
||||||
proc configuration*(marketplace: Marketplace): MarketplaceConfig {.contract, view.}
|
proc configuration*(marketplace: Marketplace): MarketplaceConfig {.contract, view.}
|
||||||
proc token*(marketplace: Marketplace): Address {.contract, view.}
|
proc token*(marketplace: Marketplace): Address {.contract, view.}
|
||||||
proc slashMisses*(marketplace: Marketplace): UInt256 {.contract, view.}
|
proc slashMisses*(marketplace: Marketplace): UInt256 {.contract, view.}
|
||||||
proc slashPercentage*(marketplace: Marketplace): UInt256 {.contract, view.}
|
proc slashPercentage*(marketplace: Marketplace): UInt256 {.contract, view.}
|
||||||
proc minCollateralThreshold*(marketplace: Marketplace): UInt256 {.contract, view.}
|
proc minCollateralThreshold*(marketplace: Marketplace): UInt256 {.contract, view.}
|
||||||
|
|
||||||
proc requestStorage*(marketplace: Marketplace, request: StorageRequest): Confirmable {.contract.}
|
proc requestStorage*(marketplace: Marketplace, request: StorageRequest): Confirmable {.contract, errors:[Marketplace_InvalidClientAddress, Marketplace_RequestAlreadyExists, Marketplace_InvalidExpiry, Marketplace_InsufficientSlots, Marketplace_InvalidMaxSlotLoss].}
|
||||||
proc fillSlot*(marketplace: Marketplace, requestId: RequestId, slotIndex: UInt256, proof: Groth16Proof): Confirmable {.contract.}
|
proc fillSlot*(marketplace: Marketplace, requestId: RequestId, slotIndex: UInt256, proof: Groth16Proof): Confirmable {.contract, errors:[Marketplace_InvalidSlot, Marketplace_ReservationRequired, Marketplace_SlotNotFree, Marketplace_StartNotBeforeExpiry, Marketplace_UnknownRequest].}
|
||||||
proc withdrawFunds*(marketplace: Marketplace, requestId: RequestId): Confirmable {.contract.}
|
proc withdrawFunds*(marketplace: Marketplace, requestId: RequestId): Confirmable {.contract, errors:[Marketplace_InvalidClientAddress, Marketplace_InvalidState, Marketplace_NothingToWithdraw, Marketplace_UnknownRequest].}
|
||||||
proc withdrawFunds*(marketplace: Marketplace, requestId: RequestId, withdrawAddress: Address): Confirmable {.contract.}
|
proc withdrawFunds*(marketplace: Marketplace, requestId: RequestId, withdrawAddress: Address): Confirmable {.contract, errors:[Marketplace_InvalidClientAddress, Marketplace_InvalidState, Marketplace_NothingToWithdraw, Marketplace_UnknownRequest].}
|
||||||
proc freeSlot*(marketplace: Marketplace, id: SlotId): Confirmable {.contract.}
|
proc freeSlot*(marketplace: Marketplace, id: SlotId): Confirmable {.contract, errors:[Marketplace_InvalidSlotHost, Marketplace_AlreadyPaid, Marketplace_StartNotBeforeExpiry, Marketplace_UnknownRequest, Marketplace_SlotIsFree].}
|
||||||
proc freeSlot*(marketplace: Marketplace, id: SlotId, rewardRecipient: Address, collateralRecipient: Address): Confirmable {.contract.}
|
proc freeSlot*(marketplace: Marketplace, id: SlotId, rewardRecipient: Address, collateralRecipient: Address): Confirmable {.contract, errors:[Marketplace_InvalidSlotHost, Marketplace_AlreadyPaid, Marketplace_StartNotBeforeExpiry, Marketplace_UnknownRequest, Marketplace_SlotIsFree].}
|
||||||
proc getRequest*(marketplace: Marketplace, id: RequestId): StorageRequest {.contract, view.}
|
proc getRequest*(marketplace: Marketplace, id: RequestId): StorageRequest {.contract, view, errors:[Marketplace_UnknownRequest].}
|
||||||
proc getHost*(marketplace: Marketplace, id: SlotId): Address {.contract, view.}
|
proc getHost*(marketplace: Marketplace, id: SlotId): Address {.contract, view.}
|
||||||
proc getActiveSlot*(marketplace: Marketplace, id: SlotId): Slot {.contract, view.}
|
proc getActiveSlot*(marketplace: Marketplace, id: SlotId): Slot {.contract, view, errors:[Marketplace_SlotIsFree].}
|
||||||
|
|
||||||
proc myRequests*(marketplace: Marketplace): seq[RequestId] {.contract, view.}
|
proc myRequests*(marketplace: Marketplace): seq[RequestId] {.contract, view.}
|
||||||
proc mySlots*(marketplace: Marketplace): seq[SlotId] {.contract, view.}
|
proc mySlots*(marketplace: Marketplace): seq[SlotId] {.contract, view.}
|
||||||
proc requestState*(marketplace: Marketplace, requestId: RequestId): RequestState {.contract, view.}
|
proc requestState*(marketplace: Marketplace, requestId: RequestId): RequestState {.contract, view, errors:[Marketplace_UnknownRequest].}
|
||||||
proc slotState*(marketplace: Marketplace, slotId: SlotId): SlotState {.contract, view.}
|
proc slotState*(marketplace: Marketplace, slotId: SlotId): SlotState {.contract, view.}
|
||||||
proc requestEnd*(marketplace: Marketplace, requestId: RequestId): SecondsSince1970 {.contract, view.}
|
proc requestEnd*(marketplace: Marketplace, requestId: RequestId): SecondsSince1970 {.contract, view.}
|
||||||
proc requestExpiry*(marketplace: Marketplace, requestId: RequestId): SecondsSince1970 {.contract, view.}
|
proc requestExpiry*(marketplace: Marketplace, requestId: RequestId): SecondsSince1970 {.contract, view.}
|
||||||
@ -49,8 +78,8 @@ proc willProofBeRequired*(marketplace: Marketplace, id: SlotId): bool {.contract
|
|||||||
proc getChallenge*(marketplace: Marketplace, id: SlotId): array[32, byte] {.contract, view.}
|
proc getChallenge*(marketplace: Marketplace, id: SlotId): array[32, byte] {.contract, view.}
|
||||||
proc getPointer*(marketplace: Marketplace, id: SlotId): uint8 {.contract, view.}
|
proc getPointer*(marketplace: Marketplace, id: SlotId): uint8 {.contract, view.}
|
||||||
|
|
||||||
proc submitProof*(marketplace: Marketplace, id: SlotId, proof: Groth16Proof): Confirmable {.contract.}
|
proc submitProof*(marketplace: Marketplace, id: SlotId, proof: Groth16Proof): Confirmable {.contract, errors:[Proofs_ProofAlreadySubmitted, Proofs_InvalidProof, Marketplace_UnknownRequest].}
|
||||||
proc markProofAsMissing*(marketplace: Marketplace, id: SlotId, period: UInt256): Confirmable {.contract.}
|
proc markProofAsMissing*(marketplace: Marketplace, id: SlotId, period: UInt256): Confirmable {.contract, errors:[Marketplace_SlotNotAcceptingProofs, Marketplace_StartNotBeforeExpiry, Proofs_PeriodNotEnded, Proofs_ValidationTimedOut, Proofs_ProofNotMissing, Proofs_ProofNotRequired, Proofs_ProofAlreadyMarkedMissing].}
|
||||||
|
|
||||||
proc reserveSlot*(marketplace: Marketplace, requestId: RequestId, slotIndex: UInt256): Confirmable {.contract.}
|
proc reserveSlot*(marketplace: Marketplace, requestId: RequestId, slotIndex: UInt256): Confirmable {.contract.}
|
||||||
proc canReserveSlot*(marketplace: Marketplace, requestId: RequestId, slotIndex: UInt256): bool {.contract, view.}
|
proc canReserveSlot*(marketplace: Marketplace, requestId: RequestId, slotIndex: UInt256): bool {.contract, view.}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import pkg/chronos
|
import pkg/chronos
|
||||||
import pkg/ethers/testing
|
|
||||||
import pkg/ethers/erc20
|
import pkg/ethers/erc20
|
||||||
import codex/contracts
|
import codex/contracts
|
||||||
import ../ethertest
|
import ../ethertest
|
||||||
@ -60,12 +59,14 @@ ethersuite "Marketplace contracts":
|
|||||||
while not (
|
while not (
|
||||||
(await marketplace.isProofRequired(slotId)) and
|
(await marketplace.isProofRequired(slotId)) and
|
||||||
(await marketplace.getPointer(slotId)) < 250
|
(await marketplace.getPointer(slotId)) < 250
|
||||||
):
|
)
|
||||||
|
:
|
||||||
await ethProvider.advanceTime(periodicity.seconds)
|
await ethProvider.advanceTime(periodicity.seconds)
|
||||||
|
|
||||||
proc startContract() {.async.} =
|
proc startContract() {.async.} =
|
||||||
for slotIndex in 1 ..< request.ask.slots:
|
for slotIndex in 1 ..< request.ask.slots:
|
||||||
discard await token.approve(marketplace.address, request.ask.collateral).confirm(1)
|
discard
|
||||||
|
await token.approve(marketplace.address, request.ask.collateral).confirm(1)
|
||||||
discard await marketplace.reserveSlot(request.id, slotIndex.u256).confirm(1)
|
discard await marketplace.reserveSlot(request.id, slotIndex.u256).confirm(1)
|
||||||
discard await marketplace.fillSlot(request.id, slotIndex.u256, proof).confirm(1)
|
discard await marketplace.fillSlot(request.id, slotIndex.u256, proof).confirm(1)
|
||||||
|
|
||||||
@ -92,7 +93,8 @@ ethersuite "Marketplace contracts":
|
|||||||
let startBalance = await token.balanceOf(address)
|
let startBalance = await token.balanceOf(address)
|
||||||
discard await marketplace.freeSlot(slotId).confirm(1)
|
discard await marketplace.freeSlot(slotId).confirm(1)
|
||||||
let endBalance = await token.balanceOf(address)
|
let endBalance = await token.balanceOf(address)
|
||||||
check endBalance == (startBalance + expectedPayout(requestEnd.u256) + request.ask.collateral)
|
check endBalance ==
|
||||||
|
(startBalance + expectedPayout(requestEnd.u256) + request.ask.collateral)
|
||||||
|
|
||||||
test "can be paid out at the end, specifying reward and collateral recipient":
|
test "can be paid out at the end, specifying reward and collateral recipient":
|
||||||
switchAccount(host)
|
switchAccount(host)
|
||||||
@ -103,7 +105,9 @@ ethersuite "Marketplace contracts":
|
|||||||
let startBalanceHost = await token.balanceOf(hostAddress)
|
let startBalanceHost = await token.balanceOf(hostAddress)
|
||||||
let startBalanceReward = await token.balanceOf(rewardRecipient)
|
let startBalanceReward = await token.balanceOf(rewardRecipient)
|
||||||
let startBalanceCollateral = await token.balanceOf(collateralRecipient)
|
let startBalanceCollateral = await token.balanceOf(collateralRecipient)
|
||||||
discard await marketplace.freeSlot(slotId, rewardRecipient, collateralRecipient).confirm(1)
|
discard await marketplace
|
||||||
|
.freeSlot(slotId, rewardRecipient, collateralRecipient)
|
||||||
|
.confirm(1)
|
||||||
let endBalanceHost = await token.balanceOf(hostAddress)
|
let endBalanceHost = await token.balanceOf(hostAddress)
|
||||||
let endBalanceReward = await token.balanceOf(rewardRecipient)
|
let endBalanceReward = await token.balanceOf(rewardRecipient)
|
||||||
let endBalanceCollateral = await token.balanceOf(collateralRecipient)
|
let endBalanceCollateral = await token.balanceOf(collateralRecipient)
|
||||||
@ -118,7 +122,5 @@ ethersuite "Marketplace contracts":
|
|||||||
switchAccount(client)
|
switchAccount(client)
|
||||||
let missingPeriod = periodicity.periodOf(await ethProvider.currentTime())
|
let missingPeriod = periodicity.periodOf(await ethProvider.currentTime())
|
||||||
await ethProvider.advanceTime(periodicity.seconds)
|
await ethProvider.advanceTime(periodicity.seconds)
|
||||||
check await marketplace
|
expect Marketplace_SlotNotAcceptingProofs:
|
||||||
.markProofAsMissing(slotId, missingPeriod)
|
discard await marketplace.markProofAsMissing(slotId, missingPeriod).confirm(1)
|
||||||
.confirm(1)
|
|
||||||
.reverts("Slot not accepting proofs")
|
|
||||||
|
2
vendor/codex-contracts-eth
vendored
2
vendor/codex-contracts-eth
vendored
@ -1 +1 @@
|
|||||||
Subproject commit dfab6102e71d2acaff86af45b87be2536530c624
|
Subproject commit 02e3b8d22b15975fd450ccdad30e32c0be7e593f
|
Loading…
x
Reference in New Issue
Block a user