"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("../../chai");
const big_js_1 = __importDefault(require("big.js"));
const sinon_1 = __importDefault(require("sinon"));
const src_1 = require("../../../src");
const monetary_js_1 = require("@interlay/monetary-js");
const vaultsTestMocks_1 = require("../mocks/vaultsTestMocks");
describe("DefaultVaultsAPI", () => {
    let vaultsApi;
    const testCollateralCurrency = monetary_js_1.Kusama;
    const testWrappedCurrency = monetary_js_1.KBtc;
    let stubbedRewardsApi;
    let stubbedTransactionApi;
    beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
        stubbedRewardsApi = sinon_1.default.createStubInstance(src_1.DefaultRewardsAPI);
        stubbedTransactionApi = sinon_1.default.createStubInstance(src_1.DefaultTransactionAPI);
        vaultsApi = new src_1.DefaultVaultsAPI(null, null, testWrappedCurrency, null, null, null, null, stubbedRewardsApi, null, stubbedTransactionApi, null, null);
    }));
    afterEach(() => {
        sinon_1.default.restore();
        sinon_1.default.reset();
    });
    describe("backingCollateralProportion", () => {
        it("should return 0 if nominator and vault have zero collateral", () => __awaiter(void 0, void 0, void 0, function* () {
            // prepare mocks
            const { nominatorId, vaultId } = vaultsTestMocks_1.prepareBackingCollateralProportionMocks(sinon_1.default, vaultsApi, stubbedRewardsApi, new big_js_1.default(0), new big_js_1.default(0), testCollateralCurrency);
            // do the thing
            const proportion = yield vaultsApi.backingCollateralProportion(vaultId, nominatorId, testCollateralCurrency);
            // check result
            const expectedProportion = new big_js_1.default(0);
            chai_1.assert.equal(proportion.toString(), expectedProportion.toString(), `Expected actual proportion to be ${expectedProportion.toString()} but it was ${proportion.toString()}`);
        }));
        it("should reject if nominator has collateral, but vault has zero collateral", () => __awaiter(void 0, void 0, void 0, function* () {
            // prepare mocks
            const nominatorAmount = new big_js_1.default(1);
            const vaultAmount = new big_js_1.default(0);
            const { nominatorId, vaultId } = vaultsTestMocks_1.prepareBackingCollateralProportionMocks(sinon_1.default, vaultsApi, stubbedRewardsApi, nominatorAmount, vaultAmount, testCollateralCurrency);
            // do & check
            const proportionPromise = vaultsApi.backingCollateralProportion(vaultId, nominatorId, testCollateralCurrency);
            chai_1.expect(proportionPromise).to.be.rejectedWith(Error);
        }));
        it("should calculate expected proportion", () => __awaiter(void 0, void 0, void 0, function* () {
            // prepare mocks
            const nominatorAmount = new big_js_1.default(1);
            const vaultAmount = new big_js_1.default(2);
            const { nominatorId, vaultId } = vaultsTestMocks_1.prepareBackingCollateralProportionMocks(sinon_1.default, vaultsApi, stubbedRewardsApi, nominatorAmount, vaultAmount, testCollateralCurrency);
            // do the thing
            const proportion = yield vaultsApi.backingCollateralProportion(vaultId, nominatorId, testCollateralCurrency);
            // check result
            const expectedProportion = nominatorAmount.div(vaultAmount);
            chai_1.assert.equal(proportion.toString(), expectedProportion.toString(), `Expected actual proportion to be ${expectedProportion.toString()} but it was ${proportion.toString()}`);
        }));
    });
    describe("registerNewCollateralVault", () => {
        const testCollateralAmount = src_1.newMonetaryAmount(new big_js_1.default(30), testCollateralCurrency);
        it("should reject if transaction API account id is not set", () => __awaiter(void 0, void 0, void 0, function* () {
            vaultsTestMocks_1.prepareRegisterNewCollateralVaultMocks(sinon_1.default, vaultsApi, stubbedTransactionApi, true);
            const voidPromise = vaultsApi.registerNewCollateralVault(testCollateralAmount);
            // check for partial string here
            chai_1.expect(voidPromise).to.be.rejectedWith("account must be set");
        }));
        it("should reject with same message if transactionApi.sendLogged rejects", () => __awaiter(void 0, void 0, void 0, function* () {
            vaultsTestMocks_1.prepareRegisterNewCollateralVaultMocks(sinon_1.default, vaultsApi, stubbedTransactionApi, false, true);
            chai_1.expect(vaultsApi.registerNewCollateralVault(testCollateralAmount)).to.be.rejectedWith(vaultsTestMocks_1.MOCKED_SEND_LOGGED_ERR_MSG);
        }));
        it("should submit call to register new vault with new collateral currency", () => __awaiter(void 0, void 0, void 0, function* () {
            const submittedMockExtrinsic = vaultsTestMocks_1.prepareRegisterNewCollateralVaultMocks(sinon_1.default, vaultsApi, stubbedTransactionApi);
            // check precondition
            chai_1.assert.isFalse(submittedMockExtrinsic == null, "Test setup error: Expected submitted mock extrinsic to be set, but it was not.");
            chai_1.expect(vaultsApi.registerNewCollateralVault(testCollateralAmount)).to.be.fulfilled;
            chai_1.expect(stubbedTransactionApi.sendLogged.callCount).to.be.equal(1, `Expected transactionApi.sendLogged to be called exactly once, 
                but it was called ${stubbedTransactionApi.sendLogged.callCount} times`);
            const actualSubmittedExtrinsic = stubbedTransactionApi.sendLogged.getCall(0).args[0];
            chai_1.expect(actualSubmittedExtrinsic).to.be.equal(submittedMockExtrinsic, `Expected submitted mock extrinsic have been submitted, but found this instead: ${actualSubmittedExtrinsic.toString()}`);
        }));
    });
    describe("getExchangeRateForLiquidation", () => {
        it("should calculate expected liquidation rate", () => __awaiter(void 0, void 0, void 0, function* () {
            const mockIssuedTokens = 1;
            const mockCollateralTokens = 3;
            const mockLiquidationThreshold = 2;
            // expect we need to pay 1.5 KSM per 1 BTC for the collateral (3 KSM) to issued (1 KBTC) ratio
            // to reach the liquidation threshold rate of 2
            const expectedLiquidationExchangeRate = 1.5;
            vaultsTestMocks_1.prepareLiquidationRateMocks(sinon_1.default, vaultsApi, mockIssuedTokens, mockCollateralTokens, mockLiquidationThreshold, testWrappedCurrency, testCollateralCurrency);
            // get the fictitious liquidation rate
            const actualRate = yield vaultsApi.getExchangeRateForLiquidation(null, // this variable is ignored, mocked away
            testCollateralCurrency);
            chai_1.expect(actualRate).to.not.be.undefined;
            chai_1.expect(actualRate === null || actualRate === void 0 ? void 0 : actualRate.toNumber()).eq(expectedLiquidationExchangeRate);
        }));
        it("should return undefined if vault has no issued tokens", () => __awaiter(void 0, void 0, void 0, function* () {
            const mockIssuedTokens = 0;
            const mockCollateralTokens = 3;
            const mockLiquidationThreshold = 2;
            vaultsTestMocks_1.prepareLiquidationRateMocks(sinon_1.default, vaultsApi, mockIssuedTokens, mockCollateralTokens, mockLiquidationThreshold, testWrappedCurrency, testCollateralCurrency);
            // get the fictitious liquidation rate
            const actualRate = yield vaultsApi.getExchangeRateForLiquidation(null, // this variable is ignored, mocked away
            testCollateralCurrency);
            chai_1.expect(actualRate).to.be.undefined;
        }));
        it("should return undefined if liquidation rate is zero", () => __awaiter(void 0, void 0, void 0, function* () {
            const mockIssuedTokens = 1;
            const mockCollateralTokens = 3;
            const mockLiquidationThreshold = 0;
            vaultsTestMocks_1.prepareLiquidationRateMocks(sinon_1.default, vaultsApi, mockIssuedTokens, mockCollateralTokens, mockLiquidationThreshold, testWrappedCurrency, testCollateralCurrency);
            // get the fictitious liquidation rate
            const actualRate = yield vaultsApi.getExchangeRateForLiquidation(null, // this variable is ignored, mocked away
            testCollateralCurrency);
            chai_1.expect(actualRate).to.be.undefined;
        }));
    });
});
//# sourceMappingURL=vaults.test.js.map