"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.SingleStakingFarmDynamicTemplateContractPositionFetcher = void 0;
const common_1 = require("@nestjs/common");
const lodash_1 = require("lodash");
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 position_utils_1 = require("../../position/position.utils");
const contract_position_template_position_fetcher_1 = require("../../position/template/contract-position.template.position-fetcher");
let SingleStakingFarmDynamicTemplateContractPositionFetcher = class SingleStakingFarmDynamicTemplateContractPositionFetcher extends contract_position_template_position_fetcher_1.ContractPositionTemplatePositionFetcher {
    constructor(appToolkit) {
        super(appToolkit);
        this.appToolkit = appToolkit;
    }
    getDefinitions() {
        return __awaiter(this, void 0, void 0, function* () {
            const farmAddresses = yield this.getFarmAddresses();
            return farmAddresses.map(address => ({ address }));
        });
    }
    getTokenDefinitions(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const stakedTokenAddress = yield this.getStakedTokenAddress(params);
            const rewardTokenAddresses = yield this.getRewardTokenAddresses(params).then(v => ((0, lodash_1.isArray)(v) ? v : [v]));
            const tokens = [];
            tokens.push({
                metaType: position_interface_1.MetaType.SUPPLIED,
                address: stakedTokenAddress.toLowerCase(),
                network: this.network,
            });
            tokens.push(...rewardTokenAddresses.map(v => ({
                metaType: position_interface_1.MetaType.CLAIMABLE,
                address: v.toLowerCase(),
                network: this.network,
            })));
            return tokens;
        });
    }
    getReserve({ contractPosition, multicall }) {
        return __awaiter(this, void 0, void 0, function* () {
            const stakedToken = contractPosition.tokens.find(position_utils_1.isSupplied);
            const stakedTokenContract = this.appToolkit.globalContracts.erc20(stakedToken);
            const reserveRaw = yield multicall.wrap(stakedTokenContract).balanceOf(contractPosition.address);
            const reserve = Number(reserveRaw) / Math.pow(10, stakedToken.decimals);
            return reserve;
        });
    }
    getDataProps(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const { contractPosition } = params;
            const stakedToken = contractPosition.tokens.find(position_utils_1.isSupplied);
            const rewardTokens = contractPosition.tokens.filter(position_utils_1.isClaimable);
            const rewardRatesRaw = yield this.getRewardRates(params).then(v => ((0, lodash_1.isArray)(v) ? v : [v]));
            const reserve = yield this.getReserve(params);
            const liquidity = reserve * stakedToken.price;
            const rewardRates = rewardTokens.map((v, i) => { var _a; return Number((_a = rewardRatesRaw[i]) !== null && _a !== void 0 ? _a : 0) / Math.pow(10, v.decimals); });
            const rewardRatesUSD = (0, lodash_1.sum)(rewardRates.map((v, i) => v * rewardTokens[i].price));
            const dailyRewardRateUSD = rewardRatesUSD * 86400;
            const dailyReturn = (dailyRewardRateUSD + liquidity) / liquidity - 1;
            const apy = dailyReturn * 365 * 100;
            const isActive = apy > 0;
            return { liquidity, apy, isActive };
        });
    }
    getLabel({ contractPosition }) {
        return __awaiter(this, void 0, void 0, function* () {
            const stakedToken = contractPosition.tokens.find(position_utils_1.isSupplied);
            return (0, image_present_1.getLabelFromToken)(stakedToken);
        });
    }
    getImages({ contractPosition }) {
        return __awaiter(this, void 0, void 0, function* () {
            return contractPosition.tokens.filter(position_utils_1.isSupplied).flatMap(v => (0, image_present_1.getImagesFromToken)(v));
        });
    }
    getTokenBalancesPerPosition(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const tokenBalances = [];
            const [stakedBalanceRaw, rewardTokenBalancesRaw] = yield Promise.all([
                this.getStakedTokenBalance(params),
                this.getRewardTokenBalances(params).then(v => ((0, lodash_1.isArray)(v) ? v : [v])),
            ]);
            tokenBalances.push(stakedBalanceRaw);
            tokenBalances.push(...rewardTokenBalancesRaw);
            return tokenBalances;
        });
    }
};
SingleStakingFarmDynamicTemplateContractPositionFetcher = __decorate([
    __param(0, (0, common_1.Inject)(app_toolkit_interface_1.APP_TOOLKIT)),
    __metadata("design:paramtypes", [Object])
], SingleStakingFarmDynamicTemplateContractPositionFetcher);
exports.SingleStakingFarmDynamicTemplateContractPositionFetcher = SingleStakingFarmDynamicTemplateContractPositionFetcher;
//# sourceMappingURL=single-staking.dynamic.template.contract-position-fetcher.js.map