From 206afbc52b050fee0363ef70a1675b2f05b914f4 Mon Sep 17 00:00:00 2001 From: zaelgohary Date: Wed, 15 May 2024 17:44:01 +0300 Subject: [PATCH 01/14] Add TFT class --- packages/grid_client/scripts/tft.ts | 10 +++++++ packages/grid_client/src/client.ts | 1 + packages/grid_client/src/modules/index.ts | 1 + packages/grid_client/src/modules/tft.ts | 35 +++++++++++++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 packages/grid_client/scripts/tft.ts create mode 100644 packages/grid_client/src/modules/tft.ts diff --git a/packages/grid_client/scripts/tft.ts b/packages/grid_client/scripts/tft.ts new file mode 100644 index 0000000000..4dd7c4142e --- /dev/null +++ b/packages/grid_client/scripts/tft.ts @@ -0,0 +1,10 @@ +import { TFT } from "../src"; + +const tft = new TFT("wss://tfchain.dev.grid.tf/ws"); + +async function main() { + const price = await tft.tftPrice(); + console.log(price); +} + +main(); diff --git a/packages/grid_client/src/client.ts b/packages/grid_client/src/client.ts index 1b06614da2..a78c98b361 100644 --- a/packages/grid_client/src/client.ts +++ b/packages/grid_client/src/client.ts @@ -41,6 +41,7 @@ class GridClient { stellar: modules.stellar; blockchain: modules.blockchain; calculator: modules.calculator; + tft: modules.TFT; utility: modules.utility; farmerbot: modules.farmerbot; farms: modules.farms; diff --git a/packages/grid_client/src/modules/index.ts b/packages/grid_client/src/modules/index.ts index 3f81550bb6..9f0b42e065 100644 --- a/packages/grid_client/src/modules/index.ts +++ b/packages/grid_client/src/modules/index.ts @@ -22,3 +22,4 @@ export * from "./farmerbot"; export * from "./farms"; export * from "./networks"; export * from "./bridge"; +export * from "./tft"; diff --git a/packages/grid_client/src/modules/tft.ts b/packages/grid_client/src/modules/tft.ts new file mode 100644 index 0000000000..693a9363d3 --- /dev/null +++ b/packages/grid_client/src/modules/tft.ts @@ -0,0 +1,35 @@ +import { QueryClient } from "@threefold/tfchain_client"; + +import { calculator as Calculator } from "./calculator"; + +class TFT { + public SUBSTRATE_URL: string; + private calculator: Calculator; + + constructor(SUBSTRATE_URL: string) { + this.SUBSTRATE_URL = SUBSTRATE_URL; + this.calculator = new Calculator(new QueryClient(this.SUBSTRATE_URL)); + } + + async tftPrice() { + return await this.calculator.tftPrice(); + } + + async fromUSD(usd: number) { + return (usd / (await this.tftPrice())).toFixed(2); + } + + async toUSD(tft: number) { + return (tft * (await this.tftPrice())).toFixed(2); + } + + toMonth(price: number) { + return (price * 24 * 30).toFixed(2); + } + + toYear(price: number) { + return (+this.toMonth(price) * 12).toFixed(2); + } +} + +export { TFT }; From 83d83292a9ac83de3fecd317de7671243aaa5e31 Mon Sep 17 00:00:00 2001 From: zaelgohary Date: Thu, 16 May 2024 15:01:01 +0300 Subject: [PATCH 02/14] Use decimal, update tft script --- packages/grid_client/scripts/tft.ts | 19 ++++++++++++++++++- packages/grid_client/src/modules/tft.ts | 11 ++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/packages/grid_client/scripts/tft.ts b/packages/grid_client/scripts/tft.ts index 4dd7c4142e..e0db2b31b0 100644 --- a/packages/grid_client/scripts/tft.ts +++ b/packages/grid_client/scripts/tft.ts @@ -1,10 +1,27 @@ import { TFT } from "../src"; +import { getClient } from "./client_loader"; const tft = new TFT("wss://tfchain.dev.grid.tf/ws"); async function main() { - const price = await tft.tftPrice(); + const grid = await getClient(); + + const price = await tft.price(); console.log(price); + + const tfts = await tft.fromUSD(10); + console.log(tfts); + + const usd = await tft.toUSD(10); + console.log(usd); + + const tftsInMonth = tft.toMonth(10); + console.log(tftsInMonth); + + const tftsInYear = tft.toYear(20); + console.log(tftsInYear); + + grid.disconnect(); } main(); diff --git a/packages/grid_client/src/modules/tft.ts b/packages/grid_client/src/modules/tft.ts index 693a9363d3..313e084e42 100644 --- a/packages/grid_client/src/modules/tft.ts +++ b/packages/grid_client/src/modules/tft.ts @@ -1,4 +1,5 @@ import { QueryClient } from "@threefold/tfchain_client"; +import Decimal from "decimal.js"; import { calculator as Calculator } from "./calculator"; @@ -11,24 +12,24 @@ class TFT { this.calculator = new Calculator(new QueryClient(this.SUBSTRATE_URL)); } - async tftPrice() { + async price() { return await this.calculator.tftPrice(); } async fromUSD(usd: number) { - return (usd / (await this.tftPrice())).toFixed(2); + return new Decimal(usd / (await this.price())).toFixed(2); } async toUSD(tft: number) { - return (tft * (await this.tftPrice())).toFixed(2); + return new Decimal(tft * (await this.price())).toFixed(2); } toMonth(price: number) { - return (price * 24 * 30).toFixed(2); + return new Decimal(price * 24 * 30).toFixed(2); } toYear(price: number) { - return (+this.toMonth(price) * 12).toFixed(2); + return new Decimal(+this.toMonth(price) * 12).toFixed(2); } } From 88120f0907ceddb0c8f550340d2e37afcff1ee7f Mon Sep 17 00:00:00 2001 From: zaelgohary Date: Sun, 19 May 2024 17:14:08 +0300 Subject: [PATCH 03/14] Add tft & usd models, update tft module & script, add tests --- packages/grid_client/scripts/tft.ts | 13 +-- packages/grid_client/src/modules/models.ts | 10 ++ packages/grid_client/src/modules/tft.ts | 37 +++++-- .../grid_client/tests/modules/tft.test.ts | 96 +++++++++++++++++++ 4 files changed, 140 insertions(+), 16 deletions(-) create mode 100644 packages/grid_client/tests/modules/tft.test.ts diff --git a/packages/grid_client/scripts/tft.ts b/packages/grid_client/scripts/tft.ts index e0db2b31b0..1d289d573a 100644 --- a/packages/grid_client/scripts/tft.ts +++ b/packages/grid_client/scripts/tft.ts @@ -1,7 +1,8 @@ -import { TFT } from "../src"; +import { substrateURL, TFT } from "../src"; import { getClient } from "./client_loader"; -const tft = new TFT("wss://tfchain.dev.grid.tf/ws"); +const substrate = "wss://tfchain.dev.grid.tf/ws" as substrateURL.DEV; +const tft = new TFT(substrate); async function main() { const grid = await getClient(); @@ -9,16 +10,16 @@ async function main() { const price = await tft.price(); console.log(price); - const tfts = await tft.fromUSD(10); + const tfts = await tft.fromUSD({ usd: 10 }); console.log(tfts); - const usd = await tft.toUSD(10); + const usd = await tft.toUSD({ tft: 10 }); console.log(usd); - const tftsInMonth = tft.toMonth(10); + const tftsInMonth = tft.toMonth({ tft: 10 }); console.log(tftsInMonth); - const tftsInYear = tft.toYear(20); + const tftsInYear = tft.toYear({ tft: 20 }); console.log(tftsInYear); grid.disconnect(); diff --git a/packages/grid_client/src/modules/models.ts b/packages/grid_client/src/modules/models.ts index b3ebd6cd75..1d43937d09 100644 --- a/packages/grid_client/src/modules/models.ts +++ b/packages/grid_client/src/modules/models.ts @@ -832,6 +832,14 @@ class GetActiveContractsModel { @Expose() @IsInt() @IsNotEmpty() @Min(1) nodeId: number; } +class USDModel { + @Expose() @IsNumber() @IsNotEmpty() @Min(0) usd: number; +} + +class TFTModel { + @Expose() @IsNumber() @IsNotEmpty() @Min(0) tft: number; +} + interface GPUCardInfo { id: string; contract: number; @@ -977,4 +985,6 @@ export { NodeCPUTest, NodeIPValidation, NodeIPerf, + USDModel, + TFTModel, }; diff --git a/packages/grid_client/src/modules/tft.ts b/packages/grid_client/src/modules/tft.ts index 313e084e42..c972e175be 100644 --- a/packages/grid_client/src/modules/tft.ts +++ b/packages/grid_client/src/modules/tft.ts @@ -1,7 +1,16 @@ import { QueryClient } from "@threefold/tfchain_client"; import Decimal from "decimal.js"; +import { expose, validateInput } from "../helpers"; import { calculator as Calculator } from "./calculator"; +import { TFTModel, USDModel } from "./models"; + +enum substrateURL { + DEV = "wss://tfchain.dev.grid.tf/ws", + QA = "wss://tfchain.qa.grid.tf/ws", + TEST = "wss://tfchain.test.grid.tf/ws", + MAIN = "wss://tfchain.grid.tf/ws", +} class TFT { public SUBSTRATE_URL: string; @@ -16,21 +25,29 @@ class TFT { return await this.calculator.tftPrice(); } - async fromUSD(usd: number) { - return new Decimal(usd / (await this.price())).toFixed(2); + @expose + @validateInput + async fromUSD(options: USDModel) { + return new Decimal(options.usd / (await this.price())).toFixed(2); } - async toUSD(tft: number) { - return new Decimal(tft * (await this.price())).toFixed(2); + @expose + @validateInput + async toUSD(options: TFTModel) { + return new Decimal(options.tft * (await this.price())).toFixed(2); } - - toMonth(price: number) { - return new Decimal(price * 24 * 30).toFixed(2); + @expose + @validateInput + toMonth(options: TFTModel) { + return new Decimal(options.tft * 24 * 30).toFixed(2); } - toYear(price: number) { - return new Decimal(+this.toMonth(price) * 12).toFixed(2); + @expose + @validateInput + toYear(options: TFTModel) { + const months = +this.toMonth(options); + return new Decimal(months * 12).toFixed(2); } } -export { TFT }; +export { TFT, substrateURL }; diff --git a/packages/grid_client/tests/modules/tft.test.ts b/packages/grid_client/tests/modules/tft.test.ts new file mode 100644 index 0000000000..98db8ea409 --- /dev/null +++ b/packages/grid_client/tests/modules/tft.test.ts @@ -0,0 +1,96 @@ +import Decimal from "decimal.js"; + +import { type GridClient, log } from "../../src"; +import { TFT } from "../../src/modules/tft"; +import { config, getClient } from "../client_loader"; + +const mock_price = jest.fn().mockResolvedValue(0.2); + +let grid: GridClient; +let tft: TFT; + +beforeAll(async () => { + grid = await getClient(); + const substrate_url = grid.getDefaultUrls(config.network).substrate; + + tft = new TFT(substrate_url); + log("mock_price" + mock_price); + + tft.price = mock_price; + await grid.disconnect(); +}); + +afterEach(() => { + jest.clearAllMocks(); +}); + +afterAll(async () => { + await grid.disconnect(); +}); + +describe("Testing TFT module", () => { + test("Create TFT instance in all networks.", () => { + // Test Data + const SUBSTRATE_DEV = "wss://tfchain.dev.grid.tf/ws"; + const SUBSTRATE_QA = "wss://tfchain.qa.grid.tf/ws"; + const SUBSTRATE_TEST = "wss://tfchain.test.grid.tf/ws"; + const SUBSTRATE_MAIN = "wss://tfchain.grid.tf/ws"; + + const tft_dev = new TFT(SUBSTRATE_DEV); + const tft_qa = new TFT(SUBSTRATE_QA); + const tft_test = new TFT(SUBSTRATE_TEST); + const tft_main = new TFT(SUBSTRATE_MAIN); + + expect(tft_dev).toBeInstanceOf(TFT); + expect(tft_qa).toBeInstanceOf(TFT); + expect(tft_test).toBeInstanceOf(TFT); + expect(tft_main).toBeInstanceOf(TFT); + }); + + test("should convert to the correct value based on the mocked price.", async () => { + const result = await tft.fromUSD({ usd: 5 }); + + expect(mock_price).toHaveBeenCalled(); + expect(result).toEqual(new Decimal(5 / 0.2).toFixed(2)); + }); + + test("fromUSD function to throw if passed a negative value.", async () => { + await expect(tft.fromUSD({ usd: -1 })).rejects.toThrow(); + }); + + test("toUSD function returns a valid value.", async () => { + const positive_usd = 1; + const positive = await tft.toUSD({ tft: positive_usd }); + + expect(positive).toBeTruthy(); + expect(positive).toEqual(new Decimal(1 * 0.2).toFixed(2)); + }); + + test("toUSD function to throw if passed a negative value.", async () => { + await expect(tft.toUSD({ tft: -1 })).rejects.toThrow(); + }); + + test("toMonth function returns a valid value.", () => { + const tfts = 1; + const tfts_in_month = tft.toMonth({ tft: tfts }); + + expect(tfts_in_month).toEqual(new Decimal(tfts * 24 * 30).toFixed(2)); + }); + + test("toMonth function throws if passed anything other than a positive value.", () => { + const result = tft.toMonth({ tft: -1 }); + + expect(() => result).toThrow(); + }); + + test("toYear function returns a valid value.", () => { + const tfts = 1; + const tfts_in_year = tft.toYear({ tft: tfts }); + + expect(tfts_in_year).toEqual((+tft.toMonth({ tft: tfts }) * 12).toFixed(2)); + }); + + test("toYear function throws if passed anything other than a positive value.", () => { + expect(() => tft.toYear({ tft: -1 })).toThrow(); + }); +}); From 95ef9a6d341eae59f15e0d8e73ab4b9108eb06a0 Mon Sep 17 00:00:00 2001 From: zaelgohary Date: Sun, 19 May 2024 20:33:06 +0300 Subject: [PATCH 04/14] Fix tests --- packages/grid_client/scripts/tft.ts | 4 +-- .../grid_client/tests/modules/tft.test.ts | 36 ++++++++++--------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/packages/grid_client/scripts/tft.ts b/packages/grid_client/scripts/tft.ts index 1d289d573a..8e59d55681 100644 --- a/packages/grid_client/scripts/tft.ts +++ b/packages/grid_client/scripts/tft.ts @@ -16,10 +16,10 @@ async function main() { const usd = await tft.toUSD({ tft: 10 }); console.log(usd); - const tftsInMonth = tft.toMonth({ tft: 10 }); + const tftsInMonth = await tft.toMonth({ tft: 1 }); console.log(tftsInMonth); - const tftsInYear = tft.toYear({ tft: 20 }); + const tftsInYear = await tft.toYear({ tft: 20 }); console.log(tftsInYear); grid.disconnect(); diff --git a/packages/grid_client/tests/modules/tft.test.ts b/packages/grid_client/tests/modules/tft.test.ts index 98db8ea409..11ab81947b 100644 --- a/packages/grid_client/tests/modules/tft.test.ts +++ b/packages/grid_client/tests/modules/tft.test.ts @@ -1,9 +1,11 @@ import Decimal from "decimal.js"; -import { type GridClient, log } from "../../src"; +import { type GridClient } from "../../src"; import { TFT } from "../../src/modules/tft"; import { config, getClient } from "../client_loader"; +jest.setTimeout(300000); + const mock_price = jest.fn().mockResolvedValue(0.2); let grid: GridClient; @@ -14,10 +16,8 @@ beforeAll(async () => { const substrate_url = grid.getDefaultUrls(config.network).substrate; tft = new TFT(substrate_url); - log("mock_price" + mock_price); tft.price = mock_price; - await grid.disconnect(); }); afterEach(() => { @@ -51,46 +51,48 @@ describe("Testing TFT module", () => { const result = await tft.fromUSD({ usd: 5 }); expect(mock_price).toHaveBeenCalled(); - expect(result).toEqual(new Decimal(5 / 0.2).toFixed(2)); + expect(typeof result).toBe("string"); + expect(result).toBe(new Decimal(5 / 0.2).toFixed(2)); }); test("fromUSD function to throw if passed a negative value.", async () => { - await expect(tft.fromUSD({ usd: -1 })).rejects.toThrow(); + expect(tft.fromUSD({ usd: -1 })).rejects.toThrow(); }); test("toUSD function returns a valid value.", async () => { - const positive_usd = 1; - const positive = await tft.toUSD({ tft: positive_usd }); + const usd = 1; + const result = await tft.toUSD({ tft: usd }); - expect(positive).toBeTruthy(); - expect(positive).toEqual(new Decimal(1 * 0.2).toFixed(2)); + expect(typeof result).toBe("string"); + expect(result).toEqual(new Decimal(1 * 0.2).toFixed(2)); }); test("toUSD function to throw if passed a negative value.", async () => { - await expect(tft.toUSD({ tft: -1 })).rejects.toThrow(); + expect(tft.toUSD({ tft: -1 })).rejects.toThrow(); }); test("toMonth function returns a valid value.", () => { const tfts = 1; - const tfts_in_month = tft.toMonth({ tft: tfts }); + const result = tft.toMonth({ tft: tfts }); + const expected_result = new Decimal(tfts * 24 * 30).toFixed(2); - expect(tfts_in_month).toEqual(new Decimal(tfts * 24 * 30).toFixed(2)); + expect(result).resolves.toBe(expected_result); }); test("toMonth function throws if passed anything other than a positive value.", () => { const result = tft.toMonth({ tft: -1 }); - expect(() => result).toThrow(); + expect(result).rejects.toThrow(); }); test("toYear function returns a valid value.", () => { const tfts = 1; - const tfts_in_year = tft.toYear({ tft: tfts }); - - expect(tfts_in_year).toEqual((+tft.toMonth({ tft: tfts }) * 12).toFixed(2)); + const result = tft.toYear({ tft: tfts }); + const expected_result = new Decimal(+tft.toMonth({ tft: tfts }) * 12).toFixed(2); + expect(result).resolves.toBe(expected_result); }); test("toYear function throws if passed anything other than a positive value.", () => { - expect(() => tft.toYear({ tft: -1 })).toThrow(); + expect(tft.toYear({ tft: -1 })).rejects.toThrow(); }); }); From 51a6cd4cc73bc13ab3e81ad42076949c42f2212c Mon Sep 17 00:00:00 2001 From: zaelgohary Date: Mon, 20 May 2024 14:04:08 +0300 Subject: [PATCH 05/14] Edit validateINput decorator to return sync only --- packages/grid_client/src/helpers/validator.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/grid_client/src/helpers/validator.ts b/packages/grid_client/src/helpers/validator.ts index 16d84b693d..15bb9c4a75 100644 --- a/packages/grid_client/src/helpers/validator.ts +++ b/packages/grid_client/src/helpers/validator.ts @@ -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); @@ -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); }; } From 5575f78780eea1166fde965c83cde3ec77e20fdb Mon Sep 17 00:00:00 2001 From: zaelgohary Date: Mon, 20 May 2024 14:05:57 +0300 Subject: [PATCH 06/14] Edit tft abstraction & script, replace usd & tft models w currency & hourly models --- packages/grid_client/scripts/tft.ts | 55 +++++++++++++++------- packages/grid_client/src/client.ts | 2 +- packages/grid_client/src/modules/models.ts | 12 ++--- packages/grid_client/src/modules/tft.ts | 46 ++++++++---------- 4 files changed, 65 insertions(+), 50 deletions(-) diff --git a/packages/grid_client/scripts/tft.ts b/packages/grid_client/scripts/tft.ts index 8e59d55681..94491bdf35 100644 --- a/packages/grid_client/scripts/tft.ts +++ b/packages/grid_client/scripts/tft.ts @@ -1,28 +1,51 @@ -import { substrateURL, TFT } from "../src"; +import { CurrencyModel, HourlyTFTModel } from "../src"; import { getClient } from "./client_loader"; +import { log } from "./utils"; -const substrate = "wss://tfchain.dev.grid.tf/ws" as substrateURL.DEV; -const tft = new TFT(substrate); +async function convertTFTtoUSD(client, amount) { + const res = await client.tft.convertTFTtoUSD(amount); + log("================= Convert TFT ================="); + log(res); + log("================= Convert TFT ================="); +} -async function main() { - const grid = await getClient(); +async function convertUSDtoTFT(client, amount) { + const res = await client.tft.convertUSDtoTFT(amount); + log("================= Convert USD ================="); + log(res); + log("================= Convert USD ================="); +} - const price = await tft.price(); - console.log(price); +async function monthlyTFT(client, hourlyTFT) { + const res = await client.tft.monthlyTFT(hourlyTFT); + log("================= Monthly TFT ================="); + log(res); + log("================= Monthly TFT ================="); +} - const tfts = await tft.fromUSD({ usd: 10 }); - console.log(tfts); +async function yearlyTFT(client, hourlyTFT) { + const res = await client.tft.yearlyTFT(hourlyTFT); + log("================= Yearly TFT ================="); + log(res); + log("================= Yearly TFT ================="); +} +async function main() { + const grid = await getClient(); - const usd = await tft.toUSD({ tft: 10 }); - console.log(usd); + const amount: CurrencyModel = { + amount: 5, + }; - const tftsInMonth = await tft.toMonth({ tft: 1 }); - console.log(tftsInMonth); + const hourlyTFT: HourlyTFTModel = { + amount: 5, + }; - const tftsInYear = await tft.toYear({ tft: 20 }); - console.log(tftsInYear); + await convertTFTtoUSD(grid, amount); + await convertUSDtoTFT(grid, amount); + await monthlyTFT(grid, hourlyTFT); + await yearlyTFT(grid, hourlyTFT); - grid.disconnect(); + await grid.disconnect(); } main(); diff --git a/packages/grid_client/src/client.ts b/packages/grid_client/src/client.ts index a78c98b361..4f46acfde9 100644 --- a/packages/grid_client/src/client.ts +++ b/packages/grid_client/src/client.ts @@ -41,7 +41,7 @@ class GridClient { stellar: modules.stellar; blockchain: modules.blockchain; calculator: modules.calculator; - tft: modules.TFT; + tft: modules.tft; utility: modules.utility; farmerbot: modules.farmerbot; farms: modules.farms; diff --git a/packages/grid_client/src/modules/models.ts b/packages/grid_client/src/modules/models.ts index 1d43937d09..52f12f2868 100644 --- a/packages/grid_client/src/modules/models.ts +++ b/packages/grid_client/src/modules/models.ts @@ -832,12 +832,12 @@ class GetActiveContractsModel { @Expose() @IsInt() @IsNotEmpty() @Min(1) nodeId: number; } -class USDModel { - @Expose() @IsNumber() @IsNotEmpty() @Min(0) usd: number; +class CurrencyModel { + @Expose() @IsNumber() @IsNotEmpty() @Min(0) amount: number; } -class TFTModel { - @Expose() @IsNumber() @IsNotEmpty() @Min(0) tft: number; +class HourlyTFTModel { + @Expose() @IsNumber() @IsNotEmpty() @Min(0) amount: number; } interface GPUCardInfo { @@ -985,6 +985,6 @@ export { NodeCPUTest, NodeIPValidation, NodeIPerf, - USDModel, - TFTModel, + CurrencyModel, + HourlyTFTModel, }; diff --git a/packages/grid_client/src/modules/tft.ts b/packages/grid_client/src/modules/tft.ts index c972e175be..707eca330b 100644 --- a/packages/grid_client/src/modules/tft.ts +++ b/packages/grid_client/src/modules/tft.ts @@ -1,53 +1,45 @@ -import { QueryClient } from "@threefold/tfchain_client"; import Decimal from "decimal.js"; +import { TFClient } from ".."; +import { GridClientConfig } from "../config"; import { expose, validateInput } from "../helpers"; -import { calculator as Calculator } from "./calculator"; -import { TFTModel, USDModel } from "./models"; - -enum substrateURL { - DEV = "wss://tfchain.dev.grid.tf/ws", - QA = "wss://tfchain.qa.grid.tf/ws", - TEST = "wss://tfchain.test.grid.tf/ws", - MAIN = "wss://tfchain.grid.tf/ws", -} +import { CurrencyModel, HourlyTFTModel } from "./models"; -class TFT { - public SUBSTRATE_URL: string; - private calculator: Calculator; +class TFTUSDConversionService { + private client: TFClient; - constructor(SUBSTRATE_URL: string) { - this.SUBSTRATE_URL = SUBSTRATE_URL; - this.calculator = new Calculator(new QueryClient(this.SUBSTRATE_URL)); + constructor(public config: GridClientConfig) { + this.client = config.tfclient; } async price() { - return await this.calculator.tftPrice(); + return await this.client.tftPrice.get(); } @expose @validateInput - async fromUSD(options: USDModel) { - return new Decimal(options.usd / (await this.price())).toFixed(2); + async convertUSDtoTFT(options: CurrencyModel): Promise { + return new Decimal(options.amount / (await this.price())).toFixed(2); } @expose @validateInput - async toUSD(options: TFTModel) { - return new Decimal(options.tft * (await this.price())).toFixed(2); + async convertTFTtoUSD(options: CurrencyModel): Promise { + return new Decimal(options.amount * (await this.price())).toFixed(2); } @expose @validateInput - toMonth(options: TFTModel) { - return new Decimal(options.tft * 24 * 30).toFixed(2); + monthlyTFT(options: HourlyTFTModel): string { + return new Decimal(options.amount * 24 * 30).toFixed(2); } @expose @validateInput - toYear(options: TFTModel) { - const months = +this.toMonth(options); - return new Decimal(months * 12).toFixed(2); + yearlyTFT(options: HourlyTFTModel): string { + const months = this.monthlyTFT(options); + + return new Decimal(+months * 12).toFixed(2); } } -export { TFT, substrateURL }; +export { TFTUSDConversionService as tft }; From 9412364b0e159f6be6c13d634f093eb8ea7a3017 Mon Sep 17 00:00:00 2001 From: zaelgohary Date: Mon, 20 May 2024 14:26:20 +0300 Subject: [PATCH 07/14] Add decimals field --- packages/grid_client/src/modules/tft.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/grid_client/src/modules/tft.ts b/packages/grid_client/src/modules/tft.ts index 707eca330b..9c39a276a9 100644 --- a/packages/grid_client/src/modules/tft.ts +++ b/packages/grid_client/src/modules/tft.ts @@ -7,6 +7,7 @@ import { CurrencyModel, HourlyTFTModel } from "./models"; class TFTUSDConversionService { private client: TFClient; + private decimals = 2; constructor(public config: GridClientConfig) { this.client = config.tfclient; @@ -19,18 +20,18 @@ class TFTUSDConversionService { @expose @validateInput async convertUSDtoTFT(options: CurrencyModel): Promise { - return new Decimal(options.amount / (await this.price())).toFixed(2); + return new Decimal(options.amount / (await this.price())).toFixed(this.decimals); } @expose @validateInput async convertTFTtoUSD(options: CurrencyModel): Promise { - return new Decimal(options.amount * (await this.price())).toFixed(2); + return new Decimal(options.amount * (await this.price())).toFixed(this.decimals); } @expose @validateInput monthlyTFT(options: HourlyTFTModel): string { - return new Decimal(options.amount * 24 * 30).toFixed(2); + return new Decimal(options.amount * 24 * 30).toFixed(this.decimals); } @expose @@ -38,7 +39,7 @@ class TFTUSDConversionService { yearlyTFT(options: HourlyTFTModel): string { const months = this.monthlyTFT(options); - return new Decimal(+months * 12).toFixed(2); + return new Decimal(+months * 12).toFixed(this.decimals); } } From 2106a01f0331b60c144b39c5fe3e6be202e163fc Mon Sep 17 00:00:00 2001 From: zaelgohary Date: Mon, 20 May 2024 15:09:07 +0300 Subject: [PATCH 08/14] Fix tests --- .../grid_client/tests/modules/tft.test.ts | 77 +++++++------------ 1 file changed, 27 insertions(+), 50 deletions(-) diff --git a/packages/grid_client/tests/modules/tft.test.ts b/packages/grid_client/tests/modules/tft.test.ts index 11ab81947b..25331d9f9a 100644 --- a/packages/grid_client/tests/modules/tft.test.ts +++ b/packages/grid_client/tests/modules/tft.test.ts @@ -1,98 +1,75 @@ import Decimal from "decimal.js"; -import { type GridClient } from "../../src"; -import { TFT } from "../../src/modules/tft"; -import { config, getClient } from "../client_loader"; +import { type GridClient, tft as TFT } from "../../src"; +import { getClient } from "../client_loader"; jest.setTimeout(300000); const mock_price = jest.fn().mockResolvedValue(0.2); let grid: GridClient; -let tft: TFT; beforeAll(async () => { grid = await getClient(); - const substrate_url = grid.getDefaultUrls(config.network).substrate; - tft = new TFT(substrate_url); - - tft.price = mock_price; -}); - -afterEach(() => { - jest.clearAllMocks(); + grid.tft.price = mock_price; }); afterAll(async () => { + jest.clearAllMocks(); await grid.disconnect(); }); describe("Testing TFT module", () => { - test("Create TFT instance in all networks.", () => { - // Test Data - const SUBSTRATE_DEV = "wss://tfchain.dev.grid.tf/ws"; - const SUBSTRATE_QA = "wss://tfchain.qa.grid.tf/ws"; - const SUBSTRATE_TEST = "wss://tfchain.test.grid.tf/ws"; - const SUBSTRATE_MAIN = "wss://tfchain.grid.tf/ws"; - - const tft_dev = new TFT(SUBSTRATE_DEV); - const tft_qa = new TFT(SUBSTRATE_QA); - const tft_test = new TFT(SUBSTRATE_TEST); - const tft_main = new TFT(SUBSTRATE_MAIN); - - expect(tft_dev).toBeInstanceOf(TFT); - expect(tft_qa).toBeInstanceOf(TFT); - expect(tft_test).toBeInstanceOf(TFT); - expect(tft_main).toBeInstanceOf(TFT); + test("tft module to be instance of TFTUSDConversionService", async () => { + expect(await grid.tft).toBeInstanceOf(TFT); }); test("should convert to the correct value based on the mocked price.", async () => { - const result = await tft.fromUSD({ usd: 5 }); + const result = await grid.tft.convertTFTtoUSD({ amount: 5 }); expect(mock_price).toHaveBeenCalled(); expect(typeof result).toBe("string"); - expect(result).toBe(new Decimal(5 / 0.2).toFixed(2)); + expect(result).toBe(new Decimal(5 * 0.2).toFixed(2)); }); - test("fromUSD function to throw if passed a negative value.", async () => { - expect(tft.fromUSD({ usd: -1 })).rejects.toThrow(); + test("convertTFTtoUSD function to throw if passed a negative value.", async () => { + await expect(async () => await grid.tft.convertTFTtoUSD({ amount: -1 })).rejects.toThrow(); }); - test("toUSD function returns a valid value.", async () => { + test("convertUSDtoTFT function returns a valid value.", async () => { const usd = 1; - const result = await tft.toUSD({ tft: usd }); + const result = await grid.tft.convertUSDtoTFT({ amount: usd }); expect(typeof result).toBe("string"); - expect(result).toEqual(new Decimal(1 * 0.2).toFixed(2)); + expect(result).toEqual(new Decimal(1 / 0.2).toFixed(2)); }); - test("toUSD function to throw if passed a negative value.", async () => { - expect(tft.toUSD({ tft: -1 })).rejects.toThrow(); + test("convertUSDtoTFT function to throw if passed a negative value.", async () => { + await expect(async () => await grid.tft.convertUSDtoTFT({ amount: -1 })).rejects.toThrow(); }); - test("toMonth function returns a valid value.", () => { + test("monthlyTFT function returns a valid value.", async () => { const tfts = 1; - const result = tft.toMonth({ tft: tfts }); + const result = await grid.tft.monthlyTFT({ amount: tfts }); const expected_result = new Decimal(tfts * 24 * 30).toFixed(2); - expect(result).resolves.toBe(expected_result); + expect(result).toBe(expected_result); }); - test("toMonth function throws if passed anything other than a positive value.", () => { - const result = tft.toMonth({ tft: -1 }); - - expect(result).rejects.toThrow(); + test("monthlyTFT function throws if passed anything other than a positive value.", async () => { + await expect(async () => await grid.tft.monthlyTFT({ amount: -1 })).rejects.toThrow(); }); - test("toYear function returns a valid value.", () => { + test("yearlyTFT function returns a valid value.", async () => { const tfts = 1; - const result = tft.toYear({ tft: tfts }); - const expected_result = new Decimal(+tft.toMonth({ tft: tfts }) * 12).toFixed(2); - expect(result).resolves.toBe(expected_result); + const result = await grid.tft.yearlyTFT({ amount: tfts }); + const expected_result = new Decimal(+(await grid.tft.monthlyTFT({ amount: tfts })) * 12).toFixed(2); + + expect(result).toBe(expected_result); }); - test("toYear function throws if passed anything other than a positive value.", () => { - expect(tft.toYear({ tft: -1 })).rejects.toThrow(); + test("yearlyTFT function throws if passed anything other than a positive value.", async () => { + await expect(async () => grid.tft.yearlyTFT({ amount: -1 })).rejects.toThrow(); }); }); From d57fa282b5b00a5b6d4ee32e657dc9fba9d720d4 Mon Sep 17 00:00:00 2001 From: zaelgohary Date: Mon, 20 May 2024 16:32:56 +0300 Subject: [PATCH 09/14] Refactor tests, Add more tests --- .../grid_client/tests/modules/tft.test.ts | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/grid_client/tests/modules/tft.test.ts b/packages/grid_client/tests/modules/tft.test.ts index 25331d9f9a..12847edb64 100644 --- a/packages/grid_client/tests/modules/tft.test.ts +++ b/packages/grid_client/tests/modules/tft.test.ts @@ -1,3 +1,4 @@ +import { ValidationError } from "@threefold/types"; import Decimal from "decimal.js"; import { type GridClient, tft as TFT } from "../../src"; @@ -34,7 +35,10 @@ describe("Testing TFT module", () => { }); test("convertTFTtoUSD function to throw if passed a negative value.", async () => { - await expect(async () => await grid.tft.convertTFTtoUSD({ amount: -1 })).rejects.toThrow(); + const result = async () => await grid.tft.convertTFTtoUSD({ amount: -1 }); + + await expect(result).rejects.toThrow(); + await expect(result).rejects.toBeInstanceOf(ValidationError); }); test("convertUSDtoTFT function returns a valid value.", async () => { @@ -46,7 +50,10 @@ describe("Testing TFT module", () => { }); test("convertUSDtoTFT function to throw if passed a negative value.", async () => { - await expect(async () => await grid.tft.convertUSDtoTFT({ amount: -1 })).rejects.toThrow(); + const result = async () => await grid.tft.convertUSDtoTFT({ amount: -1 }); + + await expect(result).rejects.toThrow(); + await expect(result).rejects.toBeInstanceOf(ValidationError); }); test("monthlyTFT function returns a valid value.", async () => { @@ -58,7 +65,10 @@ describe("Testing TFT module", () => { }); test("monthlyTFT function throws if passed anything other than a positive value.", async () => { - await expect(async () => await grid.tft.monthlyTFT({ amount: -1 })).rejects.toThrow(); + const result = async () => await grid.tft.monthlyTFT({ amount: -1 }); + + await expect(result).rejects.toThrow(); + await expect(result).rejects.toBeInstanceOf(ValidationError); }); test("yearlyTFT function returns a valid value.", async () => { @@ -70,6 +80,9 @@ describe("Testing TFT module", () => { }); test("yearlyTFT function throws if passed anything other than a positive value.", async () => { - await expect(async () => grid.tft.yearlyTFT({ amount: -1 })).rejects.toThrow(); + const result = async () => grid.tft.yearlyTFT({ amount: -1 }); + + await expect(result).rejects.toThrow(); + await expect(result).rejects.toBeInstanceOf(ValidationError); }); }); From d2d78db3e8c6222ff2233ffb50872d11b54a9204 Mon Sep 17 00:00:00 2001 From: zaelgohary Date: Thu, 23 May 2024 17:34:02 +0300 Subject: [PATCH 10/14] Rename tft module to currency, add more methods to currency abstraction, update script and tests, remove unused model --- packages/grid_client/scripts/tft.ts | 52 ++++++-- packages/grid_client/src/client.ts | 4 +- packages/grid_client/src/modules/models.ts | 7 +- packages/grid_client/src/modules/tft.ts | 71 ++++++++--- .../grid_client/tests/modules/tft.test.ts | 111 ++++++++++++++---- 5 files changed, 187 insertions(+), 58 deletions(-) diff --git a/packages/grid_client/scripts/tft.ts b/packages/grid_client/scripts/tft.ts index 94491bdf35..cc704edf71 100644 --- a/packages/grid_client/scripts/tft.ts +++ b/packages/grid_client/scripts/tft.ts @@ -1,49 +1,77 @@ -import { CurrencyModel, HourlyTFTModel } from "../src"; +import { CurrencyModel } from "../src"; import { getClient } from "./client_loader"; import { log } from "./utils"; async function convertTFTtoUSD(client, amount) { - const res = await client.tft.convertTFTtoUSD(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.tft.convertUSDtoTFT(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.tft.monthlyTFT(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.tft.yearlyTFT(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(); const amount: CurrencyModel = { - amount: 5, - }; - - const hourlyTFT: HourlyTFTModel = { - amount: 5, + amount: 1, }; await convertTFTtoUSD(grid, amount); await convertUSDtoTFT(grid, amount); - await monthlyTFT(grid, hourlyTFT); - await yearlyTFT(grid, hourlyTFT); + 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(); } diff --git a/packages/grid_client/src/client.ts b/packages/grid_client/src/client.ts index 4f46acfde9..0496d321cc 100644 --- a/packages/grid_client/src/client.ts +++ b/packages/grid_client/src/client.ts @@ -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 { @@ -41,7 +41,7 @@ class GridClient { stellar: modules.stellar; blockchain: modules.blockchain; calculator: modules.calculator; - tft: modules.tft; + currency: modules.currency; utility: modules.utility; farmerbot: modules.farmerbot; farms: modules.farms; diff --git a/packages/grid_client/src/modules/models.ts b/packages/grid_client/src/modules/models.ts index 52f12f2868..1faf5821dd 100644 --- a/packages/grid_client/src/modules/models.ts +++ b/packages/grid_client/src/modules/models.ts @@ -833,11 +833,7 @@ class GetActiveContractsModel { } class CurrencyModel { - @Expose() @IsNumber() @IsNotEmpty() @Min(0) amount: number; -} - -class HourlyTFTModel { - @Expose() @IsNumber() @IsNotEmpty() @Min(0) amount: number; + @Expose() @IsNumber() @IsNotEmpty() @Min(0) amount: number; // hourly amount } interface GPUCardInfo { @@ -986,5 +982,4 @@ export { NodeIPValidation, NodeIPerf, CurrencyModel, - HourlyTFTModel, }; diff --git a/packages/grid_client/src/modules/tft.ts b/packages/grid_client/src/modules/tft.ts index 9c39a276a9..6f990f0ddb 100644 --- a/packages/grid_client/src/modules/tft.ts +++ b/packages/grid_client/src/modules/tft.ts @@ -1,46 +1,83 @@ import Decimal from "decimal.js"; -import { TFClient } from ".."; -import { GridClientConfig } from "../config"; +import { GridClientConfig, TFClient } from ".."; import { expose, validateInput } from "../helpers"; -import { CurrencyModel, HourlyTFTModel } from "./models"; +import { CurrencyModel } from "./models"; class TFTUSDConversionService { - private client: TFClient; private decimals = 2; + private tfclient: TFClient; + TFTPrice: number; constructor(public config: GridClientConfig) { - this.client = config.tfclient; + this.tfclient = config.tfclient; + this.tfclient.tftPrice.get().then(res => { + this.TFTPrice = res; + }); } - async price() { - return await this.client.tftPrice.get(); + @expose + @validateInput + normalizeCurrency(options: CurrencyModel) { + return new Decimal(options.amount).toFixed(this.decimals); + } + + @expose + @validateInput + convertUSDtoTFT(options: CurrencyModel) { + const amount = options.amount / this.TFTPrice; + return this.normalizeCurrency({ amount }); + } + + @expose + @validateInput + convertTFTtoUSD(options: CurrencyModel) { + const amount = options.amount * this.TFTPrice; + return this.normalizeCurrency({ amount }); } @expose @validateInput - async convertUSDtoTFT(options: CurrencyModel): Promise { - return new Decimal(options.amount / (await this.price())).toFixed(this.decimals); + dailyTFT(options: CurrencyModel): string { + const hours = options.amount * 24; + return this.normalizeCurrency({ amount: hours }); } @expose @validateInput - async convertTFTtoUSD(options: CurrencyModel): Promise { - return new Decimal(options.amount * (await this.price())).toFixed(this.decimals); + 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 dailyTFTs = +this.dailyTFT(options); + return this.convertTFTtoUSD({ amount: dailyTFTs }); + } + @expose @validateInput - monthlyTFT(options: HourlyTFTModel): string { - return new Decimal(options.amount * 24 * 30).toFixed(this.decimals); + monthlyUSD(options: CurrencyModel): string { + const monthlyTFTs = +this.dailyTFT(options) * 30; + return this.convertTFTtoUSD({ amount: monthlyTFTs }); } @expose @validateInput - yearlyTFT(options: HourlyTFTModel): string { - const months = this.monthlyTFT(options); + yearlyUSD(options: CurrencyModel): string { + const yearlyTFT = +this.monthlyTFT(options) * 12; - return new Decimal(+months * 12).toFixed(this.decimals); + return this.convertTFTtoUSD({ amount: yearlyTFT }); } } -export { TFTUSDConversionService as tft }; +export { TFTUSDConversionService as currency }; diff --git a/packages/grid_client/tests/modules/tft.test.ts b/packages/grid_client/tests/modules/tft.test.ts index 12847edb64..09c70cd625 100644 --- a/packages/grid_client/tests/modules/tft.test.ts +++ b/packages/grid_client/tests/modules/tft.test.ts @@ -1,41 +1,44 @@ import { ValidationError } from "@threefold/types"; import Decimal from "decimal.js"; -import { type GridClient, tft as TFT } from "../../src"; +import { currency, type GridClient } from "../../src"; import { getClient } from "../client_loader"; jest.setTimeout(300000); - -const mock_price = jest.fn().mockResolvedValue(0.2); - let grid: GridClient; +let tftPrice: number; beforeAll(async () => { grid = await getClient(); - - grid.tft.price = mock_price; + tftPrice = await grid.tfclient.tftPrice.get(); }); afterAll(async () => { - jest.clearAllMocks(); await grid.disconnect(); }); describe("Testing TFT module", () => { test("tft module to be instance of TFTUSDConversionService", async () => { - expect(await grid.tft).toBeInstanceOf(TFT); + expect(await grid.currency).toBeInstanceOf(currency); + }); + + test("should return value with 2 decimals.", async () => { + const result = await grid.currency.normalizeCurrency({ amount: 1 }); + + expect(typeof result).toBe("string"); + expect(result).toBe(new Decimal(1).toFixed(2)); }); - test("should convert to the correct value based on the mocked price.", async () => { - const result = await grid.tft.convertTFTtoUSD({ amount: 5 }); + test("should convert to the correct value based on tftPrice.", async () => { + const hourlyTFT = { amount: 1 }; + const result = await grid.currency.convertTFTtoUSD(hourlyTFT); - expect(mock_price).toHaveBeenCalled(); expect(typeof result).toBe("string"); - expect(result).toBe(new Decimal(5 * 0.2).toFixed(2)); + expect(result).toBe(new Decimal(1 * tftPrice).toFixed(2)); }); test("convertTFTtoUSD function to throw if passed a negative value.", async () => { - const result = async () => await grid.tft.convertTFTtoUSD({ amount: -1 }); + const result = async () => await grid.currency.convertTFTtoUSD({ amount: -1 }); await expect(result).rejects.toThrow(); await expect(result).rejects.toBeInstanceOf(ValidationError); @@ -43,29 +46,46 @@ describe("Testing TFT module", () => { test("convertUSDtoTFT function returns a valid value.", async () => { const usd = 1; - const result = await grid.tft.convertUSDtoTFT({ amount: usd }); + const result = await grid.currency.convertUSDtoTFT({ amount: usd }); expect(typeof result).toBe("string"); - expect(result).toEqual(new Decimal(1 / 0.2).toFixed(2)); + expect(result).toEqual(new Decimal(1 / tftPrice).toFixed(2)); }); test("convertUSDtoTFT function to throw if passed a negative value.", async () => { - const result = async () => await grid.tft.convertUSDtoTFT({ amount: -1 }); + const result = async () => await grid.currency.convertUSDtoTFT({ amount: -1 }); await expect(result).rejects.toThrow(); await expect(result).rejects.toBeInstanceOf(ValidationError); }); + test("dailyTFT function returns a valid value.", async () => { + const tfts = 1; + const result = await grid.currency.dailyTFT({ amount: tfts }); + const expected_result = new Decimal(tfts * 24).toFixed(2); + + expect(typeof result).toBe("string"); + expect(result).toBe(expected_result); + }); + + test("dailyTFT function throws if passed anything other than a positive value.", async () => { + const result = async () => await grid.currency.dailyTFT({ amount: -1 }); + + expect(result).rejects.toThrow(); + expect(result).rejects.toBeInstanceOf(ValidationError); + }); + test("monthlyTFT function returns a valid value.", async () => { const tfts = 1; - const result = await grid.tft.monthlyTFT({ amount: tfts }); + const result = await grid.currency.monthlyTFT({ amount: tfts }); const expected_result = new Decimal(tfts * 24 * 30).toFixed(2); + expect(typeof result).toBe("string"); expect(result).toBe(expected_result); }); test("monthlyTFT function throws if passed anything other than a positive value.", async () => { - const result = async () => await grid.tft.monthlyTFT({ amount: -1 }); + const result = async () => await grid.currency.monthlyTFT({ amount: -1 }); await expect(result).rejects.toThrow(); await expect(result).rejects.toBeInstanceOf(ValidationError); @@ -73,14 +93,63 @@ describe("Testing TFT module", () => { test("yearlyTFT function returns a valid value.", async () => { const tfts = 1; - const result = await grid.tft.yearlyTFT({ amount: tfts }); - const expected_result = new Decimal(+(await grid.tft.monthlyTFT({ amount: tfts })) * 12).toFixed(2); + const result = await grid.currency.yearlyTFT({ amount: tfts }); + const expected_result = new Decimal(+(await grid.currency.monthlyTFT({ amount: tfts })) * 12).toFixed(2); + expect(typeof result).toBe("string"); expect(result).toBe(expected_result); }); test("yearlyTFT function throws if passed anything other than a positive value.", async () => { - const result = async () => grid.tft.yearlyTFT({ amount: -1 }); + const result = async () => grid.currency.yearlyTFT({ amount: -1 }); + + await expect(result).rejects.toThrow(); + await expect(result).rejects.toBeInstanceOf(ValidationError); + }); + + test("dailyUSD function returns a valid value.", async () => { + const tfts = 1; + const result = await grid.currency.dailyUSD({ amount: tfts }); + const expected_result = new Decimal(tfts * 24 * tftPrice).toFixed(2); + + expect(typeof result).toBe("string"); + expect(result).toBe(expected_result); + }); + + test("dailyUSD function throws if passed anything other than a positive value.", async () => { + const result = async () => await grid.currency.dailyUSD({ amount: -1 }); + + expect(result).rejects.toThrow(); + expect(result).rejects.toBeInstanceOf(ValidationError); + }); + + test("monthlyUSD function returns a valid value.", async () => { + const tfts = 1; + const result = await grid.currency.monthlyUSD({ amount: tfts }); + const expected_result = new Decimal(tfts * 24 * 30 * tftPrice).toFixed(2); + + expect(typeof result).toBe("string"); + expect(result).toBe(expected_result); + }); + + test("monthlyUSD function throws if passed anything other than a positive value.", async () => { + const result = async () => await grid.currency.monthlyUSD({ amount: -1 }); + + await expect(result).rejects.toThrow(); + await expect(result).rejects.toBeInstanceOf(ValidationError); + }); + + test("yearlyUSD function returns a valid value.", async () => { + const tfts = 1; + const result = await grid.currency.yearlyUSD({ amount: tfts }); + const expected_result = new Decimal(+(await grid.currency.monthlyUSD({ amount: tfts })) * 12).toFixed(2); + + expect(typeof result).toBe("string"); + expect(result).toBe(expected_result); + }); + + test("yearlyUSD function throws if passed anything other than a positive value.", async () => { + const result = async () => grid.currency.yearlyUSD({ amount: -1 }); await expect(result).rejects.toThrow(); await expect(result).rejects.toBeInstanceOf(ValidationError); From 6da49788b924d043675d89e4f5aa00924a5c5136 Mon Sep 17 00:00:00 2001 From: zaelgohary Date: Thu, 23 May 2024 18:08:50 +0300 Subject: [PATCH 11/14] Edit daily, monthly & yearly usd, add decimal to constructor, update tests --- packages/grid_client/src/modules/tft.ts | 25 +++++++++---------- .../grid_client/tests/modules/tft.test.ts | 23 ++++++++++------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/packages/grid_client/src/modules/tft.ts b/packages/grid_client/src/modules/tft.ts index 6f990f0ddb..15e75b5a7b 100644 --- a/packages/grid_client/src/modules/tft.ts +++ b/packages/grid_client/src/modules/tft.ts @@ -5,14 +5,14 @@ import { expose, validateInput } from "../helpers"; import { CurrencyModel } from "./models"; class TFTUSDConversionService { - private decimals = 2; private tfclient: TFClient; - TFTPrice: number; + private rate: number; - constructor(public config: GridClientConfig) { + constructor(public config: GridClientConfig, public decimals = 2) { + this.decimals = decimals; this.tfclient = config.tfclient; this.tfclient.tftPrice.get().then(res => { - this.TFTPrice = res; + this.rate = res; }); } @@ -25,14 +25,14 @@ class TFTUSDConversionService { @expose @validateInput convertUSDtoTFT(options: CurrencyModel) { - const amount = options.amount / this.TFTPrice; + const amount = options.amount / this.rate; return this.normalizeCurrency({ amount }); } @expose @validateInput convertTFTtoUSD(options: CurrencyModel) { - const amount = options.amount * this.TFTPrice; + const amount = options.amount * this.rate; return this.normalizeCurrency({ amount }); } @@ -60,23 +60,22 @@ class TFTUSDConversionService { @expose @validateInput dailyUSD(options: CurrencyModel): string { - const dailyTFTs = +this.dailyTFT(options); - return this.convertTFTtoUSD({ amount: dailyTFTs }); + const hours = options.amount * 24; + return this.normalizeCurrency({ amount: hours }); } @expose @validateInput monthlyUSD(options: CurrencyModel): string { - const monthlyTFTs = +this.dailyTFT(options) * 30; - return this.convertTFTtoUSD({ amount: monthlyTFTs }); + const months = +this.dailyUSD(options) * 30; + return this.normalizeCurrency({ amount: months }); } @expose @validateInput yearlyUSD(options: CurrencyModel): string { - const yearlyTFT = +this.monthlyTFT(options) * 12; - - return this.convertTFTtoUSD({ amount: yearlyTFT }); + const years = +this.monthlyUSD(options) * 12; + return this.normalizeCurrency({ amount: years }); } } diff --git a/packages/grid_client/tests/modules/tft.test.ts b/packages/grid_client/tests/modules/tft.test.ts index 09c70cd625..16b257436e 100644 --- a/packages/grid_client/tests/modules/tft.test.ts +++ b/packages/grid_client/tests/modules/tft.test.ts @@ -11,6 +11,7 @@ let tftPrice: number; beforeAll(async () => { grid = await getClient(); tftPrice = await grid.tfclient.tftPrice.get(); + grid.currency.decimals = 5; }); afterAll(async () => { @@ -26,7 +27,7 @@ describe("Testing TFT module", () => { const result = await grid.currency.normalizeCurrency({ amount: 1 }); expect(typeof result).toBe("string"); - expect(result).toBe(new Decimal(1).toFixed(2)); + expect(result).toBe(new Decimal(1).toFixed(grid.currency.decimals)); }); test("should convert to the correct value based on tftPrice.", async () => { @@ -34,7 +35,7 @@ describe("Testing TFT module", () => { const result = await grid.currency.convertTFTtoUSD(hourlyTFT); expect(typeof result).toBe("string"); - expect(result).toBe(new Decimal(1 * tftPrice).toFixed(2)); + expect(result).toBe(new Decimal(1 * tftPrice).toFixed(grid.currency.decimals)); }); test("convertTFTtoUSD function to throw if passed a negative value.", async () => { @@ -49,7 +50,7 @@ describe("Testing TFT module", () => { const result = await grid.currency.convertUSDtoTFT({ amount: usd }); expect(typeof result).toBe("string"); - expect(result).toEqual(new Decimal(1 / tftPrice).toFixed(2)); + expect(result).toEqual(new Decimal(1 / tftPrice).toFixed(grid.currency.decimals)); }); test("convertUSDtoTFT function to throw if passed a negative value.", async () => { @@ -62,7 +63,7 @@ describe("Testing TFT module", () => { test("dailyTFT function returns a valid value.", async () => { const tfts = 1; const result = await grid.currency.dailyTFT({ amount: tfts }); - const expected_result = new Decimal(tfts * 24).toFixed(2); + const expected_result = new Decimal(tfts * 24).toFixed(grid.currency.decimals); expect(typeof result).toBe("string"); expect(result).toBe(expected_result); @@ -78,7 +79,7 @@ describe("Testing TFT module", () => { test("monthlyTFT function returns a valid value.", async () => { const tfts = 1; const result = await grid.currency.monthlyTFT({ amount: tfts }); - const expected_result = new Decimal(tfts * 24 * 30).toFixed(2); + const expected_result = new Decimal(tfts * 24 * 30).toFixed(grid.currency.decimals); expect(typeof result).toBe("string"); expect(result).toBe(expected_result); @@ -94,7 +95,9 @@ describe("Testing TFT module", () => { test("yearlyTFT function returns a valid value.", async () => { const tfts = 1; const result = await grid.currency.yearlyTFT({ amount: tfts }); - const expected_result = new Decimal(+(await grid.currency.monthlyTFT({ amount: tfts })) * 12).toFixed(2); + const expected_result = new Decimal(+(await grid.currency.monthlyTFT({ amount: tfts })) * 12).toFixed( + grid.currency.decimals, + ); expect(typeof result).toBe("string"); expect(result).toBe(expected_result); @@ -110,7 +113,7 @@ describe("Testing TFT module", () => { test("dailyUSD function returns a valid value.", async () => { const tfts = 1; const result = await grid.currency.dailyUSD({ amount: tfts }); - const expected_result = new Decimal(tfts * 24 * tftPrice).toFixed(2); + const expected_result = new Decimal(tfts * 24).toFixed(grid.currency.decimals); expect(typeof result).toBe("string"); expect(result).toBe(expected_result); @@ -126,7 +129,7 @@ describe("Testing TFT module", () => { test("monthlyUSD function returns a valid value.", async () => { const tfts = 1; const result = await grid.currency.monthlyUSD({ amount: tfts }); - const expected_result = new Decimal(tfts * 24 * 30 * tftPrice).toFixed(2); + const expected_result = new Decimal(tfts * 24 * 30).toFixed(grid.currency.decimals); expect(typeof result).toBe("string"); expect(result).toBe(expected_result); @@ -142,7 +145,9 @@ describe("Testing TFT module", () => { test("yearlyUSD function returns a valid value.", async () => { const tfts = 1; const result = await grid.currency.yearlyUSD({ amount: tfts }); - const expected_result = new Decimal(+(await grid.currency.monthlyUSD({ amount: tfts })) * 12).toFixed(2); + const expected_result = new Decimal(+(await grid.currency.monthlyUSD({ amount: tfts })) * 12).toFixed( + grid.currency.decimals, + ); expect(typeof result).toBe("string"); expect(result).toBe(expected_result); From c63fe24599b659b6a428624e3d7c5d12b7f7e1f0 Mon Sep 17 00:00:00 2001 From: zaelgohary Date: Thu, 23 May 2024 18:12:06 +0300 Subject: [PATCH 12/14] Add return types --- packages/grid_client/src/modules/tft.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/grid_client/src/modules/tft.ts b/packages/grid_client/src/modules/tft.ts index 15e75b5a7b..6b066b9ea8 100644 --- a/packages/grid_client/src/modules/tft.ts +++ b/packages/grid_client/src/modules/tft.ts @@ -18,20 +18,20 @@ class TFTUSDConversionService { @expose @validateInput - normalizeCurrency(options: CurrencyModel) { + normalizeCurrency(options: CurrencyModel): string { return new Decimal(options.amount).toFixed(this.decimals); } @expose @validateInput - convertUSDtoTFT(options: CurrencyModel) { + convertUSDtoTFT(options: CurrencyModel): string { const amount = options.amount / this.rate; return this.normalizeCurrency({ amount }); } @expose @validateInput - convertTFTtoUSD(options: CurrencyModel) { + convertTFTtoUSD(options: CurrencyModel): string { const amount = options.amount * this.rate; return this.normalizeCurrency({ amount }); } From 965a49205660fb44215bcd7e8c9ae0c5b126c9dd Mon Sep 17 00:00:00 2001 From: zaelgohary Date: Thu, 23 May 2024 19:08:52 +0300 Subject: [PATCH 13/14] Remove tfclient from Currency, add rate, update tests & script --- packages/grid_client/scripts/tft.ts | 2 +- packages/grid_client/src/modules/tft.ts | 13 +- .../grid_client/tests/modules/tft.test.ts | 162 ++++++++++-------- 3 files changed, 95 insertions(+), 82 deletions(-) diff --git a/packages/grid_client/scripts/tft.ts b/packages/grid_client/scripts/tft.ts index cc704edf71..04a163d58b 100644 --- a/packages/grid_client/scripts/tft.ts +++ b/packages/grid_client/scripts/tft.ts @@ -59,7 +59,7 @@ async function yearlyUSD(client, hourlyUSD) { } async function main() { const grid = await getClient(); - + grid.currency.rate = await grid.tfclient.tftPrice.get(); const amount: CurrencyModel = { amount: 1, }; diff --git a/packages/grid_client/src/modules/tft.ts b/packages/grid_client/src/modules/tft.ts index 6b066b9ea8..31dceba367 100644 --- a/packages/grid_client/src/modules/tft.ts +++ b/packages/grid_client/src/modules/tft.ts @@ -1,20 +1,11 @@ import Decimal from "decimal.js"; -import { GridClientConfig, TFClient } from ".."; import { expose, validateInput } from "../helpers"; import { CurrencyModel } from "./models"; class TFTUSDConversionService { - private tfclient: TFClient; - private rate: number; - - constructor(public config: GridClientConfig, public decimals = 2) { - this.decimals = decimals; - this.tfclient = config.tfclient; - this.tfclient.tftPrice.get().then(res => { - this.rate = res; - }); - } + // TFT rate: 1 tft = x USD + constructor(public rate: number, private decimals = 2) {} @expose @validateInput diff --git a/packages/grid_client/tests/modules/tft.test.ts b/packages/grid_client/tests/modules/tft.test.ts index 16b257436e..b5eb4f9f77 100644 --- a/packages/grid_client/tests/modules/tft.test.ts +++ b/packages/grid_client/tests/modules/tft.test.ts @@ -1,17 +1,20 @@ import { ValidationError } from "@threefold/types"; import Decimal from "decimal.js"; -import { currency, type GridClient } from "../../src"; +import { currency as TFTUSDConversionService, type GridClient } from "../../src"; import { getClient } from "../client_loader"; jest.setTimeout(300000); let grid: GridClient; -let tftPrice: number; +let rate: number; +let decimals: number; +let currency: TFTUSDConversionService; beforeAll(async () => { grid = await getClient(); - tftPrice = await grid.tfclient.tftPrice.get(); - grid.currency.decimals = 5; + rate = await grid.tfclient.tftPrice.get(); + decimals = 5; + currency = new TFTUSDConversionService(rate, decimals); }); afterAll(async () => { @@ -19,144 +22,163 @@ afterAll(async () => { }); describe("Testing TFT module", () => { - test("tft module to be instance of TFTUSDConversionService", async () => { - expect(await grid.currency).toBeInstanceOf(currency); + test("tft module to be instance of TFTUSDConversionService", () => { + expect(currency).toBeInstanceOf(TFTUSDConversionService); }); - test("should return value with 2 decimals.", async () => { - const result = await grid.currency.normalizeCurrency({ amount: 1 }); + test("should return value with 2 decimals.", () => { + const result = currency.normalizeCurrency({ amount: 1 }); expect(typeof result).toBe("string"); - expect(result).toBe(new Decimal(1).toFixed(grid.currency.decimals)); + expect(result).toBe(new Decimal(1).toFixed(decimals)); }); - test("should convert to the correct value based on tftPrice.", async () => { + test("should convert to the correct value based on tftPrice.", () => { const hourlyTFT = { amount: 1 }; - const result = await grid.currency.convertTFTtoUSD(hourlyTFT); + const result = currency.convertTFTtoUSD(hourlyTFT); expect(typeof result).toBe("string"); - expect(result).toBe(new Decimal(1 * tftPrice).toFixed(grid.currency.decimals)); + expect(result).toBe(new Decimal(1 * rate).toFixed(decimals)); }); - test("convertTFTtoUSD function to throw if passed a negative value.", async () => { - const result = async () => await grid.currency.convertTFTtoUSD({ amount: -1 }); - - await expect(result).rejects.toThrow(); - await expect(result).rejects.toBeInstanceOf(ValidationError); + test("convertTFTtoUSD function to throw if passed a negative value.", () => { + const result = () => currency.convertTFTtoUSD({ amount: -1 }); + try { + expect(result).toThrow(); + } catch (error) { + expect(result).toBeInstanceOf(ValidationError); + } }); - test("convertUSDtoTFT function returns a valid value.", async () => { + test("convertUSDtoTFT function returns a valid value.", () => { const usd = 1; - const result = await grid.currency.convertUSDtoTFT({ amount: usd }); + const result = currency.convertUSDtoTFT({ amount: usd }); expect(typeof result).toBe("string"); - expect(result).toEqual(new Decimal(1 / tftPrice).toFixed(grid.currency.decimals)); + expect(result).toEqual(new Decimal(1 / rate).toFixed(decimals)); }); - test("convertUSDtoTFT function to throw if passed a negative value.", async () => { - const result = async () => await grid.currency.convertUSDtoTFT({ amount: -1 }); + test("convertUSDtoTFT function to throw if passed a negative value.", () => { + const result = () => currency.convertUSDtoTFT({ amount: -1 }); - await expect(result).rejects.toThrow(); - await expect(result).rejects.toBeInstanceOf(ValidationError); + try { + expect(result).toThrow(); + } catch (error) { + expect(result).toBeInstanceOf(ValidationError); + } }); - test("dailyTFT function returns a valid value.", async () => { + test("dailyTFT function returns a valid value.", () => { const tfts = 1; - const result = await grid.currency.dailyTFT({ amount: tfts }); - const expected_result = new Decimal(tfts * 24).toFixed(grid.currency.decimals); + const result = currency.dailyTFT({ amount: tfts }); + const expected_result = new Decimal(tfts * 24).toFixed(decimals); expect(typeof result).toBe("string"); expect(result).toBe(expected_result); }); - test("dailyTFT function throws if passed anything other than a positive value.", async () => { - const result = async () => await grid.currency.dailyTFT({ amount: -1 }); + test("dailyTFT function throws if passed anything other than a positive value.", () => { + const result = () => currency.dailyTFT({ amount: -1 }); - expect(result).rejects.toThrow(); - expect(result).rejects.toBeInstanceOf(ValidationError); + try { + expect(result).toThrow(); + } catch (error) { + expect(result).toBeInstanceOf(ValidationError); + } }); - test("monthlyTFT function returns a valid value.", async () => { + test("monthlyTFT function returns a valid value.", () => { const tfts = 1; - const result = await grid.currency.monthlyTFT({ amount: tfts }); - const expected_result = new Decimal(tfts * 24 * 30).toFixed(grid.currency.decimals); + const result = currency.monthlyTFT({ amount: tfts }); + const expected_result = new Decimal(tfts * 24 * 30).toFixed(decimals); expect(typeof result).toBe("string"); expect(result).toBe(expected_result); }); - test("monthlyTFT function throws if passed anything other than a positive value.", async () => { - const result = async () => await grid.currency.monthlyTFT({ amount: -1 }); + test("monthlyTFT function throws if passed anything other than a positive value.", () => { + const result = () => currency.monthlyTFT({ amount: -1 }); - await expect(result).rejects.toThrow(); - await expect(result).rejects.toBeInstanceOf(ValidationError); + try { + expect(result).toThrow(); + } catch (error) { + expect(result).toBeInstanceOf(ValidationError); + } }); - test("yearlyTFT function returns a valid value.", async () => { + test("yearlyTFT function returns a valid value.", () => { const tfts = 1; - const result = await grid.currency.yearlyTFT({ amount: tfts }); - const expected_result = new Decimal(+(await grid.currency.monthlyTFT({ amount: tfts })) * 12).toFixed( - grid.currency.decimals, - ); + const result = currency.yearlyTFT({ amount: tfts }); + const expected_result = new Decimal(+currency.monthlyTFT({ amount: tfts }) * 12).toFixed(decimals); expect(typeof result).toBe("string"); expect(result).toBe(expected_result); }); - test("yearlyTFT function throws if passed anything other than a positive value.", async () => { - const result = async () => grid.currency.yearlyTFT({ amount: -1 }); + test("yearlyTFT function throws if passed anything other than a positive value.", () => { + const result = () => currency.yearlyTFT({ amount: -1 }); - await expect(result).rejects.toThrow(); - await expect(result).rejects.toBeInstanceOf(ValidationError); + try { + expect(result).toThrow(); + } catch (error) { + expect(result).toBeInstanceOf(ValidationError); + } }); - test("dailyUSD function returns a valid value.", async () => { + test("dailyUSD function returns a valid value.", () => { const tfts = 1; - const result = await grid.currency.dailyUSD({ amount: tfts }); - const expected_result = new Decimal(tfts * 24).toFixed(grid.currency.decimals); + const result = currency.dailyUSD({ amount: tfts }); + const expected_result = new Decimal(tfts * 24).toFixed(decimals); expect(typeof result).toBe("string"); expect(result).toBe(expected_result); }); - test("dailyUSD function throws if passed anything other than a positive value.", async () => { - const result = async () => await grid.currency.dailyUSD({ amount: -1 }); + test("dailyUSD function throws if passed anything other than a positive value.", () => { + const result = () => currency.dailyUSD({ amount: -1 }); - expect(result).rejects.toThrow(); - expect(result).rejects.toBeInstanceOf(ValidationError); + try { + expect(result).toThrow(); + } catch (error) { + expect(result).toBeInstanceOf(ValidationError); + } }); - test("monthlyUSD function returns a valid value.", async () => { + test("monthlyUSD function returns a valid value.", () => { const tfts = 1; - const result = await grid.currency.monthlyUSD({ amount: tfts }); - const expected_result = new Decimal(tfts * 24 * 30).toFixed(grid.currency.decimals); + const result = currency.monthlyUSD({ amount: tfts }); + const expected_result = new Decimal(tfts * 24 * 30).toFixed(decimals); expect(typeof result).toBe("string"); expect(result).toBe(expected_result); }); - test("monthlyUSD function throws if passed anything other than a positive value.", async () => { - const result = async () => await grid.currency.monthlyUSD({ amount: -1 }); + test("monthlyUSD function throws if passed anything other than a positive value.", () => { + const result = () => currency.monthlyUSD({ amount: -1 }); - await expect(result).rejects.toThrow(); - await expect(result).rejects.toBeInstanceOf(ValidationError); + try { + expect(result).toThrow(); + } catch (error) { + expect(result).toBeInstanceOf(ValidationError); + } }); - test("yearlyUSD function returns a valid value.", async () => { + test("yearlyUSD function returns a valid value.", () => { const tfts = 1; - const result = await grid.currency.yearlyUSD({ amount: tfts }); - const expected_result = new Decimal(+(await grid.currency.monthlyUSD({ amount: tfts })) * 12).toFixed( - grid.currency.decimals, - ); + const result = currency.yearlyUSD({ amount: tfts }); + const expected_result = new Decimal(+currency.monthlyUSD({ amount: tfts }) * 12).toFixed(decimals); expect(typeof result).toBe("string"); expect(result).toBe(expected_result); }); - test("yearlyUSD function throws if passed anything other than a positive value.", async () => { - const result = async () => grid.currency.yearlyUSD({ amount: -1 }); + test("yearlyUSD function throws if passed anything other than a positive value.", () => { + const result = () => currency.yearlyUSD({ amount: -1 }); - await expect(result).rejects.toThrow(); - await expect(result).rejects.toBeInstanceOf(ValidationError); + try { + expect(result).toThrow(); + } catch (error) { + expect(result).toBeInstanceOf(ValidationError); + } }); }); From dde50b57f6890edeb2607498130c39777a60790b Mon Sep 17 00:00:00 2001 From: zaelgohary Date: Sun, 26 May 2024 01:00:06 +0300 Subject: [PATCH 14/14] Protect rate & enable its access via a getter, update script --- packages/grid_client/scripts/tft.ts | 56 ++++++++++++++----------- packages/grid_client/src/modules/tft.ts | 8 +++- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/packages/grid_client/scripts/tft.ts b/packages/grid_client/scripts/tft.ts index 04a163d58b..408836fbd0 100644 --- a/packages/grid_client/scripts/tft.ts +++ b/packages/grid_client/scripts/tft.ts @@ -1,77 +1,83 @@ import { CurrencyModel } from "../src"; +import { currency as TFTUSDConversionService } from "../src"; import { getClient } from "./client_loader"; import { log } from "./utils"; -async function convertTFTtoUSD(client, amount) { - const res = await client.currency.convertTFTtoUSD(amount); +let currency: TFTUSDConversionService; + +function convertTFTtoUSD(amount) { + const res = currency.convertTFTtoUSD(amount); log("================= Convert TFT ================="); log(res); log("================= Convert TFT ================="); } -async function convertUSDtoTFT(client, amount) { - const res = await client.currency.convertUSDtoTFT(amount); +function convertUSDtoTFT(amount) { + const res = currency.convertUSDtoTFT(amount); log("================= Convert USD ================="); log(res); log("================= Convert USD ================="); } -async function dailyTFT(client, hourlyTFT) { - const res = await client.currency.dailyTFT(hourlyTFT); +function dailyTFT(hourlyTFT) { + const res = currency.dailyTFT(hourlyTFT); log("================= Daily TFT ================="); log(res); log("================= Daily TFT ================="); } -async function monthlyTFT(client, hourlyTFT) { - const res = await client.currency.monthlyTFT(hourlyTFT); +function monthlyTFT(hourlyTFT) { + const res = currency.monthlyTFT(hourlyTFT); log("================= Monthly TFT ================="); log(res); log("================= Monthly TFT ================="); } -async function yearlyTFT(client, hourlyTFT) { - const res = await client.currency.yearlyTFT(hourlyTFT); +function yearlyTFT(hourlyTFT) { + const res = currency.yearlyTFT(hourlyTFT); log("================= Yearly TFT ================="); log(res); log("================= Yearly TFT ================="); } -async function dailyUSD(client, hourlyUSD) { - const res = await client.currency.dailyUSD(hourlyUSD); +function dailyUSD(hourlyUSD) { + const res = currency.dailyUSD(hourlyUSD); log("================= Daily USD ================="); log(res); log("================= Daily USD ================="); } -async function monthlyUSD(client, hourlyUSD) { - const res = await client.currency.monthlyUSD(hourlyUSD); +function monthlyUSD(hourlyUSD) { + const res = currency.monthlyUSD(hourlyUSD); log("================= Monthly USD ================="); log(res); log("================= Monthly USD ================="); } -async function yearlyUSD(client, hourlyUSD) { - const res = await client.currency.yearlyUSD(hourlyUSD); +function yearlyUSD(hourlyUSD) { + const res = 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 rate = await grid.tfclient.tftPrice.get(); + const decimals = 3; + currency = new TFTUSDConversionService(rate, decimals); + 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); + convertTFTtoUSD(amount); + convertUSDtoTFT(amount); + dailyTFT(amount); + monthlyTFT(amount); + yearlyTFT(amount); + dailyUSD(amount); + monthlyUSD(amount); + yearlyUSD(amount); await grid.disconnect(); } diff --git a/packages/grid_client/src/modules/tft.ts b/packages/grid_client/src/modules/tft.ts index 31dceba367..37c02eb8be 100644 --- a/packages/grid_client/src/modules/tft.ts +++ b/packages/grid_client/src/modules/tft.ts @@ -4,8 +4,12 @@ import { expose, validateInput } from "../helpers"; import { CurrencyModel } from "./models"; class TFTUSDConversionService { - // TFT rate: 1 tft = x USD - constructor(public rate: number, private decimals = 2) {} + // TFT rate: 1 TFT = x USD + constructor(protected rate: number, private decimals = 2) {} + + get _rate() { + return this.rate; + } @expose @validateInput