"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 chai_1 = require("chai");
const electrs_1 = require("../../../../src/external/electrs");
const factory_1 = require("../../../../src/factory");
const config_1 = require("../../../config");
const bitcoin_core_client_1 = require("../../../../src/utils/bitcoin-core-client");
const monetary_js_1 = require("@interlay/monetary-js");
const helpers_1 = require("../../../utils/helpers");
describe("ElectrsAPI regtest", function () {
    this.timeout(100000);
    let api;
    let electrsAPI;
    let bitcoinCoreClient;
    before(() => __awaiter(this, void 0, void 0, function* () {
        api = yield factory_1.createSubstrateAPI(config_1.PARACHAIN_ENDPOINT);
        electrsAPI = new electrs_1.DefaultElectrsAPI(config_1.ESPLORA_BASE_PATH);
        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);
    }));
    after(() => __awaiter(this, void 0, void 0, function* () {
        yield api.disconnect();
    }));
    it("should getTxIdByRecipientAddress", () => __awaiter(this, void 0, void 0, function* () {
        yield helpers_1.runWhileMiningBTCBlocks(bitcoinCoreClient, () => __awaiter(this, void 0, void 0, function* () {
            const recipientAddress = helpers_1.makeRandomBitcoinAddress();
            const amount = monetary_js_1.BitcoinAmount.from.BTC(0.00022244);
            const txData = yield bitcoinCoreClient.broadcastTx(recipientAddress, amount);
            const txid = yield helpers_1.wait_success(() => electrsAPI.getEarliestPaymentToRecipientAddressTxId(recipientAddress, amount));
            chai_1.assert.strictEqual(txid, txData.txid);
        }));
    }));
    it("should getTxByOpreturn", () => __awaiter(this, void 0, void 0, function* () {
        yield helpers_1.runWhileMiningBTCBlocks(bitcoinCoreClient, () => __awaiter(this, void 0, void 0, function* () {
            const opReturnValue = "01234567891154267bf7d05901cc8c2f647414a42126c3aee89e01a2c905ae91";
            const recipientAddress = helpers_1.makeRandomBitcoinAddress();
            const amount = monetary_js_1.BitcoinAmount.from.BTC(0.00029);
            const txData = yield bitcoinCoreClient.broadcastTx(recipientAddress, amount, opReturnValue);
            const txid = yield helpers_1.wait_success(() => electrsAPI.getTxIdByOpReturn(opReturnValue, recipientAddress, amount));
            chai_1.assert.strictEqual(txid, txData.txid);
        }));
    }));
    it("should use getTxStatus to return correct confirmations", () => __awaiter(this, void 0, void 0, function* () {
        yield helpers_1.runWhileMiningBTCBlocks(bitcoinCoreClient, () => __awaiter(this, void 0, void 0, function* () {
            const opReturnValue = "01234567891154267bf7d05901cc8c2f647414a42126c3aee89e01a2c905ae91";
            const recipientAddress = helpers_1.makeRandomBitcoinAddress();
            const amount = monetary_js_1.BitcoinAmount.from.BTC(0.00029);
            const txData = yield bitcoinCoreClient.broadcastTx(recipientAddress, amount, opReturnValue);
            // transaction in mempool
            let status = yield electrsAPI.getTransactionStatus(txData.txid);
            chai_1.assert.strictEqual(status.confirmations, 0);
            // transaction in the latest block
            yield helpers_1.wait_success(() => __awaiter(this, void 0, void 0, function* () {
                status = yield electrsAPI.getTransactionStatus(txData.txid);
                chai_1.assert.strictEqual(status.confirmations, 1);
            }));
            // transaction in the parent of the latest block
            yield helpers_1.wait_success(() => __awaiter(this, void 0, void 0, function* () {
                status = yield electrsAPI.getTransactionStatus(txData.txid);
                chai_1.assert.strictEqual(status.confirmations, 2);
            }));
        }));
    }));
});
//# sourceMappingURL=electrs.test.js.map