diff --git a/src/marketplace/marketplace.ts b/src/marketplace/marketplace.ts index 2e8a70f..7eba084 100644 --- a/src/marketplace/marketplace.ts +++ b/src/marketplace/marketplace.ts @@ -8,10 +8,10 @@ import { type CodexAvailabilityCreateResponse, CodexCreateAvailabilityInput, CodexCreateStorageRequestInput, - type CodexCreateStorageRequestResponse, type CodexPurchase, type CodexReservation, type CodexSlot, + type CodexStorageRequest, CodexUpdateAvailabilityInput, } from "./types"; @@ -59,7 +59,7 @@ export class Marketplace { * Offers storage for sale */ async createAvailability( - input: CodexCreateAvailabilityInput, + input: CodexCreateAvailabilityInput ): Promise> { const result = v.safeParse(CodexCreateAvailabilityInput, input); @@ -89,7 +89,7 @@ export class Marketplace { * Existing Requests linked to this Availability will continue as is. */ async updateAvailability( - input: CodexUpdateAvailabilityInput, + input: CodexUpdateAvailabilityInput ): Promise> { const result = v.safeParse(CodexUpdateAvailabilityInput, input); @@ -119,7 +119,7 @@ export class Marketplace { * Return's list of Reservations for ongoing Storage Requests that the node hosts. */ async reservations( - availabilityId: string, + availabilityId: string ): Promise> { const url = this.url + @@ -142,6 +142,40 @@ export class Marketplace { }); } + async purchases(): Promise> { + const url = this.url + Api.config.prefix + `/storage/purchases`; + + const res = await Fetch.safeJson(url, { + method: "GET", + }); + + if (res.error) { + return res; + } + + const promises = []; + + for (const id of res.data) { + promises.push(this.purchaseDetail(id)); + } + + const purchases = await Promise.all(promises); + + return { + error: false, + data: purchases.map((p) => + p.error + ? ({ + state: "error", + error: p.data.message, + requestId: "", + request: {} as CodexStorageRequest, + } satisfies CodexPurchase) + : p.data + ), + }; + } + /** * Returns purchase details */ @@ -158,8 +192,8 @@ export class Marketplace { * Creates a new request for storage. */ async createStorageRequest( - input: CodexCreateStorageRequestInput, - ): Promise> { + input: CodexCreateStorageRequestInput + ): Promise> { const result = v.safeParse(CodexCreateStorageRequestInput, input); if (!result.success) { @@ -172,15 +206,38 @@ export class Marketplace { }; } - const { cid, ...body } = result.output; + const { + cid, + duration, + reward, + proofProbability, + nodes, + collateral, + expiry, + tolerance, + } = result.output; const url = this.url + Api.config.prefix + "/storage/request/" + cid; - return Fetch.safeJson(url, { + const res = await Fetch.safe(url, { method: "POST", - headers: { - "content-type": "application/json", - }, - body: JSON.stringify(body), + // headers: { + // "content-type": "application/json", + // }, + body: JSON.stringify({ + duration: duration.toString(), + reward: reward.toString(), + proofProbability: proofProbability.toString(), + nodes, + collateral: collateral.toString(), + expiry: expiry.toString(), + tolerance, + }), }); + + if (res.error) { + return res; + } + + return { error: false, data: await res.data.text() }; } } diff --git a/src/marketplace/types.ts b/src/marketplace/types.ts index 0897af0..7a7de58 100644 --- a/src/marketplace/types.ts +++ b/src/marketplace/types.ts @@ -181,6 +181,8 @@ export type CodexPurchase = { error: string; request: CodexStorageRequest; + + requestId: string; }; export const CodexCreateStorageRequestInput = v.strictObject({ @@ -197,8 +199,3 @@ export const CodexCreateStorageRequestInput = v.strictObject({ export type CodexCreateStorageRequestInput = v.InferOutput< typeof CodexCreateStorageRequestInput >; - -export type CodexCreateStorageRequestResponse = Omit< - CodexCreateStorageRequestInput, - "cid" ->;