"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.bestPrice = void 0;
const util_1 = require("@polkadot/api-derive/util");
const bn_js_1 = __importDefault(require("bn.js"));
const operators_1 = require("rxjs/operators");
const util_2 = require("../../util");
const bnToStringWithBase = (DIVIDER, num) => {
    if (!num) {
        return "0";
    }
    const int = num.div(DIVIDER);
    const fra = num.mod(DIVIDER);
    return `${int.toString(10)}.${fra.toString(10)}`;
};
const bestPrice = (instanceId, api) => {
    const DIVIDER = new bn_js_1.default(10).pow(new bn_js_1.default(9));
    return (0, util_1.memo)(instanceId, (token) => {
        const asset = { 0: (0, util_2.u64FromCurrency)(token) };
        return api.query.eqDex.bestPriceByAsset(asset).pipe((0, operators_1.map)((bestPrice) => ({
            ask: bnToStringWithBase(DIVIDER, bestPrice.ask.unwrapOr(undefined)),
            bid: bnToStringWithBase(DIVIDER, bestPrice.bid.unwrapOr(undefined)),
        })));
    });
};
exports.bestPrice = bestPrice;
