From afc4d40188ba3b25c891d8be1a8460b53cede6f5 Mon Sep 17 00:00:00 2001 From: Sladuca Date: Fri, 27 Sep 2024 17:17:35 -0700 Subject: [PATCH 1/3] unitssssssssssssss --- src/helpers/test/units.test.ts | 2 +- src/helpers/units.ts | 10 +++++++++- src/lib/updown.ts | 18 ++++++++++-------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/helpers/test/units.test.ts b/src/helpers/test/units.test.ts index ab7f68f..84d0d09 100644 --- a/src/helpers/test/units.test.ts +++ b/src/helpers/test/units.test.ts @@ -76,7 +76,7 @@ describe("units", () => { [10_00, "$10.00"], [100_00, "$100.00"], - [9_991, "$9.99"], + [9_99, "$9.99"], // with cents [1, "$0.01"], diff --git a/src/helpers/units.ts b/src/helpers/units.ts index 16f67dd..22d408e 100644 --- a/src/helpers/units.ts +++ b/src/helpers/units.ts @@ -78,5 +78,13 @@ export function priceWholeToCents( } export function centsToDollarsFormatted(cents: Cents): string { - return `$${(cents / 100).toFixed(2)}`; + return `$${centsToDollars(cents).toFixed(2)}`; +} + +export function centsToDollars(cents: Cents): number { + return cents / 100; +} + +export function dollarsToCents(dollars: number): Cents { + return Math.ceil(dollars * 100); } diff --git a/src/lib/updown.ts b/src/lib/updown.ts index 1bd8982..b4d72e6 100644 --- a/src/lib/updown.ts +++ b/src/lib/updown.ts @@ -4,7 +4,7 @@ import type { Command } from "commander"; import parseDuration from "parse-duration"; import { apiClient } from "../apiClient"; import { logAndQuit } from "../helpers/errors"; -import { type Cents, centsToDollarsFormatted } from "../helpers/units"; +import { type Cents, centsToDollarsFormatted, dollarsToCents } from "../helpers/units"; import { getBalance } from "./balance"; import { getQuote } from "./buy"; import { formatDuration } from "./orders"; @@ -22,7 +22,7 @@ export function registerUp(program: Command) { .option("-d, --duration ", "Specify the minimum duration") .option( "-p, --price ", - "Specify the maximum price per node per hour", + "Specify the maximum price per node-hour, in dollars", ); cmd.action(async (options) => { @@ -46,7 +46,7 @@ const DEFAULT_PRICE_PER_NODE_HOUR_IN_CENTS = 2.65 * 8 * 100; async function getDefaultProcurementOptions(props: { duration?: string; n?: string; - pricePerNodeHour?: string; + pricePerNodeHourDollars?: string; type?: string; }) { // Minimum block duration is 2 hours @@ -71,14 +71,16 @@ async function getDefaultProcurementOptions(props: { // Eventually we should replace this price with yesterday's index price let quotePrice = DEFAULT_PRICE_PER_NODE_HOUR_IN_CENTS; if (quote) { - // per hour price + console.log("quote", quote); + // per hour price in cents quotePrice = quote.price / durationHours; } - const pricePerNodeHourInDollars = props.pricePerNodeHour - ? Number.parseInt(props.pricePerNodeHour) - : quotePrice; - const pricePerNodeHourInCents = Math.ceil(pricePerNodeHourInDollars * 100); + console.log("props.pricePerNodeHour", props.pricePerNodeHourDollars); + + const pricePerNodeHourInCents = props.pricePerNodeHourDollars + ? dollarsToCents(Number.parseFloat(props.pricePerNodeHourDollars)) + : quotePrice const totalPriceInCents = pricePerNodeHourInCents * Number.parseInt(props.n ?? "1") * durationHours; From 992f3c611890323c7945d25d010f07e397f166e6 Mon Sep 17 00:00:00 2001 From: Sladuca Date: Fri, 27 Sep 2024 17:17:46 -0700 Subject: [PATCH 2/3] check --- src/lib/updown.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lib/updown.ts b/src/lib/updown.ts index b4d72e6..6351441 100644 --- a/src/lib/updown.ts +++ b/src/lib/updown.ts @@ -4,7 +4,11 @@ import type { Command } from "commander"; import parseDuration from "parse-duration"; import { apiClient } from "../apiClient"; import { logAndQuit } from "../helpers/errors"; -import { type Cents, centsToDollarsFormatted, dollarsToCents } from "../helpers/units"; +import { + type Cents, + centsToDollarsFormatted, + dollarsToCents, +} from "../helpers/units"; import { getBalance } from "./balance"; import { getQuote } from "./buy"; import { formatDuration } from "./orders"; @@ -80,7 +84,7 @@ async function getDefaultProcurementOptions(props: { const pricePerNodeHourInCents = props.pricePerNodeHourDollars ? dollarsToCents(Number.parseFloat(props.pricePerNodeHourDollars)) - : quotePrice + : quotePrice; const totalPriceInCents = pricePerNodeHourInCents * Number.parseInt(props.n ?? "1") * durationHours; From bda61986c36155e04f4e1c4f48daf0289482ce08 Mon Sep 17 00:00:00 2001 From: Sladuca Date: Fri, 27 Sep 2024 17:21:46 -0700 Subject: [PATCH 3/3] remove console log --- src/lib/updown.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/updown.ts b/src/lib/updown.ts index 6351441..2463849 100644 --- a/src/lib/updown.ts +++ b/src/lib/updown.ts @@ -75,7 +75,6 @@ async function getDefaultProcurementOptions(props: { // Eventually we should replace this price with yesterday's index price let quotePrice = DEFAULT_PRICE_PER_NODE_HOUR_IN_CENTS; if (quote) { - console.log("quote", quote); // per hour price in cents quotePrice = quote.price / durationHours; }