"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.AuraFarmContractPositionFetcher = exports.claimedBalToMintedAura = void 0;
const common_1 = require("@nestjs/common");
const ethers_1 = require("ethers");
const lodash_1 = require("lodash");
const app_toolkit_interface_1 = require("../../../app-toolkit/app-toolkit.interface");
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");
const claimedBalToMintedAura = (claimedBalAmount, currentAuraSupply) => {
    const claimedBalAmountBN = ethers_1.BigNumber.from(claimedBalAmount);
    const currentAuraSupplyBN = ethers_1.BigNumber.from(currentAuraSupply);
    const maxSupply = ethers_1.BigNumber.from(10).pow(26);
    const initMintAmount = ethers_1.BigNumber.from(10).pow(25).mul(5);
    const emissionsMaxSupply = ethers_1.BigNumber.from(10).pow(25).mul(5);
    const totalCliffs = ethers_1.BigNumber.from(500);
    const reductionPerCliff = emissionsMaxSupply.div(totalCliffs);
    const emissionsMinted = currentAuraSupplyBN.sub(initMintAmount);
    const cliff = emissionsMinted.div(reductionPerCliff);
    if (cliff.lt(totalCliffs)) {
        const reduction = totalCliffs.sub(cliff).mul(5).div(2).add(700);
        let amount = claimedBalAmountBN.mul(reduction).div(totalCliffs);
        const amtTillMax = maxSupply.sub(emissionsMinted);
        if (amount.gt(amtTillMax)) {
            amount = amtTillMax;
        }
        return amount;
    }
    return ethers_1.BigNumber.from(0);
};
exports.claimedBalToMintedAura = claimedBalToMintedAura;
let AuraFarmContractPositionFetcher = class AuraFarmContractPositionFetcher extends single_staking_dynamic_template_contract_position_fetcher_1.SingleStakingFarmDynamicTemplateContractPositionFetcher {
    constructor(appToolkit, contractFactory) {
        super(appToolkit);
        this.appToolkit = appToolkit;
        this.contractFactory = contractFactory;
    }
    getContract(address) {
        return this.contractFactory.auraBaseRewardPool({ network: this.network, address });
    }
    getStakedTokenAddress({ contract }) {
        return __awaiter(this, void 0, void 0, function* () {
            return contract.stakingToken();
        });
    }
    getRewardTokenAddresses({ contract, multicall }) {
        return __awaiter(this, void 0, void 0, function* () {
            const primaryRewardTokenAddresses = [
                '0xba100000625a3754423978a60c9317c58a424e3d',
                '0xc0c293ce456ff0ed870add98a0828dd4d2903dbf',
            ];
            const extraRewardTokenAddresses = yield Promise.all((0, lodash_1.range)(0, Number(yield contract.extraRewardsLength())).map((v) => __awaiter(this, void 0, void 0, function* () {
                const vbpAddress = yield contract.extraRewards(v);
                const vbp = this.contractFactory.auraVirtualBalanceRewardPool({ address: vbpAddress, network: this.network });
                return multicall.wrap(vbp).rewardToken();
            })));
            return [...primaryRewardTokenAddresses, ...extraRewardTokenAddresses];
        });
    }
    getRewardRates({ contract, multicall, contractPosition, }) {
        return __awaiter(this, void 0, void 0, function* () {
            const auraToken = contractPosition.tokens.find(v => v.symbol === 'AURA');
            const auraTokenContract = this.contractFactory.erc20(auraToken);
            const auraSupplyRaw = yield multicall.wrap(auraTokenContract).totalSupply();
            const balRewardRate = yield multicall.wrap(contract).rewardRate();
            const auraRewardRate = (0, exports.claimedBalToMintedAura)(balRewardRate.toString(), auraSupplyRaw.toString());
            const numExtraRewards = yield multicall.wrap(contract).extraRewardsLength().then(Number);
            const extraRewardRates = yield Promise.all((0, lodash_1.range)(0, numExtraRewards).map((v) => __awaiter(this, void 0, void 0, function* () {
                const vbpAddress = yield multicall.wrap(contract).extraRewards(v);
                const vbp = this.contractFactory.auraVirtualBalanceRewardPool({ address: vbpAddress, network: this.network });
                return multicall.wrap(vbp).rewardRate();
            })));
            return [balRewardRate, auraRewardRate, ...extraRewardRates];
        });
    }
    getStakedTokenBalance({ address, contract }) {
        return contract.balanceOf(address);
    }
    getRewardTokenBalances({ address, contract, contractPosition, multicall, }) {
        return __awaiter(this, void 0, void 0, function* () {
            const rewardTokens = contractPosition.tokens.filter(position_utils_1.isClaimable);
            const [, auraRewardToken, ...extraRewards] = rewardTokens;
            const auraTokenContract = multicall.wrap(this.contractFactory.erc20(auraRewardToken));
            const currentAuraSupply = yield auraTokenContract.totalSupply();
            const balBalanceBN = yield contract.earned(address);
            const balBalanceRaw = balBalanceBN.toString();
            const auraBalanceRaw = (0, exports.claimedBalToMintedAura)(balBalanceRaw, currentAuraSupply.toString());
            const extraRewardBalances = yield Promise.all(extraRewards.map((_, i) => __awaiter(this, void 0, void 0, function* () {
                const extraRewardAddress = yield contract.extraRewards(i);
                const extraRewardContract = this.contractFactory.auraVirtualBalanceRewardPool({
                    address: extraRewardAddress,
                    network: this.network,
                });
                const earnedBN = yield multicall.wrap(extraRewardContract).earned(address);
                return earnedBN.toString();
            })));
            return [balBalanceRaw, auraBalanceRaw, ...extraRewardBalances];
        });
    }
};
AuraFarmContractPositionFetcher = __decorate([
    __param(0, (0, common_1.Inject)(app_toolkit_interface_1.APP_TOOLKIT)),
    __param(1, (0, common_1.Inject)(contracts_1.AuraContractFactory)),
    __metadata("design:paramtypes", [Object, contracts_1.AuraContractFactory])
], AuraFarmContractPositionFetcher);
exports.AuraFarmContractPositionFetcher = AuraFarmContractPositionFetcher;
//# sourceMappingURL=aura.farm.contract-position-fetcher.js.map