UNPKG

@ic-wallet-kit/hpl

Version:
115 lines (114 loc) 5.99 kB
"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); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GetHplContactRemotesHandler = void 0; const cacheDataHandlers_1 = require("../../internalHandlers/cacheDataHandlers"); const hplRemotesCacheDataHandler_1 = require("../../internalHandlers/cacheDataHandlers/hplRemotesCacheDataHandler/hplRemotesCacheDataHandler"); const repositories_1 = require("../../repositories"); const common_1 = require("@ic-wallet-kit/common"); require("reflect-metadata"); const typedi_1 = require("typedi"); let GetHplContactRemotesHandler = class GetHplContactRemotesHandler extends common_1.BaseHandler { hplRemotesCacheDataHandler; hplRemoteAccountsStateCacheDataHandler; hplOwnerCacheDataHandler; hplDictionaryCacheDataHandler; hplAssetRepository; constructor(logger, hplRemotesCacheDataHandler, hplRemoteAccountsStateCacheDataHandler, hplOwnerCacheDataHandler, hplAssetRepository, hplDictionaryCacheDataHandler) { super(logger); this.hplRemotesCacheDataHandler = hplRemotesCacheDataHandler; this.hplRemoteAccountsStateCacheDataHandler = hplRemoteAccountsStateCacheDataHandler; this.hplOwnerCacheDataHandler = hplOwnerCacheDataHandler; this.hplDictionaryCacheDataHandler = hplDictionaryCacheDataHandler; this.hplAssetRepository = hplAssetRepository; } validate(form) { if (!form.principal) { throw new common_1.ValidationError("load.hpl.contact.remotes.principal.is.required", (0, common_1.getPropertyName)(form, f => f.principal), "Field principal is required"); } return Promise.resolve(); } async process(form) { const remotesResult = await this.hplRemotesCacheDataHandler.handle({ principal: form.principal, loadType: form.loadType }); const remotesInfo = remotesResult.data?.remotes ?? []; const remotesStateResult = await this.hplRemoteAccountsStateCacheDataHandler.handle({ accountCount: BigInt(0), ftAssetCount: BigInt(0), virtualAccountCount: BigInt(0), remoteAccounts: [{ idRange: [form.principal, BigInt(0), []] }], loadType: form.loadType }); const remotesState = remotesStateResult.data ?? []; const ownerIdResult = await this.hplOwnerCacheDataHandler.handle({ principal: form.principal, loadType: form.loadType }); const ownerId = ownerIdResult.data?.ownerId; const assetDictionary = await this.hplDictionaryCacheDataHandler.handle({ loadType: form.loadType }); const dictionaryModels = assetDictionary.data?.assetsDictionary ?? []; const remotes = await this.formatHplRemotes(remotesInfo, remotesState, ownerId, dictionaryModels); return remotes; } async formatHplRemotes(info, state, ownerId, dictionaryModels) { const assets = await this.hplAssetRepository.getAssets(); const remotes = []; info.map((rmtInfo) => { const rmtState = state.find((rmtState) => rmtInfo.accountId === rmtState.remoteAccountId); const asset = assets.find((a) => a.id == rmtInfo.remoteInfo.ft.toString()); const assetDictionary = dictionaryModels.find((d) => d.assetId === rmtInfo.remoteInfo.ft); if (rmtState) { remotes.push({ name: "", remoteAccountId: rmtInfo.accountId.toString(), expired: Number(rmtState.time), amount: rmtState.accountState.ft.toString(), assetId: rmtInfo.remoteInfo.ft.toString(), assetName: asset ? asset.name ? asset.name : asset.assetName : "", assetSymbol: asset ? asset.symbol ? asset.symbol : asset.assetSymbol : "", assetLogo: asset?.logo ? asset.logo : assetDictionary ? assetDictionary.logo : "", code: ownerId ? (0, common_1.getPxlCode)(ownerId.toString(), rmtInfo.accountId) : "", }); } }); return remotes; } }; exports.GetHplContactRemotesHandler = GetHplContactRemotesHandler; exports.GetHplContactRemotesHandler = GetHplContactRemotesHandler = __decorate([ (0, typedi_1.Service)(), __param(0, (0, typedi_1.Inject)("ILogger")), __param(4, (0, typedi_1.Inject)("HplAssetRepository")), __metadata("design:paramtypes", [Object, hplRemotesCacheDataHandler_1.HplRemotesCacheDataHandler, cacheDataHandlers_1.HplRemoteAccountsStateCacheDataHandler, cacheDataHandlers_1.HplOwnerCacheDataHandler, repositories_1.HplAssetRepository, cacheDataHandlers_1.HplDictionaryCacheDataHandler]) ], GetHplContactRemotesHandler);