Skip to content

Commit

Permalink
fix several buy bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Flaque committed Oct 21, 2024
1 parent af51c87 commit 476180c
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions src/lib/buy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,19 @@ async function buyOrderAction(options: SfBuyOptions) {
// normalize inputs

const isQuoteOnly = options.quote ?? false;

// parse duration
let durationSeconds = parseDuration(options.duration, "s");

if (!durationSeconds) {
return logAndQuit(`Invalid duration: ${options.duration}`);
}

if (durationSeconds < 3600) {
return logAndQuit(
`Duration must be at least 1 hour, instead was ${durationSeconds} seconds. Try using -d '1h' instead.`,
);
}

const colocateWithContractIds = options.colocate ? options.colocate : [];

// default to 1 node if not specified
Expand Down Expand Up @@ -189,11 +195,27 @@ async function buyOrderAction(options: SfBuyOptions) {
didQuote = true;
} else {
// Quote an aggressive price based on an index if the order won't fill immediately
const aggressivePrice = await getAggressivePrice(options.type);
if (!aggressivePrice) {
const aggressivePricePerHour = await getAggressivePricePerHour(
options.type,
);
if (!aggressivePricePerHour) {
return logAndQuit("Not enough data exists to quote this order.");
}
priceCents = aggressivePrice;
const roundedStartDate =
startDate !== "NOW" ? roundStartDate(startDate) : startDate;

// round the end date.
const roundedEndDate = roundEndDate(endDate);

const roundedDurationSeconds = computeApproximateDurationSeconds(
roundedStartDate,
roundedEndDate,
);
priceCents =
aggressivePricePerHour *
GPUS_PER_NODE *
quantity *
Math.ceil(roundedDurationSeconds / 3600);
}
}

Expand Down Expand Up @@ -448,6 +470,7 @@ export async function getQuote(options: QuoteOptions) {
if (!response.ok) {
switch (response.status) {
case 400:
console.log("Error:", error);
return logAndQuit(`Bad Request: ${error?.message}`);
case 401:
return await logSessionTokenExpiredAndQuit();
Expand Down Expand Up @@ -502,11 +525,20 @@ async function getMostRecentIndexAvgPrice(instanceType: string) {
return data.data[0].gpu_hour;
}

async function getAggressivePrice(instanceType: string) {
async function getAggressivePricePerHour(instanceType: string) {
const mostRecentPrice = await getMostRecentIndexAvgPrice(instanceType);
// We'll set a floor on the recommended price here, because the index price
// will report 0 if there was no data, which might happen due to an outage.
const minimumPrice = 75; // 75 cents

if (!mostRecentPrice) {
return undefined;
return minimumPrice;
}

const recommendedIndexPrice = (mostRecentPrice.avg + mostRecentPrice.max) / 2;
if (recommendedIndexPrice < minimumPrice) {
return minimumPrice;
}

return (mostRecentPrice.avg + mostRecentPrice.max) / 2;
return recommendedIndexPrice;
}

0 comments on commit 476180c

Please sign in to comment.