import { __awaiter, __generator } from "tslib";
import { AcrossConfigStore__factory } from "../typechain";
import { object, string, assert, mask, record, optional } from "superstruct";
import { filterFalsyKeys } from "../utils";
var RateModelSs = object({
    UBar: string(), // denote the utilization kink along the rate model where the slope of the interest rate model changes.
    R0: string(), // is the interest rate charged at 0 utilization
    R1: string(), // R_0+R_1 is the interest rate charged at UBar
    R2: string(), // R_0+R_1+R_2 is the interest rate charged at 100% utilization
});
var L1TokenConfigSs = object({
    rateModel: RateModelSs,
    routeRateModel: optional(record(string(), RateModelSs)),
});
var Client = /** @class */ (function () {
    function Client(address, provider) {
        this.contract = AcrossConfigStore__factory.connect(address, provider);
    }
    Client.parseL1TokenConfig = function (data) {
        var l1TokenConfig = JSON.parse(data);
        var l1TokenConfigMask = filterFalsyKeys(mask(l1TokenConfig, L1TokenConfigSs));
        assert(l1TokenConfigMask, L1TokenConfigSs);
        return l1TokenConfigMask;
    };
    Client.prototype.getL1TokenConfig = function (l1TokenAddress_1) {
        return __awaiter(this, arguments, void 0, function (l1TokenAddress, overrides) {
            var data;
            if (overrides === void 0) { overrides = {}; }
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.contract.l1TokenConfig(l1TokenAddress, overrides)];
                    case 1:
                        data = _a.sent();
                        return [2 /*return*/, Client.parseL1TokenConfig(data)];
                }
            });
        });
    };
    Client.prototype.getRateModel = function (l1TokenAddress_1) {
        return __awaiter(this, arguments, void 0, function (l1TokenAddress, overrides, originChainId, destinationChainId) {
            var l1TokenConfig, routeRateModelKey;
            if (overrides === void 0) { overrides = {}; }
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0: return [4 /*yield*/, this.getL1TokenConfig(l1TokenAddress, overrides)];
                    case 1:
                        l1TokenConfig = _a.sent();
                        if (originChainId === undefined || destinationChainId === undefined)
                            return [2 /*return*/, l1TokenConfig.rateModel];
                        routeRateModelKey = "".concat(originChainId, "-").concat(destinationChainId);
                        if (l1TokenConfig.routeRateModel && l1TokenConfig.routeRateModel[routeRateModelKey]) {
                            return [2 /*return*/, l1TokenConfig.routeRateModel[routeRateModelKey]];
                        }
                        return [2 /*return*/, l1TokenConfig.rateModel];
                }
            });
        });
    };
    return Client;
}());
export { Client };
//# sourceMappingURL=acrossConfigStore.js.map