"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.BinanceSmartChainPancakeswapPoolAddressCacheManager = 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 cache_on_interval_decorator_1 = require("../../../cache/cache-on-interval.decorator");
const network_interface_1 = require("../../../types/network.interface");
const contracts_1 = require("../contracts");
let BinanceSmartChainPancakeswapPoolAddressCacheManager = class BinanceSmartChainPancakeswapPoolAddressCacheManager {
    constructor(appToolkit, contractFactory) {
        this.appToolkit = appToolkit;
        this.contractFactory = contractFactory;
    }
    getTopPoolAddresses() {
        return __awaiter(this, void 0, void 0, function* () {
            return [];
        });
    }
    getChefPoolAddresses() {
        return __awaiter(this, void 0, void 0, function* () {
            const network = network_interface_1.Network.BINANCE_SMART_CHAIN_MAINNET;
            const chefAddress = '0x73feaa1ee314f8c655e354234017be2193c9e24e';
            const chefContract = this.contractFactory.pancakeswapChef({ address: chefAddress, network });
            const provider = this.appToolkit.getNetworkProvider(network);
            const multicall = this.appToolkit.getMulticall(network);
            const numPools = yield multicall.wrap(chefContract).poolLength();
            const allAddresses = yield Promise.all((0, lodash_1.range)(0, Number(numPools)).map((v) => __awaiter(this, void 0, void 0, function* () {
                const poolInfo = yield multicall.wrap(chefContract).poolInfo(v);
                const lpTokenAddress = poolInfo.lpToken.toLowerCase();
                const lpTokenContract = this.contractFactory.pancakeswapPair({ address: lpTokenAddress, network });
                const code = yield provider.getCode(lpTokenAddress);
                if (code === '0x')
                    return false;
                const [symbol, factoryAddressRaw] = yield Promise.all([
                    multicall
                        .wrap(lpTokenContract)
                        .symbol()
                        .catch(_err => ''),
                    multicall
                        .wrap(lpTokenContract)
                        .factory()
                        .catch(_err => ''),
                ]);
                const V2_FACTORY_ADDRESS = '0xca143ce32fe78f1f7019d7d551a6402fc5350c73';
                const isV2Pair = factoryAddressRaw.toLowerCase() === V2_FACTORY_ADDRESS;
                const isCakeLp = symbol === 'Cake-LP';
                if (!isV2Pair || !isCakeLp)
                    return null;
                return lpTokenAddress;
            })));
            return (0, lodash_1.compact)(allAddresses);
        });
    }
    getChefV2PoolAddresses() {
        return __awaiter(this, void 0, void 0, function* () {
            const network = network_interface_1.Network.BINANCE_SMART_CHAIN_MAINNET;
            const chefAddress = '0xa5f8c5dbd5f286960b9d90548680ae5ebff07652';
            const chefContract = this.contractFactory.pancakeswapChefV2({ address: chefAddress, network });
            const provider = this.appToolkit.getNetworkProvider(network);
            const multicall = this.appToolkit.getMulticall(network);
            const numPools = yield multicall.wrap(chefContract).poolLength();
            const allAddresses = yield Promise.all((0, lodash_1.range)(0, Number(numPools)).map((v) => __awaiter(this, void 0, void 0, function* () {
                const lpTokenAddressRaw = yield multicall.wrap(chefContract).lpToken(v);
                const lpTokenAddress = lpTokenAddressRaw.toLowerCase();
                const lpTokenContract = this.contractFactory.pancakeswapPair({ address: lpTokenAddress, network });
                const code = yield provider.getCode(lpTokenAddress);
                if (code === '0x')
                    return false;
                const [symbol, factoryAddressRaw] = yield Promise.all([
                    multicall
                        .wrap(lpTokenContract)
                        .symbol()
                        .catch(_err => ''),
                    multicall
                        .wrap(lpTokenContract)
                        .factory()
                        .catch(_err => ''),
                ]);
                const V2_FACTORY_ADDRESS = '0xca143ce32fe78f1f7019d7d551a6402fc5350c73';
                const isV2Pair = factoryAddressRaw.toLowerCase() === V2_FACTORY_ADDRESS;
                const isCakeLp = symbol === 'Cake-LP';
                if (!isV2Pair || !isCakeLp)
                    return null;
                return lpTokenAddress;
            })));
            return (0, lodash_1.compact)(allAddresses);
        });
    }
    getStaticPoolAddresses() {
        return __awaiter(this, void 0, void 0, function* () {
            return ['0x351a295afbab020bc7eedcb7fd5a823c01a95fda'];
        });
    }
    getPoolAddresses() {
        return __awaiter(this, void 0, void 0, function* () {
            const [topPoolAddresses, chefPoolAddresses, chefV2PoolAddresses, staticPoolAddresses] = yield Promise.all([
                this.getTopPoolAddresses(),
                this.getChefPoolAddresses(),
                this.getChefV2PoolAddresses(),
                this.getStaticPoolAddresses(),
            ]);
            return (0, lodash_1.uniq)([...topPoolAddresses, ...chefPoolAddresses, ...chefV2PoolAddresses, ...staticPoolAddresses]);
        });
    }
};
__decorate([
    (0, cache_on_interval_decorator_1.CacheOnInterval)({
        key: `studio:pancakeswap:graph-top-pool-addresses`,
        timeout: 15 * 60 * 1000,
        failOnMissingData: false,
    }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], BinanceSmartChainPancakeswapPoolAddressCacheManager.prototype, "getTopPoolAddresses", null);
__decorate([
    (0, cache_on_interval_decorator_1.CacheOnInterval)({
        key: `studio:pancakeswap:chef-pool-addresses`,
        timeout: 15 * 60 * 1000,
        failOnMissingData: false,
    }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], BinanceSmartChainPancakeswapPoolAddressCacheManager.prototype, "getChefPoolAddresses", null);
__decorate([
    (0, cache_on_interval_decorator_1.CacheOnInterval)({
        key: `studio:pancakeswap:chef-v2-pool-addresses`,
        timeout: 15 * 60 * 1000,
        failOnMissingData: false,
    }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], BinanceSmartChainPancakeswapPoolAddressCacheManager.prototype, "getChefV2PoolAddresses", null);
__decorate([
    (0, cache_on_interval_decorator_1.CacheOnInterval)({
        key: `studio:pancakeswap:static-pool-addresses`,
        timeout: 15 * 60 * 1000,
        failOnMissingData: false,
    }),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], BinanceSmartChainPancakeswapPoolAddressCacheManager.prototype, "getStaticPoolAddresses", null);
BinanceSmartChainPancakeswapPoolAddressCacheManager = __decorate([
    (0, common_1.Injectable)(),
    __param(0, (0, common_1.Inject)(app_toolkit_interface_1.APP_TOOLKIT)),
    __param(1, (0, common_1.Inject)(contracts_1.PancakeswapContractFactory)),
    __metadata("design:paramtypes", [Object, contracts_1.PancakeswapContractFactory])
], BinanceSmartChainPancakeswapPoolAddressCacheManager);
exports.BinanceSmartChainPancakeswapPoolAddressCacheManager = BinanceSmartChainPancakeswapPoolAddressCacheManager;
//# sourceMappingURL=pancakeswap.pool.cache-manager.js.map