Codex SDK in Javascript to interact with the Codex decentralized storage network.
Go to file
Arnaud 7c40e4af5f
Drastically reduce the bundle size by replacing Typebox with Valibot.
2024-08-15 12:11:16 +02:00
src Drastically reduce the bundle size by replacing Typebox with Valibot. 2024-08-15 12:11:16 +02:00
.editorconfig Add marketplace endpoints 2024-08-15 12:11:16 +02:00
.gitignore Add marketplace endpoints 2024-08-15 12:11:16 +02:00
.npmignore Add marketplace endpoints 2024-08-15 12:11:16 +02:00
LICENSE Initial commit 2024-08-15 12:10:44 +02:00
README.md Fix purchase detail description in README 2024-08-15 12:11:16 +02:00
package-lock.json Drastically reduce the bundle size by replacing Typebox with Valibot. 2024-08-15 12:11:16 +02:00
package.json Drastically reduce the bundle size by replacing Typebox with Valibot. 2024-08-15 12:11:16 +02:00
tsconfig.json Drastically reduce the bundle size by replacing Typebox with Valibot. 2024-08-15 12:11:16 +02:00

README.md

Codex SDK

The Codex SDK provides an API for interacting with the Codex decentralized storage network.

Import

import { Codex } from "@codex/sdk-js";

or

const { Codex } = require("@codex/sdk-js");

How to use

To create a Codex instance, provide the REST API url to interact with the Codex client:

const codex = new Codex("http://localhost:3000")

Error handling

The SDK provides a type called SafeValue for error handling instead of throwing errors. It is inspired by Go's "error as value" concept. If the value represents an error, error is true and data will contain the error. If the value is not an error, error is false and data will contain the requested data.

The error type is a CodexError which can be error object of 3 types:

  • error: Object containing the error message
  • api: Object containing the api error message and the status code
  • validation: Object containing the error message and a field errors of type ValidationError containing the error message for each fields.

Example:

const slots = await codex.marketplace.activeSlots(); 

if (slots.error) {
    // Do something to handle the error in slots.data 
    return 
}

// Access the slots within slots.data.

Marketplace

activeSlots()

Returns active slots.

Example:

const slots = await codex.marketplace.activeSlots(); 

activeSlot(slotId)

Returns active slot with id {slotId} for the host.

  • slotId (string, required)
  • returns Promise<CodexSlot[]>

Example:

const slotId=  "AB9........"
const slot = await codex.marketplace.activeSlot(slotId); 

availabilities

Returns storage that is for sale.

Example:

const availabilities = await codex.marketplace.availabilities(); 

createAvailability

Offers storage for sale.

Example:

const response = await codex.marketplace.createAvailability({
    maxCollateral: 1,
    totalSize: 3000,
    minPrice: 100,
    duration: 100,
}); 

reservations

Return list of reservations for ongoing Storage Requests that the node hosts.

Example:

const reservations = await codex.marketplace.reservations("Ox..."); 

createStorageRequest

Creates a new Request for storage

Example:

const request = await codex.marketplace.createStorageRequest({
    duration: 3000,
    reward: 100,
    proofProbability: 1,
    nodes: 1,
    tolerance: 0,
    collateral: 100,
    expiry: 3000
}); 

purchaseIds

Returns list of purchase IDs

  • returns Promise<string[]>

Example:

const ids = await codex.marketplace.purchaseIds(); 

purchaseDetail

Returns purchase details

Example:

const purchaseId =  "Ox........"
const purchase = await codex.marketplace.purchaseDetail(purchaseId);