"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 monetary_js_1 = require("@interlay/monetary-js");
const factory_1 = require("../../../../../src/factory");
const chai_1 = require("../../../../chai");
const config_1 = require("../../../../config");
const src_1 = require("../../../../../src");
const helpers_1 = require("../../../../utils/helpers");
describe("OracleAPI", () => {
    let api;
    let interBtcAPI;
    let collateralCurrencies;
    let oracleAccount;
    let aliceAccount;
    let bobAccount;
    let charlieAccount;
    before(() => __awaiter(void 0, void 0, void 0, function* () {
        api = yield factory_1.createSubstrateAPI(config_1.PARACHAIN_ENDPOINT);
        const ss58Prefix = src_1.getSS58Prefix(api);
        const keyring = new api_1.Keyring({ type: "sr25519", ss58Format: ss58Prefix });
        oracleAccount = keyring.addFromUri(config_1.ORACLE_URI);
        aliceAccount = keyring.addFromUri("//Alice");
        bobAccount = keyring.addFromUri("//Bob");
        charlieAccount = keyring.addFromUri("//Charlie");
        interBtcAPI = new src_1.DefaultInterBtcApi(api, "regtest", oracleAccount, config_1.ESPLORA_BASE_PATH);
        collateralCurrencies = src_1.getCorrespondingCollateralCurrencies(interBtcAPI.getGovernanceCurrency());
    }));
    after(() => {
        return api.disconnect();
    });
    it("should set exchange rate", () => __awaiter(void 0, void 0, void 0, function* () {
        for (const collateralCurrency of collateralCurrencies) {
            const exchangeRateValue = helpers_1.getExchangeRateValueToSetForTesting(collateralCurrency);
            const newExchangeRate = new monetary_js_1.ExchangeRate(monetary_js_1.Bitcoin, collateralCurrency, exchangeRateValue);
            yield interBtcAPI.oracle.setExchangeRate(newExchangeRate);
            yield interBtcAPI.oracle.waitForExchangeRateUpdate(newExchangeRate);
        }
    }));
    it("should convert satoshi to collateral currency", () => __awaiter(void 0, void 0, void 0, function* () {
        for (const collateralCurrency of collateralCurrencies) {
            const bitcoinAmount = new monetary_js_1.BitcoinAmount(100);
            const exchangeRate = yield interBtcAPI.oracle.getExchangeRate(collateralCurrency);
            const expectedCollateral = exchangeRate.toBig().mul(bitcoinAmount.toBig(monetary_js_1.Bitcoin.decimals)).round(0, 0);
            const collateralAmount = yield interBtcAPI.oracle.convertWrappedToCurrency(bitcoinAmount, collateralCurrency);
            chai_1.assert.equal(collateralAmount.toBig(collateralCurrency.decimals).round(0, 0).toString(), expectedCollateral.toString(), `Unexpected collateral (${collateralCurrency.ticker}) amount`);
        }
    }));
    it("should get names by id", () => __awaiter(void 0, void 0, void 0, function* () {
        const expectedSources = new Map();
        expectedSources.set(aliceAccount.address, "Alice");
        expectedSources.set(bobAccount.address, "Bob");
        expectedSources.set(charlieAccount.address, "Charlie");
        const sources = yield interBtcAPI.oracle.getSourcesById();
        for (const entry of sources.entries()) {
            chai_1.assert.equal(entry[1], expectedSources.get(entry[0]));
        }
    }));
    it("should getOnlineTimeout", () => __awaiter(void 0, void 0, void 0, function* () {
        const onlineTimeout = yield interBtcAPI.oracle.getOnlineTimeout();
        const expectedOnlineTimeout = helpers_1.ORACLE_MAX_DELAY;
        chai_1.assert.equal(onlineTimeout, expectedOnlineTimeout);
    }));
    it("should getValidUntil", () => __awaiter(void 0, void 0, void 0, function* () {
        for (const collateralCurrency of collateralCurrencies) {
            const validUntil = yield interBtcAPI.oracle.getValidUntil(collateralCurrency);
            const dateAnHourFromNow = new Date();
            dateAnHourFromNow.setMinutes(dateAnHourFromNow.getMinutes() + 30);
            chai_1.assert.isTrue(validUntil > dateAnHourFromNow, `lastExchangeRateTime is older than one hour (${collateralCurrency.ticker})`);
        }
    }));
    it("should be online", () => __awaiter(void 0, void 0, void 0, function* () {
        const isOnline = yield interBtcAPI.oracle.isOnline();
        chai_1.assert.isTrue(isOnline);
    }));
});
//# sourceMappingURL=oracle.test.js.map