Codex SDK in Javascript to interact with the Codex decentralized storage network.
Go to file
Arnaud dd348801cd
Update dependencies
2024-09-20 10:50:54 +02:00
.github/workflows Add publish workflow (#1) 2024-08-30 13:58:25 +00:00
src Update availability endpoints 2024-09-20 10:50:35 +02:00
.gitignore Fix documentation and types 2024-08-30 12:24:00 +02:00
.npmignore Add marketplace endpoints 2024-08-15 12:11:16 +02:00
LICENSE-APACHEv2 Update licences 2024-08-29 19:41:01 +02:00
LICENSE-MIT Update licences 2024-08-29 19:41:01 +02:00
README.md Fix readme 2024-09-20 10:50:43 +02:00
package-lock.json Update dependencies 2024-09-20 10:50:54 +02:00
package.json Update dependencies 2024-09-20 10:50:54 +02:00
prettier.config.cjs Apply prettier format 2024-08-15 12:11:17 +02:00
tsconfig.json Fix documentation and types 2024-08-30 12:24:00 +02:00
tsconfig.test.json Add data and marketplace API endpoints 2024-08-15 12:11:17 +02:00

README.md

Codex SDK

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

The SDK has a small bundle size and support tree shaking.

The SDK is currently under early development and the API can change at any time.

How to use

Sync api

The easiest way is to use the sync API, but you will not benefit from tree shaking.

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

or

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

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

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

Then you can access any module like this:

const marketplace = codex.marketplace;

Async api

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

or

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

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

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

To use a module, you need to use the await syntax. If the module is not loaded yet, it will be imported first and then cached in memory.

const marketplace = await codex.marketplace;

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 CodexError contains a message and 3 optionals properties:

  • code: The (http) code error when it comes from a request
  • errors: A {ValidationError} array when it comes from an object validation process
  • stack: The error stack when the CodexError results from a error thrown

Example:

const slots = marketplace.activeSlots();

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

// Access the slots within slots.data.

Marketplace

The following API assume that you have already a marketplace module loaded, example:

const codex = new Codex("http://localhost:3000");
const marketplace = await codex.marketplace();

activeSlots()

Returns active slots.

Example:

const slots = await 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 marketplace.activeSlot(slotId);

availabilities

Returns storage that is for sale.

Example:

const availabilities = await marketplace.availabilities();

createAvailability

Offers storage for sale.

Example:

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

updateAvailability

Updates availability.

Example:

const response = await marketplace.updateAvailability({
  id: "0x.....................",
  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 marketplace.reservations("Ox...");

createStorageRequest

Creates a new Request for storage

Example:

const request = await 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 marketplace.purchaseIds();

purchaseDetail

Returns purchase details

Example:

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

Data

The following API assume that you have already a data module loaded, example:

const codex = new Codex("http://localhost:3000");
const data = await codex.data;

cids

Returns the manifest stored locally in node.

Example:

const cids = await data.cids();

space

Returns a summary of the storage space allocation of the node

Example:

const space = await data.space();

upload

Upload a file in a streaming manner

  • file (File, require)
  • onProgress (onProgress: (loaded: number, total: number) => void, optional)
  • returns Promise<UploadResponse[]>

Example:

// Get file from previous event
const [file] = e.target.files

const upload = await data.upload(file, (loaded: number, total: number) => {
  // Use loaded and total so update a progress bar for example
});
await upload.result();

Debug

The following API assume that you have already a node module loaded, example:

const codex = new Codex("http://localhost:3000");
const data = await codex.debug;

setLogLevel

Set log level at run time.

Example:

await debug.setLogLevel("DEBUG");

info

Gets node information

Example:

const info = await debug.info();

Node

The following API assume that you have already a node module loaded, example:

const codex = new Codex("http://localhost:3000");
const node = await codex.node;

spr

Get Node's SPR

Example:

const spr = await node.spr();