"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());
    });
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const web3Utils = __importStar(require("web3-utils"));
const Contracts_1 = require("../../../../Contracts");
const getExchangeIndex_1 = require("../calls/getExchangeIndex");
const emptyAddress_1 = require("../../../../utils/constants/emptyAddress");
const transactionFactory_1 = require("../../../../utils/solidity/transactionFactory");
const getHub_1 = require("../../hub/calls/getHub");
const getRoutes_1 = require("../../hub/calls/getRoutes");
const ensureSufficientBalance_1 = require("../../../dependencies/token/guards/ensureSufficientBalance");
const ensureFundOwner_1 = require("../guards/ensureFundOwner");
const ensureIsNotShutDown_1 = require("../../hub/guards/ensureIsNotShutDown");
const ensureTakePermitted_1 = require("../guards/ensureTakePermitted");
const FunctionSignatures_1 = require("../utils/FunctionSignatures");
const guard = (environment, { makerQuantity, takerQuantity, fillTakerQuantity = takerQuantity }, contractAddress) => __awaiter(this, void 0, void 0, function* () {
    const hubAddress = yield getHub_1.getHub(environment, contractAddress);
    const { vaultAddress } = yield getRoutes_1.getRoutes(environment, hubAddress);
    yield ensureSufficientBalance_1.ensureSufficientBalance(environment, takerQuantity, vaultAddress);
    yield ensureFundOwner_1.ensureFundOwner(environment, contractAddress);
    yield ensureIsNotShutDown_1.ensureIsNotShutDown(environment, hubAddress);
    yield ensureTakePermitted_1.ensureTakePermitted(environment, contractAddress, Contracts_1.Exchanges.KyberNetwork, makerQuantity, takerQuantity, fillTakerQuantity);
});
const prepareArgs = (environment, { makerQuantity, takerQuantity, fillTakerQuantity = takerQuantity }, contractAddress) => __awaiter(this, void 0, void 0, function* () {
    const exchangeIndex = yield getExchangeIndex_1.getExchangeIndex(environment, contractAddress, {
        exchange: Contracts_1.Exchanges.KyberNetwork,
    });
    const takerAsset = takerQuantity.token.address;
    const makerAsset = makerQuantity.token.address;
    const takerAssetAmount = takerQuantity.quantity.toString();
    const makerAssetAmount = makerQuantity.quantity.toString();
    const fillTakerAmount = fillTakerQuantity.quantity.toString();
    const args = [
        exchangeIndex,
        FunctionSignatures_1.FunctionSignatures.takeOrder,
        [
            emptyAddress_1.emptyAddress,
            emptyAddress_1.emptyAddress,
            makerAsset,
            takerAsset,
            emptyAddress_1.emptyAddress,
            emptyAddress_1.emptyAddress,
        ],
        [makerAssetAmount, takerAssetAmount, 0, 0, 0, 0, fillTakerAmount, 0],
        web3Utils.padLeft('0x0', 64),
        web3Utils.padLeft('0x0', 64),
        web3Utils.padLeft('0x0', 64),
        web3Utils.padLeft('0x0', 64),
    ];
    return args;
});
const takeOrderOnKyber = transactionFactory_1.transactionFactory('callOnExchange', Contracts_1.Contracts.Trading, guard, prepareArgs);
exports.takeOrderOnKyber = takeOrderOnKyber;
