"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.EthereumRhinoFiDepositContractPositionFetcher = void 0;
const common_1 = require("@nestjs/common");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const lodash_1 = require("lodash");
const app_toolkit_interface_1 = require("../../../app-toolkit/app-toolkit.interface");
const position_template_decorator_1 = require("../../../app-toolkit/decorators/position-template.decorator");
const drill_balance_helper_1 = require("../../../app-toolkit/helpers/drill-balance.helper");
const image_present_1 = require("../../../app-toolkit/helpers/presentation/image.present");
const position_interface_1 = require("../../../position/position.interface");
const custom_contract_position_template_position_fetcher_1 = require("../../../position/template/custom-contract-position.template.position-fetcher");
const rhino_fi_api_client_1 = require("../common/rhino-fi.api-client");
const rhino_fi_cache_manager_1 = require("../common/rhino-fi.cache-manager");
const contracts_1 = require("../contracts");
let EthereumRhinoFiDepositContractPositionFetcher = class EthereumRhinoFiDepositContractPositionFetcher extends custom_contract_position_template_position_fetcher_1.CustomContractPositionTemplatePositionFetcher {
    constructor(appToolkit, contractFactory, cacheManager, apiClient) {
        super(appToolkit);
        this.appToolkit = appToolkit;
        this.contractFactory = contractFactory;
        this.cacheManager = cacheManager;
        this.apiClient = apiClient;
        this.groupLabel = 'Deposits';
    }
    getContract(address) {
        return this.contractFactory.rhinoFiStarkEx({ address, network: this.network });
    }
    getDefinitions() {
        return __awaiter(this, void 0, void 0, function* () {
            const supportedTokens = yield this.cacheManager.getCachedSupportedTokens();
            return supportedTokens.map(supportedToken => ({
                address: '0xed9d63a96c27f87b07115b56b2e3572827f21646',
                tokenAddress: supportedToken.address,
            }));
        });
    }
    getTokenDefinitions({ definition }) {
        return __awaiter(this, void 0, void 0, function* () {
            return [
                {
                    metaType: position_interface_1.MetaType.SUPPLIED,
                    address: definition.tokenAddress,
                    network: this.network,
                },
                {
                    metaType: position_interface_1.MetaType.LOCKED,
                    address: definition.tokenAddress,
                    network: this.network,
                },
            ];
        });
    }
    getLabel({ contractPosition, }) {
        return __awaiter(this, void 0, void 0, function* () {
            return `${(0, image_present_1.getLabelFromToken)(contractPosition.tokens[0])} Rhino-Fi Deposit`;
        });
    }
    getTokenBalancesPerPosition() {
        return __awaiter(this, void 0, void 0, function* () {
            return [];
        });
    }
    getBalances(address) {
        return __awaiter(this, void 0, void 0, function* () {
            const balancesResponse = yield this.apiClient.getUserBalances(address);
            if (balancesResponse.length === 0)
                return [];
            const supportedTokens = yield this.cacheManager.getCachedSupportedTokens();
            const contractPositions = yield this.appToolkit.getAppContractPositions({
                appId: this.appId,
                network: this.network,
                groupIds: [this.groupId],
            });
            const balances = balancesResponse.map(userBalance => {
                const supportedToken = supportedTokens.find(supportedToken => supportedToken.symbol === userBalance.token);
                if (!supportedToken)
                    return null;
                const contractPosition = contractPositions.find(v => v.tokens[0].address === supportedToken.address);
                if (!contractPosition)
                    return null;
                const quantization = supportedToken.quantization;
                const availableBalanceRaw = new bignumber_js_1.default(userBalance.available).times(quantization).toFixed(0);
                const lockedBalanceRaw = new bignumber_js_1.default(userBalance.locked).times(quantization).toFixed(0);
                const availableTokenBalance = (0, drill_balance_helper_1.drillBalance)(contractPosition.tokens[0], availableBalanceRaw);
                const lockedTokenBalance = (0, drill_balance_helper_1.drillBalance)(contractPosition.tokens[1], lockedBalanceRaw);
                const tokens = [availableTokenBalance, lockedTokenBalance];
                const balanceUSD = (0, lodash_1.sumBy)(tokens, t => t.balanceUSD);
                const balance = Object.assign(Object.assign({}, contractPosition), { tokens, balanceUSD });
                return balance;
            });
            return (0, lodash_1.compact)(balances);
        });
    }
};
EthereumRhinoFiDepositContractPositionFetcher = __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.RhinoFiContractFactory)),
    __param(2, (0, common_1.Inject)(rhino_fi_cache_manager_1.RhinoFiCacheManager)),
    __param(3, (0, common_1.Inject)(rhino_fi_api_client_1.RhinoFiApiClient)),
    __metadata("design:paramtypes", [Object, contracts_1.RhinoFiContractFactory,
        rhino_fi_cache_manager_1.RhinoFiCacheManager,
        rhino_fi_api_client_1.RhinoFiApiClient])
], EthereumRhinoFiDepositContractPositionFetcher);
exports.EthereumRhinoFiDepositContractPositionFetcher = EthereumRhinoFiDepositContractPositionFetcher;
//# sourceMappingURL=rhino-fi.deposit.contract-position-fetcher.js.map