"use strict";
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.CompoundSupplyTokenFetcher = void 0;
const address_1 = require("../../../app-toolkit/constants/address");
const blocks_1 = require("../../../app-toolkit/constants/blocks");
const multicall_ethers_1 = require("../../../multicall/multicall.ethers");
const display_interface_1 = require("../../../position/display.interface");
const app_token_template_position_fetcher_1 = require("../../../position/template/app-token.template.position-fetcher");
class CompoundSupplyTokenFetcher extends app_token_template_position_fetcher_1.AppTokenTemplatePositionFetcher {
    constructor() {
        super(...arguments);
        this.isExchangeable = false;
    }
    getContract(address) {
        return this.getCompoundCTokenContract(address);
    }
    getAddresses(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const comptroller = this.getCompoundComptrollerContract(this.comptrollerAddress);
            return this.getMarkets(Object.assign(Object.assign({}, params), { contract: comptroller }));
        });
    }
    getUnderlyingTokenDefinitions(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const underlyingAddressRaw = yield this.getUnderlyingAddress(params).catch(err => {
                if ((0, multicall_ethers_1.isMulticallUnderlyingError)(err))
                    return address_1.ZERO_ADDRESS;
                throw err;
            });
            const underlyingAddress = underlyingAddressRaw.toLowerCase().replace(address_1.ETH_ADDR_ALIAS, address_1.ZERO_ADDRESS);
            return [{ address: underlyingAddress, network: this.network }];
        });
    }
    getExchangeRateMantissa(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const { appToken } = params;
            const [underlyingToken] = appToken.tokens;
            return underlyingToken.decimals + 10;
        });
    }
    getPricePerShare(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const [rateRaw, mantissa] = yield Promise.all([this.getExchangeRate(params), this.getExchangeRateMantissa(params)]);
            return [Number(rateRaw) / Math.pow(10, mantissa)];
        });
    }
    getLabel({ appToken }) {
        return __awaiter(this, void 0, void 0, function* () {
            const [underlyingToken] = appToken.tokens;
            return underlyingToken.symbol;
        });
    }
    getLabelDetailed({ appToken }) {
        return __awaiter(this, void 0, void 0, function* () {
            return appToken.symbol;
        });
    }
    getBalanceDisplayMode(_params) {
        return __awaiter(this, void 0, void 0, function* () {
            return display_interface_1.BalanceDisplayMode.UNDERLYING;
        });
    }
    getLiquidity({ appToken }) {
        return __awaiter(this, void 0, void 0, function* () {
            return appToken.price * appToken.supply;
        });
    }
    getReserves({ appToken }) {
        return __awaiter(this, void 0, void 0, function* () {
            return [appToken.pricePerShare[0] * appToken.supply];
        });
    }
    getApy(params) {
        return __awaiter(this, void 0, void 0, function* () {
            const supplyRate = yield this.getSupplyRate(params);
            const blocksPerDay = blocks_1.BLOCKS_PER_DAY[this.network];
            return 100 * (Math.pow(1 + (blocksPerDay * Number(supplyRate)) / Number(1e18), 365) - 1);
        });
    }
}
exports.CompoundSupplyTokenFetcher = CompoundSupplyTokenFetcher;
//# sourceMappingURL=compound.supply.token-fetcher.js.map