"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 initTestEnvironment_1 = require("../../../../tests/utils/initTestEnvironment");
const getContract_1 = require("../../../../utils/solidity/getContract");
const deployContract_1 = require("../../../../utils/solidity/deployContract");
const deploy_1 = require("../../../dependencies/token/transactions/deploy");
const deployVaultFactory_1 = require("../transactions/deployVaultFactory");
const createVaultInstance_1 = require("../transactions/createVaultInstance");
const Contracts_1 = require("../../../../Contracts");
describe('withdraw', () => {
    const shared = {};
    beforeAll(() => __awaiter(this, void 0, void 0, function* () {
        shared.env = yield initTestEnvironment_1.initTestEnvironment();
        const tokenAddress = yield deploy_1.deployToken(shared.env);
        shared.token = getContract_1.getContract(shared.env, Contracts_1.Contracts.PreminedToken, tokenAddress);
        shared.factoryAddress = yield deployVaultFactory_1.deployVaultFactory(shared.env);
        shared.authAddress = yield deployContract_1.deployContract(shared.env, Contracts_1.Contracts.PermissiveAuthority);
    }));
    beforeEach(() => __awaiter(this, void 0, void 0, function* () {
        const vaultAddress = yield createVaultInstance_1.createVaultInstance(shared.env, shared.factoryAddress, {
            hubAddress: shared.authAddress,
        });
        shared.vault = getContract_1.getContract(shared.env, Contracts_1.Contracts.Vault, vaultAddress);
    }));
    it('withdraw token that is not present', () => __awaiter(this, void 0, void 0, function* () {
        yield expect(shared.vault.methods
            .withdraw(shared.token.options.address, 100)
            .send({ from: shared.env.wallet.address })).rejects.toThrow();
    }));
    it('withdraw available token', () => __awaiter(this, void 0, void 0, function* () {
        const amount = 100000;
        const amountInWalletPre = Number(yield shared.token.methods.balanceOf(shared.env.wallet.address).call());
        yield shared.token.methods
            .transfer(shared.vault.options.address, amount)
            .send({ from: shared.env.wallet.address });
        let amountInVault = Number(yield shared.token.methods.balanceOf(shared.vault.options.address).call());
        yield expect(amountInVault).toBe(amount);
        yield shared.vault.methods
            .withdraw(shared.token.options.address, amount)
            .send({ from: shared.env.wallet.address });
        amountInVault = Number(yield shared.token.methods.balanceOf(shared.vault.options.address).call());
        const amountInWalletPost = Number(yield shared.token.methods.balanceOf(shared.env.wallet.address).call());
        yield expect(amountInVault).toBe(0);
        yield expect(amountInWalletPost).toBe(amountInWalletPre);
    }));
});
