"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 getContract_1 = require("../../../../utils/solidity/getContract");
const Contracts_1 = require("../../../../Contracts");
const callFactory_1 = require("../../../../utils/solidity/callFactory");
const getLogCurried_1 = require("../../../../utils/environment/getLogCurried");
const getLog = getLogCurried_1.getLogCurried('melon:protocol:contracts:token:getInfo');
const postProcess = (environment, totalSupply, prepared) => __awaiter(this, void 0, void 0, function* () {
    const log = getLog(environment);
    const fromDeployment = (environment.deployment &&
        environment.deployment.thirdPartyContracts.tokens.find(t => t.address.toLowerCase() === prepared.contractAddress.toLowerCase())) ||
        {};
    log.debug('Token info from deployment', fromDeployment);
    try {
        const contract = getContract_1.getContract(environment, Contracts_1.Contracts.PreminedToken, prepared.contractAddress);
        const info = {
            decimals: fromDeployment.decimals ||
                parseInt(yield contract.methods.decimals().call(), 10),
            name: fromDeployment.name || (yield contract.methods.name().call()),
            symbol: fromDeployment.symbol || (yield contract.methods.symbol().call()),
            totalSupply,
        };
        return info;
    }
    catch (error) {
        throw new Error(`getInfo failed for token with address: ${prepared.contractAddress}: ${error.message}`);
    }
});
exports.getInfo = callFactory_1.callFactoryWithoutParams('totalSupply', Contracts_1.Contracts.PreminedToken, {
    postProcess,
});
