"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.VectorFinanceFarmContractPositionFetcher = 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 position_template_decorator_1 = require("../../../app-toolkit/decorators/position-template.decorator");
const multicall_ethers_1 = require("../../../multicall/multicall.ethers");
const position_utils_1 = require("../../../position/position.utils");
const master_chef_v2_template_contract_position_fetcher_1 = require("../../../position/template/master-chef-v2.template.contract-position-fetcher");
const master_chef_template_contract_position_fetcher_1 = require("../../../position/template/master-chef.template.contract-position-fetcher");
const contracts_1 = require("../contracts");
let VectorFinanceFarmContractPositionFetcher = class VectorFinanceFarmContractPositionFetcher extends master_chef_v2_template_contract_position_fetcher_1.MasterChefV2TemplateContractPositionFetcher {
    constructor(appToolkit, contractFactory) {
        super(appToolkit);
        this.appToolkit = appToolkit;
        this.contractFactory = contractFactory;
        this.groupLabel = 'Farms';
        this.chefAddress = '0x423d0fe33031aa4456a17b150804aa57fc157d97';
        this.rewardRateUnit = master_chef_template_contract_position_fetcher_1.RewardRateUnit.SECOND;
    }
    getContract(address) {
        return this.contractFactory.vectorFinanceMasterChef({ address, network: this.network });
    }
    getExtraRewarderContract(address) {
        return this.contractFactory.vectorFinanceMasterChefRewarder({ address, network: this.network });
    }
    getPoolLength(contract) {
        return __awaiter(this, void 0, void 0, function* () {
            return contract.poolLength();
        });
    }
    getStakedTokenAddress(contract, poolIndex, multicall) {
        return __awaiter(this, void 0, void 0, function* () {
            const registeredToken = yield contract.registeredToken(poolIndex);
            const poolInfo = yield contract.addressToPoolInfo(registeredToken);
            const _helper = this.contractFactory.vectorFinanceMasterChefPoolHelper({
                address: poolInfo.helper,
                network: this.network,
            });
            const helper = multicall.wrap(_helper);
            return helper.depositToken();
        });
    }
    getRewardTokenAddress(contract) {
        return contract.vtx();
    }
    getExtraRewarder(contract, poolIndex) {
        return __awaiter(this, void 0, void 0, function* () {
            const registeredToken = yield contract.registeredToken(poolIndex);
            const poolInfo = yield contract.addressToPoolInfo(registeredToken);
            return poolInfo.rewarder;
        });
    }
    getExtraRewardTokenAddresses(contract) {
        return __awaiter(this, void 0, void 0, function* () {
            const rewardTokens = yield Promise.all((0, lodash_1.range)(0, 2).map(i => {
                return contract.rewardTokens(i).catch(err => {
                    if ((0, multicall_ethers_1.isMulticallUnderlyingError)(err))
                        return null;
                    throw err;
                });
            }));
            return (0, lodash_1.compact)(rewardTokens);
        });
    }
    getReserve({ contract, contractPosition, definition, }) {
        return __awaiter(this, void 0, void 0, function* () {
            const stakedToken = contractPosition.tokens.find(position_utils_1.isSupplied);
            const registeredToken = yield contract.registeredToken(definition.poolIndex);
            const poolInfo = yield contract.getPoolInfo(registeredToken);
            return Number(poolInfo.sizeOfPool) / Math.pow(10, stakedToken.decimals);
        });
    }
    getTotalAllocPoints({ contract }) {
        return __awaiter(this, void 0, void 0, function* () {
            return contract.totalAllocPoint();
        });
    }
    getTotalRewardRate({ contract }) {
        return __awaiter(this, void 0, void 0, function* () {
            return contract.vtxPerSec();
        });
    }
    getPoolAllocPoints({ contract, definition }) {
        return __awaiter(this, void 0, void 0, function* () {
            const registeredToken = yield contract.registeredToken(definition.poolIndex);
            const poolInfo = yield contract.addressToPoolInfo(registeredToken);
            return poolInfo.allocPoint;
        });
    }
    getExtraRewardTokenRewardRates({ contractPosition, }) {
        return __awaiter(this, void 0, void 0, function* () {
            const [, ...extraRewardTokens] = contractPosition.tokens.filter(position_utils_1.isClaimable);
            return Promise.all(extraRewardTokens.map(() => __awaiter(this, void 0, void 0, function* () { return 0; })));
        });
    }
    getStakedTokenBalance({ address, contract, contractPosition, }) {
        return __awaiter(this, void 0, void 0, function* () {
            const registeredToken = yield contract.registeredToken(contractPosition.dataProps.poolIndex);
            return contract.depositInfo(registeredToken, address);
        });
    }
    getRewardTokenBalance({ address, contract, contractPosition, }) {
        return __awaiter(this, void 0, void 0, function* () {
            const anyClaimable = contractPosition.tokens.find(position_utils_1.isClaimable);
            const registeredToken = yield contract.registeredToken(contractPosition.dataProps.poolIndex);
            const pendingTokens = yield contract.pendingTokens(registeredToken, address, anyClaimable === null || anyClaimable === void 0 ? void 0 : anyClaimable.address);
            return pendingTokens.pendingVTX;
        });
    }
    getExtraRewardTokenBalances({ address, contractPosition, rewarderContract, }) {
        return __awaiter(this, void 0, void 0, function* () {
            const [, ...extraRewardTokens] = contractPosition.tokens.filter(position_utils_1.isClaimable);
            return Promise.all(extraRewardTokens.map(v => rewarderContract.earned(address, v.address)));
        });
    }
};
VectorFinanceFarmContractPositionFetcher = __decorate([
    (0, position_template_decorator_1.PositionTemplate)(),
    __param(0, (0, common_1.Inject)(app_toolkit_interface_1.APP_TOOLKIT)),
    __param(1, (0, common_1.Inject)(contracts_1.VectorFinanceContractFactory)),
    __metadata("design:paramtypes", [Object, contracts_1.VectorFinanceContractFactory])
], VectorFinanceFarmContractPositionFetcher);
exports.VectorFinanceFarmContractPositionFetcher = VectorFinanceFarmContractPositionFetcher;
//# sourceMappingURL=vector-finance.farm.contract-position-fetcher.js.map