"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EthereumIlluviumFarmContractPositionFetcher = void 0;
const common_1 = require("@nestjs/common");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
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 position_utils_1 = require("../../../position/position.utils");
const single_staking_dynamic_template_contract_position_fetcher_1 = require("../../../position/template/single-staking.dynamic.template.contract-position-fetcher");
const contracts_1 = require("../contracts");
let EthereumIlluviumFarmContractPositionFetcher = class EthereumIlluviumFarmContractPositionFetcher extends single_staking_dynamic_template_contract_position_fetcher_1.SingleStakingFarmDynamicTemplateContractPositionFetcher {
    constructor(appToolkit, contractFactory) {
        super(appToolkit);
        this.appToolkit = appToolkit;
        this.contractFactory = contractFactory;
        this.groupLabel = 'Staking';
    }
    getContract(address) {
        return this.contractFactory.illuviumCorePool({ address, network: this.network });
    }
    getFarmAddresses() {
        return ['0x25121eddf746c884dde4619b573a7b10714e2a36', '0x8b4d8443a0229349a9892d4f7cbe89ef5f843f72'];
    }
    getStakedTokenAddress({ contract }) {
        return contract.poolToken();
    }
    getRewardTokenAddresses({ contract }) {
        return contract.ilv();
    }
    getRewardRates() {
        return __awaiter(this, void 0, void 0, function* () {
            return 0;
        });
    }
    getStakedTokenBalance({ address, contract, contractPosition }) {
        return __awaiter(this, void 0, void 0, function* () {
            const stakedToken = contractPosition.tokens.find(position_utils_1.isSupplied);
            const v2StakingAddress = '0x7f5f854ffb6b7701540a00c69c4ab2de2b34291d';
            const v2StakingContract = this.contractFactory.illuviumIlvPoolV2({
                address: v2StakingAddress,
                network: this.network,
            });
            let voidAmountBN = new bignumber_js_1.default(0);
            if (stakedToken.symbol === 'ILV') {
                const LAST_V1_YIELD_CREATED = 1642660625;
                const depositsLength = Number(yield contract.getDepositsLength(address));
                const depositIndexes = (0, lodash_1.range)(0, depositsLength);
                const deposits = yield Promise.all(depositIndexes.map(v => contract.getDeposit(address, v)));
                const v1YieldMinted = yield Promise.all(depositIndexes.map(v => v2StakingContract.v1YieldMinted(address, v)));
                const voidDeposits = deposits.filter((v, i) => {
                    const isAfterLastV1Yield = Number(v.lockedFrom) > LAST_V1_YIELD_CREATED;
                    const isV1YieldMinted = v1YieldMinted[i];
                    return v.isYield && (isAfterLastV1Yield || isV1YieldMinted);
                });
                voidAmountBN = voidDeposits.reduce((acc, v) => acc.plus(v.tokenAmount.toString()), new bignumber_js_1.default(0));
            }
            const stakedBalance = yield contract.balanceOf(address);
            return new bignumber_js_1.default(stakedBalance.toString()).minus(voidAmountBN).toString();
        });
    }
    getRewardTokenBalances() {
        return __awaiter(this, void 0, void 0, function* () {
            return 0;
        });
    }
};
EthereumIlluviumFarmContractPositionFetcher = __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.IlluviumContractFactory)),
    __metadata("design:paramtypes", [Object, contracts_1.IlluviumContractFactory])
], EthereumIlluviumFarmContractPositionFetcher);
exports.EthereumIlluviumFarmContractPositionFetcher = EthereumIlluviumFarmContractPositionFetcher;
//# sourceMappingURL=illuvium.farm.contract-position-fetcher.js.map