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 4 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
28 changes: 28 additions & 0 deletions packages/grid_client/scripts/tft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { substrateURL, TFT } from "../src";
zaelgohary marked this conversation as resolved.
Show resolved Hide resolved
import { getClient } from "./client_loader";

const substrate = "wss://tfchain.dev.grid.tf/ws" as substrateURL.DEV;
zaelgohary marked this conversation as resolved.
Show resolved Hide resolved
const tft = new TFT(substrate);

async function main() {
const grid = await getClient();

const price = await tft.price();
console.log(price);

const tfts = await tft.fromUSD({ usd: 10 });
console.log(tfts);

const usd = await tft.toUSD({ tft: 10 });
console.log(usd);

const tftsInMonth = await tft.toMonth({ tft: 1 });
console.log(tftsInMonth);

const tftsInYear = await tft.toYear({ tft: 20 });
console.log(tftsInYear);

grid.disconnect();
}

main();
1 change: 1 addition & 0 deletions packages/grid_client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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,3 +22,4 @@ export * from "./farmerbot";
export * from "./farms";
export * from "./networks";
export * from "./bridge";
export * from "./tft";
10 changes: 10 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,14 @@ class GetActiveContractsModel {
@Expose() @IsInt() @IsNotEmpty() @Min(1) nodeId: number;
}

class USDModel {
zaelgohary marked this conversation as resolved.
Show resolved Hide resolved
@Expose() @IsNumber() @IsNotEmpty() @Min(0) usd: number;
}

class TFTModel {
@Expose() @IsNumber() @IsNotEmpty() @Min(0) tft: number;
}

interface GPUCardInfo {
id: string;
contract: number;
Expand Down Expand Up @@ -977,4 +985,6 @@ export {
NodeCPUTest,
NodeIPValidation,
NodeIPerf,
USDModel,
TFTModel,
};
53 changes: 53 additions & 0 deletions packages/grid_client/src/modules/tft.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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 {
zaelgohary marked this conversation as resolved.
Show resolved Hide resolved
public SUBSTRATE_URL: string;
private calculator: Calculator;

zaelgohary marked this conversation as resolved.
Show resolved Hide resolved
constructor(SUBSTRATE_URL: string) {
zaelgohary marked this conversation as resolved.
Show resolved Hide resolved
this.SUBSTRATE_URL = SUBSTRATE_URL;
this.calculator = new Calculator(new QueryClient(this.SUBSTRATE_URL));
}

async price() {
return await this.calculator.tftPrice();
}

@expose
@validateInput
async fromUSD(options: USDModel) {
zaelgohary marked this conversation as resolved.
Show resolved Hide resolved
return new Decimal(options.usd / (await this.price())).toFixed(2);
}

@expose
@validateInput
async toUSD(options: TFTModel) {
zaelgohary marked this conversation as resolved.
Show resolved Hide resolved
return new Decimal(options.tft * (await this.price())).toFixed(2);
}
@expose
@validateInput
toMonth(options: TFTModel) {
zaelgohary marked this conversation as resolved.
Show resolved Hide resolved
return new Decimal(options.tft * 24 * 30).toFixed(2);
}

@expose
@validateInput
toYear(options: TFTModel) {
zaelgohary marked this conversation as resolved.
Show resolved Hide resolved
const months = +this.toMonth(options);
return new Decimal(months * 12).toFixed(2);
}
}

export { TFT, substrateURL };
98 changes: 98 additions & 0 deletions packages/grid_client/tests/modules/tft.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import Decimal from "decimal.js";

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;
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();
});

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";
zaelgohary marked this conversation as resolved.
Show resolved Hide resolved

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(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 () => {
expect(tft.fromUSD({ usd: -1 })).rejects.toThrow();
});

test("toUSD function returns a valid value.", async () => {
const usd = 1;
const result = await tft.toUSD({ tft: usd });

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 () => {
expect(tft.toUSD({ tft: -1 })).rejects.toThrow();
});

test("toMonth function returns a valid value.", () => {
const tfts = 1;
const result = tft.toMonth({ tft: tfts });
const expected_result = 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).rejects.toThrow();
});

test("toYear function returns a valid value.", () => {
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);
});

test("toYear function throws if passed anything other than a positive value.", () => {
expect(tft.toYear({ tft: -1 })).rejects.toThrow();
});
});
Loading