[marketplace] get active slots from chain

This commit is contained in:
Eric Mastro 2022-11-10 15:43:48 +11:00
parent f889426ec0
commit 8066c902e6
No known key found for this signature in database
GPG Key ID: 141E3048D95A4E63
6 changed files with 54 additions and 12 deletions

View File

@ -31,6 +31,9 @@ method getSigner*(market: OnChainMarket): Future[Address] {.async.} =
method myRequests*(market: OnChainMarket): Future[seq[RequestId]] {.async.} = method myRequests*(market: OnChainMarket): Future[seq[RequestId]] {.async.} =
return await market.contract.myRequests return await market.contract.myRequests
method mySlots*(market: OnChainMarket): Future[seq[SlotId]] {.async.} =
return await market.contract.mySlots
method requestStorage(market: OnChainMarket, method requestStorage(market: OnChainMarket,
request: StorageRequest): request: StorageRequest):
Future[StorageRequest] {.async.} = Future[StorageRequest] {.async.} =
@ -66,6 +69,14 @@ method getHost(market: OnChainMarket,
else: else:
return none Address 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, method fillSlot(market: OnChainMarket,
requestId: RequestId, requestId: RequestId,
slotIndex: UInt256, slotIndex: UInt256,

View File

@ -39,6 +39,12 @@ type
Cancelled Cancelled
Finished Finished
Failed Failed
Slot* = object
host*: Address
hostPaid*: bool
requestId*: RequestId
slotIndex*: UInt256
proof*: seq[byte]
proc `==`*(x, y: Nonce): bool {.borrow.} proc `==`*(x, y: Nonce): bool {.borrow.}
proc `==`*(x, y: RequestId): bool {.borrow.} proc `==`*(x, y: RequestId): bool {.borrow.}
@ -48,6 +54,9 @@ proc hash*(x: SlotId): Hash {.borrow.}
func toArray*(id: RequestId | SlotId | Nonce): array[32, byte] = func toArray*(id: RequestId | SlotId | Nonce): array[32, byte] =
array[32, byte](id) array[32, byte](id)
proc `$`*(id: RequestId | SlotId | Nonce): string =
id.toArray.toHex
func fromTuple(_: type StorageRequest, tupl: tuple): StorageRequest = func fromTuple(_: type StorageRequest, tupl: tuple): StorageRequest =
StorageRequest( StorageRequest(
client: tupl[0], client: tupl[0],
@ -57,6 +66,13 @@ func fromTuple(_: type StorageRequest, tupl: tuple): StorageRequest =
nonce: tupl[4] 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 = func fromTuple(_: type StorageAsk, tupl: tuple): StorageAsk =
StorageAsk( StorageAsk(
slots: tupl[0], slots: tupl[0],
@ -122,6 +138,9 @@ func encode*(encoder: var AbiEncoder, id: RequestId | SlotId | Nonce) =
func encode*(encoder: var AbiEncoder, request: StorageRequest) = func encode*(encoder: var AbiEncoder, request: StorageRequest) =
encoder.write(request.fieldValues) encoder.write(request.fieldValues)
func encode*(encoder: var AbiEncoder, slot: Slot) =
encoder.write(slot.fieldValues)
func decode*[T: RequestId | SlotId | Nonce](decoder: var AbiDecoder, func decode*[T: RequestId | SlotId | Nonce](decoder: var AbiDecoder,
_: type T): ?!T = _: type T): ?!T =
let nonce = ?decoder.read(type array[32, byte]) let nonce = ?decoder.read(type array[32, byte])
@ -147,6 +166,10 @@ func decode*(decoder: var AbiDecoder, T: type StorageRequest): ?!T =
let tupl = ?decoder.read(StorageRequest.fieldTypes) let tupl = ?decoder.read(StorageRequest.fieldTypes)
success StorageRequest.fromTuple(tupl) 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 = func id*(request: StorageRequest): RequestId =
let encoding = AbiEncoder.encode((request, )) let encoding = AbiEncoder.encode((request, ))
RequestId(keccak256.digest(encoding).data) RequestId(keccak256.digest(encoding).data)

View File

@ -43,8 +43,10 @@ proc withdrawFunds*(storage: Storage, requestId: RequestId) {.contract.}
proc payoutSlot*(storage: Storage, requestId: RequestId, slotIndex: UInt256) {.contract.} proc payoutSlot*(storage: Storage, requestId: RequestId, slotIndex: UInt256) {.contract.}
proc getRequest*(storage: Storage, id: RequestId): StorageRequest {.contract, view.} proc getRequest*(storage: Storage, id: RequestId): StorageRequest {.contract, view.}
proc getHost*(storage: Storage, id: SlotId): Address {.contract, view.} proc getHost*(storage: Storage, id: SlotId): Address {.contract, view.}
proc getSlot*(storage: Storage, id: SlotId): Slot {.contract, view.}
proc myRequests*(storage: Storage): seq[RequestId] {.contract, view.} proc myRequests*(storage: Storage): seq[RequestId] {.contract, view.}
proc mySlots*(storage: Storage): seq[SlotId] {.contract, view.}
proc state*(storage: Storage, requestId: RequestId): RequestState {.contract, view.} proc state*(storage: Storage, requestId: RequestId): RequestState {.contract, view.}
proc requestEnd*(storage: Storage, requestId: RequestId): SecondsSince1970 {.contract, view.} proc requestEnd*(storage: Storage, requestId: RequestId): SecondsSince1970 {.contract, view.}

View File

@ -12,11 +12,11 @@ export SecondsSince1970
type type
Market* = ref object of RootObj Market* = ref object of RootObj
Subscription* = ref object of RootObj Subscription* = ref object of RootObj
OnRequest* = proc(id: RequestId, ask: StorageAsk) {.gcsafe, upraises:[].} OnRequest* = proc(id: RequestId, ask: StorageAsk): Future[void] {.gcsafe, upraises:[].}
OnFulfillment* = proc(requestId: RequestId) {.gcsafe, upraises: [].} OnFulfillment* = proc(requestId: RequestId): Future[void] {.gcsafe, upraises: [].}
OnSlotFilled* = proc(requestId: RequestId, slotIndex: UInt256) {.gcsafe, upraises:[].} OnSlotFilled* = proc(requestId: RequestId, slotIndex: UInt256): Future[void] {.gcsafe, upraises:[].}
OnRequestCancelled* = proc(requestId: RequestId) {.gcsafe, upraises:[].} OnRequestCancelled* = proc(requestId: RequestId): Future[void] {.gcsafe, upraises:[].}
OnRequestFailed* = proc(requestId: RequestId) {.gcsafe, upraises:[].} OnRequestFailed* = proc(requestId: RequestId): Future[void] {.gcsafe, upraises:[].}
method getSigner*(market: Market): Future[Address] {.base, async.} = method getSigner*(market: Market): Future[Address] {.base, async.} =
raiseAssert("not implemented") raiseAssert("not implemented")
@ -29,6 +29,9 @@ method requestStorage*(market: Market,
method myRequests*(market: Market): Future[seq[RequestId]] {.base, async.} = method myRequests*(market: Market): Future[seq[RequestId]] {.base, async.} =
raiseAssert("not implemented") raiseAssert("not implemented")
method mySlots*(market: Market): Future[seq[SlotId]] {.base, async.} =
raiseAssert("not implemented")
method getRequest*(market: Market, method getRequest*(market: Market,
id: RequestId): id: RequestId):
Future[?StorageRequest] {.base, async.} = Future[?StorageRequest] {.base, async.} =
@ -47,6 +50,10 @@ method getHost*(market: Market,
slotIndex: UInt256): Future[?Address] {.base, async.} = slotIndex: UInt256): Future[?Address] {.base, async.} =
raiseAssert("not implemented") raiseAssert("not implemented")
method getSlot*(market: Market,
slotId: SlotId): Future[?Slot] {.base, async.} =
raiseAssert("not implemented")
method fillSlot*(market: Market, method fillSlot*(market: Market,
requestId: RequestId, requestId: RequestId,
slotIndex: UInt256, slotIndex: UInt256,

View File

@ -9,6 +9,7 @@ export tables
type type
MockMarket* = ref object of Market MockMarket* = ref object of Market
activeRequests*: Table[Address, seq[RequestId]] activeRequests*: Table[Address, seq[RequestId]]
activeSlots*: Table[Address, seq[SlotId]]
requested*: seq[StorageRequest] requested*: seq[StorageRequest]
requestEnds*: Table[RequestId, SecondsSince1970] requestEnds*: Table[RequestId, SecondsSince1970]
state*: Table[RequestId, RequestState] state*: Table[RequestId, RequestState]
@ -21,11 +22,6 @@ type
requestId*: RequestId requestId*: RequestId
proof*: seq[byte] proof*: seq[byte]
host*: Address host*: Address
Slot* = object
requestId*: RequestId
slotIndex*: UInt256
proof*: seq[byte]
host*: Address
Subscriptions = object Subscriptions = object
onRequest: seq[RequestSubscription] onRequest: seq[RequestSubscription]
onFulfillment: seq[FulfillmentSubscription] onFulfillment: seq[FulfillmentSubscription]
@ -77,6 +73,9 @@ method requestStorage*(market: MockMarket,
method myRequests*(market: MockMarket): Future[seq[RequestId]] {.async.} = method myRequests*(market: MockMarket): Future[seq[RequestId]] {.async.} =
return market.activeRequests[market.signer] return market.activeRequests[market.signer]
method mySlots*(market: MockMarket): Future[seq[SlotId]] {.async.} =
return market.activeSlots[market.signer]
method getRequest(market: MockMarket, method getRequest(market: MockMarket,
id: RequestId): Future[?StorageRequest] {.async.} = id: RequestId): Future[?StorageRequest] {.async.} =
for request in market.requested: for request in market.requested:
@ -92,7 +91,7 @@ method getRequestEnd*(market: MockMarket,
id: RequestId): Future[SecondsSince1970] {.async.} = id: RequestId): Future[SecondsSince1970] {.async.} =
return market.requestEnds[id] return market.requestEnds[id]
method getHost(market: MockMarket, method getHost*(market: MockMarket,
requestId: RequestId, requestId: RequestId,
slotIndex: UInt256): Future[?Address] {.async.} = slotIndex: UInt256): Future[?Address] {.async.} =
for slot in market.filled: for slot in market.filled:

@ -1 +1 @@
Subproject commit 61b8f5fc352838866b0fe27b936323de45bf269c Subproject commit 5d428dd2c79a8f62b5944eb99d75ee79a41e13d5