"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddressAggregator = exports.adapters = void 0;
const tslib_1 = require("tslib");
const ethers_1 = require("ethers");
const utils_1 = require("../utils");
const types_1 = require("./types");
const adapters = tslib_1.__importStar(require("./adapters"));
exports.adapters = tslib_1.__importStar(require("./adapters"));
class AddressAggregator {
    adapters;
    logger;
    constructor(adapters, logger) {
        this.adapters = adapters;
        this.logger = logger;
    }
    static sources() {
        return Object.keys(adapters);
    }
    async update() {
        this.logger?.debug({
            at: "AddressAggregator::update",
            message: "Updating addresses.",
            sources: this.adapters.map((adapter) => adapter.name),
            supportedSources: AddressAggregator.sources(),
        });
        const allAddresses = await (0, utils_1.mapAsync)(this.adapters, async (adapter) => {
            const invalidAddresses = [];
            const addresses = (await adapter.update())
                .map((address) => {
                try {
                    return ethers_1.utils.getAddress(address.toLowerCase());
                }
                catch {
                    invalidAddresses.push(address);
                    return types_1.INVALID_ADDRESS;
                }
            })
                .filter((address) => address !== types_1.INVALID_ADDRESS);
            if (invalidAddresses.length > 0) {
                this.logger?.warn({
                    at: "AddressAggregator::update()",
                    message: `Read ${invalidAddresses.length} malformed addresses on ${adapter.name}.`,
                    invalidAddresses,
                });
            }
            this.logger?.debug({
                at: "AddressAggregator::update",
                message: `Loaded ${addresses.length} addresses from ${adapter.name}.`,
            });
            return addresses;
        });
        const addresses = new Set(allAddresses.flat());
        this.logger?.debug({
            at: "AddressAggregator::update",
            message: `Loaded ${addresses.size} addresses.`,
            sources: this.adapters.map((adapter) => adapter.name),
        });
        return addresses;
    }
}
exports.AddressAggregator = AddressAggregator;
async function run() {
    const addressList = new AddressAggregator([new adapters.risklabs.AddressList({ throwOnError: true })]);
    const addresses = await addressList.update();
    console.log(`Retrieved ${addresses.size} addresses: ${JSON.stringify(Array.from(addresses), null, 2)}`);
    return 0;
}
if (require.main === module) {
    run()
        .then((result) => {
        process.exitCode = result;
    })
        .catch((error) => {
        console.error("Process exited with", error);
        process.exitCode = 127;
    });
}
//# sourceMappingURL=index.js.map