"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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.EthereumBProtocolMakerVaultContractPositionFetcher = void 0;
const common_1 = require("@nestjs/common");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const ethers_1 = require("ethers");
const app_toolkit_interface_1 = require("../../../app-toolkit/app-toolkit.interface");
const position_template_decorator_1 = require("../../../app-toolkit/decorators/position-template.decorator");
const contracts_1 = require("../../../apps/maker/contracts");
const position_interface_1 = require("../../../position/position.interface");
const contract_position_template_position_fetcher_1 = require("../../../position/template/contract-position.template.position-fetcher");
const contracts_2 = require("../contracts");
let EthereumBProtocolMakerVaultContractPositionFetcher = class EthereumBProtocolMakerVaultContractPositionFetcher extends contract_position_template_position_fetcher_1.ContractPositionTemplatePositionFetcher {
    constructor(appToolkit, makerContractFactory, bProtocolContractFactory) {
        super(appToolkit);
        this.appToolkit = appToolkit;
        this.makerContractFactory = makerContractFactory;
        this.bProtocolContractFactory = bProtocolContractFactory;
        this.groupLabel = 'Maker Vaults';
        this.BCDP_MANGER = '0x3f30c2381cd8b917dd96eb2f1a4f96d91324bbed';
        this.CDP_MANAGER = '0x5ef30b9986345249bc32d8928b7ee64de9435e39';
        this.GET_CDPS = '0x36a724bd100c39f0ea4d3a20f7097ee01a8ff573';
        this.MCD_VAT = '0x35d1b3f3d7966a1dfe207aa4514c12a259a0492b';
        this.MCD_SPOT = '0x65c79fcb50ca1594b025960e539ed7a9a6d434a3';
        this.PROXY_REGISTRY = '0x4678f0a6958e4d2bc4f1baf7bc52e8f3564f3fe4';
        this.JAR = '0x3c36ccf03dab88c1b1ac1eb9c3fb5db0b6763cff';
    }
    getContract(address) {
        return this.makerContractFactory.makerGemJoin({ address, network: this.network });
    }
    getDefinitions({ multicall }) {
        return __awaiter(this, void 0, void 0, function* () {
            const supportedIlks = [
                '0x4554482d41000000000000000000000000000000000000000000000000000000',
                '0x4554482d42000000000000000000000000000000000000000000000000000000',
                '0x4554482d43000000000000000000000000000000000000000000000000000000',
                '0x574254432d410000000000000000000000000000000000000000000000000000',
            ];
            const ilkRegAddress = '0x5a464c28d19848f44199d003bef5ecc87d090f87';
            const ilkRegContract = this.makerContractFactory.makerIlkRegistry({
                address: ilkRegAddress,
                network: this.network,
            });
            const definitions = yield Promise.all(supportedIlks.map((ilk) => __awaiter(this, void 0, void 0, function* () {
                const [gem, join] = yield Promise.all([
                    multicall.wrap(ilkRegContract).gem(ilk),
                    multicall.wrap(ilkRegContract).join(ilk),
                ]);
                const ilkName = ethers_1.ethers.utils.parseBytes32String(ilk);
                const address = join.toLowerCase();
                const collateralTokenAddress = gem.toLowerCase();
                return { address, ilkName, collateralTokenAddress, ilk };
            })));
            return definitions;
        });
    }
    getTokenDefinitions({ definition }) {
        return __awaiter(this, void 0, void 0, function* () {
            return [
                { metaType: position_interface_1.MetaType.SUPPLIED, address: definition.collateralTokenAddress, network: this.network },
                { metaType: position_interface_1.MetaType.BORROWED, address: '0x6b175474e89094c44da98b954eedeac495271d0f', network: this.network },
            ];
        });
    }
    getDataProps({ definition, }) {
        return __awaiter(this, void 0, void 0, function* () {
            return { ilkName: definition.ilkName, ilk: definition.ilk };
        });
    }
    getLabel({ contractPosition, }) {
        return __awaiter(this, void 0, void 0, function* () {
            return `${contractPosition.dataProps.ilkName} Vault`;
        });
    }
    getTokenBalancesPerPosition({ address, contractPosition, multicall, }) {
        return __awaiter(this, void 0, void 0, function* () {
            const infoContractAddress = '0x468960199c8045dedcf6aeb33e28dc57346ad3ff';
            const infoContract = this.bProtocolContractFactory.bProtocolGetInfo({
                address: infoContractAddress,
                network: this.network,
            });
            const userInfo = yield multicall
                .wrap(infoContract)
                .callStatic.getInfo(address, contractPosition.dataProps.ilk, this.BCDP_MANGER, this.CDP_MANAGER, this.GET_CDPS, this.MCD_VAT, this.MCD_SPOT, this.PROXY_REGISTRY, this.JAR);
            const debt = userInfo.bCdpInfo.daiDebt.toString();
            const collateral = new bignumber_js_1.default(userInfo.bCdpInfo.ethDeposit.toString())
                .times(Math.pow(10, contractPosition.tokens[0].decimals))
                .div(Math.pow(10, 18))
                .toString();
            return [collateral, debt];
        });
    }
};
EthereumBProtocolMakerVaultContractPositionFetcher = __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.MakerContractFactory)),
    __param(2, (0, common_1.Inject)(contracts_2.BProtocolContractFactory)),
    __metadata("design:paramtypes", [Object, contracts_1.MakerContractFactory,
        contracts_2.BProtocolContractFactory])
], EthereumBProtocolMakerVaultContractPositionFetcher);
exports.EthereumBProtocolMakerVaultContractPositionFetcher = EthereumBProtocolMakerVaultContractPositionFetcher;
//# sourceMappingURL=b-protocol.maker-vault.contract-position-fetcher.js.map