logos-storage-js/README.md

184 lines
3.9 KiB
Markdown
Raw Normal View History

2024-08-15 12:08:48 +02:00
# Codex SDK
2024-08-15 12:08:15 +02:00
The Codex SDK provides an API for interacting with the Codex decentralized storage network.
2024-08-15 12:08:48 +02:00
The SDK has a small bundle size and support tree shaking.
2024-08-15 12:08:41 +02:00
2024-08-15 12:08:51 +02:00
The SDK is currently under early development and the API can change at any time.
2024-08-15 12:08:48 +02:00
## Import
2024-08-15 12:08:15 +02:00
```js
import { Codex } from "@codex/sdk-js";
```
2024-08-15 12:08:48 +02:00
or
2024-08-15 12:08:15 +02:00
```js
const { Codex } = require("@codex/sdk-js");
```
2024-08-15 12:08:48 +02:00
## How to use
2024-08-15 12:08:15 +02:00
2024-08-15 12:08:48 +02:00
To create a Codex instance, provide the REST API url to interact with the Codex client:
2024-08-15 12:08:15 +02:00
```js
2024-08-15 12:08:48 +02:00
const codex = new Codex("http://localhost:3000");
2024-08-15 12:08:15 +02:00
```
2024-08-15 12:08:41 +02:00
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.
```js
2024-08-15 12:08:48 +02:00
const marketplace = await codex.marketplace();
2024-08-15 12:08:41 +02:00
```
2024-08-15 12:08:48 +02:00
### Error handling
2024-08-15 12:08:15 +02:00
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.
2024-08-15 12:08:48 +02:00
The [CodexError](./src/errors/errors.ts#L16) contains a message and 3 optionals properties:
2024-08-15 12:08:15 +02:00
2024-08-15 12:08:48 +02:00
- `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
2024-08-15 12:08:15 +02:00
2024-08-15 12:08:48 +02:00
Example:
2024-08-15 12:08:15 +02:00
```js
2024-08-15 12:08:48 +02:00
const slots = marketplace.activeSlots();
2024-08-15 12:08:15 +02:00
if (slots.error) {
2024-08-15 12:08:48 +02:00
// Do something to handle the error in slots.data
return;
2024-08-15 12:08:15 +02:00
}
// Access the slots within slots.data.
```
2024-08-15 12:08:48 +02:00
### Marketplace
2024-08-15 12:08:15 +02:00
2024-08-15 12:08:48 +02:00
The following API assume that you have already a marketplace module loaded, example:
2024-08-15 12:08:41 +02:00
```js
2024-08-15 12:08:48 +02:00
const codex = new Codex("http://localhost:3000");
const marketplace = await codex.marketplace();
2024-08-15 12:08:41 +02:00
```
2024-08-15 12:08:15 +02:00
#### activeSlots()
Returns active slots.
2024-08-15 12:08:48 +02:00
- returns Promise<[CodexSlot](./src/marketplace/types.ts#L86)[]>
2024-08-15 12:08:15 +02:00
2024-08-15 12:08:48 +02:00
Example:
2024-08-15 12:08:15 +02:00
```js
2024-08-15 12:08:48 +02:00
const slots = await marketplace.activeSlots();
2024-08-15 12:08:15 +02:00
```
#### activeSlot(slotId)
Returns active slot with id {slotId} for the host.
- slotId (string, required)
- returns Promise<[CodexSlot](./src/marketplace/types.ts#L86)[]>
2024-08-15 12:08:48 +02:00
Example:
2024-08-15 12:08:15 +02:00
```js
2024-08-15 12:08:48 +02:00
const slotId = "AB9........";
const slot = await marketplace.activeSlot(slotId);
2024-08-15 12:08:15 +02:00
```
#### availabilities
Returns storage that is for sale.
- returns Promise<[CodexAvailability](./src/marketplace/types.ts#L100)>
2024-08-15 12:08:48 +02:00
Example:
2024-08-15 12:08:15 +02:00
```js
2024-08-15 12:08:48 +02:00
const availabilities = await marketplace.availabilities();
2024-08-15 12:08:15 +02:00
```
#### createAvailability
Offers storage for sale.
- input ([CodexCreateAvailabilityInput](./src/marketplace/types.ts#L133), required)
2024-08-15 12:08:48 +02:00
- returns Promise<[CodexAvailabilityCreateResponse](./src/marketplace/types.ts#L124)[]>
2024-08-15 12:08:15 +02:00
2024-08-15 12:08:48 +02:00
Example:
2024-08-15 12:08:15 +02:00
```js
2024-08-15 12:08:41 +02:00
const response = await marketplace.createAvailability({
2024-08-15 12:08:48 +02:00
maxCollateral: 1,
totalSize: 3000,
minPrice: 100,
duration: 100,
});
2024-08-15 12:08:15 +02:00
```
#### reservations
Return list of reservations for ongoing Storage Requests that the node hosts.
- availabilityId (string, required)
2024-08-15 12:08:48 +02:00
- returns Promise<[CodexReservation](./src/marketplace/types.ts#L156)[]>
2024-08-15 12:08:15 +02:00
2024-08-15 12:08:48 +02:00
Example:
2024-08-15 12:08:15 +02:00
```js
2024-08-15 12:08:48 +02:00
const reservations = await marketplace.reservations("Ox...");
2024-08-15 12:08:15 +02:00
```
#### createStorageRequest
Creates a new Request for storage
2024-08-15 12:08:48 +02:00
- input ([CodexCreateStorageRequestInput](./src/marketplace/types.ts#L186), required)
- returns Promise<[CodexCreateStorageRequestResponse](./src/marketplace/types.ts#L201)[]>
2024-08-15 12:08:15 +02:00
2024-08-15 12:08:48 +02:00
Example:
2024-08-15 12:08:15 +02:00
```js
2024-08-15 12:08:41 +02:00
const request = await marketplace.createStorageRequest({
2024-08-15 12:08:48 +02:00
duration: 3000,
reward: 100,
proofProbability: 1,
nodes: 1,
tolerance: 0,
collateral: 100,
expiry: 3000,
});
2024-08-15 12:08:15 +02:00
```
#### purchaseIds
Returns list of purchase IDs
- returns Promise<string[]>
2024-08-15 12:08:48 +02:00
Example:
2024-08-15 12:08:15 +02:00
```js
2024-08-15 12:08:48 +02:00
const ids = await marketplace.purchaseIds();
2024-08-15 12:08:15 +02:00
```
#### purchaseDetail
Returns purchase details
2024-08-15 12:08:15 +02:00
- purchaseId (string, required)
2024-08-15 12:08:48 +02:00
- returns Promise<[CodexPurchase](./src/marketplace/types.ts#L172)[]>
2024-08-15 12:08:15 +02:00
2024-08-15 12:08:48 +02:00
Example:
2024-08-15 12:08:15 +02:00
```js
2024-08-15 12:08:48 +02:00
const purchaseId = "Ox........";
const purchase = await marketplace.purchaseDetail(purchaseId);
2024-08-15 12:08:15 +02:00
```