Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GridClient types and docstrings #3024

Merged
merged 45 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
5984fcf
chore: WIP:: Thinking about creating a base types module for each mod…
Mahmoud-Emad Jun 25, 2024
0b48b73
style: Write docstrings for all GridClient functions to clarify their…
Mahmoud-Emad Jun 26, 2024
3e98ca0
docs: Documented the networks module.
Mahmoud-Emad Jun 26, 2024
d4d1f4d
docs: Documented the GridClient module.
Mahmoud-Emad Jun 26, 2024
534ff31
docs: Documented the client loader:
Mahmoud-Emad Jun 26, 2024
9cb0ca3
Merge branch 'development_2.6' into development_2.6_gridclient_types
Mahmoud-Emad Jun 26, 2024
fc3fb2e
docs: Documented the machines module
Mahmoud-Emad Jun 26, 2024
bf3c723
docs: Documented the Algorand module
Mahmoud-Emad Jun 26, 2024
c714dd3
docs: Documented the BackendStorage module
Mahmoud-Emad Jun 26, 2024
4425500
fix: Fix the build workflow.
Mahmoud-Emad Jun 27, 2024
a8ea7ae
docs: Documented the BlockChain module
Mahmoud-Emad Jun 27, 2024
e67a4f7
docs: Documented the BlockChain, Calculator modules
Mahmoud-Emad Jun 27, 2024
4cd8e60
Merge branch 'development_2.6_gridclient_types' of https://github.com…
Mahmoud-Emad Jun 27, 2024
68bb435
docs: Documented the ZOS module
Mahmoud-Emad Jun 27, 2024
103c17b
docs: Documented the TFChain module
Mahmoud-Emad Jun 27, 2024
8622e14
Merge branch 'development_2.6' into development_2.6_gridclient_types
Mahmoud-Emad Jul 1, 2024
5575ac9
docs: Documented the Contracts module
Mahmoud-Emad Jul 1, 2024
9f55b43
docs: Documented the Balance module
Mahmoud-Emad Jul 1, 2024
f2eb051
chore: WIP:: Documented the base module:
Mahmoud-Emad Jul 1, 2024
50bc24a
docs: Documented the Bridge module.
Mahmoud-Emad Jul 1, 2024
1b15eb4
chore: Reduce the import size.
Mahmoud-Emad Jul 1, 2024
1d6814a
docs: Documented the Capacity module.
Mahmoud-Emad Jul 1, 2024
5261a23
docs: Documented the Currency module.
Mahmoud-Emad Jul 1, 2024
f18ff5e
docs: Documented the DAO, FarmerBot modules.
Mahmoud-Emad Jul 1, 2024
075d4ca
docs: Documented the Farm module.
Mahmoud-Emad Jul 2, 2024
262edfb
docs: Documented the Gateway module.
Mahmoud-Emad Jul 2, 2024
520b4ad
docs: Documented the K8S module.
Mahmoud-Emad Jul 2, 2024
b8075d6
docs: Documented the TFKVStore module.
Mahmoud-Emad Jul 2, 2024
9ae80aa
docs: Documented the Nodes/TFChain.contracts modules.
Mahmoud-Emad Jul 2, 2024
f18b36d
docs: Documented the QSFSZdbs module.
Mahmoud-Emad Jul 2, 2024
3750110
docs: Documented the Stellar module.
Mahmoud-Emad Jul 2, 2024
b4e1cd6
docs: Documented the Twin module.
Mahmoud-Emad Jul 2, 2024
d65984d
docs: Documented the Utils module.
Mahmoud-Emad Jul 2, 2024
2ee97d4
docs: Documented the ZDB module.
Mahmoud-Emad Jul 2, 2024
692575f
Merge branch 'development' into development_2.6_gridclient_types
Mahmoud-Emad Jul 3, 2024
4fedf32
Merge branch 'development' into development_2.6_gridclient_types
Mahmoud-Emad Jul 3, 2024
93d29c4
feat: Added the test-report into the gitignore.
Mahmoud-Emad Jul 3, 2024
0640514
feat: Added the addAccess attr to the network interface in the kubern…
Mahmoud-Emad Jul 3, 2024
bcd26b1
fix: Applied changes:
Mahmoud-Emad Jul 10, 2024
21bae53
Merge branch 'development' into development_2.6_gridclient_types
Mahmoud-Emad Jul 16, 2024
9e462ba
Merge branch 'development' into development_2.6_gridclient_types
Mahmoud-Emad Aug 4, 2024
841a243
style: Apply the comments.
Mahmoud-Emad Aug 4, 2024
c96ecb5
chore: Remove the secret
Mahmoud-Emad Aug 7, 2024
40196ea
chore: Remove the test script
Mahmoud-Emad Aug 7, 2024
f9383b3
chore: Apply comments:
Mahmoud-Emad Aug 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ dist/
yarn-error.log
packages/grid_client/docs/tests/
packages/grid_client/docs/api/
packages/grid_client/tests/test-reports/

