"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.AbracadabraMagicGlpTokenFetcher = void 0;
const common_1 = require("@nestjs/common");
const app_toolkit_interface_1 = require("../../../app-toolkit/app-toolkit.interface");
const image_present_1 = require("../../../app-toolkit/helpers/presentation/image.present");
const position_interface_1 = require("../../../position/position.interface");
const erc4626_vault_template_token_fetcher_1 = require("../../../position/template/erc4626-vault.template.token-fetcher");
const contracts_1 = require("../contracts");
const SECONDS_PER_YEAR = 31536000;
const BASIS_POINTS_DIVISOR = 10000;
let AbracadabraMagicGlpTokenFetcher = class AbracadabraMagicGlpTokenFetcher extends erc4626_vault_template_token_fetcher_1.Erc4626VaultTemplateTokenFetcher {
    constructor(appToolkit, contractFactory) {
        super(appToolkit);
        this.appToolkit = appToolkit;
        this.contractFactory = contractFactory;
        this.groupLabel = 'Magic GLP';
    }
    getUnderlyingTokenDefinitions() {
        return __awaiter(this, void 0, void 0, function* () {
            const glpTokenDefinitions = yield this.appToolkit.getAppTokenPositionsFromDatabase({
                appId: 'gmx',
                groupIds: ['glp'],
                network: this.network,
            });
            const glpUnderlying = glpTokenDefinitions[0];
            return [{ address: glpUnderlying.address, network: this.network }];
        });
    }
    getLabel({ contract }) {
        return __awaiter(this, void 0, void 0, function* () {
            return contract.name();
        });
    }
    getImages({ appToken, }) {
        return __awaiter(this, void 0, void 0, function* () {
            return [(0, image_present_1.getTokenImg)(appToken.address, this.network)];
        });
    }
    getApy({ multicall, appToken, tokenLoader, }) {
        return __awaiter(this, void 0, void 0, function* () {
            const [magicGlpHarvestorAddress, magicGlpAnnualHarvests, rewardTrackerAddresses] = yield Promise.all([
                this.magicGlpHarvestorAddress,
                this.magicGlpAnnualHarvests,
                Promise.all(this.rewardTrackerAddresses),
            ]);
            const magicGlpHarvestor = multicall.wrap(this.contractFactory.abracadabraMagicGlpHarvestor({
                address: magicGlpHarvestorAddress,
                network: this.network,
            }));
            const rewardTrackers = rewardTrackerAddresses.map(rewardTrackerAddress => multicall.wrap(this.contractFactory.gmxRewardTracker({
                address: rewardTrackerAddress,
                network: this.network,
            })));
            const [magicGlpFeeProcent, annualUsdRewards] = yield Promise.all([
                magicGlpHarvestor.feePercentBips().then(magicGlpFeeProcent => magicGlpFeeProcent / BASIS_POINTS_DIVISOR),
                Promise.all(rewardTrackers.map((tracker) => __awaiter(this, void 0, void 0, function* () {
                    const [tokensPerInterval, rewardToken] = yield Promise.all([
                        tracker.tokensPerInterval(),
                        tracker.rewardToken().then(rewardTokenAddress => tokenLoader.getOne({
                            network: this.network,
                            address: rewardTokenAddress,
                        })),
                    ]);
                    if (rewardToken == null) {
                        return 0;
                    }
                    else {
                        const tokensPerYearRaw = tokensPerInterval.mul(SECONDS_PER_YEAR);
                        const tokensPerYear = Number(tokensPerYearRaw) / Math.pow(10, rewardToken.decimals);
                        return tokensPerYear * rewardToken.price;
                    }
                }))),
            ]);
            const glp = appToken.tokens[0];
            const glpSupplyUsd = (0, position_interface_1.isAppToken)(glp) ? glp.supply * glp.price : null;
            if (glpSupplyUsd == null || glpSupplyUsd <= 0) {
                return 0;
            }
            else {
                const apr = annualUsdRewards.map(annualUsdReward => annualUsdReward / glpSupplyUsd).reduce((a, b) => a + b, 0);
                const apy = Math.pow(1 + apr / magicGlpAnnualHarvests, magicGlpAnnualHarvests) - 1;
                const apyWithFees = apy * (1 - magicGlpFeeProcent);
                return apyWithFees * 100;
            }
        });
    }
};
AbracadabraMagicGlpTokenFetcher = __decorate([
    __param(0, (0, common_1.Inject)(app_toolkit_interface_1.APP_TOOLKIT)),
    __param(1, (0, common_1.Inject)(contracts_1.AbracadabraContractFactory)),
    __metadata("design:paramtypes", [Object, contracts_1.AbracadabraContractFactory])
], AbracadabraMagicGlpTokenFetcher);
exports.AbracadabraMagicGlpTokenFetcher = AbracadabraMagicGlpTokenFetcher;
//# sourceMappingURL=abracadabra.magic-glp.token-fetcher.js.map