import { __awaiter, __extends, __generator } from "tslib";
import { msToS } from "../priceClient";
import { BaseHTTPAdapter } from "./baseAdapter";
var PriceFeed = /** @class */ (function (_super) {
    __extends(PriceFeed, _super);
    function PriceFeed(_a) {
        var _b = _a === void 0 ? {} : _a, _c = _b.name, name = _c === void 0 ? "Across API" : _c, _d = _b.host, host = _d === void 0 ? "across.to" : _d, _e = _b.timeout, timeout = _e === void 0 ? 5000 : _e, // ms
        _f = _b.retries, // ms
        retries = _f === void 0 ? 3 : _f;
        // Allow host to be overridden for test or alternative deployments.
        return _super.call(this, name, host, { timeout: timeout, retries: retries }) || this;
    }
    PriceFeed.prototype.getPriceByAddress = function (address_1) {
        return __awaiter(this, arguments, void 0, function (address, currency) {
            var queryArgs, now, response;
            if (currency === void 0) { currency = "usd"; }
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        queryArgs = {
                            l1Token: address,
                            baseCurrency: currency,
                        };
                        now = msToS(Date.now()) - 60;
                        return [4 /*yield*/, this.query("api/coingecko", queryArgs)];
                    case 1:
                        response = _a.sent();
                        if (!this.validateResponse(response))
                            throw new Error("Unexpected ".concat(this.name, " response: ").concat(JSON.stringify(response)));
                        return [2 /*return*/, { address: address, price: response.price, timestamp: now }];
                }
            });
        });
    };
    // todo: Support bundled prices in the API endpoint.
    PriceFeed.prototype.getPricesByAddress = function (addresses, currency) {
        var _this = this;
        if (currency === void 0) { currency = "usd"; }
        return Promise.all(addresses.map(function (address) { return _this.getPriceByAddress(address, currency); }));
    };
    PriceFeed.prototype.validateResponse = function (response) {
        if (typeof response !== "object")
            return false;
        return response !== null && typeof response.price === "number";
    };
    return PriceFeed;
}(BaseHTTPAdapter));
export { PriceFeed };
//# sourceMappingURL=acrossApi.js.map