packages/stats/public/build/
packages/playground/dist/
Expand Down
7 changes: 3 additions & 4 deletions packages/grid_client/scripts/add_node_to_network.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { NetworkAddNodeModel } from "../src";
import { GridClient, NetworkAddNodeModel } from "../src";
import { getClient } from "./client_loader";
import { log } from "./utils";

async function addNode(client, node) {
async function addNode(client: GridClient, node: NetworkAddNodeModel) {
const res = await client.networks.addNode(node);
log("================= Adding node =================");
log(res);
log("================= Adding node =================");
}

async function main() {
const grid3 = await getClient();
const grid3: GridClient = await getClient("vm/vmu0sir");
const node: NetworkAddNodeModel = {
name: "wedtest",
ipRange: "10.249.0.0/16",
Expand All @@ -19,7 +19,6 @@ async function main() {
};
// if the network is not created, it will create one and add this node to it.
await addNode(grid3, node);

await grid3.disconnect();
}
main();
16 changes: 8 additions & 8 deletions packages/grid_client/scripts/calculator.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
import { CalculatorModel, CUModel, SUModel } from "../src";
import { CalculatorModel, CUModel, GridClient, SUModel } from "../src";
import { getClient } from "./client_loader";
import { log } from "./utils";

async function calculateCU(client, CUModel) {
async function calculateCU(client: GridClient, CUModel: CUModel) {
const res = await client.calculator.calCU(CUModel);
log("================= Calculating CU =================");
log(res);
log("================= Calculating CU =================");
}

async function calculateSU(client, SUModel) {
async function calculateSU(client: GridClient, SUModel: SUModel) {
const res = await client.calculator.calSU(SUModel);
log("================= Calculating SU =================");
log(res);
log("================= Calculating SU =================");
}

async function getTFTPrice(client) {
async function getTFTPrice(client: GridClient) {
const res = await client.calculator.tftPrice();
log("================= TFT Price =================");
log(res);
log("================= TFT Price =================");
}

async function getPricingPolicy(client) {
async function getPricingPolicy(client: GridClient) {
const res = await client.calculator.getPrices();
log("================= Pricing Policy =================");
log(res);
log("================= Pricing Policy =================");
}

async function calculateDeployment(client, CalculatorModel) {
async function calculateDeployment(client: GridClient, CalculatorModel: CalculatorModel) {
const res = await client.calculator.calculate(CalculatorModel);
log("================= Deployment Cost =================");
log(res);
log("================= Deployment Cost =================");
}

async function calculateDeploymentWithCurrentBalance(client, CalculatorModel) {
async function calculateDeploymentWithCurrentBalance(client: GridClient, CalculatorModel: CalculatorModel) {
const res = await client.calculator.calculateWithMyBalance(CalculatorModel);
log("================= Deployment Cost with current balance =================");
log(res);
log("================= Deployment Cost with current balance =================");
}

async function main() {
const grid3 = await getClient();
const grid3: GridClient = await getClient();
const CalculatorModel: CalculatorModel = {
cru: 16,
mru: 8, // GB
Expand Down
6 changes: 3 additions & 3 deletions packages/grid_client/scripts/checkNodeAlive.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { PingNodeOptionsModel } from "../src";
import { GridClient, PingNodeOptionsModel } from "../src";
import { getClient } from "./client_loader";
import { log } from "./utils";

async function pingNode(client, nodeId) {
const res = await client.zos.pingNode(nodeId);
async function pingNode(client: GridClient, node: PingNodeOptionsModel) {
const res = await client.zos.pingNode(node);
log("================= Ping result =================");
log(res);
log("================= Ping result =================");
Expand Down
57 changes: 43 additions & 14 deletions packages/grid_client/scripts/client_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,51 @@ import fs from "fs";
import path from "path";
import { env } from "process";

import { GridClient } from "../src";
import { ClientOptions, GridClient, NetworkEnv } from "../src";

const network = env.NETWORK;
const network = env.NETWORK as NetworkEnv;
const mnemonic = env.MNEMONIC;
const storeSecret = env.STORE_SECRET;
const ssh_key = env.SSH_KEY;
let config;

if (network === undefined || mnemonic === undefined || ssh_key === undefined) {
console.log("Credentials not all found in env variables. Loading all credentials from default config.json...");
config = JSON.parse(fs.readFileSync(path.join(__dirname, "./config.json"), "utf-8"));
} else {
console.log("Credentials loaded from env variables...");
config = {
network: network,
mnemonic: mnemonic,
storeSecret: storeSecret,
ssh_key: ssh_key,
};
let config: { ssh_key: string } & ClientOptions;
setConfig();

/**
* Loads configuration either from environment variables or from a config.json file.
* If environment variables are not fully set, it falls back to loading from the config.json file.
*/
function setConfig() {
if (network === undefined || mnemonic === undefined || ssh_key === undefined) {
console.log("Credentials not all found in env variables. Loading all credentials from default config.json...");
config = JSON.parse(fs.readFileSync(path.join(__dirname, "./config.json"), "utf-8"));
} else {
console.log("Credentials loaded from env variables...");
config = {
network: network,
mnemonic: mnemonic,
storeSecret: storeSecret,
ssh_key: ssh_key,
};
}
}

/**
* Function to instantiate and connect a GridClient with a specified project name.
*
* The project name is important to list your deployments.
* @example
*
* const deploymentName = 'testVM'
* const projectName = 'vm' + deploymentName
Mahmoud-Emad marked this conversation as resolved.
Show resolved Hide resolved
* const gc = await getClient(projectName)
* await gc.machines.deploy({...});
* // When you deploy using this project name, you'll only be able to list your deployments using this same project name.
* await gc.machines.getObj(deploymentName);
*
* @param projectName - Optional project name to use with the GridClient instance.
* @returns {Promise<GridClient>} - A promise that resolves to a connected GridClient instance.
*/
async function getClient(projectName = ""): Promise<GridClient> {
const gridClient = new GridClient({
network: config.network,
Expand All @@ -34,6 +58,11 @@ async function getClient(projectName = ""): Promise<GridClient> {
return gridClient;
}

/**
* Function to instantiate and connect a new GridClient using the existing mnemonic as the store secret.
*
* @returns {Promise<GridClient>} - A promise that resolves to a connected GridClient instance.
*/
async function getNewClient(): Promise<GridClient> {
const gridClient = new GridClient({
network: config.network,
Expand Down
2 changes: 1 addition & 1 deletion packages/grid_client/scripts/create_account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { NetworkEnv } from "../src/config";
import config from "./config.json";
import { log } from "./utils";

async function createAccount(client, relay) {
async function createAccount(client: GridClient, relay: string) {
const res = await client.tfchain.createAccount(relay, true);
log("================= Creating account =================");
log(res);
Expand Down
66 changes: 64 additions & 2 deletions packages/grid_client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class GridClient {
config: GridClientConfig;
rmbClient: RMBClient;
tfclient: TFClient;
/**The `MachinesModule` class provides methods to interact with machine operations.*/
machines: modules.machines;
k8s: modules.k8s;
zdbs: modules.zdbs;
Expand All @@ -36,22 +37,36 @@ class GridClient {
capacity: modules.capacity;
twinId: number;
nodes: modules.nodes;
/**The `Algorand` class provides methods to interact with algorand operations.*/
algorand: modules.algorand;
/**The `TFChain` class provides methods to interact with tfchain operations.*/
tfchain: modules.tfchain;
/**The `Stellar` class provides methods to interact with stellar operations.*/
stellar: modules.stellar;
/**The `Blockchain` class representing a Blockchain interface that manages accounts across different blockchain types such as Algorand, Stellar, and TFChain.*/
blockchain: modules.blockchain;
/**The `Calculator` class for performing various calculations related to pricing and resources.*/
calculator: modules.calculator;
currency: modules.currency;
utility: modules.utility;
farmerbot: modules.farmerbot;
farms: modules.farms;
/**The `NetworkModule` class provides methods to interact with network operations.*/
networks: modules.networks;
dao: modules.dao;
bridge: modules.bridge;
modules: string[] = [];

readonly _mnemonic: string;

/**
* The `GridClient` class is a main entry point for interacting with the Grid.
* It provides methods to connect to the `chain`, manage node resources, and interact with various modules.
*
* Initializes a new instance of the `GridClient` class.
*
* @param {ClientOptions} clientOptions - The client options for configuring the Grid client.
*/
constructor(public clientOptions: ClientOptions) {
if (!clientOptions.storeSecret && validateMnemonic(clientOptions.mnemonic)) {
this._mnemonic = clientOptions.mnemonic;
Expand Down Expand Up @@ -85,6 +100,17 @@ class GridClient {
throw new GridClientError(`Unknown NETWORK selected! Acceptable networks are [dev | qa | test | main ]`);
}
}

/**
* Connects to the Grid based on the network, could be [`devnet`, `qanet`, `testnet`, `mainnet`].
*
* This method sets up the necessary clients for interacting with the Grid,
* establishes connections, and performs key migrations if required.
*
* @returns {Promise<void>} A promise that resolves when the connection is established.
*
* @throws {TwinNotExistError} If the twin for the provided mnemonic does not exist on the network.
*/
async connect(): Promise<void> {
const urls = this.getDefaultUrls(this.clientOptions.network);

Expand Down Expand Up @@ -119,6 +145,11 @@ class GridClient {
await migrateKeysEncryption.apply(this, [GridClient]);
}

/**
* Internal method to initialize the client configuration and modules.
*
* This method is called internally to set up the configuration and instantiate the modules.
*/
_connect(): void {
const urls = this.getDefaultUrls(this.clientOptions.network);
const storePath = PATH.join(appPath, this.clientOptions.network, String(this.twinId));
Expand Down Expand Up @@ -151,6 +182,15 @@ class GridClient {
}
}

/**
* Tests the connection URLs to ensure they are reachable.
*
* @param {Record<string, string>} urls - The URLs to test.
*
* @returns {Promise<void>} A promise that resolves when the URLs are successfully tested.
*
* @throws {Error} If any of the URLs fail to connect.
*/
async testConnectionUrls(urls: Record<string, string>): Promise<void> {
try {
await send("get", urlJoin(urls.rmbProxy, "version"), "", {});
Expand All @@ -170,6 +210,13 @@ class GridClient {
}
}

/**
* Gets the default URLs for the specified network environment.
Mahmoud-Emad marked this conversation as resolved.
Show resolved Hide resolved
*
* @param {NetworkEnv} network - The network environment.
*
* @returns {Record<string, string>} The default URLs for the network environment.
*/
getDefaultUrls(network: NetworkEnv): Record<string, string> {
const base = network === NetworkEnv.main ? "grid.tf" : `${network}.grid.tf`;
const { proxyURL, relayURL, substrateURL, graphqlURL, activationURL } = this.clientOptions;
Expand All @@ -184,18 +231,33 @@ class GridClient {
return urls;
}

/**
* Disconnects from the Grid clients.
*
* This method disconnects the RMB and TF clients, terminating the connection to the Grid network.
*
* @returns {Promise<void>} A promise that resolves when the disconnection is complete.
*/
async disconnect(): Promise<void> {
if (this.rmbClient) await this.rmbClient.disconnect();
if (this.tfclient) await this.tfclient.disconnect();
}

async invoke(message, args) {
/**
* Invokes a method on a specified module with the provided arguments.
*
* This method allows dynamic invocation of module methods by specifying the module and method names.
*
* @param {string} message - The message specifying the module and method.
* @param {string} args - The args of the module.
*/
async invoke(message: string, args: any) {
const namespaces = message.split(".");
if (namespaces.length > 2) {
throw new ValidationError(`Message must include 2 parts only not ${namespaces.length}.`);
}

const method = namespaces.pop();
const method = namespaces.pop() as string;

const module_name = namespaces[0];
if (!this.modules.includes(module_name)) {
Expand Down
34 changes: 31 additions & 3 deletions packages/grid_client/src/clients/tf-grid/balances.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import { Balance, Balances, BalanceTransferOptions, Client, QueryBalancesGetOptions } from "@threefold/tfchain_client";
import {
Balance,
Balances,
BalanceTransferOptions,
Client,
ExtrinsicResult,
QueryBalancesGetOptions,
} from "@threefold/tfchain_client";
import { GridClientError } from "@threefold/types";
import { Decimal } from "decimal.js";

class TFBalances extends Balances {
/**
* Get the balance for a specific address.
*
* This method retrieves the balance for a specific address by dividing the retrieved balances by 10^7.
*
Mahmoud-Emad marked this conversation as resolved.
Show resolved Hide resolved
* @param {QueryBalancesGetOptions} options - The options for getting the balance, including the address.
* @returns {Promise<Balance>} A promise that resolves with the balance object containing free, reserved, and frozen amounts.
*/
async get(options: QueryBalancesGetOptions): Promise<Balance> {
const balances = await super.get(options);
for (const b of Object.keys(balances)) {
Expand All @@ -12,13 +27,26 @@ class TFBalances extends Balances {
return balances;
}

async transfer(options: BalanceTransferOptions) {
/**
* Transfer a specified amount of TFT (`ThreeFold Tokens`) to a given address.
*
Mahmoud-Emad marked this conversation as resolved.
Show resolved Hide resolved
* @param {BalanceTransferOptions} options - The transfer options, including the destination address and the amount to transfer.
* @returns {Promise<ExtrinsicResult<number>>} A promise that resolves once the transfer is successfully completed.
*/
Mahmoud-Emad marked this conversation as resolved.
Show resolved Hide resolved
async transfer(options: BalanceTransferOptions): Promise<ExtrinsicResult<number>> {
const decimalAmount = new Decimal(options.amount);
const decimalAmountInTFT = decimalAmount.mul(10 ** 7).toNumber();
return await super.transfer({ address: options.address, amount: decimalAmountInTFT });
}

async getMoreFunds() {
/**
* Fund the wallet with `100TFT` on `Dev | QA` nets for the current user.
*
* This method fetches additional funds for the current user from the TFChain.
*
* @returns {Promise<number>} A promise that resolves once the funds are successfully retrieved.
*/
async getMoreFunds(): Promise<number> {
if (!this.client.url.includes("qa") && !this.client.url.includes("dev")) {
throw new GridClientError("Unable to get more TFTs.");
}
Expand Down
Loading
Loading