"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 ensureAccountAddress_1 = require("../../../utils/environment/ensureAccountAddress");
const isAddress_1 = require("../../../utils/checks/isAddress");
const ensure_1 = require("../../../utils/guards/ensure");
const getPrices_1 = require("../calls/getPrices");
const Contracts_1 = require("../../../Contracts");
const transactionFactory_1 = require("../../../utils/solidity/transactionFactory");
const guard = (environment, _, contractAddress) => __awaiter(this, void 0, void 0, function* () {
    isAddress_1.ensureAddress(contractAddress);
    ensureAccountAddress_1.ensureAccountAddress(environment);
    // TODO: check if given price is against quote
});
const prepareArgs = (_, prices) => __awaiter(this, void 0, void 0, function* () {
    return [
        prices.map(p => p.base.token.address),
        prices.map(p => `${token_math_1.toAtomic(p)}`),
    ];
});
const postProcess = (environment, _, prices, contractAddress) => __awaiter(this, void 0, void 0, function* () {
    const updatedPrices = yield getPrices_1.getPrices(environment, contractAddress, prices.map(p => p.base.token));
    ensure_1.ensure(token_math_1.isEqual(updatedPrices[0], prices[0]), 'Price did not update', {
        is: JSON.stringify(updatedPrices[0]),
        should: JSON.stringify(prices[0]),
    });
    return updatedPrices;
});
exports.update = transactionFactory_1.transactionFactory('update', Contracts_1.Contracts.TestingPriceFeed, guard, prepareArgs, postProcess);
