"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 beginSetup_1 = require("../../contracts/factory/transactions/beginSetup");
const getAmguToken_1 = require("../../contracts/engine/calls/getAmguToken");
const getPrices_1 = require("../../contracts/prices/calls/getPrices");
const update_1 = require("../../contracts/prices/transactions/update");
const setAmguPrice_1 = require("../../contracts/engine/transactions/setAmguPrice");
const getAmguPrice_1 = require("../../contracts/engine/calls/getAmguPrice");
const signTransaction_1 = require("../../utils/environment/signTransaction");
const deployAndInitTestEnv_1 = require("../utils/deployAndInitTestEnv");
const Environment_1 = require("../../utils/environment/Environment");
const shared = {};
beforeAll(() => __awaiter(this, void 0, void 0, function* () {
    shared.env = yield deployAndInitTestEnv_1.deployAndInitTestEnv();
    shared.accounts = yield shared.env.eth.getAccounts();
}));
const randomString = (length = 4) => Math.random()
    .toString(36)
    .substr(2, length);
test('Set amgu and check its usage', () => __awaiter(this, void 0, void 0, function* () {
    const fundName = `test-fund-${randomString()}`;
    const { exchangeConfigs, melonContracts, thirdPartyContracts, } = shared.env.deployment;
    const [quoteToken, baseToken] = thirdPartyContracts.tokens;
    const defaultTokens = [quoteToken, baseToken];
    const fees = [];
    const amguToken = yield getAmguToken_1.getAmguToken(shared.env, melonContracts.version);
    const amguPrice = token_math_1.createQuantity(amguToken, '1000000000');
    const oldAmguPrice = yield getAmguPrice_1.getAmguPrice(shared.env, melonContracts.engine);
    const newAmguPrice = yield setAmguPrice_1.setAmguPrice(shared.env, melonContracts.engine, amguPrice);
    expect(token_math_1.isEqual(newAmguPrice, amguPrice)).toBe(true);
    expect(token_math_1.isEqual(newAmguPrice, oldAmguPrice)).toBe(false);
    const args = {
        defaultTokens,
        exchangeConfigs,
        fees,
        fundName,
        nativeToken: quoteToken,
        priceSource: melonContracts.priceSource,
        quoteToken,
    };
    if (shared.env.track === Environment_1.Tracks.TESTING) {
        const newPrice = token_math_1.createPrice(token_math_1.createQuantity(baseToken, '1'), token_math_1.createQuantity(quoteToken, '2'));
        yield update_1.update(shared.env, melonContracts.priceSource, [newPrice]);
        const [price] = yield getPrices_1.getPrices(shared.env, melonContracts.priceSource, [
            baseToken,
        ]);
        expect(token_math_1.isEqual(price, newPrice)).toBe(true);
    }
    const prepared = yield beginSetup_1.beginSetup.prepare(shared.env, melonContracts.version, args);
    const preBalance = yield shared.env.eth.getBalance(shared.accounts[0]);
    const signedTransactionData = yield signTransaction_1.signTransaction(shared.env, prepared.rawTransaction);
    const result = yield beginSetup_1.beginSetup.send(shared.env, melonContracts.version, signedTransactionData, args, undefined);
    const postBalance = yield shared.env.eth.getBalance(shared.accounts[0]);
    const diffQ = token_math_1.subtract(token_math_1.toBI(preBalance), token_math_1.toBI(postBalance));
    expect(result).toBeTruthy();
    expect(token_math_1.greaterThan(diffQ, new token_math_1.BigInteger(prepared.rawTransaction.gas))).toBe(true);
}));
