nim-codex/codex/contracts/market.nim

84 lines
2.9 KiB
Nim

import pkg/ethers
import pkg/upraises
import pkg/questionable
import ../market
import ./storage
export market
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 =
without signer =? contract.signer:
raiseAssert("Storage contract should have a signer")
OnChainMarket(
contract: contract,
signer: signer,
)
method getSigner*(market: OnChainMarket): Future[Address] {.async.} =
return await market.signer.getAddress()
method requestStorage(market: OnChainMarket,
request: StorageRequest):
Future[StorageRequest] {.async.} =
var request = request
request.client = await market.signer.getAddress()
await market.contract.requestStorage(request)
return request
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
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
method fulfillRequest(market: OnChainMarket,
requestId: array[32, byte],
proof: seq[byte]) {.async.} =
await market.contract.fulfillRequest(requestId, proof)
method fillSlot(market: OnChainMarket,
requestId: array[32, byte],
slotIndex: UInt256,
proof: seq[byte]) {.async.} =
await market.contract.fillSlot(requestId, slotIndex, proof)
method subscribeRequests(market: OnChainMarket,
callback: OnRequest):
Future[MarketSubscription] {.async.} =
proc onEvent(event: StorageRequested) {.upraises:[].} =
callback(event.requestId, event.ask)
let subscription = await market.contract.subscribe(StorageRequested, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription)
method subscribeFulfillment(market: OnChainMarket,
requestId: array[32, byte],
callback: OnFulfillment):
Future[MarketSubscription] {.async.} =
proc onEvent(event: RequestFulfilled) {.upraises:[].} =
if event.requestId == requestId:
callback(event.requestId)
let subscription = await market.contract.subscribe(RequestFulfilled, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription)
method unsubscribe*(subscription: OnChainMarketSubscription) {.async.} =
await subscription.eventSubscription.unsubscribe()