"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.EthereumApecoinStakingContractPositionFetcher = 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 drill_balance_helper_1 = require("../../../app-toolkit/helpers/drill-balance.helper");
const position_interface_1 = require("../../../position/position.interface");
const custom_contract_position_template_position_fetcher_1 = require("../../../position/template/custom-contract-position.template.position-fetcher");
const contracts_1 = require("../contracts");
var PoolTypes;
(function (PoolTypes) {
    PoolTypes[PoolTypes["APECOIN"] = 0] = "APECOIN";
    PoolTypes[PoolTypes["BAYC"] = 1] = "BAYC";
    PoolTypes[PoolTypes["MAYC"] = 2] = "MAYC";
    PoolTypes[PoolTypes["BAKC"] = 3] = "BAKC";
})(PoolTypes || (PoolTypes = {}));
const POOL_LABEL_PREFIXES = {
    [PoolTypes.APECOIN]: 'Apecoin (APE) Pool',
    [PoolTypes.BAYC]: 'Bored Ape (BAYC) Pool',
    [PoolTypes.MAYC]: 'Mutant (MAYC) Pool',
    [PoolTypes.BAKC]: 'Paired (BAKC) Pool',
};
let EthereumApecoinStakingContractPositionFetcher = class EthereumApecoinStakingContractPositionFetcher extends custom_contract_position_template_position_fetcher_1.CustomContractPositionTemplatePositionFetcher {
    constructor(appToolkit, contractFactory) {
        super(appToolkit);
        this.appToolkit = appToolkit;
        this.contractFactory = contractFactory;
        this.groupLabel = 'Staking';
    }
    getDefinitions() {
        return __awaiter(this, void 0, void 0, function* () {
            return [{ address: '0x5954ab967bc958940b7eb73ee84797dc8a2afbb9' }];
        });
    }
    getTokenDefinitions() {
        return __awaiter(this, void 0, void 0, function* () {
            const apecoinAddress = '0x4d224452801aced8b2f0aebe155379bb5d594381';
            return [
                {
                    metaType: position_interface_1.MetaType.SUPPLIED,
                    address: apecoinAddress,
                    network: this.network,
                },
                {
                    metaType: position_interface_1.MetaType.CLAIMABLE,
                    address: apecoinAddress,
                    network: this.network,
                },
            ];
        });
    }
    getContract(address) {
        return this.contractFactory.apecoinStaking({ network: this.network, address });
    }
    getLabel() {
        return __awaiter(this, void 0, void 0, function* () {
            return `Staked Apecoin`;
        });
    }
    getTokenBalancesPerPosition() {
        return __awaiter(this, void 0, void 0, function* () {
            throw new Error('Method not implemented.');
        });
    }
    getBalances(address) {
        return __awaiter(this, void 0, void 0, function* () {
            const multicall = this.appToolkit.getMulticall(this.network);
            const contractPositions = yield this.appToolkit.getAppContractPositions({
                appId: this.appId,
                network: this.network,
                groupIds: [this.groupId],
            });
            const contractPosition = contractPositions[0];
            if (!contractPosition)
                return [];
            const stakingContract = this.contractFactory.apecoinStaking(contractPosition);
            const positions = yield multicall.wrap(stakingContract).getAllStakes(address);
            if (positions.length === 0)
                return [];
            const allPositions = yield Promise.all(positions.map((position) => __awaiter(this, void 0, void 0, function* () {
                const depositAmountRaw = position.deposited;
                const claimableAmountRaw = position.unclaimed;
                const [depositToken, claimableToken] = contractPosition.tokens;
                const tokens = [
                    (0, drill_balance_helper_1.drillBalance)(depositToken, depositAmountRaw.toString()),
                    (0, drill_balance_helper_1.drillBalance)(claimableToken, claimableAmountRaw.toString()),
                ];
                const balanceUSD = (0, lodash_1.sumBy)(tokens, v => v.balanceUSD);
                const positionKey = `${position.poolId}:${position.tokenId}`;
                let labelSuffix = `${POOL_LABEL_PREFIXES[Number(position.poolId)]}`;
                if (Number(position.tokenId) > 0)
                    labelSuffix += `, Token ID #${position.tokenId}`;
                const label = `Staked APE in ${labelSuffix}`;
                const contractPositionBalance = (0, lodash_1.merge)({}, contractPosition, {
                    tokens,
                    balanceUSD,
                    dataProps: { positionKey },
                    displayProps: { label },
                });
                return contractPositionBalance;
            })));
            return allPositions.flat();
        });
    }
    getRawBalances(address) {
        return __awaiter(this, void 0, void 0, function* () {
            const multicall = this.appToolkit.getMulticall(this.network);
            const contractPositions = yield this.appToolkit.getAppContractPositions({
                appId: this.appId,
                network: this.network,
                groupIds: [this.groupId],
            });
            const contractPosition = contractPositions[0];
            if (!contractPosition)
                return [];
            const stakingContract = this.contractFactory.apecoinStaking(contractPosition);
            const positions = yield multicall.wrap(stakingContract).getAllStakes(address);
            if (positions.length === 0)
                return [];
            return (yield Promise.all(positions
                .map((position) => __awaiter(this, void 0, void 0, function* () {
                const depositAmountRaw = position.deposited;
                const claimableAmountRaw = position.unclaimed;
                return [
                    {
                        key: this.appToolkit.getPositionKey(contractPositions[0]),
                        tokens: [
                            {
                                key: this.appToolkit.getPositionKey(contractPositions[0].tokens[0]),
                                balance: depositAmountRaw.toString(),
                            },
                            {
                                key: `${this.appToolkit.getPositionKey(contractPositions[0].tokens[1])}-claimable`,
                                balance: claimableAmountRaw.toString(),
                            },
                        ],
                    },
                ];
            }))
                .flat())).flat();
        });
    }
};
EthereumApecoinStakingContractPositionFetcher = __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.ApecoinContractFactory)),
    __metadata("design:paramtypes", [Object, contracts_1.ApecoinContractFactory])
], EthereumApecoinStakingContractPositionFetcher);
exports.EthereumApecoinStakingContractPositionFetcher = EthereumApecoinStakingContractPositionFetcher;
//# sourceMappingURL=apecoin.staking.contract-position-fetcher.js.map