"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.EthereumGearboxLendingTokenFetcher = void 0;
const common_1 = require("@nestjs/common");
const utils_1 = require("ethers/lib/utils");
const app_toolkit_interface_1 = require("../../../app-toolkit/app-toolkit.interface");
const position_template_decorator_1 = require("../../../app-toolkit/decorators/position-template.decorator");
const app_token_template_position_fetcher_1 = require("../../../position/template/app-token.template.position-fetcher");
const network_interface_1 = require("../../../types/network.interface");
const contracts_1 = require("../contracts");
const network = network_interface_1.Network.ETHEREUM_MAINNET;
let EthereumGearboxLendingTokenFetcher = class EthereumGearboxLendingTokenFetcher extends app_token_template_position_fetcher_1.AppTokenTemplatePositionFetcher {
    constructor(appToolkit, gearboxContractFactory) {
        super(appToolkit);
        this.appToolkit = appToolkit;
        this.gearboxContractFactory = gearboxContractFactory;
        this.groupLabel = 'Lending';
    }
    _getPoolAddresses() {
        return __awaiter(this, void 0, void 0, function* () {
            const contractsRegister = this.gearboxContractFactory.contractsRegister({
                address: '0xa50d4e7d8946a7c90652339cdbd262c375d54d99',
                network,
            });
            return contractsRegister.getPools();
        });
    }
    _getDieselTokens(pools) {
        return __awaiter(this, void 0, void 0, function* () {
            const multicall = this.appToolkit.getMulticall(network);
            return Promise.all(pools.map(pool => multicall.wrap(this.gearboxContractFactory.poolService({ address: pool, network })).dieselToken()));
        });
    }
    _getPoolContract(definition) {
        return this.gearboxContractFactory.poolService({ address: definition.poolAddress, network });
    }
    getAddresses(_) {
        return __awaiter(this, void 0, void 0, function* () {
            const pools = yield this._getPoolAddresses();
            return this._getDieselTokens(pools);
        });
    }
    getDefinitions(_) {
        return __awaiter(this, void 0, void 0, function* () {
            const pools = yield this._getPoolAddresses();
            const dieselTokens = yield this._getDieselTokens(pools);
            return pools.map((pool, idx) => ({ address: dieselTokens[idx], poolAddress: pool }));
        });
    }
    getContract(address) {
        return this.gearboxContractFactory.dieselToken({ address, network });
    }
    getLiquidity(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const multicall = this.appToolkit.getMulticall(network);
            const poolContract = this._getPoolContract(params.definition);
            const [liquidity, underlyingToken] = yield Promise.all([
                multicall.wrap(poolContract).expectedLiquidity(),
                multicall.wrap(poolContract).underlyingToken(),
            ]);
            const underlyingTokenDecimals = yield this.gearboxContractFactory
                .erc20({ address: underlyingToken, network })
                .decimals();
            const underlyingBalance = +(0, utils_1.formatUnits)(liquidity, underlyingTokenDecimals);
            return underlyingBalance * params.appToken.tokens[0].price;
        });
    }
    getReserves(_) {
        return __awaiter(this, void 0, void 0, function* () {
            return [];
        });
    }
    getUnderlyingTokenDefinitions({ definition, }) {
        return __awaiter(this, void 0, void 0, function* () {
            const underlyingTokenAddress = yield this._getPoolContract(definition).underlyingToken();
            return [{ address: underlyingTokenAddress, network: this.network }];
        });
    }
    getPricePerShare({ contract: dieselTokenContract, definition, }) {
        return __awaiter(this, void 0, void 0, function* () {
            const multicall = this.appToolkit.getMulticall(network);
            const poolContract = this._getPoolContract(definition);
            const [underlying, underlyingToken, dieselTokenTotalSupply, dieselTokenDecimals] = yield Promise.all([
                multicall.wrap(poolContract).expectedLiquidity(),
                multicall.wrap(poolContract).underlyingToken(),
                multicall.wrap(dieselTokenContract).totalSupply(),
                multicall.wrap(dieselTokenContract).decimals(),
            ]);
            const underlyingTokenContract = this.gearboxContractFactory.erc20({ address: underlyingToken, network });
            const underlyingTokenDecimals = yield underlyingTokenContract.decimals();
            const pricePerShare = +(0, utils_1.formatUnits)(underlying, underlyingTokenDecimals) / +(0, utils_1.formatUnits)(dieselTokenTotalSupply, dieselTokenDecimals);
            return [pricePerShare];
        });
    }
};
EthereumGearboxLendingTokenFetcher = __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.GearboxContractFactory)),
    __metadata("design:paramtypes", [Object, contracts_1.GearboxContractFactory])
], EthereumGearboxLendingTokenFetcher);
exports.EthereumGearboxLendingTokenFetcher = EthereumGearboxLendingTokenFetcher;
//# sourceMappingURL=gearbox.lending.token-fetcher.js.map