'use strict';

/**
 * DEFAULT_TOKENS — the symbol → mint map that TRADING_PAIRS is resolved against.
 *
 * ─────────────────────────────────────────────────────────────────────────────
 * NFR-20 (TECH_SPEC_V5 §6) — STANDING PROCESS REQUIREMENT, NOT A ONE-TIME CHECK
 *
 * Any token added here must have its mint address verified against an official
 * source — not from memory, not from a single unverified source — BEFORE it is
 * committed. This codebase has already had one real near-miss with a malformed
 * hardcoded address (a Polygon USDT contract address caught during the USDT
 * payment work). Treat this class of mistake as a standing risk to guard
 * against deliberately.
 *
 * Every entry below records where its address was verified and when. If you add
 * a token, add its provenance too. An entry without provenance is a defect.
 * ─────────────────────────────────────────────────────────────────────────────
 */

const DEFAULT_TOKENS = {
  SOL: {
    // Wrapped SOL. Jupiter quotes SOL as wSOL; the Jupiter API unwraps
    // automatically when wrapAndUnwrapSol is set on the swap request.
    mint: 'So11111111111111111111111111111111111111112',
    decimals: 9,
    // Verified 2026-07-20 against:
    //   1. Jupiter token API — api.jup.ag/tokens/v2/search (isVerified: true,
    //      tags include "strict"/"verified", name "Wrapped SOL", decimals 9)
    //   2. Solana Explorer / well-known system address, cross-checked via the
    //      same token API's canonical record
    verified: '2026-07-20',
  },

  USDC: {
    mint: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
    decimals: 6,
    // Verified 2026-07-20 against two INDEPENDENT sources:
    //   1. Circle (the issuer) — developers.circle.com/stablecoins/usdc-contract-addresses
    //   2. Jupiter token API — api.jup.ag/tokens/v2/search (isVerified: true)
    verified: '2026-07-20',
  },

  USDT: {
    mint: 'Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB',
    decimals: 6,
    // Verified 2026-07-20 against two INDEPENDENT sources:
    //   1. Tether (the issuer) — tether.to/en/supported-protocols/, corroborated
    //      by Solana Explorer's token record for this mint
    //   2. Jupiter token API — api.jup.ag/tokens/v2/search (isVerified: true)
    verified: '2026-07-20',
  },

  JUP: {
    mint: 'JUPyiwrYJFskUPiHa7hkeR8VUtAeFoSYbKedZNsDvCN',
    decimals: 6,
    // Verified 2026-07-20 against two INDEPENDENT sources:
    //   1. Jupiter token API — api.jup.ag/tokens/v2/search (isVerified: true,
    //      name "Jupiter", official site jup.ag)
    //   2. Solana Explorer token metadata for this mint
    // NOTE: many knockoff tokens use the symbol JUP. This is the governance
    // token; do not resolve JUP by symbol lookup at runtime.
    verified: '2026-07-20',
  },
};

/** Base unit conversion. Returns a string — never a float, never rounded. */
function toBaseUnits(amount, decimals) {
  if (!Number.isFinite(amount) || amount < 0) {
    throw new Error(`toBaseUnits: invalid amount ${amount}`);
  }
  // Do the scaling in integer space to avoid float drift on values like 0.1.
  const [whole, frac = ''] = String(amount).split('.');
  const paddedFrac = frac.padEnd(decimals, '0').slice(0, decimals);
  return String(BigInt(whole) * 10n ** BigInt(decimals) + BigInt(paddedFrac || '0'));
}

/** Base units → human units, for display only. Never feed this back into a quote. */
function fromBaseUnits(baseUnits, decimals) {
  return Number(BigInt(baseUnits)) / 10 ** decimals;
}

module.exports = { DEFAULT_TOKENS, toBaseUnits, fromBaseUnits };
