Skip to content

Commit

Permalink
Merge branch 'development' into development_2.7_fix_multiple_keys
Browse files Browse the repository at this point in the history
  • Loading branch information
samaradel committed Feb 25, 2025
2 parents e5ebf78 + 0a86125 commit d533baa
Show file tree
Hide file tree
Showing 75 changed files with 876 additions and 403 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"version": "2.7.0-rc1",
"version": "2.7.0-rc2",
"npmClient": "yarn"
}
2 changes: 1 addition & 1 deletion packages/UI/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@threefold/ui",
"version": "2.7.0-rc1",
"version": "2.7.0-rc2",
"private": false,
"main": "dist/threefold-ui.umd.js",
"publishConfig": {
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql_client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@threefold/graphql_client",
"version": "2.7.0-rc1",
"version": "2.7.0-rc2",
"main": "./dist/node/index.js",
"module": "./dist/es6/index.js",
"exports": {
Expand All @@ -18,7 +18,7 @@
"es6-build": "tsc --build tsconfig.json"
},
"dependencies": {
"@threefold/types": "2.7.0-rc1",
"@threefold/types": "2.7.0-rc2",
"ts-mixer": "^6.0.2"
},
"devDependencies": {
Expand Down
12 changes: 6 additions & 6 deletions packages/grid_client/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@threefold/grid_client",
"author": "Ahmed Hanafy",
"version": "2.7.0-rc1",
"version": "2.7.0-rc2",
"license": "ISC",
"homepage": "https://github.com/threefoldtech/tfgrid-sdk-ts/tree/development/packages/grid_client/README.md",
"repository": {
Expand All @@ -14,11 +14,11 @@
"dependencies": {
"@jimber/pkid": "1.0.4",
"@noble/secp256k1": "^1.7.1",
"@threefold/gridproxy_client": "2.7.0-rc1",
"@threefold/monitoring": "2.7.0-rc1",
"@threefold/rmb_direct_client": "2.7.0-rc1",
"@threefold/tfchain_client": "2.7.0-rc1",
"@threefold/types": "2.7.0-rc1",
"@threefold/gridproxy_client": "2.7.0-rc2",
"@threefold/monitoring": "2.7.0-rc2",
"@threefold/rmb_direct_client": "2.7.0-rc2",
"@threefold/tfchain_client": "2.7.0-rc2",
"@threefold/types": "2.7.0-rc2",
"algosdk": "^1.19.0",
"appdata-path": "^1.0.0",
"await-lock": "^2.2.2",
Expand Down
139 changes: 139 additions & 0 deletions packages/grid_client/scripts/applications/nostr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { Features, FilterOptions, GatewayNameModel, GridClient, MachinesModel, NodeInfo } from "../../src";
import { config, getClient } from "../client_loader";
import { log, pingNodes } from "../utils";

async function deploy(client: GridClient, vms: MachinesModel, subdomain: string, gatewayNode: NodeInfo) {
// Deploy VM
const resultVM = await client.machines.deploy(vms);
log("================= Deploying VM =================");
log(resultVM);
log("================= Deploying VM =================");

// Get WG interface details
const wgnet = (await client.machines.getObj(vms.name))[0].interfaces[0];

// Deploy Gateway
const gateway: GatewayNameModel = {
name: subdomain,
network: wgnet.network,
node_id: gatewayNode.nodeId,
tls_passthrough: false,
backends: [`http://${wgnet.ip}:8080`],
};

const resultGateway = await client.gateway.deploy_name(gateway);
log("================= Deploying Gateway =================");
log(resultGateway);
log("================= Deploying Gateway =================");
}

async function getDeployment(client: GridClient, name: string, subdomain: string) {
// Get VM deployment
const resultVM = await client.machines.getObj(name);
log("================= Getting VM Deployment =================");
log(resultVM);
log("================= Getting VM Deployment =================");

// Get Gateway deployment
const resultGateway = await client.gateway.getObj(subdomain);
log("================= Getting Gateway Deployment =================");
log(resultGateway);
log(`https://${resultGateway[0].domain}`);
log("================= Getting Gateway Deployment =================");
}

async function cancel(client: GridClient, name: string, subdomain: string) {
// Cancel VM deployment
const resultVM = await client.machines.delete({ name });
log("================= Canceling VM Deployment =================");
log(resultVM);
log("================= Canceling VM Deployment =================");

// Cancel Gateway deployment
const resultGateway = await client.gateway.delete_name({ name: subdomain });
log("================= Canceling Gateway Deployment =================");
log(resultGateway);
log("================= Canceling Gateway Deployment =================");
}

async function main() {
const name = "newnostr1";
const grid3 = await getClient(`nostr/${name}`);
const subdomain = `ntt${grid3.twinId}${name}`;
const instanceCapacity = { cru: 2, mru: 4, sru: 50 };

// VM Query Options
const vmQueryOptions: FilterOptions = {
features: [Features.wireguard, Features.mycelium],
cru: instanceCapacity.cru,
mru: instanceCapacity.mru,
sru: instanceCapacity.sru,
availableFor: grid3.twinId,
farmId: 1,
};

// Gateway Query Options
const gatewayQueryOptions: FilterOptions = {
features: [Features.wireguard, Features.mycelium],
gateway: true,
availableFor: grid3.twinId,
};

const gatewayNodes = await grid3.capacity.filterNodes(gatewayQueryOptions);
const gatewayNodeId = await pingNodes(grid3, gatewayNodes);
const gatewayNode = gatewayNodes.find(node => node.nodeId == gatewayNodeId);
const nodes = await grid3.capacity.filterNodes(vmQueryOptions);
const vmNode = await pingNodes(grid3, nodes);
const domain = `${subdomain}.${gatewayNode!.publicConfig.domain}`;

const vms: MachinesModel = {
name,
network: {
name: "nostrnet",
ip_range: "10.252.0.0/16",
addAccess: true,
accessNodeId: gatewayNode!.nodeId,
},
machines: [
{
name: "nostr",
node_id: vmNode,
disks: [
{
name: "nsDisk",
size: instanceCapacity.sru,
mountpoint: "/mnt/data",
},
],
planetary: true,
public_ip: false,
public_ip6: false,
mycelium: true,
cpu: instanceCapacity.cru,
memory: 1024 * instanceCapacity.mru,
rootfs_size: 0,
flist: "https://hub.grid.tf/tf-official-apps/nostr_relay-mycelium.flist",
entrypoint: "/sbin/zinit init",
env: {
SSH_KEY: config.ssh_key,
NOSTR_HOSTNAME: domain,
},
},
],
metadata: "",
description: "Deploying Nostr instance via TS Grid3 client",
};

// Deploy VM and Gateway
await deploy(grid3, vms, subdomain, gatewayNode!);

// Get the deployment details
await getDeployment(grid3, name, subdomain);

// Uncomment the line below to cancel the deployment
// await cancel(grid3, name, subdomain);

await grid3.disconnect();
}

main();
32 changes: 22 additions & 10 deletions packages/grid_client/src/clients/tf-grid/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ export interface GetConsumptionOptions {
graphqlURL: string;
id: number;
}
export interface Consumption {
amountBilled: number;
discountReceived: DiscountLevel;
}

export interface GetDiscountPackageOptions {
graphqlURL: string;
Expand Down Expand Up @@ -268,7 +272,7 @@ class TFContracts extends Contracts {
const gqlClient = new Graphql(options.graphqlURL);

const body = `query getConsumption($contractId: BigInt!){
contractBillReports(where: {contractID_eq: $contractId} , orderBy: timestamp_DESC) {
contractBillReports(where: {contractID_eq: $contractId} , orderBy: timestamp_DESC, limit:1) {
discountReceived
}
Expand All @@ -282,7 +286,7 @@ class TFContracts extends Contracts {
if (billReports.length === 0) {
return "None";
} else {
const discountPackage = billReports[billReports.length - 1].discountReceived;
const discountPackage = billReports[0].discountReceived;
return discountPackage;
}
} catch (err) {
Expand All @@ -291,17 +295,19 @@ class TFContracts extends Contracts {
}
}
/**
* Get contract consumption per hour in TFT.
* Get the contract consumption details per hour in TFT.
*
* @param {GetConsumptionOptions} options
* @returns {Promise<number>}
* @returns {Promise<Consumption>} A promise resolving to the consumption details,
* including the amount billed and the discount received.
*/
async getConsumption(options: GetConsumptionOptions): Promise<number> {
async getConsumption(options: GetConsumptionOptions): Promise<Consumption> {
const gqlClient = new Graphql(options.graphqlURL);
const body = `query getConsumption($contractId: BigInt!){
contractBillReports(where: {contractID_eq: $contractId}, limit: 2 , orderBy: timestamp_DESC) {
amountBilled
timestamp
discountReceived
}
nodeContracts(where: {contractID_eq: $contractId}) {
createdAt
Expand All @@ -318,7 +324,10 @@ class TFContracts extends Contracts {
const gqlConsumption: GqlConsumption = response["data"] as GqlConsumption;
const billReports = gqlConsumption.contractBillReports;
if (billReports.length === 0) {
return 0;
return {
amountBilled: 0,
discountReceived: "None",
};
} else {
let duration = 1;
const amountBilled = new Decimal(billReports[0].amountBilled);
Expand All @@ -338,10 +347,13 @@ class TFContracts extends Contracts {
}
}
}
return amountBilled
.div(duration || 1)
.div(10 ** 7)
.toNumber();
return {
amountBilled: amountBilled
.div(duration || 1)
.div(10 ** 7)
.toNumber(),
discountReceived: billReports[0].discountReceived,
};
}
} catch (err) {
(err as Error).message = formatErrorMessage(`Error getting consumption for contract ${options.id}.`, err);
Expand Down
10 changes: 6 additions & 4 deletions packages/grid_client/src/modules/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { GridClientError } from "@threefold/types";
import * as PATH from "path";

import {
Consumption,
ContractsOverdue,
type DiscountLevel,
GqlContracts,
Expand Down Expand Up @@ -539,17 +540,18 @@ class Contracts {
return this.client.contracts.getDiscountPackage({ id: options.id, graphqlURL: this.config.graphqlURL });
}
/**
* Get contract consumption per hour in TFT.
* Get the contract consumption details per hour in TFT.
*
* @param {ContractConsumption} options
* @returns {Promise<number>}
* @param {ContractConsumption} options - The contract consumption parameters.
* @returns {Promise<Consumption>} A promise resolving to the consumption details,
* including the amount billed and the discount received.
* @decorators
* - `@expose`: Exposes the method for external use.
* - `@validateInput`: Validates the input options.
*/
@expose
@validateInput
async getConsumption(options: ContractConsumption): Promise<number> {
async getConsumption(options: ContractConsumption): Promise<Consumption> {
return this.client.contracts.getConsumption({ id: options.id, graphqlURL: this.config.graphqlURL });
}

Expand Down
3 changes: 2 additions & 1 deletion packages/grid_client/src/modules/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,16 +624,17 @@ class FilterOptions {
@Expose() @IsOptional() @IsInt({ each: true }) @Min(1, { each: true }) nodeExclude?: number[];
@Expose() @IsOptional() @IsInt({ each: true }) @Min(1, { each: true }) farmIds?: number[];
@Expose() @IsOptional() @IsInt() @Min(1) farmId?: number;
@Expose() @IsOptional() @IsInt() @Min(1) nodeId?: number;
@Expose() @IsOptional() @IsString() farmName?: string;
@Expose() @IsOptional() @IsString() country?: string;
@Expose() @IsOptional() @IsString() city?: string;
@Expose() @IsOptional() @IsBoolean() dedicated?: boolean;
@Expose() @IsOptional() @IsInt() @Min(1) availableFor?: number;
@Expose() @IsOptional() @IsInt() page?: number;
@Expose() @IsOptional() @IsInt() size?: number;
@Expose() @IsOptional() @IsInt() @Min(1) rentedBy?: number;
@Expose() @IsOptional() @IsBoolean() hasGPU?: boolean;
@Expose() @IsOptional() @IsBoolean() rentable?: boolean;
@Expose() @IsOptional() @IsInt() @Min(1) rentedBy?: number;
@Expose() @IsOptional() @IsBoolean() randomize?: boolean;
@Expose() @IsOptional() @IsBoolean() ret_count?: boolean;
@Expose() @IsOptional() @Transform(({ value }) => NodeStatus[value]) @IsEnum(NodeStatus) status?: NodeStatus;
Expand Down
4 changes: 3 additions & 1 deletion packages/grid_client/src/modules/tfchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,9 @@ class TFChain implements blockchainInterface {
});
}
await (
await client.termsAndConditions.accept({ documentLink: "https://library.threefold.me/info/legal/#/" })
await client.termsAndConditions.accept({
documentLink: "https://manual.grid.tf/knowledge_base/legal/terms_conditions_all3.html",
})
).apply();
const ret = await (await client.twins.create({ relay })).apply();
if (disconnect) await client.disconnect();
Expand Down
1 change: 1 addition & 0 deletions packages/grid_client/src/primitives/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ class Nodes {
healthy: options.healthy,
sort_by: SortBy.FreeCRU,
sort_order: SortOrder.Desc,
node_id: options.nodeId,
rentable_or_rented_by: options.rentableOrRentedBy,
features: options.features,
};
Expand Down
8 changes: 4 additions & 4 deletions packages/grid_client/src/zos/zmachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ class Zmachine extends WorkloadData {
out += key;
out += "=";
out += this.env[key];
if (this.gpu) {
for (const g of this.gpu) {
out += g;
}
}
if (this.gpu) {
for (const g of this.gpu) {
out += g;
}
}
return out;
Expand Down
8 changes: 4 additions & 4 deletions packages/grid_client/src/zos/zmachine_light.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ class ZmachineLight extends WorkloadData {
out += key;
out += "=";
out += this.env[key];
if (this.gpu) {
for (const g of this.gpu) {
out += g;
}
}
if (this.gpu) {
for (const g of this.gpu) {
out += g;
}
}
return out;
Expand Down
4 changes: 2 additions & 2 deletions packages/grid_http_server/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@threefold/grid_http_server",
"author": "Ahmed Hanafy",
"version": "2.7.0-rc1",
"version": "2.7.0-rc2",
"license": "ISC",
"homepage": "https://github.com/threefoldtech/tfgrid-sdk-ts/blob/development/packages/grid_http_server/README.md",
"repository": {
Expand All @@ -12,7 +12,7 @@
"access": "public"
},
"dependencies": {
"@threefold/grid_client": "2.7.0-rc1",
"@threefold/grid_client": "2.7.0-rc2",
"express": "^4.21.2",
"http-server": "^14.1.1",
"typescript": "^4.7.4"
Expand Down
Loading

0 comments on commit d533baa

Please sign in to comment.