import { estimateChain138DodoLiquidityUsd } from './chain138-dodo-liquidity';

describe('estimateChain138DodoLiquidityUsd', () => {
  it('values Chain 138 stable-to-stable DODO pools with token decimals', () => {
    const result = estimateChain138DodoLiquidityUsd({
      token0Address: '0xf22258f57794CC8E06237084b353Ab30fFfa640b',
      token1Address: '0x71D6687F38b93CCad569Fa6352c876eea967201b',
      reserve0: 999_999_997_998n,
      reserve1: 999_999_997_998n,
    });

    expect(result.reserve0Usd).toBeCloseTo(999_999.997998, 6);
    expect(result.reserve1Usd).toBeCloseTo(999_999.997998, 6);
    expect(result.totalLiquidityUsd).toBeCloseTo(1_999_999.995996, 6);
  });

  it('values WETH/stable DODO pools with stable decimals and oracle price', () => {
    const result = estimateChain138DodoLiquidityUsd({
      token0Address: '0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f',
      token1Address: '0x004b63A7B5b0E06f6bB6adb4a5F9f590BF3182D1',
      reserve0: 50n * 10n ** 18n,
      reserve1: 105_830n * 10n ** 6n,
      price: 2_100n * 10n ** 18n,
    });

    expect(result.reserve0Usd).toBe(105_000);
    expect(result.reserve1Usd).toBe(105_830);
    expect(result.totalLiquidityUsd).toBe(210_830);
  });

  it('keeps WETH9 on the ETH peg even when live oracle price is unavailable', () => {
    const result = estimateChain138DodoLiquidityUsd({
      token0Address: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
      token1Address: '0x004b63A7B5b0E06f6bB6adb4a5F9f590BF3182D1',
      reserve0: 10n * 10n ** 18n,
      reserve1: 24_900n * 10n ** 6n,
    });

    expect(result.reserve0Usd).toBe(24_900);
    expect(result.reserve1Usd).toBe(24_900);
    expect(result.totalLiquidityUsd).toBe(49_800);
  });

  it('values non-USD canonical pairs from their repo-local peg references', () => {
    const result = estimateChain138DodoLiquidityUsd({
      token0Address: '0xf4BB2e28688e89fCcE3c0580D37d36A7672E8A9f',
      token1Address: '0x290E52a8819A4fbD0714E517225429aA2B70EC6b',
      reserve0: 10n * 10n ** 18n,
      reserve1: 5n * 10n ** 6n,
    });

    expect(result.reserve0Usd).toBe(24_900);
    expect(result.reserve1Usd).toBeCloseTo(25_816.700630164178, 6);
    expect(result.totalLiquidityUsd).toBeCloseTo(50_716.70063016418, 6);
  });

  it('values XAU/stable DODO pools from the canonical gold peg', () => {
    const result = estimateChain138DodoLiquidityUsd({
      token0Address: '0x290E52a8819A4fbD0714E517225429aA2B70EC6b',
      token1Address: '0x004b63A7B5b0E06f6bB6adb4a5F9f590BF3182D1',
      reserve0: 5n * 10n ** 6n,
      reserve1: 25_816n * 10n ** 6n,
    });

    expect(result.reserve0Usd).toBeCloseTo(25_816.700630164178, 6);
    expect(result.reserve1Usd).toBe(25_816);
    expect(result.totalLiquidityUsd).toBeCloseTo(51_632.70063016418, 6);
  });

  it('values cBTC/stable DODO pools using satoshi precision and the BTC fallback price', () => {
    const result = estimateChain138DodoLiquidityUsd({
      token0Address: '0xcb7c000000000000000000000000000000000138',
      token1Address: '0x004b63A7B5b0E06f6bB6adb4a5F9f590BF3182D1',
      reserve0: 2n * 10n ** 8n,
      reserve1: 181_000n * 10n ** 6n,
    });

    expect(result.reserve0Usd).toBe(180_000);
    expect(result.reserve1Usd).toBe(181_000);
    expect(result.totalLiquidityUsd).toBe(361_000);
  });
});
