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

Implemented QueryNodes class to excute chain methods inside the nodes module, added some interfaces to wrap the result of the get method. #482

Merged
merged 3 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 0 additions & 2 deletions packages/playground/src/utils/getMetricsUrl.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import type { GridClient } from "@threefold/grid_client";

import type { Machine } from "./deploy_vm";

export interface IGrafanaArgs {
orgID: number;
network: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/tfchain_client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Balances, QueryBalances } from "./balances";
import { Contracts, QueryContracts } from "./contracts";
import { QueryFarms } from "./farms";
import { KVStore } from "./kvstore";
import { Nodes } from "./nodes";
import { Nodes, QueryNodes } from "./nodes";
import { QueryPricingPolicies } from "./pricing_policies";
import { TermsAndConditions } from "./terms_and_conditions";
import { QueryTFTPrice } from "./tft_price";
Expand Down Expand Up @@ -49,6 +49,7 @@ class QueryClient {
tftPrice: QueryTFTPrice = new QueryTFTPrice(this);
pricingPolicies: QueryPricingPolicies = new QueryPricingPolicies(this);
twins: QueryTwins = new QueryTwins(this);
nodes: QueryNodes = new QueryNodes(this);
constructor(public url: string) {}

async loadKeyPairOrSigner(): Promise<void> {} // to be overridden in the full client
Expand Down
74 changes: 70 additions & 4 deletions packages/tfchain_client/src/nodes.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,82 @@
import { Client } from "./client";
import { Client, QueryClient } from "./client";
import { checkConnection } from "./utils";

export interface SetPowerOptions {
nodeId: number;
power: boolean;
}

class Nodes {
constructor(public client: Client) {
interface Node {
id: number;
farmId: number;
twinId: number;
resources: NodeResources;
location: NodeLocation;
publicConfig: PublicConfig;
created: number;
farmingPolicyId: number;
interfaces: NetworkInterfaceType[];
certification: string;
secureBoot: boolean;
virtualized: boolean;
serialNumber: string;
connectionPrice: number;
}

interface NetworkInterfaceType {
name: string;
mac: string;
ips: string[];
}

interface IPConfigInterface {
ip: string;
gw: string;
}
interface PublicConfig {
ip4: IPConfigInterface;
ip6: IPConfigInterface;
domain: string;
}

interface NodeResources {
hru: number;
sru: number;
cru: number;
mru: number;
}

interface NodeLocation {
city: string;
country: string;
latitude: number;
longitude: number;
}

export interface QueryNodesGetOptions {
id: number;
}

class QueryNodes {
constructor(public client: QueryClient) {
this.client = client;
}

@checkConnection
async get(options: QueryNodesGetOptions): Promise<Node> {
if (isNaN(options.id) || options.id <= 0) {
throw Error("Invalid node id. Node id must be postive integer");
}
const res = await this.client.api.query.tfgridModule.nodes(options.id);
return res.toPrimitive() as unknown as Node;
}
}

class Nodes extends QueryNodes {
constructor(public client: Client) {
super(client);
}

@checkConnection
async setPower(options: SetPowerOptions) {
let powerTarget: { up?: boolean; down?: boolean };
Expand All @@ -29,4 +95,4 @@ class Nodes {
}
}

export { Nodes };
export { Nodes, QueryNodes };