import fs from "fs";
import axios from "axios";
import bs58check from "bs58check";
import path from "path";
import { signedList, whitelist } from "./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.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";
export const importTRC10Tokens = async (outputDir) => {
    console.log("importing trc10 tokens...");
    const allTokens = [];
    const { data } = await axios.get(TRONGRID_TOKENS_URL);
    allTokens.push(...data.data);
    let nextLink = data.meta.links?.next;
    while (nextLink) {
        const { data } = await axios.get(nextLink);
        allTokens.push(...data.data);
        nextLink = data.meta.links?.next;
    }
    const tokens = allTokens
        .map(token => {
        const tokenSignature = signedList.find(signature => signature.id === token.id);
        const ledgerSignature = tokenSignature?.message;
        if (!ledgerSignature)
            return null;
        const delisted = !whitelist.some(id => id === token.id);
        return convertTRC10({ ...token, delisted, ledgerSignature });
    })
        .filter((token) => !!token);
    const filePath = path.join(outputDir, "trc10");
    fs.writeFileSync(`${filePath}.json`, JSON.stringify(tokens));
    fs.writeFileSync(`${filePath}.ts`, ts);
    console.log(`importing trc10 tokens success, imported ${tokens.length} tokens`);
};
//# sourceMappingURL=index.js.map