"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.EthereumRariFundTokenFetcher = void 0;
const common_1 = require("@nestjs/common");
const app_toolkit_interface_1 = require("../../../app-toolkit/app-toolkit.interface");
const address_1 = require("../../../app-toolkit/constants/address");
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 contracts_1 = require("../contracts");
const SYMBOL_TO_ADDRESS = {
    DAI: '0x6b175474e89094c44da98b954eedeac495271d0f',
    USDC: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
    USDT: '0xdac17f958d2ee523a2206206994597c13d831ec7',
    mUSD: '0xe2f2a5c287993345a840db3b0845fbc70f5935a5',
};
const RARI_POOL_DEFINITIONS = [
    {
        label: 'USDC Pool',
        poolManagerAddress: '0xc6bf8c8a55f77686720e0a88e2fd1feef58ddf4a',
        poolTokenAddress: '0x016bf078abcacb987f0589a6d3beadd4316922b0',
    },
    {
        label: 'Ether Pool',
        isEther: true,
        poolManagerAddress: '0xd6e194af3d9674b62d1b30ec676030c23961275e',
        poolTokenAddress: '0xcda4770d65b4211364cb870ad6be19e7ef1d65f4',
    },
    {
        label: 'Yield Pool',
        poolManagerAddress: '0x59fa438cd0731ebf5f4cdcaf72d4960efd13fce6',
        poolTokenAddress: '0x3baa6b7af0d72006d3ea770ca29100eb848559ae',
    },
    {
        label: 'DAI Pool',
        poolManagerAddress: '0xb465baf04c087ce3ed1c266f96ca43f4847d9635',
        poolTokenAddress: '0x0833cfcb11a5ba89fbaf73a407831c98ad2d7648',
    },
];
let EthereumRariFundTokenFetcher = class EthereumRariFundTokenFetcher extends app_token_template_position_fetcher_1.AppTokenTemplatePositionFetcher {
    constructor(appToolkit, contractFactory) {
        super(appToolkit);
        this.appToolkit = appToolkit;
        this.contractFactory = contractFactory;
        this.groupLabel = 'Funds';
    }
    getContract(address) {
        return this.contractFactory.erc20({ address, network: this.network });
    }
    getAddresses() {
        return __awaiter(this, void 0, void 0, function* () {
            return RARI_POOL_DEFINITIONS.map(v => v.poolTokenAddress);
        });
    }
    getUnderlyingTokenDefinitions({ multicall, address }) {
        return __awaiter(this, void 0, void 0, function* () {
            const { poolManagerAddress, isEther } = RARI_POOL_DEFINITIONS.find(v => v.poolTokenAddress === address);
            if (isEther)
                return [{ address: address_1.ZERO_ADDRESS, network: this.network }];
            const managerContract = this.contractFactory.rariFundManager({
                address: poolManagerAddress,
                network: this.network,
            });
            const symbols = yield multicall.wrap(managerContract).callStatic.getAcceptedCurrencies();
            return symbols.map(v => ({ address: SYMBOL_TO_ADDRESS[v], network: this.network }));
        });
    }
    getPricePerShare({ appToken }) {
        return __awaiter(this, void 0, void 0, function* () {
            const { poolManagerAddress } = RARI_POOL_DEFINITIONS.find(v => v.poolTokenAddress === appToken.address);
            const managerContract = this.contractFactory.rariFundManager({
                address: poolManagerAddress,
                network: this.network,
            });
            const liquidityRaw = yield managerContract.callStatic.getFundBalance();
            const liquidity = Number(liquidityRaw) / Math.pow(10, 18);
            const reserves = appToken.tokens.map(() => liquidity / appToken.tokens.length);
            const pricePerShare = reserves.map(v => v / appToken.supply);
            return pricePerShare;
        });
    }
    getLiquidity({ appToken }) {
        return __awaiter(this, void 0, void 0, function* () {
            return appToken.supply * appToken.price;
        });
    }
    getReserves({ appToken }) {
        return __awaiter(this, void 0, void 0, function* () {
            return appToken.pricePerShare.map(t => t * appToken.supply);
        });
    }
    getApy(_params) {
        return __awaiter(this, void 0, void 0, function* () {
            return 0;
        });
    }
    getLabel({ appToken }) {
        return __awaiter(this, void 0, void 0, function* () {
            const { label } = RARI_POOL_DEFINITIONS.find(v => v.poolTokenAddress === appToken.address);
            return label;
        });
    }
};
EthereumRariFundTokenFetcher = __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.RariContractFactory)),
    __metadata("design:paramtypes", [Object, contracts_1.RariContractFactory])
], EthereumRariFundTokenFetcher);
exports.EthereumRariFundTokenFetcher = EthereumRariFundTokenFetcher;
//# sourceMappingURL=rari.fund.token-fetcher.js.map