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

units #30

Merged
merged 3 commits into from
Sep 28, 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
2 changes: 1 addition & 1 deletion src/helpers/test/units.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
10 changes: 9 additions & 1 deletion src/helpers/units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
19 changes: 12 additions & 7 deletions src/lib/updown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } from "../helpers/units";
import {
type Cents,
centsToDollarsFormatted,
dollarsToCents,
} from "../helpers/units";
import { getBalance } from "./balance";
import { getQuote } from "./buy";
import { formatDuration } from "./orders";
Expand All @@ -22,7 +26,7 @@ export function registerUp(program: Command) {
.option("-d, --duration <duration>", "Specify the minimum duration")
.option(
"-p, --price <price>",
"Specify the maximum price per node per hour",
"Specify the maximum price per node-hour, in dollars",
);

cmd.action(async (options) => {
Expand All @@ -46,7 +50,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
Expand All @@ -71,14 +75,15 @@ 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
// per hour price in cents
quotePrice = quote.price / durationHours;
}

const pricePerNodeHourInDollars = props.pricePerNodeHour
? Number.parseInt(props.pricePerNodeHour)
console.log("props.pricePerNodeHour", props.pricePerNodeHourDollars);

const pricePerNodeHourInCents = props.pricePerNodeHourDollars
? dollarsToCents(Number.parseFloat(props.pricePerNodeHourDollars))
: quotePrice;
const pricePerNodeHourInCents = Math.ceil(pricePerNodeHourInDollars * 100);

const totalPriceInCents =
pricePerNodeHourInCents * Number.parseInt(props.n ?? "1") * durationHours;
Expand Down
Loading