2022-03-28 15:24:28 +00:00
|
|
|
import pkg/ethers
|
|
|
|
import pkg/upraises
|
2022-03-28 15:28:51 +00:00
|
|
|
import pkg/questionable
|
2022-03-28 15:24:28 +00:00
|
|
|
import ../market
|
|
|
|
import ./storage
|
|
|
|
|
2022-03-29 14:17:20 +00:00
|
|
|
export market
|
|
|
|
|
2022-03-28 15:24:28 +00:00
|
|
|
type
|
|
|
|
OnChainMarket* = ref object of Market
|
|
|
|
contract: Storage
|
|
|
|
signer: Signer
|
|
|
|
MarketSubscription = market.Subscription
|
|
|
|
EventSubscription = ethers.Subscription
|
|
|
|
OnChainMarketSubscription = ref object of MarketSubscription
|
|
|
|
eventSubscription: EventSubscription
|
|
|
|
|
|
|
|
func new*(_: type OnChainMarket, contract: Storage): OnChainMarket =
|
2022-03-28 15:28:51 +00:00
|
|
|
without signer =? contract.signer:
|
|
|
|
raiseAssert("Storage 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-03-31 08:46:03 +00:00
|
|
|
method requestStorage(market: OnChainMarket,
|
|
|
|
request: StorageRequest):
|
|
|
|
Future[StorageRequest] {.async.} =
|
2022-03-29 07:47:49 +00:00
|
|
|
var request = request
|
|
|
|
request.client = await market.signer.getAddress()
|
|
|
|
await market.contract.requestStorage(request)
|
2022-03-31 08:46:03 +00:00
|
|
|
return request
|
2022-03-29 07:47:49 +00:00
|
|
|
|
2022-06-15 12:12:34 +00:00
|
|
|
method getRequest(market: OnChainMarket,
|
|
|
|
id: array[32, byte]): Future[?StorageRequest] {.async.} =
|
|
|
|
let request = await market.contract.getRequest(id)
|
|
|
|
if request != StorageRequest.default:
|
|
|
|
return some request
|
|
|
|
else:
|
|
|
|
return none StorageRequest
|
|
|
|
|
2022-07-04 10:11:18 +00:00
|
|
|
method getHost(market: OnChainMarket,
|
|
|
|
id: array[32, byte]): Future[?Address] {.async.} =
|
|
|
|
let address = await market.contract.getHost(id)
|
|
|
|
if address != Address.default:
|
|
|
|
return some address
|
|
|
|
else:
|
|
|
|
return none Address
|
|
|
|
|
2022-06-14 07:47:05 +00:00
|
|
|
method fulfillRequest(market: OnChainMarket,
|
|
|
|
requestId: array[32, byte],
|
|
|
|
proof: seq[byte]) {.async.} =
|
|
|
|
await market.contract.fulfillRequest(requestId, proof)
|
2022-03-29 09:20:07 +00:00
|
|
|
|
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-06-14 07:47:05 +00:00
|
|
|
method subscribeFulfillment(market: OnChainMarket,
|
|
|
|
requestId: array[32, byte],
|
|
|
|
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-03-28 15:24:28 +00:00
|
|
|
method unsubscribe*(subscription: OnChainMarketSubscription) {.async.} =
|
|
|
|
await subscription.eventSubscription.unsubscribe()
|