Skip to content
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: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getApiUrl } from "./helpers/urls.ts";
import { getAppBanner } from "./lib/app-banner.ts";
import { registerBalance } from "./lib/balance.ts";
import { registerBuy } from "./lib/buy/index.tsx";
import { registerExtend } from "./lib/extend/index.tsx";
import { registerClusters } from "./lib/clusters/clusters.tsx";
import { registerContracts } from "./lib/contracts/index.tsx";
import { registerDev } from "./lib/dev.ts";
Expand All @@ -35,6 +36,7 @@ program
// commands
registerLogin(program);
registerBuy(program);
registerExtend(program);
registerOrders(program);
registerContracts(program);
registerSell(program);
Expand Down
12 changes: 6 additions & 6 deletions src/lib/buy/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import { analytics } from "../posthog.ts";
dayjs.extend(relativeTime);
dayjs.extend(duration);

type SfBuyOptions = ReturnType<ReturnType<typeof _registerBuy>["opts"]>;
export type SfBuyOptions = ReturnType<ReturnType<typeof _registerBuy>["opts"]>;

function _registerBuy(program: Command) {
export function _registerBuy(program: Command) {
return program
.command("buy")
.description("Place a buy order")
Expand Down Expand Up @@ -141,7 +141,7 @@ function parseEnd(value: string) {
return roundEndDate(parsed);
}

function parseDuration(duration?: string) {
export function parseDuration(duration?: string) {
if (!duration) {
return 1 * 60 * 60; // 1 hour
}
Expand All @@ -159,7 +159,7 @@ function parseDuration(duration?: string) {
return parsed / 1000;
}

function parsePricePerGpuHour(price?: string) {
export function parsePricePerGpuHour(price?: string) {
if (!price) {
return null;
}
Expand All @@ -169,7 +169,7 @@ function parsePricePerGpuHour(price?: string) {
return Number.parseFloat(priceWithoutDollar) * 100;
}

function QuoteComponent(props: { options: SfBuyOptions }) {
export function QuoteComponent(props: { options: SfBuyOptions }) {
const [quote, setQuote] = useState<Quote | null>(null);
const [isLoading, setIsLoading] = useState(true);

Expand All @@ -193,7 +193,7 @@ function QuoteComponent(props: { options: SfBuyOptions }) {
: <QuoteDisplay quote={quote} />;
}

function QuoteAndBuy(props: { options: SfBuyOptions }) {
export function QuoteAndBuy(props: { options: SfBuyOptions }) {
const [orderProps, setOrderProps] = useState<BuyOrderProps | null>(null);

// submit a quote request, handle loading state
Expand Down
7 changes: 7 additions & 0 deletions src/lib/contracts/ContractDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ export function ContractDisplay(props: { contract: Contract }) {
value={props.contract.colocate_with.join(", ")}
/>
)}
{state === "Active" && (
<Row
headWidth={COLUMN_WIDTH}
head="Extend"
value={`sf extend --contract ${props.contract.id} --duration 1h`}
/>
)}

<Box flexDirection="column">
{intervalData.map((data, index) => {
Expand Down
20 changes: 18 additions & 2 deletions src/lib/contracts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
export type ContractState = "Upcoming" | "Active" | "Expired";
export type ContractRange = { startsAt: Date; endsAt: Date };

export function getContractRange(shape: {
intervals: string[];
quantities: number[];
}): ContractRange {
const startsAt = new Date(shape.intervals[0]);
const endsAt = new Date(shape.intervals[shape.intervals.length - 1]);
return { startsAt, endsAt };
}

export function getContractAcceleratorQuantity(shape: {
intervals: string[];
quantities: number[];
}): number {
return shape.quantities[0];
}

export function getContractState(shape: {
intervals: string[];
quantities: number[];
}): ContractState {
const now = new Date();
const startsAt = new Date(shape.intervals[0]);
const endsAt = new Date(shape.intervals[shape.intervals.length - 1]);
const { startsAt, endsAt } = getContractRange(shape);

if (startsAt > now) {
return "Upcoming";
Expand Down
98 changes: 98 additions & 0 deletions src/lib/extend/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { type Command } from "@commander-js/extra-typings";
import { render } from "ink";
import dayjs from "npm:dayjs@1.11.13";
import duration from "npm:dayjs@1.11.13/plugin/duration.js";
import relativeTime from "npm:dayjs@1.11.13/plugin/relativeTime.js";
import { parseDuration, SfBuyOptions } from "../buy/index.tsx";
import React from "react";
import { getContract } from "../../helpers/fetchers.ts";
import { logAndQuit } from "../../helpers/errors.ts";
import { QuoteAndBuy, QuoteComponent } from "../buy/index.tsx";
import { ActiveContract } from "../contracts/types.ts";
import { GPUS_PER_NODE } from "../constants.ts";
import {
getContractAcceleratorQuantity,
getContractRange,
} from "../contracts/utils.ts";

dayjs.extend(relativeTime);
dayjs.extend(duration);

function _registerExtend(program: Command) {
return program
.command("extend")
.description("Extend an existing contract")
.requiredOption(
"-c, --contract <contract>",
"Contract ID to extend e.g. cont_a9IcKaLesUBTHEY",
)
.requiredOption(
"-d, --duration <duration>",
"Extension duration (rounded up to the nearest hour)",
parseDuration,
)
.option("-p, --price <price>", "Price in dollars per GPU hour")
.option("-y, --yes", "Automatically confirm the extension")
.option(
"-q, --quote",
"Get a price quote without placing an extension order",
)
.configureHelp({
optionDescription: (option) => {
if (option.flags === "-h, --help") {
return "Display help for extend";
}
return option.description;
},
})
.addHelpText(
"after",
`
Examples:
\x1b[2m# Get a Quote to extend a contract for 1 hour\x1b[0m]
$ sf extend --contract <contract_id> --duration 1h --quote

\x1b[2m# Auto confirm extending a contract by 1 hour at market price\x1b[0m]
$ sf extend -c <contract_id> -d 1h --yes

\x1b[2m# Extend a contract for 2 hours at a specific price\x1b[0m]
$ sf extend -c <contract_id> -d 2h --price 1.50
`,
)
.action(async function extendAction(options) {
const contract = await getContract(options.contract);
if (!contract) {
logAndQuit(`Contract ${options.contract} not found`);
}

if (contract.status !== "active") {
logAndQuit(
`Contract ${contract.id} is ${contract.status}. Only active contracts can be extended.`,
);
}

const activeContract = contract as ActiveContract;
const activeContractRange = getContractRange(activeContract.shape);

const quoteOptions: SfBuyOptions = {
type: activeContract.instance_type,
accelerators: getContractAcceleratorQuantity(activeContract.shape) *
GPUS_PER_NODE,
colocate: [activeContract.id],
duration: options.duration,
price: options.price,
start: activeContractRange.endsAt,
quote: options.quote,
yes: options.yes,
};

if (options.quote) {
render(<QuoteComponent options={quoteOptions} />);
}
render(<QuoteAndBuy options={quoteOptions} />);
Comment thread
joshi4 marked this conversation as resolved.
});
}

export function registerExtend(program: Command) {
_registerExtend(program);
}
Comment on lines +96 to +98
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bit confused on why you didn't just export registerExtend directly here?