import { fetchWithTimeout } from "../../utils";
import assert from "assert";
export class BaseHTTPAdapter {
    name;
    host;
    _retries = 0;
    _timeout = 0;
    get retries() {
        return this._retries;
    }
    set retries(retries) {
        assert(retries >= 0);
        this._retries = retries;
    }
    get timeout() {
        return this._timeout;
    }
    set timeout(timeout) {
        assert(timeout >= 0);
        this._timeout = timeout;
    }
    constructor(name, host, { timeout = 1000, retries = 1 }) {
        this.name = name;
        this.host = host;
        this.retries = retries;
        this.timeout = timeout; // ms
    }
    async query(path, urlArgs) {
        const url = `https://${this.host}/${path ?? ""}`;
        const errs = [];
        let tries = 0;
        do {
            try {
                return await fetchWithTimeout(url, urlArgs ?? {}, { "User-Agent": process.env.ACROSS_USER_AGENT ?? "across-protocol" }, this.timeout);
            }
            catch (err) {
                const errMsg = err instanceof Error ? err.message : "unknown error";
                errs.push(errMsg);
                if (++tries <= this.retries)
                    await this.sleep(Math.pow(1.5, tries) * 1000); // simple backoff
            }
        } while (tries <= this.retries);
        throw new Error(`${this.name} price lookup failure (${errs.join(", ")})`);
    }
    sleep(ms) {
        return new Promise((r) => setTimeout(r, ms));
    }
}
//# sourceMappingURL=baseAdapter.js.map