"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExactlyDefinitionsResolver = exports.PREVIEWER_ADDRESSES = void 0;
const common_1 = require("@nestjs/common");
const ethers_1 = require("ethers");
const network_interface_1 = require("../../../types/network.interface");
const ethers_2 = require("../contracts/ethers");
exports.PREVIEWER_ADDRESSES = {
    [network_interface_1.Network.ETHEREUM_MAINNET]: '0x5fe09baaa75fd107a8df8565813f66b3603a13d3',
    [network_interface_1.Network.OPTIMISM_MAINNET]: '0xb8b1f590272b541b263a49b28bf52f8774b0e6c9',
};
let ExactlyDefinitionsResolver = class ExactlyDefinitionsResolver {
    getDefinitions({ multicall, network, account }) {
        return __awaiter(this, void 0, void 0, function* () {
            const address = exports.PREVIEWER_ADDRESSES[network];
            if (!address)
                throw new Error(`missing previewer on ${network}`);
            const previewer = ethers_2.Previewer__factory.createInterface();
            const { address: multicallAddress, interface: multicallInterface } = multicall.contract;
            const [block, [{ data: exactly }, { data: ts }]] = yield multicall.wrap(multicall.contract).callStatic.aggregate([
                { target: address, callData: previewer.encodeFunctionData('exactly', [account !== null && account !== void 0 ? account : ethers_1.constants.AddressZero]) },
                { target: multicallAddress, callData: multicallInterface.encodeFunctionData('getCurrentBlockTimestamp') },
            ], true);
            const blockNumber = block.toNumber();
            const timestamp = multicall.contract.interface.decodeFunctionResult('getCurrentBlockTimestamp', ts)[0].toNumber();
            return previewer.decodeFunctionResult('exactly', exactly)[0].map(m => (Object.assign({ address: m.market.toLowerCase(), blockNumber, timestamp }, m)));
        });
    }
    getDefinition(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const definitions = yield this.getDefinitions(params);
            const definition = definitions.find(({ address }) => address === params.market.toLowerCase());
            if (!definition)
                throw new Error(`missing definition for ${params.market}`);
            return definition;
        });
    }
};
ExactlyDefinitionsResolver = __decorate([
    (0, common_1.Injectable)()
], ExactlyDefinitionsResolver);
exports.ExactlyDefinitionsResolver = ExactlyDefinitionsResolver;
//# sourceMappingURL=exactly.definitions-resolver.js.map