2022-09-20 03:24:28 +00:00
|
|
|
import std/strutils
|
2022-03-28 15:24:28 +00:00
|
|
|
import pkg/ethers
|
2022-09-20 03:24:28 +00:00
|
|
|
import pkg/ethers/testing
|
2022-03-28 15:24:28 +00:00
|
|
|
import pkg/upraises
|
2022-03-28 15:28:51 +00:00
|
|
|
import pkg/questionable
|
2022-03-28 15:24:28 +00:00
|
|
|
import ../market
|
2023-01-19 05:58:04 +00:00
|
|
|
import ./marketplace
|
2022-03-28 15:24:28 +00:00
|
|
|
|
2022-03-29 14:17:20 +00:00
|
|
|
export market
|
|
|
|
|
2022-03-28 15:24:28 +00:00
|
|
|
type
|
|
|
|
OnChainMarket* = ref object of Market
|
2023-01-19 05:58:04 +00:00
|
|
|
contract: Marketplace
|
2022-03-28 15:24:28 +00:00
|
|
|
signer: Signer
|
|
|
|
MarketSubscription = market.Subscription
|
|
|
|
EventSubscription = ethers.Subscription
|
|
|
|
OnChainMarketSubscription = ref object of MarketSubscription
|
|
|
|
eventSubscription: EventSubscription
|
|
|
|
|
2023-01-19 05:58:04 +00:00
|
|
|
func new*(_: type OnChainMarket, contract: Marketplace): OnChainMarket =
|
2022-03-28 15:28:51 +00:00
|
|
|
without signer =? contract.signer:
|
2023-01-19 05:58:04 +00:00
|
|
|
raiseAssert("Marketplace contract should have a signer")
|
2022-03-29 14:17:20 +00:00
|
|
|
OnChainMarket(
|
|
|
|
contract: contract,
|
|
|
|
signer: signer,
|
|
|
|
)
|
2022-03-28 15:24:28 +00:00
|
|
|
|
2022-07-04 12:14:56 +00:00
|
|
|
method getSigner*(market: OnChainMarket): Future[Address] {.async.} =
|
|
|
|
return await market.signer.getAddress()
|
|
|
|
|
2022-11-08 07:10:17 +00:00
|
|
|
method myRequests*(market: OnChainMarket): Future[seq[RequestId]] {.async.} =
|
|
|
|
return await market.contract.myRequests
|
|
|
|
|
|
|
|
method requestStorage(market: OnChainMarket, request: StorageRequest){.async.} =
|
2022-03-29 07:47:49 +00:00
|
|
|
await market.contract.requestStorage(request)
|
|
|
|
|
2022-06-15 12:12:34 +00:00
|
|
|
method getRequest(market: OnChainMarket,
|
2022-08-17 02:29:44 +00:00
|
|
|
id: RequestId): Future[?StorageRequest] {.async.} =
|
2022-08-26 04:08:02 +00:00
|
|
|
try:
|
2022-09-19 02:42:17 +00:00
|
|
|
return some await market.contract.getRequest(id)
|
2022-09-21 02:50:11 +00:00
|
|
|
except ProviderError as e:
|
2022-09-20 03:24:28 +00:00
|
|
|
if e.revertReason.contains("Unknown request"):
|
|
|
|
return none StorageRequest
|
2022-09-21 02:50:11 +00:00
|
|
|
raise e
|
2022-06-15 12:12:34 +00:00
|
|
|
|
2022-11-08 07:10:17 +00:00
|
|
|
method getState*(market: OnChainMarket,
|
|
|
|
requestId: RequestId): Future[?RequestState] {.async.} =
|
|
|
|
try:
|
|
|
|
return some await market.contract.state(requestId)
|
|
|
|
except ProviderError as e:
|
|
|
|
if e.revertReason.contains("Unknown request"):
|
|
|
|
return none RequestState
|
|
|
|
raise e
|
|
|
|
|
|
|
|
method getRequestEnd*(market: OnChainMarket,
|
|
|
|
id: RequestId): Future[SecondsSince1970] {.async.} =
|
|
|
|
return await market.contract.requestEnd(id)
|
|
|
|
|
2022-07-04 10:11:18 +00:00
|
|
|
method getHost(market: OnChainMarket,
|
2022-08-17 02:29:44 +00:00
|
|
|
requestId: RequestId,
|
2022-07-27 14:54:34 +00:00
|
|
|
slotIndex: UInt256): Future[?Address] {.async.} =
|
|
|
|
let slotId = slotId(requestId, slotIndex)
|
|
|
|
let address = await market.contract.getHost(slotId)
|
2022-07-04 10:11:18 +00:00
|
|
|
if address != Address.default:
|
|
|
|
return some address
|
|
|
|
else:
|
|
|
|
return none Address
|
|
|
|
|
2022-07-20 13:59:07 +00:00
|
|
|
method fillSlot(market: OnChainMarket,
|
2022-08-17 02:29:44 +00:00
|
|
|
requestId: RequestId,
|
2022-07-20 13:59:07 +00:00
|
|
|
slotIndex: UInt256,
|
|
|
|
proof: seq[byte]) {.async.} =
|
|
|
|
await market.contract.fillSlot(requestId, slotIndex, proof)
|
|
|
|
|
2022-08-18 06:01:47 +00:00
|
|
|
method withdrawFunds(market: OnChainMarket,
|
2022-08-17 04:18:30 +00:00
|
|
|
requestId: RequestId) {.async.} =
|
2022-08-18 06:01:47 +00:00
|
|
|
await market.contract.withdrawFunds(requestId)
|
|
|
|
|
2022-03-28 15:24:28 +00:00
|
|
|
method subscribeRequests(market: OnChainMarket,
|
|
|
|
callback: OnRequest):
|
|
|
|
Future[MarketSubscription] {.async.} =
|
|
|
|
proc onEvent(event: StorageRequested) {.upraises:[].} =
|
2022-04-11 18:03:55 +00:00
|
|
|
callback(event.requestId, event.ask)
|
2022-03-28 15:24:28 +00:00
|
|
|
let subscription = await market.contract.subscribe(StorageRequested, onEvent)
|
|
|
|
return OnChainMarketSubscription(eventSubscription: subscription)
|
|
|
|
|
2022-07-28 12:33:17 +00:00
|
|
|
method subscribeSlotFilled*(market: OnChainMarket,
|
2022-08-17 02:29:44 +00:00
|
|
|
requestId: RequestId,
|
2022-07-28 12:33:17 +00:00
|
|
|
slotIndex: UInt256,
|
|
|
|
callback: OnSlotFilled):
|
|
|
|
Future[MarketSubscription] {.async.} =
|
|
|
|
proc onEvent(event: SlotFilled) {.upraises:[].} =
|
|
|
|
if event.requestId == requestId and event.slotIndex == slotIndex:
|
|
|
|
callback(event.requestId, event.slotIndex)
|
|
|
|
let subscription = await market.contract.subscribe(SlotFilled, onEvent)
|
|
|
|
return OnChainMarketSubscription(eventSubscription: subscription)
|
|
|
|
|
2022-06-14 07:47:05 +00:00
|
|
|
method subscribeFulfillment(market: OnChainMarket,
|
2022-08-17 02:29:44 +00:00
|
|
|
requestId: RequestId,
|
2022-06-14 07:47:05 +00:00
|
|
|
callback: OnFulfillment):
|
|
|
|
Future[MarketSubscription] {.async.} =
|
|
|
|
proc onEvent(event: RequestFulfilled) {.upraises:[].} =
|
2022-03-29 09:20:07 +00:00
|
|
|
if event.requestId == requestId:
|
2022-06-14 07:47:05 +00:00
|
|
|
callback(event.requestId)
|
|
|
|
let subscription = await market.contract.subscribe(RequestFulfilled, onEvent)
|
2022-03-29 09:20:07 +00:00
|
|
|
return OnChainMarketSubscription(eventSubscription: subscription)
|
|
|
|
|
2022-08-18 06:01:47 +00:00
|
|
|
method subscribeRequestCancelled*(market: OnChainMarket,
|
2022-08-17 04:18:30 +00:00
|
|
|
requestId: RequestId,
|
2022-08-18 06:01:47 +00:00
|
|
|
callback: OnRequestCancelled):
|
|
|
|
Future[MarketSubscription] {.async.} =
|
|
|
|
proc onEvent(event: RequestCancelled) {.upraises:[].} =
|
|
|
|
if event.requestId == requestId:
|
|
|
|
callback(event.requestId)
|
|
|
|
let subscription = await market.contract.subscribe(RequestCancelled, onEvent)
|
|
|
|
return OnChainMarketSubscription(eventSubscription: subscription)
|
|
|
|
|
2022-11-08 07:10:17 +00:00
|
|
|
method subscribeRequestFailed*(market: OnChainMarket,
|
|
|
|
requestId: RequestId,
|
|
|
|
callback: OnRequestFailed):
|
|
|
|
Future[MarketSubscription] {.async.} =
|
|
|
|
proc onEvent(event: RequestFailed) {.upraises:[].} =
|
|
|
|
if event.requestId == requestId:
|
|
|
|
callback(event.requestId)
|
|
|
|
let subscription = await market.contract.subscribe(RequestFailed, onEvent)
|
|
|
|
return OnChainMarketSubscription(eventSubscription: subscription)
|
|
|
|
|
2022-03-28 15:24:28 +00:00
|
|
|
method unsubscribe*(subscription: OnChainMarketSubscription) {.async.} =
|
|
|
|
await subscription.eventSubscription.unsubscribe()
|