import { msToS } from "../priceClient";
import { BaseHTTPAdapter } from "./baseAdapter";
export class PriceFeed extends BaseHTTPAdapter {
    constructor({ name = "Across API", host = "across.to", timeout = 5000, // ms
    retries = 3, } = {}) {
        // Allow host to be overridden for test or alternative deployments.
        super(name, host, { timeout, retries });
    }
    async getPriceByAddress(address, currency = "usd") {
        const queryArgs = {
            l1Token: address,
            baseCurrency: currency,
        };
        const now = msToS(Date.now()) - 60; // Assume price is 60 seconds old.
        const response = await this.query("api/coingecko", queryArgs);
        if (!this.validateResponse(response))
            throw new Error(`Unexpected ${this.name} response: ${JSON.stringify(response)}`);
        return { address: address, price: response.price, timestamp: now };
    }
    // todo: Support bundled prices in the API endpoint.
    getPricesByAddress(addresses, currency = "usd") {
        return Promise.all(addresses.map((address) => this.getPriceByAddress(address, currency)));
    }
    validateResponse(response) {
        if (typeof response !== "object")
            return false;
        return response !== null && typeof response.price === "number";
    }
}
//# sourceMappingURL=acrossApi.js.map