"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.EthereumConcaveLiquidStakingContractPositionFetcher = exports.GET_STAKING_V1_LOCK_EVENTS = void 0;
const common_1 = require("@nestjs/common");
const utils_1 = require("ethers/lib/utils");
const graphql_request_1 = require("graphql-request");
const lodash_1 = require("lodash");
const moment_1 = require("moment");
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 the_graph_helper_1 = require("../../../app-toolkit/helpers/the-graph.helper");
const contract_interface_1 = require("../../../position/contract.interface");
const position_interface_1 = require("../../../position/position.interface");
const position_utils_1 = require("../../../position/position.utils");
const custom_contract_position_template_position_fetcher_1 = require("../../../position/template/custom-contract-position.template.position-fetcher");
const contracts_1 = require("../contracts");
exports.GET_STAKING_V1_LOCK_EVENTS = (0, graphql_request_1.gql) `
  query GetStakingV1Locks($address: String!) {
    logStakingV1_Lock(where: { to: { _eq: $address } }) {
      deposit
      maturity
      poolBalance
      poolID
      poolTerm
      positionID
      rewardDebt
      to
    }
  }
`;
let EthereumConcaveLiquidStakingContractPositionFetcher = class EthereumConcaveLiquidStakingContractPositionFetcher extends custom_contract_position_template_position_fetcher_1.CustomContractPositionTemplatePositionFetcher {
    constructor(appToolkit, concaveContractFactory) {
        super(appToolkit);
        this.appToolkit = appToolkit;
        this.concaveContractFactory = concaveContractFactory;
        this.groupLabel = 'Liquid Staking';
    }
    getContract(address) {
        return this.concaveContractFactory.lsdcnv({ address, network: this.network });
    }
    getDefinitions() {
        return __awaiter(this, void 0, void 0, function* () {
            return [{ address: '0x93c3a816242e50ea8871a29bf62cc3df58787fbd' }];
        });
    }
    getTokenDefinitions(_params) {
        return __awaiter(this, void 0, void 0, function* () {
            return [
                {
                    metaType: position_interface_1.MetaType.SUPPLIED,
                    address: '0x000000007a58f5f58e697e51ab0357bc9e260a04',
                    network: this.network,
                },
                {
                    metaType: position_interface_1.MetaType.CLAIMABLE,
                    address: '0x000000007a58f5f58e697e51ab0357bc9e260a04',
                    network: this.network,
                },
            ];
        });
    }
    getLabel() {
        return __awaiter(this, void 0, void 0, function* () {
            return 'Liquid Staked CNV';
        });
    }
    getTokenBalancesPerPosition(_params) {
        throw new Error('Method not implemented.');
    }
    getBalances(address) {
        return __awaiter(this, void 0, void 0, function* () {
            const multicall = this.appToolkit.getMulticall(this.network);
            const [lsdCnv] = yield this.appToolkit.getAppContractPositions({
                appId: this.appId,
                network: this.network,
                groupIds: [this.groupId],
            });
            const contract = multicall.wrap(this.getContract(lsdCnv.address));
            const balanceRaw = yield contract.balanceOf(address);
            if (Number(balanceRaw) === 0)
                return [];
            const lockData = yield (0, the_graph_helper_1.gqlFetch)({
                endpoint: 'https://concave.hasura.app/v1/graphql',
                query: exports.GET_STAKING_V1_LOCK_EVENTS,
                variables: { address: (0, utils_1.getAddress)(address) },
            });
            const positions = yield Promise.all(lockData.logStakingV1_Lock.map((event) => __awaiter(this, void 0, void 0, function* () {
                const positionId = event.positionID;
                const unlockDate = (0, moment_1.unix)(event.maturity).format('LL');
                const label = `Liquid Staking (#${positionId}) - Unlock: ${unlockDate}`;
                const [positionInfo, positionRewardInfo] = yield Promise.all([
                    multicall.wrap(contract).positions(positionId),
                    multicall.wrap(contract).viewPositionRewards(positionId),
                ]);
                const stakedToken = lsdCnv.tokens.find(position_utils_1.isSupplied);
                const rewardToken = lsdCnv.tokens.find(position_utils_1.isClaimable);
                const stakedTokenBalance = (0, drill_balance_helper_1.drillBalance)(stakedToken, positionInfo.deposit.toString());
                const rewardBalanceRaw = positionRewardInfo.totalRewards.sub(positionRewardInfo.amountDeposited).toString();
                const rewardTokenBalance = (0, drill_balance_helper_1.drillBalance)(rewardToken, rewardBalanceRaw);
                const tokens = [stakedTokenBalance, rewardTokenBalance];
                const balanceUSD = (0, lodash_1.sumBy)(tokens, v => v.balanceUSD);
                const position = {
                    type: contract_interface_1.ContractType.POSITION,
                    address: lsdCnv.address,
                    appId: lsdCnv.appId,
                    groupId: lsdCnv.groupId,
                    network: lsdCnv.network,
                    tokens,
                    balanceUSD,
                    dataProps: {
                        poolID: event.poolID,
                        tokenId: event.positionID,
                        unlockTime: event.maturity,
                    },
                    displayProps: {
                        label,
                        images: lsdCnv.displayProps.images,
                    },
                };
                return position;
            })));
            return positions;
        });
    }
};
EthereumConcaveLiquidStakingContractPositionFetcher = __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.ConcaveContractFactory)),
    __metadata("design:paramtypes", [Object, contracts_1.ConcaveContractFactory])
], EthereumConcaveLiquidStakingContractPositionFetcher);
exports.EthereumConcaveLiquidStakingContractPositionFetcher = EthereumConcaveLiquidStakingContractPositionFetcher;
//# sourceMappingURL=concave.liquid-staking.contract-position-fetcher.js.map