@codex-storage/sdk-js
Version:
Codex SDK to interact with the Codex decentralized storage network.
294 lines (291 loc) • 8.36 kB
JavaScript
import { Api } from './chunk-JUEFSJIF.mjs';
import { Fetch, FetchAuthBuilder } from './chunk-2VOCE3TZ.mjs';
import { CodexError, CodexValibotIssuesMap } from './chunk-MVZZ6JVF.mjs';
import * as v from 'valibot';
var CodexCreateAvailabilityInput = v.strictObject({
totalSize: v.pipe(v.number(), v.minValue(1)),
duration: v.pipe(v.number(), v.minValue(1)),
minPricePerBytePerSecond: v.number(),
totalCollateral: v.number(),
enabled: v.optional(v.boolean()),
until: v.optional(v.number())
});
var CodexAvailabilityPatchInput = v.strictObject({
id: v.string(),
totalSize: v.pipe(v.number(), v.minValue(1)),
duration: v.pipe(v.number(), v.minValue(1)),
minPricePerBytePerSecond: v.number(),
totalCollateral: v.number(),
enabled: v.optional(v.boolean()),
until: v.optional(v.number())
});
var CodexCreateStorageRequestInput = v.strictObject({
cid: v.string(),
duration: v.pipe(v.number(), v.minValue(1)),
pricePerBytePerSecond: v.number(),
proofProbability: v.number(),
nodes: v.optional(v.number(), 1),
tolerance: v.optional(v.number(), 0),
expiry: v.pipe(v.number(), v.minValue(1)),
collateralPerByte: v.number()
});
// src/marketplace/marketplace.ts
var CodexMarketplace = class {
url;
auth = {};
constructor(url, options) {
this.url = url;
if (options == null ? void 0 : options.auth) {
this.auth = options.auth;
}
}
/**
* Returns active slots
*/
async activeSlots() {
const url = this.url + Api.config.prefix + "/sales/slots";
return Fetch.safeJson(url, {
method: "GET",
headers: FetchAuthBuilder.build(this.auth)
});
}
/**
* Returns active slot with id {slotId} for the host
*/
async activeSlot(slotId) {
const url = this.url + Api.config.prefix + "/sales/slots/" + slotId;
return Fetch.safeJson(url, {
method: "GET",
headers: FetchAuthBuilder.build(this.auth)
});
}
transformAvailability({
freeSize,
...a
}) {
const availability = {
...a,
minPricePerBytePerSecond: parseInt(a.minPricePerBytePerSecond, 10),
totalCollateral: parseInt(a.totalCollateral, 10),
totalRemainingCollateral: parseInt(a.totalRemainingCollateral, 10)
};
if (freeSize) {
availability.freeSize = freeSize;
}
return availability;
}
/**
* Returns storage that is for sale
*/
async availabilities() {
const url = this.url + Api.config.prefix + "/sales/availability";
const res = await Fetch.safeJson(url, {
method: "GET",
headers: FetchAuthBuilder.build(this.auth)
});
if (res.error) {
return res;
}
return {
error: false,
data: res.data.map(this.transformAvailability)
};
}
/**
* Offers storage for sale
*/
async createAvailability(input) {
const result = v.safeParse(CodexCreateAvailabilityInput, input);
if (!result.success) {
return {
error: true,
data: new CodexError("Cannot validate the input", {
errors: CodexValibotIssuesMap(result.issues)
})
};
}
const url = this.url + Api.config.prefix + "/sales/availability";
const body = {
totalSize: result.output.totalSize,
duration: result.output.duration,
minPricePerBytePerSecond: result.output.minPricePerBytePerSecond.toString(),
totalCollateral: result.output.totalCollateral.toString()
};
if (result.output.enabled) {
body.enabled = result.output.enabled;
}
if (result.output.until) {
body.until = result.output.until;
}
return Fetch.safeJson(url, {
method: "POST",
headers: FetchAuthBuilder.build(this.auth),
body: JSON.stringify(body)
}).then((result2) => {
if (result2.error) {
return result2;
}
return { error: false, data: this.transformAvailability(result2.data) };
});
}
/**
* The new parameters will be only considered for new requests.
* Existing Requests linked to this Availability will continue as is.
*/
async updateAvailability(input) {
const result = v.safeParse(CodexAvailabilityPatchInput, input);
if (!result.success) {
return {
error: true,
data: new CodexError("Cannot validate the input", {
errors: CodexValibotIssuesMap(result.issues)
})
};
}
const url = this.url + Api.config.prefix + "/sales/availability/" + result.output.id;
const body = {
totalSize: result.output.totalSize,
duration: result.output.duration,
minPricePerBytePerSecond: result.output.minPricePerBytePerSecond.toString(),
totalCollateral: result.output.totalCollateral.toString()
};
if (result.output.enabled) {
body.enabled = result.output.enabled;
}
if (result.output.until) {
body.until = result.output.until;
}
const res = await Fetch.safe(url, {
method: "PATCH",
headers: FetchAuthBuilder.build(this.auth),
body: JSON.stringify(body)
});
if (res.error) {
return res;
}
return { error: false, data: "" };
}
/**
* Return's list of Reservations for ongoing Storage Requests that the node hosts.
*/
async reservations(availabilityId) {
const url = this.url + Api.config.prefix + `/sales/availability/${availabilityId}/reservations`;
return Fetch.safeJson(url, {
method: "GET",
headers: FetchAuthBuilder.build(this.auth)
});
}
/**
* Returns list of purchase IDs
*/
async purchaseIds() {
const url = this.url + Api.config.prefix + `/storage/purchases`;
return Fetch.safeJson(url, {
method: "GET",
headers: FetchAuthBuilder.build(this.auth)
});
}
transformPurchase(p) {
const purchase = {
requestId: p.requestId,
state: p.state
};
if (p.error) {
purchase.error = p.error;
}
if (!p.request) {
return purchase;
}
return {
...purchase,
request: {
...p.request,
ask: {
...p.request.ask,
proofProbability: parseInt(p.request.ask.proofProbability, 10),
pricePerBytePerSecond: parseInt(
p.request.ask.pricePerBytePerSecond,
10
)
}
}
};
}
async purchases() {
const res = await this.purchaseIds();
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: ""
} : p.data
)
};
}
/**
* Returns purchase details
*/
async purchaseDetail(purchaseId) {
const url = this.url + Api.config.prefix + `/storage/purchases/` + purchaseId;
return Fetch.safeJson(url, {
headers: FetchAuthBuilder.build(this.auth),
method: "GET"
}).then((res) => {
if (res.error) {
return res;
}
return { error: false, data: this.transformPurchase(res.data) };
});
}
/**
* Creates a new request for storage.
*/
async createStorageRequest(input) {
const result = v.safeParse(CodexCreateStorageRequestInput, input);
if (!result.success) {
return {
error: true,
data: new CodexError("Cannot validate the input", {
errors: CodexValibotIssuesMap(result.issues)
})
};
}
const {
cid,
duration,
pricePerBytePerSecond,
proofProbability,
nodes,
collateralPerByte,
expiry,
tolerance
} = result.output;
const url = this.url + Api.config.prefix + "/storage/request/" + cid;
return Fetch.safeText(url, {
method: "POST",
headers: FetchAuthBuilder.build(this.auth),
body: JSON.stringify({
duration,
pricePerBytePerSecond: pricePerBytePerSecond.toString(),
proofProbability: proofProbability.toString(),
nodes,
collateralPerByte: collateralPerByte.toString(),
expiry,
tolerance
})
});
}
};
export { CodexAvailabilityPatchInput, CodexCreateAvailabilityInput, CodexCreateStorageRequestInput, CodexMarketplace };
//# sourceMappingURL=chunk-IKKEHM5H.mjs.map
//# sourceMappingURL=chunk-IKKEHM5H.mjs.map