"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const token_math_1 = require("@melonproject/token-math");
const callFactory_1 = require("../../../utils/solidity/callFactory");
const Contracts_1 = require("../../../Contracts");
const getContract_1 = require("../../../utils/solidity/getContract");
const getToken_1 = require("../../dependencies/token/calls/getToken");
const isEmptyAddress_1 = require("../../../utils/checks/isEmptyAddress");
const postProcess = (environment, result, prepared) => __awaiter(this, void 0, void 0, function* () {
    try {
        const registryContract = yield getContract_1.getContract(environment, Contracts_1.Contracts.Registry, prepared.contractAddress);
        const mlnAddress = yield registryContract.methods.mlnToken().call();
        const mlnToken = isEmptyAddress_1.isEmptyAddress(mlnAddress)
            ? undefined
            : yield getToken_1.getToken(environment, mlnAddress);
        const engine = isEmptyAddress_1.isEmptyAddress(result) ? undefined : new token_math_1.Address(result);
        const ethfinexWrapperRegistry = yield registryContract.methods
            .ethfinexWrapperRegistry()
            .call();
        const priceSource = yield registryContract.methods.priceSource().call();
        const registryInformation = {
            engine,
            ethfinexWrapperRegistry: isEmptyAddress_1.isEmptyAddress(ethfinexWrapperRegistry)
                ? undefined
                : new token_math_1.Address(ethfinexWrapperRegistry),
            mlnToken,
            priceSource: isEmptyAddress_1.isEmptyAddress(priceSource)
                ? undefined
                : new token_math_1.Address(priceSource),
            registeredAssets: {},
            registeredExchanges: {},
            registeredVersions: {},
        };
        const registeredAssets = yield registryContract.methods
            .getRegisteredAssets()
            .call();
        const registeredExchanges = yield registryContract.methods
            .getRegisteredExchanges()
            .call();
        const registeredVersions = yield registryContract.methods
            .getRegisteredVersions()
            .call();
        for (const asset of registeredAssets) {
            const assetInfo = yield registryContract.methods
                .assetInformation(asset)
                .call();
            registryInformation.registeredAssets[asset.toLowerCase()] = {
                address: asset,
                decimals: assetInfo.decimals,
                name: assetInfo.name,
                sigs: assetInfo.sigs,
                standards: assetInfo.standards,
                symbol: assetInfo.symbol,
                url: assetInfo.url,
            };
        }
        for (const exchange of registeredExchanges) {
            const exchangeInfo = yield registryContract.methods
                .exchangeInformation(exchange)
                .call();
            registryInformation.registeredExchanges[exchange.toLowerCase()] = {
                adapter: new token_math_1.Address(exchangeInfo.adapter),
                address: new token_math_1.Address(exchange),
                sigs: exchangeInfo.sigs,
                takesCustody: exchangeInfo.takesCustody,
            };
        }
        for (const version of registeredVersions) {
            const versionInfo = yield registryContract.methods
                .versionInformation(version)
                .call();
            registryInformation.registeredVersions[version.toLowerCase()] = {
                address: new token_math_1.Address(version),
                name: versionInfo.name,
            };
        }
        return registryInformation;
    }
    catch (error) {
        throw new Error(`getRegistryInformation failed: ${error.message}`);
    }
});
const getRegistryInformation = callFactory_1.callFactoryWithoutParams('engine', Contracts_1.Contracts.Registry, {
    postProcess,
});
exports.getRegistryInformation = getRegistryInformation;
