Skip to main content

Balance Enforcement & Immutable Ledger

Every financial operation through a GpkeysBot API key is strictly backed by the owning user's USDT balance and logged to an append-only SQL ledger.

How Balance Enforcement Works​

1. Pre-Execution Balance Check​

On every authenticated /api/v1/* request, after verifying the key and user account, the backend reads the user's live balances.available balance before executing the handler.

2. Fast-Fail + Row-Level Atomic Deduction​

Before charging on POST /api/v1/purchases or POST /api/v1/actions:

  1. The server computes the authoritative cost from the database (products.priceUsdt * quantity or server action tariff)—client-supplied prices are never trusted.
  2. If cached/live pre-check balance is below cost, the server rejects immediately with 402 Payment Required (INSUFFICIENT_BALANCE) and writes a BALANCE_REJECTED audit row.
  3. Inside db.$transaction, the API acquires a PostgreSQL row-level exclusive lock on the user's wallet (SELECT available, version FROM balances WHERE "userId" = $1 FOR UPDATE).
  4. Holding that lock, it re-verifies live key ACTIVE status, non-expiration, user ACTIVE status, perRequestCap, monthlySpendCap, and balanceAfter >= 0.
  5. Because concurrent requests targeting the same account wait on the same balances row lock, parallel requests cannot overspend the balance or exceed monthlySpendCap.

3. Decimal Precision Rules​

  • All monetary quantities use 6 decimal places in USDT (for example "48.500000" or "-12.000000").
  • Treat monetary values as exact strings or use a decimal library (decimal.js, Python Decimal, PHP bcmath)—never native IEEE-754 floating-point math.

Ledger Entries (GET /api/v1/ledger)​

Every charge creates an immutable row in ledger_transactions (enforced by a database BEFORE UPDATE OR DELETE trigger):

FieldExampleMeaning
id"cld_01abc"Unique ledger transaction identifier.
userId"usr_01xyz"Owning customer account ID.
apiKeyId"key_01def"API key that triggered the charge.
requestType"PRODUCT_PURCHASE"Charge classification (PRODUCT_PURCHASE, BULK_CATALOG_EXPORT, STOCK_RESERVATION_PING, etc.).
type"PURCHASE"Financial direction (PURCHASE is strictly negative < 0; credits are positive).
amount"-12.000000"Signed transaction amount in USDT.
balanceAfter"36.500000"Account balance immediately after applying this entry.
referenceType"ORDER"Linked business entity type (ORDER, API_ACTION, etc.).
referenceId"ord_01uvw"ID of the resulting order or scoped action reference.
createdAt"2026-11-30T18:10:00.000Z"UTC timestamp of charge commit.

Use GET /api/v1/balance to check live funds and GET /api/v1/ledger (optionally with ?keyOnly=true) to audit charges.