"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("@polkadot/api");
const index_1 = require("../../../../../src/index");
const bitcoin_core_client_1 = require("../../../../../src/utils/bitcoin-core-client");
const factory_1 = require("../../../../../src/factory");
const config_1 = require("../../../../config");
const chai_1 = require("../../../../chai");
const issueRedeem_1 = require("../../../../../src/utils/issueRedeem");
const src_1 = require("../../../../../src");
const monetary_js_1 = require("@interlay/monetary-js");
describe("refund", () => {
    let api;
    let bitcoinCoreClient;
    let keyring;
    let userAccount;
    let vault_3;
    let vault_3_ids;
    let wrappedCurrency;
    let interBtcAPI;
    let assetRegistry;
    before(function () {
        return __awaiter(this, void 0, void 0, function* () {
            api = yield factory_1.createSubstrateAPI(config_1.PARACHAIN_ENDPOINT);
            keyring = new api_1.Keyring({ type: "sr25519" });
            bitcoinCoreClient = new bitcoin_core_client_1.BitcoinCoreClient(config_1.BITCOIN_CORE_NETWORK, config_1.BITCOIN_CORE_HOST, config_1.BITCOIN_CORE_USERNAME, config_1.BITCOIN_CORE_PASSWORD, config_1.BITCOIN_CORE_PORT, config_1.BITCOIN_CORE_WALLET);
            userAccount = keyring.addFromUri(config_1.USER_1_URI);
            assetRegistry = new index_1.DefaultAssetRegistryAPI(api);
            interBtcAPI = new index_1.DefaultInterBtcApi(api, "regtest", userAccount, config_1.ESPLORA_BASE_PATH);
            const collateralCurrencies = index_1.getCorrespondingCollateralCurrencies(interBtcAPI.getGovernanceCurrency());
            wrappedCurrency = interBtcAPI.getWrappedCurrency();
            vault_3 = keyring.addFromUri(config_1.VAULT_3_URI);
            vault_3_ids = collateralCurrencies.map((collateralCurrency) => src_1.newVaultId(api, vault_3.address, collateralCurrency, wrappedCurrency));
        });
    });
    after(() => __awaiter(void 0, void 0, void 0, function* () {
        api.disconnect();
    }));
    it("should not generate a refund request", () => __awaiter(void 0, void 0, void 0, function* () {
        for (const vault_3_id of vault_3_ids) {
            const currencyTicker = (yield index_1.currencyIdToMonetaryCurrency(assetRegistry, vault_3_id.currencies.collateral))
                .ticker;
            const issueResult = yield issueRedeem_1.issueSingle(interBtcAPI, bitcoinCoreClient, userAccount, index_1.newMonetaryAmount(0.00005, wrappedCurrency, true), vault_3_id, false, false);
            chai_1.assert.isRejected(interBtcAPI.refund.getRequestByIssueId(issueResult.request.id), `Expected rejection for refund request with vault 3 (${currencyTicker})`);
        }
    })).timeout(2000000);
    it("should generate a refund request", function () {
        return __awaiter(this, void 0, void 0, function* () {
            if (interBtcAPI.getGovernanceCurrency().ticker === monetary_js_1.Interlay.ticker) {
                // Skip this test when running the INTR parachain (gov currency: INTR).
                // Vaults have had too much collateral, resulting in issuing more IBTC rather than
                // refunding the overpaid amount. So we had false negative failed tests.
                this.skip();
            }
            const refundRequestsBefore = yield interBtcAPI.refund.list();
            for (const vault_3_id of vault_3_ids) {
                const currencyTicker = (yield index_1.currencyIdToMonetaryCurrency(assetRegistry, vault_3_id.currencies.collateral))
                    .ticker;
                const issueResult = yield issueRedeem_1.issueSingle(interBtcAPI, bitcoinCoreClient, userAccount, index_1.newMonetaryAmount(0.000012, wrappedCurrency, true), vault_3_id, true, true);
                const refund = yield interBtcAPI.refund.getRequestByIssueId(issueResult.request.id);
                chai_1.assert.notEqual(refund.amountBtc.toString(), "0", `Expected non-zero amount for refund request with vault 3 (${currencyTicker})`);
            }
            const refundRequestsAfter = yield interBtcAPI.refund.list();
            const countOfNewRefundRequests = refundRequestsAfter.length - refundRequestsBefore.length;
            chai_1.assert.equal(countOfNewRefundRequests, vault_3_ids.length, `Expected to find ${vault_3_ids.length} new refund requests, but found ${countOfNewRefundRequests}`);
        });
    }).timeout(2000000);
});
//# sourceMappingURL=refund.test.js.map