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 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
85 changes: 85 additions & 0 deletions packages/grid_client/scripts/tft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { CurrencyModel } from "../src";
import { currency as TFTUSDConversionService } from "../src";
import { getClient } from "./client_loader";
import { log } from "./utils";

let currency: TFTUSDConversionService;

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

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

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

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

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

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

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

function yearlyUSD(hourlyUSD) {
const res = currency.yearlyUSD(hourlyUSD);
log("================= Yearly USD =================");
log(res);
log("================= Yearly USD =================");
}
async function main() {
const grid = await getClient();
const rate = await grid.tfclient.tftPrice.get();
const decimals = 3;
currency = new TFTUSDConversionService(rate, decimals);

const amount: CurrencyModel = {
amount: 1,
};

convertTFTtoUSD(amount);
convertUSDtoTFT(amount);
dailyTFT(amount);
monthlyTFT(amount);
yearlyTFT(amount);
dailyUSD(amount);
monthlyUSD(amount);
yearlyUSD(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,
};
77 changes: 77 additions & 0 deletions packages/grid_client/src/modules/tft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import Decimal from "decimal.js";

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

class TFTUSDConversionService {
// TFT rate: 1 TFT = x USD
constructor(protected rate: number, private decimals = 2) {}

get _rate() {
return this.rate;
}

@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