"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 __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
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.IdleTranchesDefinitionsResolver = void 0;
const common_1 = require("@nestjs/common");
const app_toolkit_interface_1 = require("../../../app-toolkit/app-toolkit.interface");
const cache_decorator_1 = require("../../../cache/cache.decorator");
const network_interface_1 = require("../../../types/network.interface");
const contracts_1 = require("../contracts");
let IdleTranchesDefinitionsResolver = class IdleTranchesDefinitionsResolver {
    constructor(appToolkit, contractFactory) {
        this.appToolkit = appToolkit;
        this.contractFactory = contractFactory;
    }
    getTranchesDefinitionsData(network) {
        return __awaiter(this, void 0, void 0, function* () {
            const multicall = this.appToolkit.getMulticall(network);
            const perpYieldTranchesAddresses = [
                '0x34dcd573c5de4672c8248cd12a99f875ca112ad8',
                '0xf87ec7e1ee467d7d78862089b92dd40497cba5b8',
                '0x4ccaf1392a17203edab55a1f2af3079a8ac513e7',
                '0x7ecfc031758190eb1cb303d8238d553b1d4bc8ef',
                '0x008c589c471fd0a13ac2b9338b69f5f7a1a843e1',
                '0x858f5a3a5c767f8965cf7b77c51fd178c4a92f05',
                '0xf324dca1dc621fcf118690a9c6bae40fbd8f09b7',
                '0xf5a3d259bfe7288284bd41823ec5c8327a314054',
                '0x46c1f702a6aad1fd810216a5ff15aab1c62ca826',
                '0xd5469df8ca36e7eaedb35d428f28e13380ec8ede',
                '0x2398bc075fa62ee88d7fab6a18cd30bff869bda4',
                '0xf615a552c000b114ddaa09636bbf4205de49333c',
                '0x860b1d25903dbdffec579d30012da268aeb0d621',
                '0xec964d06cd71a68531fc9d083a142c48441f391c',
                '0xdbcee5ae2e9daf0f5d93473e08780c9f45dfeb93',
                '0xdce26b2c78609b983cf91cccd43e238353653b0e',
            ];
            return Promise.all(perpYieldTranchesAddresses.map((address) => __awaiter(this, void 0, void 0, function* () {
                const perpYieldTranchesContract = this.contractFactory.idlePerpYieldTranches({
                    address,
                    network,
                });
                const [juniorAddressRaw, seniorAddressRaw, underlyingTokenAddressRaw] = yield Promise.all([
                    multicall.wrap(perpYieldTranchesContract).BBTranche(),
                    multicall.wrap(perpYieldTranchesContract).AATranche(),
                    multicall.wrap(perpYieldTranchesContract).token(),
                ]);
                return {
                    perpYieldTrancheAddress: address,
                    juniorAddress: juniorAddressRaw.toLowerCase(),
                    seniorAddress: seniorAddressRaw.toLowerCase(),
                    underlyingTokenAddress: underlyingTokenAddressRaw.toLowerCase(),
                };
            })));
        });
    }
    getJuniorTrancheAddresses(network) {
        return __awaiter(this, void 0, void 0, function* () {
            const trancheDefinitions = yield this.getTranchesDefinitionsData(network);
            return trancheDefinitions.map(tranche => {
                return {
                    address: tranche.juniorAddress,
                    underlyingTokenAddress: tranche.underlyingTokenAddress,
                    cdoAddress: tranche.perpYieldTrancheAddress,
                };
            });
        });
    }
    getSeniorTrancheAddresses(network) {
        return __awaiter(this, void 0, void 0, function* () {
            const trancheDefinitions = yield this.getTranchesDefinitionsData(network);
            return trancheDefinitions.map(tranche => {
                return {
                    address: tranche.seniorAddress,
                    underlyingTokenAddress: tranche.underlyingTokenAddress,
                    cdoAddress: tranche.perpYieldTrancheAddress,
                };
            });
        });
    }
};
__decorate([
    (0, cache_decorator_1.Cache)({
        key: network => `studio:idle:${network}:tranches-definitions`,
        ttl: 5 * 60,
    }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String]),
    __metadata("design:returntype", Promise)
], IdleTranchesDefinitionsResolver.prototype, "getTranchesDefinitionsData", null);
IdleTranchesDefinitionsResolver = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, common_1.Inject)(app_toolkit_interface_1.APP_TOOLKIT)),
    __param(1, (0, common_1.Inject)(contracts_1.IdleContractFactory)),
    __metadata("design:paramtypes", [Object, contracts_1.IdleContractFactory])
], IdleTranchesDefinitionsResolver);
exports.IdleTranchesDefinitionsResolver = IdleTranchesDefinitionsResolver;
//# sourceMappingURL=idle.tranche.token-definitions-resolver.js.map