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

Add currency abstraction #2707

Merged
merged 15 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
79 changes: 79 additions & 0 deletions packages/grid_client/scripts/tft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { CurrencyModel } from "../src";
import { getClient } from "./client_loader";
import { log } from "./utils";

async function convertTFTtoUSD(client, amount) {
const res = await client.currency.convertTFTtoUSD(amount);
log("================= Convert TFT =================");
log(res);
log("================= Convert TFT =================");
}

async function convertUSDtoTFT(client, amount) {
const res = await client.currency.convertUSDtoTFT(amount);
log("================= Convert USD =================");
log(res);
log("================= Convert USD =================");
}

async function dailyTFT(client, hourlyTFT) {
const res = await client.currency.dailyTFT(hourlyTFT);
log("================= Daily TFT =================");
log(res);
log("================= Daily TFT =================");
}

async function monthlyTFT(client, hourlyTFT) {
const res = await client.currency.monthlyTFT(hourlyTFT);
log("================= Monthly TFT =================");
log(res);
log("================= Monthly TFT =================");
}

async function yearlyTFT(client, hourlyTFT) {
const res = await client.currency.yearlyTFT(hourlyTFT);
log("================= Yearly TFT =================");
log(res);
log("================= Yearly TFT =================");
}

async function dailyUSD(client, hourlyUSD) {
const res = await client.currency.dailyUSD(hourlyUSD);
log("================= Daily USD =================");
log(res);
log("================= Daily USD =================");
}

async function monthlyUSD(client, hourlyUSD) {
const res = await client.currency.monthlyUSD(hourlyUSD);
log("================= Monthly USD =================");
log(res);
log("================= Monthly USD =================");
}

async function yearlyUSD(client, hourlyUSD) {
const res = await client.currency.yearlyUSD(hourlyUSD);
log("================= Yearly USD =================");
log(res);
log("================= Yearly USD =================");
}
async function main() {
const grid = await getClient();
grid.currency.rate = await grid.tfclient.tftPrice.get();
const amount: CurrencyModel = {
amount: 1,
};

await convertTFTtoUSD(grid, amount);
await convertUSDtoTFT(grid, amount);
await dailyTFT(grid, amount);
await monthlyTFT(grid, amount);
await yearlyTFT(grid, amount);
await dailyUSD(grid, amount);
await monthlyUSD(grid, amount);
await yearlyUSD(grid, amount);

await grid.disconnect();
}

main();
3 changes: 2 additions & 1 deletion packages/grid_client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { isExposed } from "./helpers/expose";
import { formatErrorMessage, generateString } from "./helpers/utils";
import * as modules from "./modules/index";
import { appPath } from "./storage/backend";
import { BackendStorage, BackendStorageType } from "./storage/backend";
import { BackendStorageType } from "./storage/backend";
import { KeypairType } from "./zos/deployment";

class GridClient {
Expand Down Expand Up @@ -41,6 +41,7 @@ class GridClient {
stellar: modules.stellar;
blockchain: modules.blockchain;
calculator: modules.calculator;
currency: modules.currency;
utility: modules.utility;
farmerbot: modules.farmerbot;
farms: modules.farms;
Expand Down
12 changes: 6 additions & 6 deletions packages/grid_client/src/helpers/validator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ValidationError } from "@threefold/types";
import { plainToInstance } from "class-transformer";
import { validate } from "class-validator";
import { validateSync } from "class-validator";

async function validateObject(obj) {
const errors = await validate(obj);
function validateObject(obj) {
const errors = validateSync(obj);
// errors is an array of validation errors
if (errors.length > 0) {
console.log("Validation failed. errors:", errors);
Expand All @@ -13,13 +13,13 @@ async function validateObject(obj) {
// used as decorator
function validateInput(target, propertyKey: string, descriptor: PropertyDescriptor) {
const method = descriptor.value;
descriptor.value = async function (...args) {
descriptor.value = function (...args) {
const types = Reflect.getMetadata("design:paramtypes", target, propertyKey);
for (let i = 0; i < args.length; i++) {
const input = plainToInstance(types[i], args[i], { excludeExtraneousValues: true });
await validateObject(input);
validateObject(input);
}
return await method.apply(this, args);
return method.apply(this, args);
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/grid_client/src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export * from "./farmerbot";
export * from "./farms";
export * from "./networks";
export * from "./bridge";
export * from "./tft";
export * from "./base";
5 changes: 5 additions & 0 deletions packages/grid_client/src/modules/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,10 @@ class GetActiveContractsModel {
@Expose() @IsInt() @IsNotEmpty() @Min(1) nodeId: number;
}

class CurrencyModel {
@Expose() @IsNumber() @IsNotEmpty() @Min(0) amount: number; // hourly amount
}

interface GPUCardInfo {
id: string;
contract: number;
Expand Down Expand Up @@ -977,4 +981,5 @@ export {
NodeCPUTest,
NodeIPValidation,
NodeIPerf,
CurrencyModel,
};
73 changes: 73 additions & 0 deletions packages/grid_client/src/modules/tft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import Decimal from "decimal.js";

import { expose, validateInput } from "../helpers";
import { CurrencyModel } from "./models";

class TFTUSDConversionService {
// TFT rate: 1 tft = x USD
constructor(public rate: number, private decimals = 2) {}
zaelgohary marked this conversation as resolved.
Show resolved Hide resolved

@expose
@validateInput
normalizeCurrency(options: CurrencyModel): string {
return new Decimal(options.amount).toFixed(this.decimals);
}

@expose
@validateInput
convertUSDtoTFT(options: CurrencyModel): string {
const amount = options.amount / this.rate;
return this.normalizeCurrency({ amount });
}

@expose
@validateInput
convertTFTtoUSD(options: CurrencyModel): string {
const amount = options.amount * this.rate;
return this.normalizeCurrency({ amount });
}

zaelgohary marked this conversation as resolved.
Show resolved Hide resolved
@expose
@validateInput
dailyTFT(options: CurrencyModel): string {
const hours = options.amount * 24;
return this.normalizeCurrency({ amount: hours });
}

@expose
@validateInput
monthlyTFT(options: CurrencyModel): string {
const months = +this.dailyTFT(options) * 30;
return this.normalizeCurrency({ amount: months });
}

@expose
@validateInput
yearlyTFT(options: CurrencyModel): string {
const years = +this.monthlyTFT(options) * 12;
return this.normalizeCurrency({ amount: years });
}

@expose
@validateInput
dailyUSD(options: CurrencyModel): string {
const hours = options.amount * 24;
return this.normalizeCurrency({ amount: hours });
}

@expose
@validateInput
monthlyUSD(options: CurrencyModel): string {
const months = +this.dailyUSD(options) * 30;
return this.normalizeCurrency({ amount: months });
}

@expose
@validateInput
yearlyUSD(options: CurrencyModel): string {
const years = +this.monthlyUSD(options) * 12;
return this.normalizeCurrency({ amount: years });
}
}

export { TFTUSDConversionService as currency };
Loading
Loading