"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 api_1 = require("@polkadot/api");
const src_1 = require("../../../src/");
describe("DefaultAssetRegistryAPI", () => {
    let api;
    let assetRegistryApi;
    let mockMetadata;
    const mockMetadataValues = {
        name: "Mock Coin One",
        symbol: "MCO",
        decimals: 8,
        existentialDeposit: 42,
        feesPerMinute: 15,
    };
    before(() => {
        api = new api_1.ApiPromise();
        // disconnect immediately to avoid printing errors
        // we only need the instance to create variables
        api.disconnect();
        // register just enough from OrmlTraitsAssetRegistryAssetMetadata to construct
        // meaningful representations for our tests
        api.registerTypes({
            OrmlTraitsAssetRegistryAssetMetadata: {
                name: "Bytes",
                symbol: "Bytes",
                decimals: "u32",
                existentialDeposit: "u128",
                additional: "InterbtcPrimitivesCustomMetadata",
            },
            InterbtcPrimitivesCustomMetadata: {
                feePerSecond: "u128",
            },
        });
    });
    beforeEach(() => {
        assetRegistryApi = new src_1.DefaultAssetRegistryAPI(api);
        // reset to base values
        mockMetadata = {
            name: api.createType("Bytes", mockMetadataValues.name),
            symbol: api.createType("Bytes", mockMetadataValues.symbol),
            decimals: api.createType("u32", mockMetadataValues.decimals),
            existentialDeposit: api.createType("u128", mockMetadataValues.existentialDeposit),
            additional: api.createType("InterbtcPrimitivesCustomMetadata", {
                feePerSecond: api.createType("u128", mockMetadataValues.feesPerMinute),
            }),
        };
    });
    afterEach(() => {
        sinon_1.default.restore();
        sinon_1.default.reset();
    });
    describe("getForeignAssetsAsCurrencies", () => {
        it("should return empty list if chain returns no foreign assets", () => __awaiter(void 0, void 0, void 0, function* () {
            // mock empty list returned from chain
            sinon_1.default.stub(assetRegistryApi, "getAssetRegistryEntries").returns(Promise.resolve([]));
            const actual = yield assetRegistryApi.getForeignAssetsAsCurrencies();
            chai_1.expect(actual).to.be.empty;
        }));
        it("should ignore empty optionals in foreign assets data from chain", () => __awaiter(void 0, void 0, void 0, function* () {
            const chainDataReturned = [
                // one "good" returned value
                [
                    api.createType("StorageKey<[u32]>", "0x0000000000000001"),
                    api.createType("Option<OrmlTraitsAssetRegistryAssetMetadata>", mockMetadata),
                ],
                // one empty option
                [
                    api.createType("StorageKey<[u32]>", "0x0000000000000002"),
                    api.createType("Option<OrmlTraitsAssetRegistryAssetMetadata>", undefined),
                ],
            ];
            sinon_1.default.stub(assetRegistryApi, "getAssetRegistryEntries").returns(Promise.resolve(chainDataReturned));
            const actual = yield assetRegistryApi.getForeignAssetsAsCurrencies();
            chai_1.expect(actual).to.have.lengthOf(1, `Expected only one currency to be returned, but got ${actual.length}`);
            const actualCurrency = actual[0];
            chai_1.expect(actualCurrency.ticker).to.equal(mockMetadataValues.symbol, `Expected the returned currency ticker to be ${mockMetadataValues.symbol}, but it was ${actualCurrency.ticker}`);
        }));
    });
    describe("metadataToCurrency", () => {
        it("should convert foreign asset metadata to currency", () => __awaiter(void 0, void 0, void 0, function* () {
            const actual = src_1.DefaultAssetRegistryAPI.metadataToCurrency(mockMetadata);
            chai_1.expect(actual.ticker).to.equal(mockMetadataValues.symbol, `Expected currency ticker to be ${mockMetadataValues.symbol}, but was ${actual.ticker}`);
            chai_1.expect(actual.name).to.equal(mockMetadataValues.name, `Expected currency name to be ${mockMetadataValues.name}, but was ${actual.name}`);
            chai_1.expect(actual.base).to.equal(mockMetadataValues.decimals, `Expected currency base to be ${mockMetadataValues.decimals}, but was ${actual.base}`);
            // rawBase should always be zero
            chai_1.expect(actual.rawBase).to.equal(0, `Expected currency rawBase to be zero, but was ${actual.rawBase}`);
            // check atomic units are defined and zero
            chai_1.expect(actual.units.atomic).to.exist;
            chai_1.expect(actual.units.atomic).to.equal(0, `Expected currency atomic unit value to be zero, but was ${actual.units.atomic}`);
            // check "main" unit is defined and has expected decimals value
            chai_1.expect(actual.units[actual.ticker]).to.equal(actual.base, `Expected currency unit value for ${actual.ticker} to be ${actual.base}, but was ${actual.units[actual.ticker]}`);
        }));
    });
});
//# sourceMappingURL=asset-registry.test.js.map