fix: stop Surplus offer at provider spend cap
This commit is contained in:
@@ -262,6 +262,7 @@ export class SurplusSellerController {
|
||||
costMultiplierPpm: quote.costMultiplierPpm,
|
||||
inputMicroUsdPer1m: quote.inputMicroUsdPer1m,
|
||||
outputMicroUsdPer1m: quote.outputMicroUsdPer1m,
|
||||
baselineRemainingUsdNanos: allowance.remainingUsdNanos.toString(),
|
||||
dailyCapUsd: Number(this.config.perOfferCapUsd),
|
||||
idempotencyKey: this.mutationId(),
|
||||
createdAt: new Date(this.now()).toISOString(),
|
||||
@@ -445,6 +446,28 @@ export class SurplusSellerController {
|
||||
allowance,
|
||||
parseUsdDecimal(this.config.perOfferCapUsd, "ceil"),
|
||||
);
|
||||
const baselineAllowance = BigInt(
|
||||
currentState.intent.baselineRemainingUsdNanos,
|
||||
);
|
||||
const providerSpend = baselineAllowance > allowance.remainingUsdNanos
|
||||
? baselineAllowance - allowance.remainingUsdNanos
|
||||
: 0n;
|
||||
const spendCap = parseUsdDecimal(this.config.perOfferCapUsd, "ceil");
|
||||
if (providerSpend >= spendCap) {
|
||||
const stopping: SurplusControllerState = {
|
||||
version: 1,
|
||||
phase: "stopping",
|
||||
offerId: currentState.offerId,
|
||||
intent: currentState.intent,
|
||||
highestTrades24h: currentState.highestTrades24h,
|
||||
tradeObservedAt: currentState.intent.createdAt,
|
||||
pauseIdempotencyKey: this.mutationId(),
|
||||
};
|
||||
await this.stateStore.save(stopping);
|
||||
this.state = stopping;
|
||||
await this.finishStopping();
|
||||
return;
|
||||
}
|
||||
const discovered = await this.client.discoverModels(
|
||||
this.config.providerApiKey,
|
||||
this.config.providerBaseUrl,
|
||||
|
||||
@@ -18,6 +18,7 @@ export interface SurplusOfferIntent {
|
||||
inputMicroUsdPer1m: number;
|
||||
outputMicroUsdPer1m: number;
|
||||
dailyCapUsd: number;
|
||||
baselineRemainingUsdNanos: string;
|
||||
idempotencyKey: string;
|
||||
createdAt: string;
|
||||
baselineTrades24h: number;
|
||||
@@ -283,6 +284,10 @@ function parseIntent(value: unknown): SurplusOfferIntent {
|
||||
intent.dailyCapUsd,
|
||||
"Surplus state intent.dailyCapUsd",
|
||||
),
|
||||
baselineRemainingUsdNanos: nonNegativeIntegerString(
|
||||
intent.baselineRemainingUsdNanos,
|
||||
"Surplus state intent.baselineRemainingUsdNanos",
|
||||
),
|
||||
idempotencyKey: nonEmptyString(
|
||||
intent.idempotencyKey,
|
||||
"Surplus state intent.idempotencyKey",
|
||||
@@ -309,6 +314,13 @@ function nonEmptyString(value: unknown, field: string): string {
|
||||
return value;
|
||||
}
|
||||
|
||||
function nonNegativeIntegerString(value: unknown, field: string): string {
|
||||
if (typeof value !== "string" || !/^(?:0|[1-9]\d*)$/.test(value)) {
|
||||
throw new Error(`${field} must be a non-negative integer string`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function isoTimestamp(value: unknown, field: string): string {
|
||||
const timestamp = nonEmptyString(value, field);
|
||||
if (!Number.isFinite(Date.parse(timestamp))) {
|
||||
|
||||
Reference in New Issue
Block a user