File size: 1,790 Bytes
6111b2b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | /**
* Token sharing — transfer tokens between API keys.
*
* @module lib/gamification/sharing
*/
import crypto from "crypto";
/**
* Transfer tokens from one API key to another.
* Uses double-entry ledger with idempotency key.
*/
export async function transferTokens(
fromApiKeyId: string,
toApiKeyId: string,
amount: number,
reason?: string,
idempotencyKey?: string
): Promise<{ success: boolean; idempotencyKey: string; error?: string }> {
if (fromApiKeyId === toApiKeyId) {
return { success: false, idempotencyKey: "", error: "Cannot transfer to yourself" };
}
if (amount <= 0) {
return { success: false, idempotencyKey: "", error: "Amount must be positive" };
}
const key = idempotencyKey || crypto.randomUUID();
try {
const { transferTokens: dbTransfer } = await import("../db/gamification");
const result = dbTransfer(fromApiKeyId, toApiKeyId, amount, reason || "transfer", key);
if (!result.success) {
return { success: false, idempotencyKey: key, error: result.error };
}
return { success: true, idempotencyKey: key };
} catch (err: unknown) {
const message = err instanceof Error ? err.message : "Unknown error";
return { success: false, idempotencyKey: key, error: message };
}
}
/**
* Get token balance for an API key.
*/
export async function getBalance(apiKeyId: string): Promise<number> {
const { getBalance: dbGetBalance } = await import("../db/gamification");
return dbGetBalance(apiKeyId);
}
/**
* Get transfer history for an API key.
*/
export async function getHistory(apiKeyId: string, limit: number = 20) {
const { getHistory: dbGetHistory } = await import("../db/gamification");
return dbGetHistory(apiKeyId, limit);
}
|