Skip to content

Commit

Permalink
docs: add documentation to client-vms
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberglot committed Sep 25, 2024
1 parent 3b45990 commit 5e742ee
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/client-vms/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import debug from "debug";

/*
* `Log` is a debug logger that can be used to log messages to the console.
* @param message: string
*/
export const Log = debug("nillion:vms");
Log.log = console.log.bind(console);
13 changes: 13 additions & 0 deletions packages/client-vms/src/nada.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ import {

import { StoreValueArgs } from "./types";

/**
* `toNadaValues` is a function that takes a `NamedValue` or `string` and a `NadaPrimitiveValue` or `StoreValueArgs` and returns a `NadaValues` object.
* @param args: {@link toNadaValuesArgs}
* @returns {@link NadaValues}
* @throws
* @example
* ```ts
* const nadaValues = toNadaValues({
* name: "myValue",
* value: "myValueData",
* });
* ```
*/
export const toNadaValues = (args: {
name: NamedValue | string;
value: NadaPrimitiveValue | StoreValueArgs;
Expand Down
28 changes: 28 additions & 0 deletions packages/client-vms/src/nilvm/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ import * as Wasm from "@nillion/client-wasm";

import { Log } from "../logger";

/* `NilVmClientConfig` is an object that contains the configuration for the NilVmClient
* @param bootnodes: `Multiaddr[]`
* @param clusterId: `ClusterId`
* @param userSeed: `string`
*/
export const NilVmClientConfig = z.object({
bootnodes: z.array(Multiaddr),
clusterId: ClusterId,
Expand All @@ -43,6 +48,10 @@ export const NilVmClientConfig = z.object({

export type NilVmClientConfig = z.infer<typeof NilVmClientConfig>;

/**
* `NilVmClient` is a class that allows you to interact with Nillion.
* @class
*/
export class NilVmClient {
// The wasm bundle is loaded asynchronously which can be problematic because most environments don't
// support top-level awaits. To manage this complexity `this._client` is lazily initialized and guarded
Expand All @@ -54,29 +63,35 @@ export class NilVmClient {

private constructor(private _config: NilVmClientConfig) {}

/* `ready` is a boolean that indicates whether the client is ready to be used */
get ready(): boolean {
return this._ready;
}

/* `partyId` is a `PartyId` object that represents the party ID of the client */
get partyId(): PartyId {
this.isReadyGuard();
return PartyId.parse(this._client.party_id);
}

/* `userId` is a `UserId` object that represents the user ID of the client */
get userId(): UserId {
this.isReadyGuard();
return UserId.parse(this._client.user_id);
}

/* `clusterId` is a `ClusterId` object that represents the cluster ID of the client */
get clusterId(): ClusterId {
return this._config.clusterId;
}

/* `client` is a `Wasm.NillionClient` object that represents the client */
get client(): Wasm.NillionClient {
this.isReadyGuard();
return this._client;
}

/* `connect` is an async function that connects the client to the cluster */
async connect(): Promise<boolean> {
if (!globalThis.__NILLION?.initialized) {
await init();
Expand All @@ -95,6 +110,7 @@ export class NilVmClient {
return this._ready;
}

/* `isReadyGuard` is a function that checks if the client is ready */
private isReadyGuard(): void | never {
if (!this._ready) {
const message = "NilVmClient not ready. Call `await client.connect()`.";
Expand All @@ -103,6 +119,7 @@ export class NilVmClient {
}
}

/* `fetchClusterInfo` is an effect that fetches the cluster information */
fetchClusterInfo(): E.Effect<ClusterDescriptor, UnknownException> {
return E.tryPromise(async () => {
const response = await this.client.cluster_information(this.clusterId);
Expand All @@ -112,6 +129,7 @@ export class NilVmClient {
});
}

/* `fetchComputeOutput` is an effect that fetches the compute */
fetchComputeOutput(args: {
id: ComputeOutputId;
}): E.Effect<Record<string, NadaPrimitiveValue>, UnknownException> {
Expand All @@ -126,6 +144,7 @@ export class NilVmClient {
});
}

/* `fetchOperationQuote` is an effect that fetches the operation quote */
fetchOperationQuote(args: {
operation: IntoWasmQuotableOperation & { type: OperationType };
}): E.Effect<PriceQuote, UnknownException> {
Expand All @@ -141,6 +160,7 @@ export class NilVmClient {
});
}

/* `fetchValue` is an effect that fetches the value */
fetchValue(args: {
receipt: PaymentReceipt;
operation: FetchValue;
Expand All @@ -165,6 +185,7 @@ export class NilVmClient {
});
}

/* `fetchStoreAcl` is an effect that fetches the store acl */
fetchStoreAcl(args: {
receipt: PaymentReceipt;
operation: FetchStoreAcl;
Expand All @@ -188,6 +209,7 @@ export class NilVmClient {
});
}

/* `setStoreAcl` is an effect that sets the store acl */
setStoreAcl(args: {
receipt: PaymentReceipt;
operation: SetStoreAcl;
Expand All @@ -214,6 +236,7 @@ export class NilVmClient {
});
}

/* `compute` is an effect that computes the operation */
compute(args: {
receipt: PaymentReceipt;
operation: Compute;
Expand Down Expand Up @@ -241,6 +264,7 @@ export class NilVmClient {
});
}

/* `storeProgram` is an effect that stores the program */
storeProgram(args: {
receipt: PaymentReceipt;
operation: StoreProgram;
Expand All @@ -265,6 +289,7 @@ export class NilVmClient {
});
}

/* `deleteProgram` is an effect that deletes the program */
deleteValues(args: { id: StoreId }): E.Effect<StoreId, UnknownException> {
return E.tryPromise(async () => {
const { id } = args;
Expand All @@ -274,6 +299,7 @@ export class NilVmClient {
});
}

/* `deleteProgram` is an effect that deletes the program */
updateValues(args: {
receipt: PaymentReceipt;
operation: UpdateValue;
Expand All @@ -299,6 +325,7 @@ export class NilVmClient {
});
}

/* `storeValues` is an effect that stores the values */
storeValues(args: {
receipt: PaymentReceipt;
operation: StoreValue;
Expand Down Expand Up @@ -326,5 +353,6 @@ export class NilVmClient {
});
}

/* `create` is a static function that creates a new NilVmClient */
static create = (args: NilVmClientConfig) => new NilVmClient(args);
}
17 changes: 17 additions & 0 deletions packages/client-vms/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,22 @@ import {
UserSeed,
} from "@nillion/client-core";

/** `StoreValueArgs` is an interface that can be passed to the `store` function
* @param data: `NadaPrimitiveValue
* @param secret: `boolean`
*/
export interface StoreValueArgs {
data: NadaPrimitiveValue;
secret: boolean;
}

/** `NetworkConfig` is an interface that can be passed to the `toNadaValues` function
* @param bootnotes: `Multiaddr[]`
* @param clusterId: `ClusterId`
* @param nilChainId: `ChainId`
* @param nilChainEndpoint: `Url`
* @type
*/
export const NetworkConfig = z.object({
bootnodes: z.array(Multiaddr),
clusterId: ClusterId,
Expand All @@ -24,6 +35,12 @@ export const NetworkConfig = z.object({
});
export type NetworkConfig = z.infer<typeof NetworkConfig>;

/** `UserCredentials` is an interface that can be passed to the `toNadaValues` function
* @param nodeSeed: `NodeSeed`
* @param signer: `OfflineSigner`
* @param userSeed: `UserSeed`
* @type
*/
export const UserCredentials = z.object({
userSeed: UserSeed,
nodeSeed: NodeSeed.default(() => window.crypto.randomUUID()),
Expand Down

0 comments on commit 5e742ee

Please sign in to comment.