"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 order_utils_1 = require("@0x/order-utils");
const web3Utils = __importStar(require("web3-utils"));
const token_math_1 = require("@melonproject/token-math");
const transactionFactory_1 = require("../../../../utils/solidity/transactionFactory");
const getExchangeIndex_1 = require("../calls/getExchangeIndex");
const getToken_1 = require("../../../dependencies/token/calls/getToken");
const Contracts_1 = require("../../../../Contracts");
const FunctionSignatures_1 = require("../utils/FunctionSignatures");
const fillOrder_1 = require("../../../exchanges/third-party/0x/transactions/fillOrder");
const getFeeToken_1 = require("../../../exchanges/third-party/0x/calls/getFeeToken");
const emptyAddress_1 = require("../../../../utils/constants/emptyAddress");
const guard = () => __awaiter(this, void 0, void 0, function* () {
    /* TODO:
    - [ ] Check if takerQuantity is the token in signedOrder
    - [ ] Check policy (policyManager.methods.preValidate)
  
    See: makeOasisDexOrder.ts
    */
});
const prepareArgs = (environment, { signedOrder, takerQuantity: providedTakerQuantity }, contractAddress) => __awaiter(this, void 0, void 0, function* () {
    const exchangeIndex = yield getExchangeIndex_1.getExchangeIndex(environment, contractAddress, {
        exchange: Contracts_1.Exchanges.ZeroEx,
    });
    const makerTokenAddress = order_utils_1.assetDataUtils.decodeERC20AssetData(signedOrder.makerAssetData).tokenAddress;
    const takerTokenAddress = order_utils_1.assetDataUtils.decodeERC20AssetData(signedOrder.takerAssetData).tokenAddress;
    const takerToken = yield getToken_1.getToken(environment, takerTokenAddress);
    const takerQuantity = providedTakerQuantity ||
        token_math_1.createQuantity(takerToken, signedOrder.takerAssetAmount.toString());
    const args = [
        exchangeIndex,
        FunctionSignatures_1.FunctionSignatures.takeOrder,
        [
            signedOrder.makerAddress.toString(),
            emptyAddress_1.emptyAddress /*contractAddress.toString() */,
            makerTokenAddress,
            takerTokenAddress,
            signedOrder.feeRecipientAddress,
            emptyAddress_1.emptyAddress,
        ],
        [
            signedOrder.makerAssetAmount.toFixed(),
            signedOrder.takerAssetAmount.toFixed(),
            signedOrder.makerFee.toFixed(),
            signedOrder.takerFee.toFixed(),
            signedOrder.expirationTimeSeconds.toFixed(),
            signedOrder.salt.toFixed(),
            takerQuantity.quantity.toString(),
            0,
        ],
        web3Utils.padLeft('0x0', 64),
        signedOrder.makerAssetData,
        signedOrder.takerAssetData,
        signedOrder.signature,
    ];
    return args;
});
const postProcess = (environment, receipt) => __awaiter(this, void 0, void 0, function* () {
    const zeroExAddress = environment.deployment.exchangeConfigs[Contracts_1.Exchanges.ZeroEx].exchange;
    const feeToken = yield getFeeToken_1.getFeeToken(environment, zeroExAddress);
    const fillValues = receipt.events.Fill.returnValues;
    const result = yield fillOrder_1.parse0xFillReceipt(environment, {
        feeToken,
        fillValues,
    });
    return result;
});
const take0xOrder = transactionFactory_1.transactionFactory('callOnExchange', Contracts_1.Contracts.Trading, guard, prepareArgs, postProcess);
exports.take0xOrder = take0xOrder;
