"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 sinon_1 = __importDefault(require("sinon"));
const src_1 = require("../../../src");
const monetary_js_1 = require("@interlay/monetary-js");
const big_js_1 = __importDefault(require("big.js"));
describe("DefaultRedeemAPI", () => {
    let redeemApi;
    let stubbedVaultsApi;
    beforeEach(() => {
        // only mock/stub what we really need
        // add more if/when needed
        stubbedVaultsApi = sinon_1.default.createStubInstance(src_1.DefaultVaultsAPI);
        redeemApi = new src_1.DefaultRedeemAPI(null, null, null, null, stubbedVaultsApi, null, null);
    });
    afterEach(() => {
        sinon_1.default.restore();
        sinon_1.default.reset();
    });
    describe("getBurnExchangeRate", () => {
        afterEach(() => {
            sinon_1.default.restore();
            sinon_1.default.reset();
        });
        it("should reject if burnable amount is zero", () => __awaiter(void 0, void 0, void 0, function* () {
            const zeroKbtcAmount = src_1.newMonetaryAmount(0, monetary_js_1.KBtc);
            const mockVaultExt = {
                toBeIssuedTokens: zeroKbtcAmount,
                issuedTokens: zeroKbtcAmount,
                toBeRedeemedTokens: zeroKbtcAmount,
                collateral: src_1.newMonetaryAmount(10, monetary_js_1.Kintsugi),
            };
            // stub internal call to reutn our mocked vault
            stubbedVaultsApi.getLiquidationVault.withArgs(sinon_1.default.match.any).resolves(mockVaultExt);
            yield chai_1.expect(redeemApi.getBurnExchangeRate(monetary_js_1.Kintsugi)).to.be.rejectedWith("no burnable tokens");
        }));
        it("should return an exchange rate", () => __awaiter(void 0, void 0, void 0, function* () {
            const mockVaultExt = {
                toBeIssuedTokens: src_1.newMonetaryAmount(1, monetary_js_1.KBtc),
                issuedTokens: src_1.newMonetaryAmount(10, monetary_js_1.KBtc),
                toBeRedeemedTokens: src_1.newMonetaryAmount(2, monetary_js_1.KBtc),
                collateral: src_1.newMonetaryAmount(100, monetary_js_1.Kintsugi),
            };
            // stub internal call to reutn our mocked vault
            stubbedVaultsApi.getLiquidationVault.withArgs(sinon_1.default.match.any).resolves(mockVaultExt);
            const exchangeRate = yield redeemApi.getBurnExchangeRate(monetary_js_1.Kintsugi);
            chai_1.expect(exchangeRate).to.be.an.instanceof(monetary_js_1.ExchangeRate);
            chai_1.expect(exchangeRate.rate.toNumber()).to.be.greaterThan(0);
        }));
        it("should return a specific exchange rate for given values", () => __awaiter(void 0, void 0, void 0, function* () {
            // This test serves as canary / regression test when code gets refactored.
            // Using specific values from when we found an issue with the calculation.
            const mockVaultExt = {
                toBeIssuedTokens: src_1.newMonetaryAmount(1000, monetary_js_1.KBtc),
                issuedTokens: src_1.newMonetaryAmount(300000, monetary_js_1.KBtc),
                toBeRedeemedTokens: src_1.newMonetaryAmount(200000, monetary_js_1.KBtc),
                collateral: src_1.newMonetaryAmount(big_js_1.default("39518270000"), monetary_js_1.Kintsugi),
            };
            // nmagic number of which rate to expect with collateral / (issued + toBeIssued - toBeRedeemed)
            // (manually calculated)
            const expectedExchangeRate = big_js_1.default("391270");
            // bring the numbers back down into a easier readable range of a JS number (expect 3.9127)
            const testMultiplier = 0.00001;
            // stub internal call to reutn our mocked vault
            stubbedVaultsApi.getLiquidationVault.withArgs(sinon_1.default.match.any).resolves(mockVaultExt);
            const exchangeRate = yield redeemApi.getBurnExchangeRate(monetary_js_1.Kintsugi);
            chai_1.expect(exchangeRate).to.be.an.instanceof(monetary_js_1.ExchangeRate);
            chai_1.expect(exchangeRate.rate.mul(testMultiplier).toNumber()).to.be.closeTo(expectedExchangeRate.mul(testMultiplier).toNumber(), 0.000001);
        }));
    });
});
//# sourceMappingURL=redeem.test.js.map