"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.importEVMTokens = exports.importTokenByChainId = void 0;
const path_1 = __importDefault(require("path"));
const promises_1 = __importDefault(require("fs/promises"));
const fs_1 = require("fs");
const fetch_1 = require("../../fetch");
const utils_1 = require("../../utils");
const LIMIT_TOKENS_BY_CHAIN = 100;
const mustHaveForTests = [
    "0xe41d2489571d322189246dafa5ebde1f4699f498",
    "0xE41d2489571d322189246DaFA5ebDe1F4699F498",
    "0xdac17f958d2ee523a2206206994597c13d831ec7",
    "0xdAC17F958D2ee523a2206206994597C13D831ec7",
];
const chainNames = new Map();
const importTokenByChainId = (outputDir, chainId, tokensOrderedByMarketCap) => __awaiter(void 0, void 0, void 0, function* () {
    var _a;
    try {
        console.log(`importing chain with chainId: ${chainId}...`);
        const { tokens, hash } = yield (0, fetch_1.fetchTokensFromCALService)({
            chainId,
            standard: chainId === 56 ? "bep20" : "erc20",
        }, [
            "blockchain_name",
            "id",
            "ticker",
            "decimals",
            "name",
            "live_signature",
            "contract_address",
            "delisted",
        ]);
        if (!tokens[0]) {
            console.warn(`no tokens found for chainId: ${chainId}`);
            return;
        }
        const chainName = (_a = tokens[0]) === null || _a === void 0 ? void 0 : _a.blockchain_name;
        const topTokensByMarketCap = tokensOrderedByMarketCap
            .filter(token => token.startsWith(chainName))
            .slice(0, LIMIT_TOKENS_BY_CHAIN);
        if (!chainName) {
            console.warn(`no chainName found for chainId: ${chainId}`);
        }
        const tokensFiltered = tokens.filter((token) => {
            return (topTokensByMarketCap.includes(token.id) || mustHaveForTests.includes(token.contract_address));
        });
        const { erc20, erc20Signatures } = (0, utils_1.getErc20DescriptorsAndSignatures)(tokensFiltered, chainId);
        const indexTsStringified = `import { ERC20Token } from "../../../types";
import erc20 from "./erc20.json";
export const tokens = erc20 as ERC20Token[];
export { default as signatures } from "./erc20-signatures.json";
${hash ? `export { default as hash } from "./erc20-hash.json";` : ""}
`;
        if ((erc20 === null || erc20 === void 0 ? void 0 : erc20.length) && (erc20Signatures === null || erc20Signatures === void 0 ? void 0 : erc20Signatures.length)) {
            const dirPath = path_1.default.join(outputDir, "evm", chainId.toString());
            if (!(0, fs_1.existsSync)(dirPath)) {
                yield promises_1.default.mkdir(dirPath, { recursive: true });
            }
            const [currencyName] = erc20[0];
            chainNames.set(chainId, currencyName);
            yield promises_1.default.writeFile(path_1.default.join(dirPath, "erc20.json"), JSON.stringify(erc20));
            if (hash) {
                yield promises_1.default.writeFile(path_1.default.join(dirPath, "erc20-hash.json"), JSON.stringify(hash));
            }
            yield promises_1.default.writeFile(path_1.default.join(dirPath, "erc20-signatures.json"), JSON.stringify(erc20Signatures.toString("base64")));
            yield promises_1.default.writeFile(path_1.default.join(dirPath, "index.ts"), indexTsStringified);
            console.log(`importing chain with chainId: ${chainId} (${currencyName}) success`);
        }
    }
    catch (err) {
        console.error(err);
    }
});
exports.importTokenByChainId = importTokenByChainId;
const importEVMTokens = (outputDir) => __awaiter(void 0, void 0, void 0, function* () {
    console.log("Importing evm tokens... --------");
    const { tokens } = yield (0, fetch_1.fetchTokensOrderedByMarketCap)();
    const supportedChainIds = [
        1, 10, 14, 19, 25, 30, 40, 42, 56, 57, 61, 106, 137, 199, 246, 250, 288, 592, 1088, 1101, 1284,
        1285, 1442, 8217, 8453, 17000, 42161, 43114, 59141, 59144, 81457, 84532, 421614, 534351, 534352,
        11155111, 11155420, 168587773, 245022934,
    ];
    yield Promise.allSettled(supportedChainIds.map(chainId => (0, exports.importTokenByChainId)(outputDir, chainId, tokens)));
    const rootIndexStringified = `import { ERC20Token } from "../../types";

${supportedChainIds
        .map(chainId => chainNames.has(chainId)
        ? `import ${chainNames.get(chainId)}_tokens from "./${chainId}/erc20.json"`
        : null)
        .filter(Boolean)
        .join(";" + String.fromCharCode(10))};

${supportedChainIds
        .map(chainId => chainNames.has(chainId) &&
        (0, fs_1.existsSync)(path_1.default.join(outputDir, "evm", chainId.toString(), "erc20-hash.json"))
        ? `import ${chainNames.get(chainId)}_tokens_hash from "./${chainId}/erc20-hash.json"`
        : null)
        .filter(Boolean)
        .join(";" + String.fromCharCode(10))};

${supportedChainIds
        .map(chainId => chainNames.has(chainId)
        ? `import ${chainNames.get(chainId)}_signatures from "./${chainId}/erc20-signatures.json"`
        : null)
        .filter(Boolean)
        .join(";" + String.fromCharCode(10))};

export const tokens = {
${supportedChainIds
        .map(chainId => chainNames.has(chainId)
        ? `  ${chainId}: ${chainNames.get(chainId)}_tokens as ERC20Token[]`
        : null)
        .filter(Boolean)
        .join("," + String.fromCharCode(10))},
};

export const signatures = {
${supportedChainIds
        .map(chainId => chainNames.has(chainId) ? `  ${chainId}: ${chainNames.get(chainId)}_signatures` : null)
        .filter(Boolean)
        .join("," + String.fromCharCode(10))},
};

export const hashes = {
${supportedChainIds
        .map(chainId => chainNames.has(chainId) &&
        (0, fs_1.existsSync)(path_1.default.join(outputDir, "evm", chainId.toString(), "erc20-hash.json"))
        ? `  ${chainId}: ${chainNames.get(chainId)}_tokens_hash`
        : null)
        .filter(Boolean)
        .join("," + String.fromCharCode(10))},
};

export default {
  tokens,
  signatures,
  hashes,
};
`;
    yield promises_1.default.writeFile(`${outputDir}/evm/index.ts`, rootIndexStringified);
    console.log("Importing evm tokens success");
});
exports.importEVMTokens = importEVMTokens;
//# sourceMappingURL=index.js.map