"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.importTRC10Tokens = void 0;
const fs_1 = __importDefault(require("fs"));
const axios_1 = __importDefault(require("axios"));
const bs58check_1 = __importDefault(require("bs58check"));
const path_1 = __importDefault(require("path"));
const trc10_tokens_1 = require("./trc10-tokens");
const ts = `export type TRC10Token = [
  number, // id
  string, // abbr
  string, // name
  string, // contractAddress
  number, // precision
  boolean, // delisted
  string, // ledgerSignature
  boolean? // enableCountervalues
];

import tokens from "./trc10.json";

export default tokens as TRC10Token[];
`;
const b58 = (hex) => bs58check_1.default.encode(Buffer.from(hex, "hex"));
const convertTRC10 = ({ name, abbr, id, owner_address, precision, delisted, ledgerSignature, }) => [
    id,
    abbr.toUpperCase(),
    name,
    b58(owner_address),
    precision || 0,
    delisted,
    ledgerSignature,
];
const TRONGRID_TOKENS_URL = "https://api.trongrid.io/v1/assets?limit=200";
const importTRC10Tokens = async (outputDir) => {
    console.log("importing trc10 tokens...");
    const allTokens = [];
    const { data } = await axios_1.default.get(TRONGRID_TOKENS_URL);
    allTokens.push(...data.data);
    let nextLink = data.meta.links?.next;
    while (nextLink) {
        const { data } = await axios_1.default.get(nextLink);
        allTokens.push(...data.data);
        nextLink = data.meta.links?.next;
    }
    const tokens = allTokens
        .map(token => {
        const tokenSignature = trc10_tokens_1.signedList.find(signature => signature.id === token.id);
        const ledgerSignature = tokenSignature?.message;
        if (!ledgerSignature)
            return null;
        const delisted = !trc10_tokens_1.whitelist.some(id => id === token.id);
        return convertTRC10({ ...token, delisted, ledgerSignature });
    })
        .filter((token) => !!token);
    const filePath = path_1.default.join(outputDir, "trc10");
    fs_1.default.writeFileSync(`${filePath}.json`, JSON.stringify(tokens));
    fs_1.default.writeFileSync(`${filePath}.ts`, ts);
    console.log(`importing trc10 tokens success, imported ${tokens.length} tokens`);
};
exports.importTRC10Tokens = importTRC10Tokens;
//# sourceMappingURL=index.js.map