"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 getEngine_1 = require("../../version/calls/getEngine");
const getAmguPrice_1 = require("./getAmguPrice");
const getPrices_1 = require("../../prices/calls/getPrices");
const getPriceSource_1 = require("./getPriceSource");
const getAmguToken_1 = require("./getAmguToken");
const calcAmguInEth = (environment, contractAddress, gasEstimation) => __awaiter(this, void 0, void 0, function* () {
    const amguToken = yield getAmguToken_1.getAmguToken(environment, contractAddress);
    const engineAddress = yield getEngine_1.getEngine(environment, contractAddress);
    const priceSourceAddress = yield getPriceSource_1.getPriceSource(environment, contractAddress);
    const mlnPerAmgu = yield getAmguPrice_1.getAmguPrice(environment, engineAddress);
    const ethPerMln = yield getPrices_1.getPrices(environment, priceSourceAddress, [
        amguToken,
    ]);
    return token_math_1.createQuantity('ETH', token_math_1.multiply(new token_math_1.BigInteger(token_math_1.toAtomic(ethPerMln[0])), new token_math_1.BigInteger(mlnPerAmgu.quantity), new token_math_1.BigInteger(gasEstimation)).slice(0, -18));
});
exports.calcAmguInEth = calcAmguInEth;
