diff --git a/codex/contracts/market.nim b/codex/contracts/market.nim index 69dab2cf..9e39d6a2 100644 --- a/codex/contracts/market.nim +++ b/codex/contracts/market.nim @@ -31,6 +31,9 @@ method getSigner*(market: OnChainMarket): Future[Address] {.async.} = method myRequests*(market: OnChainMarket): Future[seq[RequestId]] {.async.} = return await market.contract.myRequests +method mySlots*(market: OnChainMarket): Future[seq[SlotId]] {.async.} = + return await market.contract.mySlots + method requestStorage(market: OnChainMarket, request: StorageRequest){.async.} = await market.contract.requestStorage(request) @@ -66,6 +69,14 @@ method getHost(market: OnChainMarket, else: return none Address +method getSlot(market: OnChainMarket, slotId: SlotId): Future[?Slot] {.async.} = + try: + return some await market.contract.getSlot(slotId) + except ProviderError as e: + if e.revertReason.contains("Slot empty"): + return none Slot + raise e + method fillSlot(market: OnChainMarket, requestId: RequestId, slotIndex: UInt256, diff --git a/codex/contracts/requests.nim b/codex/contracts/requests.nim index 2f4551b3..d835cdab 100644 --- a/codex/contracts/requests.nim +++ b/codex/contracts/requests.nim @@ -39,6 +39,12 @@ type Cancelled Finished Failed + Slot* = object + host*: Address + hostPaid*: bool + requestId*: RequestId + slotIndex*: UInt256 + proof*: seq[byte] proc `==`*(x, y: Nonce): bool {.borrow.} proc `==`*(x, y: RequestId): bool {.borrow.} @@ -60,6 +66,13 @@ func fromTuple(_: type StorageRequest, tupl: tuple): StorageRequest = nonce: tupl[4] ) +func fromTuple(_: type Slot, tupl: tuple): Slot = + Slot( + host: tupl[0], + hostPaid: tupl[1], + requestId: tupl[2] + ) + func fromTuple(_: type StorageAsk, tupl: tuple): StorageAsk = StorageAsk( slots: tupl[0], @@ -121,6 +134,13 @@ func encode*(encoder: var AbiEncoder, id: RequestId | SlotId | Nonce) = func encode*(encoder: var AbiEncoder, request: StorageRequest) = encoder.write(request.fieldValues) +func encode*(encoder: var AbiEncoder, slot: Slot) = + encoder.write(slot.fieldValues) + +func decode*[T: RequestId | SlotId | Nonce](decoder: var AbiDecoder, + _: type T): ?!T = + let nonce = ?decoder.read(type array[32, byte]) + success T(nonce) func decode*(decoder: var AbiDecoder, T: type StoragePoR): ?!T = let tupl = ?decoder.read(StoragePoR.fieldTypes) @@ -142,6 +162,10 @@ func decode*(decoder: var AbiDecoder, T: type StorageRequest): ?!T = let tupl = ?decoder.read(StorageRequest.fieldTypes) success StorageRequest.fromTuple(tupl) +func decode*(decoder: var AbiDecoder, T: type Slot): ?!T = + let tupl = ?decoder.read(Slot.fieldTypes) + success Slot.fromTuple(tupl) + func id*(request: StorageRequest): RequestId = let encoding = AbiEncoder.encode((request, )) RequestId(keccak256.digest(encoding).data) diff --git a/codex/market.nim b/codex/market.nim index c3cd039a..713f1ac0 100644 --- a/codex/market.nim +++ b/codex/market.nim @@ -12,11 +12,11 @@ export SecondsSince1970 type Market* = ref object of RootObj Subscription* = ref object of RootObj - OnRequest* = proc(id: RequestId, ask: StorageAsk) {.gcsafe, upraises:[].} - OnFulfillment* = proc(requestId: RequestId) {.gcsafe, upraises: [].} - OnSlotFilled* = proc(requestId: RequestId, slotIndex: UInt256) {.gcsafe, upraises:[].} - OnRequestCancelled* = proc(requestId: RequestId) {.gcsafe, upraises:[].} - OnRequestFailed* = proc(requestId: RequestId) {.gcsafe, upraises:[].} + OnRequest* = proc(id: RequestId, ask: StorageAsk): Future[void] {.gcsafe, upraises:[].} + OnFulfillment* = proc(requestId: RequestId): Future[void] {.gcsafe, upraises: [].} + OnSlotFilled* = proc(requestId: RequestId, slotIndex: UInt256): Future[void] {.gcsafe, upraises:[].} + OnRequestCancelled* = proc(requestId: RequestId): Future[void] {.gcsafe, upraises:[].} + OnRequestFailed* = proc(requestId: RequestId): Future[void] {.gcsafe, upraises:[].} method getSigner*(market: Market): Future[Address] {.base, async.} = raiseAssert("not implemented") @@ -28,6 +28,9 @@ method requestStorage*(market: Market, method myRequests*(market: Market): Future[seq[RequestId]] {.base, async.} = raiseAssert("not implemented") +method mySlots*(market: Market): Future[seq[SlotId]] {.base, async.} = + raiseAssert("not implemented") + method getRequest*(market: Market, id: RequestId): Future[?StorageRequest] {.base, async.} = @@ -46,6 +49,10 @@ method getHost*(market: Market, slotIndex: UInt256): Future[?Address] {.base, async.} = raiseAssert("not implemented") +method getSlot*(market: Market, + slotId: SlotId): Future[?Slot] {.base, async.} = + raiseAssert("not implemented") + method fillSlot*(market: Market, requestId: RequestId, slotIndex: UInt256, diff --git a/tests/codex/helpers/mockmarket.nim b/tests/codex/helpers/mockmarket.nim index b8a11f6f..a8ac19dc 100644 --- a/tests/codex/helpers/mockmarket.nim +++ b/tests/codex/helpers/mockmarket.nim @@ -9,6 +9,7 @@ export tables type MockMarket* = ref object of Market activeRequests*: Table[Address, seq[RequestId]] + activeSlots*: Table[Address, seq[SlotId]] requested*: seq[StorageRequest] requestEnds*: Table[RequestId, SecondsSince1970] state*: Table[RequestId, RequestState] @@ -21,11 +22,6 @@ type requestId*: RequestId proof*: seq[byte] host*: Address - Slot* = object - requestId*: RequestId - slotIndex*: UInt256 - proof*: seq[byte] - host*: Address Subscriptions = object onRequest: seq[RequestSubscription] onFulfillment: seq[FulfillmentSubscription] @@ -74,6 +70,9 @@ method requestStorage*(market: MockMarket, request: StorageRequest) {.async.} = method myRequests*(market: MockMarket): Future[seq[RequestId]] {.async.} = return market.activeRequests[market.signer] +method mySlots*(market: MockMarket): Future[seq[SlotId]] {.async.} = + return market.activeSlots[market.signer] + method getRequest(market: MockMarket, id: RequestId): Future[?StorageRequest] {.async.} = for request in market.requested: @@ -89,7 +88,7 @@ method getRequestEnd*(market: MockMarket, id: RequestId): Future[SecondsSince1970] {.async.} = return market.requestEnds[id] -method getHost(market: MockMarket, +method getHost*(market: MockMarket, requestId: RequestId, slotIndex: UInt256): Future[?Address] {.async.} = for slot in market.filled: