"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 withNewAccount_1 = require("~/utils/environment/withNewAccount");
const token_math_1 = require("@melonproject/token-math");
const sendEth_1 = require("~/utils/evm/sendEth");
const setupInvestedTestFund_1 = require("../utils/setupInvestedTestFund");
const deposit_1 = require("~/contracts/dependencies/token/transactions/deposit");
const getTokenBySymbol_1 = require("~/utils/environment/getTokenBySymbol");
const toBeTrueWith_1 = require("../utils/toBeTrueWith");
const getSystemTestEnvironment_1 = require("../utils/getSystemTestEnvironment");
const Environment_1 = require("~/utils/environment/Environment");
const createOrder_1 = require("~/contracts/exchanges/third-party/0x/utils/createOrder");
const signOrder_1 = require("~/contracts/exchanges/third-party/0x/utils/signOrder");
const setEthfinexWrapperRegistry_1 = require("~/contracts/version/transactions/setEthfinexWrapperRegistry");
const getWrapperLock_1 = require("~/contracts/exchanges/third-party/ethfinex/calls/getWrapperLock");
const isValidSignature_1 = require("~/contracts/exchanges/third-party/0x/calls/isValidSignature");
const makeEthfinexOrder_1 = require("~/contracts/fund/trading/transactions/makeEthfinexOrder");
const Contracts_1 = require("~/Contracts");
expect.extend({ toBeTrueWith: toBeTrueWith_1.toBeTrueWith });
describe('playground', () => {
    test('Happy path', () => __awaiter(this, void 0, void 0, function* () {
        const master = yield getSystemTestEnvironment_1.getSystemTestEnvironment(Environment_1.Tracks.KYBER_PRICE);
        const manager = yield withNewAccount_1.withNewAccount(master);
        const weth = getTokenBySymbol_1.getTokenBySymbol(master, 'WETH');
        const mln = getTokenBySymbol_1.getTokenBySymbol(master, 'MLN');
        const ethfinex = manager.deployment.exchangeConfigs[Contracts_1.Exchanges.Ethfinex].exchange;
        const wrapperRegistryEFX = manager.deployment.thirdPartyContracts.exchanges.ethfinex
            .wrapperRegistryEFX;
        const wethWrapperLock = yield getWrapperLock_1.getWrapperLock(master, wrapperRegistryEFX, {
            token: weth,
        });
        yield sendEth_1.sendEth(master, {
            howMuch: token_math_1.createQuantity('ETH', 2),
            to: manager.wallet.address,
        });
        const quantity = token_math_1.createQuantity(weth, 1);
        yield deposit_1.deposit(manager, quantity.token.address, undefined, {
            value: quantity.quantity.toString(),
        });
        const routes = yield setupInvestedTestFund_1.setupInvestedTestFund(manager);
        yield setEthfinexWrapperRegistry_1.setEthfinexWrapperRegistry(master, manager.deployment.melonContracts.registry, {
            address: wrapperRegistryEFX,
        });
        const makerQuantity = token_math_1.createQuantity(wethWrapperLock, 0.05);
        const takerQuantity = token_math_1.createQuantity(mln, 1);
        const unsignedEthfinexOrder = yield createOrder_1.createOrder(manager, ethfinex, {
            makerAddress: routes.tradingAddress,
            makerQuantity,
            takerQuantity,
        });
        const signedOrder = yield signOrder_1.signOrder(manager, unsignedEthfinexOrder);
        const isSignatureValidBefore = yield isValidSignature_1.isValidSignature(manager, ethfinex, {
            signedOrder,
        });
        const result = yield makeEthfinexOrder_1.makeEthfinexOrder(manager, routes.tradingAddress, {
            signedOrder,
        });
        const isSignatureValidAfter = yield isValidSignature_1.isValidSignature(manager, ethfinex, {
            signedOrder,
        });
        expect(result).toBeTruthy();
        expect(isSignatureValidBefore).toBeFalsy();
        expect(isSignatureValidAfter).toBeTruthy();
    }));
});
