"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const bignumber_js_1 = require("bignumber.js");
const currencies_1 = require("../currencies");
const erc20_1 = require("@ledgerhq/hw-app-eth/erc20");
/*
  skipped because ledgerjs data is now lighter (from the POV of hw-app-eth)
  (using cryptoassets-evm-signatures instead of cryptoassets)
*/
test.skip("erc20 are all consistent with those on ledgerjs side", () => {
    const normalList = (0, currencies_1.listTokens)();
    const delistedList = (0, currencies_1.listTokens)({
        withDelisted: true,
    });
    expect(delistedList.length).toBeGreaterThan(normalList.length);
    for (const token of delistedList) {
        if (token.delisted) {
            expect(normalList.find(o => o.id === token.id)).toBeUndefined();
        }
        if (token.tokenType === "erc20") {
            if (token.parentCurrency.family === "filecoin") {
                continue;
            }
            const tokenData = (0, erc20_1.byContractAddressAndChainId)(token.contractAddress, token.parentCurrency.ethereumLikeInfo?.chainId || 0);
            if (!tokenData) {
                throw new Error(token.name + " not available in ledgerjs data");
            }
            expect(token.ticker.toLowerCase()).toBe(tokenData.ticker.toLowerCase());
            expect(token.contractAddress.toLowerCase()).toBe(tokenData.contractAddress.toLowerCase());
            expect(token.units[0].magnitude).toBe(tokenData.decimals);
        }
    }
});
test("can format a currency unit", () => {
    const btc = (0, currencies_1.getCryptoCurrencyById)("bitcoin").units[0];
    expect((0, currencies_1.formatCurrencyUnit)(btc, new bignumber_js_1.BigNumber(100000000))).toBe("1");
    expect((0, currencies_1.formatCurrencyUnit)(btc, new bignumber_js_1.BigNumber(1000000), {
        showCode: true,
    })).toBe("0.01 BTC");
    expect((0, currencies_1.formatCurrencyUnit)(btc, new bignumber_js_1.BigNumber(100000000), {
        showCode: true,
    })).toBe("1 BTC");
    expect((0, currencies_1.formatCurrencyUnit)(btc, new bignumber_js_1.BigNumber(100000000), {
        showCode: true,
        showAllDigits: true,
    })).toBe("1.00000000 BTC");
    expect((0, currencies_1.formatCurrencyUnit)(btc, new bignumber_js_1.BigNumber(100000000), {
        showCode: true,
        showAllDigits: true,
        alwaysShowSign: true,
    })).toBe("+1.00000000 BTC");
});
test("do not consider 'showAllDigits: undefined' as false", () => {
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getFiatCurrencyByTicker)("USD").units[0], new bignumber_js_1.BigNumber(-1234500), {
        showCode: true,
        showAllDigits: undefined,
    })).toBe("-$12,345.00");
});
test("can enable discreet mode", () => {
    const btc = (0, currencies_1.getCryptoCurrencyById)("bitcoin").units[0];
    expect((0, currencies_1.formatCurrencyUnit)(btc, new bignumber_js_1.BigNumber(100000000), {
        discreet: true,
    })).toBe("***");
    expect((0, currencies_1.formatCurrencyUnit)(btc, new bignumber_js_1.BigNumber(1000000), {
        discreet: true,
        showCode: true,
    })).toBe("*** BTC");
    expect((0, currencies_1.formatCurrencyUnit)(btc, new bignumber_js_1.BigNumber(100000000), {
        discreet: true,
        showCode: true,
    })).toBe("*** BTC");
    expect((0, currencies_1.formatCurrencyUnit)(btc, new bignumber_js_1.BigNumber(100000000), {
        discreet: true,
        showCode: true,
        showAllDigits: true,
    })).toBe("*** BTC");
    expect((0, currencies_1.formatCurrencyUnit)(btc, new bignumber_js_1.BigNumber(100000000), {
        discreet: true,
        showCode: true,
        showAllDigits: true,
        alwaysShowSign: true,
    })).toBe("+*** BTC");
});
test("formatter will floor values by default", () => {
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getCryptoCurrencyById)("bitcoin").units[0], new bignumber_js_1.BigNumber(1000001), {})).toBe("0.01");
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getCryptoCurrencyById)("bitcoin").units[0], new bignumber_js_1.BigNumber(1000001))).toBe("0.01");
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getCryptoCurrencyById)("bitcoin").units[0], new bignumber_js_1.BigNumber(1000100))).toBe("0.010001");
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getCryptoCurrencyById)("bitcoin").units[0], new bignumber_js_1.BigNumber("999999999999"))).toBe("9,999.99");
});
test("formatter rounding can be disabled", () => {
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getCryptoCurrencyById)("bitcoin").units[0], new bignumber_js_1.BigNumber("999999999999"), {
        disableRounding: true,
    })).toBe("9,999.99999999");
});
test("sub magnitude", () => {
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getFiatCurrencyByTicker)("USD").units[0], new bignumber_js_1.BigNumber(0.04), {
        subMagnitude: 2,
    })).toBe("0.0004");
    // digits will be round after subMagnitude
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getFiatCurrencyByTicker)("USD").units[0], new bignumber_js_1.BigNumber(0.03987654), {
        subMagnitude: 2,
        showCode: true,
    })).toBe("$0.0003");
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getFiatCurrencyByTicker)("USD").units[0], new bignumber_js_1.BigNumber(0.03987654), {
        subMagnitude: 2,
        disableRounding: true,
    })).toBe("0.0003");
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getFiatCurrencyByTicker)("USD").units[0], new bignumber_js_1.BigNumber(0.03987654), {
        subMagnitude: 5,
        disableRounding: true,
    })).toBe("0.0003987");
    // even tho the USD unit showAllDigits, it does not force the sub magnitude digits to show
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getFiatCurrencyByTicker)("USD").units[0], new bignumber_js_1.BigNumber(0.03), {
        subMagnitude: 5,
        disableRounding: true,
    })).toBe("0.0003");
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getCryptoCurrencyById)("bitcoin").units[0], new bignumber_js_1.BigNumber(9.123456), {
        subMagnitude: 2,
        disableRounding: true,
    })).toBe("0.0000000912");
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getCryptoCurrencyById)("bitcoin").units[0], new bignumber_js_1.BigNumber("999999999999.123456"), {
        disableRounding: true,
        subMagnitude: 2,
    })).toBe("9,999.9999999912");
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getCryptoCurrencyById)("bitcoin").units[0], new bignumber_js_1.BigNumber("999999999999.123456"), {
        subMagnitude: 2,
    })).toBe("9,999.99");
});
test("parseCurrencyUnit", () => {
    expect((0, currencies_1.parseCurrencyUnit)((0, currencies_1.getCryptoCurrencyById)("bitcoin").units[0], "9999.99999999").toNumber()).toBe(999999999999);
    expect((0, currencies_1.parseCurrencyUnit)((0, currencies_1.getCryptoCurrencyById)("bitcoin").units[0], ".987654").toNumber()).toBe(98765400);
    expect((0, currencies_1.parseCurrencyUnit)((0, currencies_1.getCryptoCurrencyById)("bitcoin").units[0], "9999").toNumber()).toBe(999900000000);
    expect((0, currencies_1.parseCurrencyUnit)((0, currencies_1.getCryptoCurrencyById)("bitcoin").units[0], "1").toNumber()).toBe(100000000);
    /*expect(
      parseCurrencyUnit(getCryptoCurrencyById("bitcoin").units[0], "0x1").toNumber()
    ).toBe(0);*/
    expect((0, currencies_1.parseCurrencyUnit)((0, currencies_1.getCryptoCurrencyById)("bitcoin").units[0], "NOPE").toNumber()).toBe(0);
});
test("formatter works with fiats", () => {
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getFiatCurrencyByTicker)("EUR").units[0], new bignumber_js_1.BigNumber(12345), {
        showCode: true,
    })).toBe("€123.45");
    // by default, fiats always show the digits
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getFiatCurrencyByTicker)("EUR").units[0], new bignumber_js_1.BigNumber(12300))).toBe("123.00");
});
test("formatter useGrouping", () => {
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getFiatCurrencyByTicker)("EUR").units[0], new bignumber_js_1.BigNumber(1234500), {
        useGrouping: true,
    })).toBe("12,345.00");
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getFiatCurrencyByTicker)("EUR").units[0], new bignumber_js_1.BigNumber(1234500), {
        useGrouping: false,
    })).toBe("12345.00");
});
test("formatter can change locale", () => {
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getFiatCurrencyByTicker)("USD").units[0], new bignumber_js_1.BigNumber(-1234567), {
        showCode: true,
    })).toBe("-$12,345.67");
});
test("formatter does not show very small value in rounding mode", () => {
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getCryptoCurrencyById)("ethereum").units[0], new bignumber_js_1.BigNumber(1))).toBe("0");
    expect((0, currencies_1.formatCurrencyUnit)((0, currencies_1.getCryptoCurrencyById)("ethereum").units[0], new bignumber_js_1.BigNumber(1000))).toBe("0");
});
test("formatShort", () => {
    expect((0, currencies_1.formatShort)((0, currencies_1.getFiatCurrencyByTicker)("EUR").units[0], new bignumber_js_1.BigNumber(123456789))).toBe("1.2m");
    expect((0, currencies_1.formatShort)((0, currencies_1.getFiatCurrencyByTicker)("EUR").units[0], new bignumber_js_1.BigNumber(123456))).toBe("1.2k");
    expect((0, currencies_1.formatShort)((0, currencies_1.getCryptoCurrencyById)("ethereum").units[0], new bignumber_js_1.BigNumber(600000))).toBe("0");
});
test("encodeURIScheme", () => {
    expect((0, currencies_1.encodeURIScheme)({
        currency: (0, currencies_1.getCryptoCurrencyById)("bitcoin"),
        address: "1gre1noAY9HiK2qxoW8FzSdjdFBcoZ5fV",
    })).toBe("bitcoin:1gre1noAY9HiK2qxoW8FzSdjdFBcoZ5fV");
    expect((0, currencies_1.encodeURIScheme)({
        currency: (0, currencies_1.getCryptoCurrencyById)("bitcoin"),
        address: "1gre1noAY9HiK2qxoW8FzSdjdFBcoZ5fV",
        amount: new bignumber_js_1.BigNumber("1234567000000"),
    })).toBe("bitcoin:1gre1noAY9HiK2qxoW8FzSdjdFBcoZ5fV?amount=12345.67");
});
test("decodeURIScheme", () => {
    expect((0, currencies_1.decodeURIScheme)("bitcoin:1gre1noAY9HiK2qxoW8FzSdjdFBcoZ5fV")).toMatchObject({
        currency: (0, currencies_1.getCryptoCurrencyById)("bitcoin"),
        address: "1gre1noAY9HiK2qxoW8FzSdjdFBcoZ5fV",
    });
    expect((0, currencies_1.decodeURIScheme)("bitcoin:1gre1noAY9HiK2qxoW8FzSdjdFBcoZ5fV?amount=12345.67")).toMatchObject({
        currency: (0, currencies_1.getCryptoCurrencyById)("bitcoin"),
        address: "1gre1noAY9HiK2qxoW8FzSdjdFBcoZ5fV",
        amount: new bignumber_js_1.BigNumber("1234567000000"),
    });
    expect((0, currencies_1.decodeURIScheme)("ethereum:0x931d387731bbbc988b312206c74f77d004d6b84b?gas=100&gasPrice=200&value=" + 10 ** 18)).toMatchObject({
        currency: (0, currencies_1.getCryptoCurrencyById)("ethereum"),
        address: "0x931d387731bbbc988b312206c74f77d004d6b84b",
        amount: new bignumber_js_1.BigNumber(10 ** 18),
        userGasLimit: new bignumber_js_1.BigNumber(100),
        gasPrice: new bignumber_js_1.BigNumber(200),
    });
    expect((0, currencies_1.decodeURIScheme)("ethereum:0x931d387731bbbc988b312206c74f77d004d6b84b?gas=-1&gasPrice=-1&value=-1")).toMatchObject({
        currency: (0, currencies_1.getCryptoCurrencyById)("ethereum"),
        address: "0x931d387731bbbc988b312206c74f77d004d6b84b",
        amount: new bignumber_js_1.BigNumber(0),
        userGasLimit: new bignumber_js_1.BigNumber(0),
        gasPrice: new bignumber_js_1.BigNumber(0),
    });
    expect((0, currencies_1.decodeURIScheme)("ethereum:0x072b04a9b047C3c7a2A455FFF5264D785e6E55C9?amount=0.0647&gasPrice=77000000000")).toMatchObject({
        currency: (0, currencies_1.getCryptoCurrencyById)("ethereum"),
        address: "0x072b04a9b047C3c7a2A455FFF5264D785e6E55C9",
        amount: new bignumber_js_1.BigNumber(0.0647).times(10 ** 18),
        gasPrice: new bignumber_js_1.BigNumber(77000000000),
    });
});
test("sanitizeValueString", () => {
    const bitcoin = (0, currencies_1.getCryptoCurrencyById)("bitcoin");
    const btcUnit = bitcoin.units[0];
    const satUnit = bitcoin.units[bitcoin.units.length - 1];
    expect((0, currencies_1.sanitizeValueString)(btcUnit, "")).toMatchObject({
        display: "",
    });
    expect((0, currencies_1.sanitizeValueString)(btcUnit, "123456")).toMatchObject({
        display: "123456",
        value: "12345600000000",
    });
    expect((0, currencies_1.sanitizeValueString)(btcUnit, "1")).toMatchObject({
        display: "1",
        value: "100000000",
    });
    expect((0, currencies_1.sanitizeValueString)(btcUnit, "1.00")).toMatchObject({
        display: "1.00",
        value: "100000000",
    });
    expect((0, currencies_1.sanitizeValueString)(btcUnit, ".00")).toMatchObject({
        display: "0.00",
    });
    expect((0, currencies_1.sanitizeValueString)(btcUnit, ".1")).toMatchObject({
        display: "0.1",
    });
    expect((0, currencies_1.sanitizeValueString)(btcUnit, ".123456789")).toMatchObject({
        display: "0.12345678",
    });
    expect((0, currencies_1.sanitizeValueString)(btcUnit, "1ab")).toMatchObject({
        display: "1",
    });
    expect((0, currencies_1.sanitizeValueString)(btcUnit, "1,3")).toMatchObject({
        display: "1.3",
    });
    expect((0, currencies_1.sanitizeValueString)(btcUnit, "1 300")).toMatchObject({
        display: "1300",
    });
    expect((0, currencies_1.sanitizeValueString)(btcUnit, "13.")).toMatchObject({
        display: "13.",
        value: "1300000000",
    });
    expect((0, currencies_1.sanitizeValueString)(satUnit, "13.")).toMatchObject({
        display: "13",
        value: "13",
    });
    expect((0, currencies_1.sanitizeValueString)(btcUnit, "000.12345678")).toMatchObject({
        display: "0.12345678",
        value: "12345678",
    });
    expect((0, currencies_1.sanitizeValueString)(btcUnit, "001.23456789")).toMatchObject({
        display: "1.23456789",
        value: "123456789",
    });
});
//# sourceMappingURL=currencies.js.map