UNPKG

test-ic-wallet-middleware-icrc

Version:
106 lines (105 loc) 5.57 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.GetListContactHandler = void 0; const common_1 = require("@ic-wallet-middleware/common"); const getIcrcAllowanceForContactCacheHandler_1 = require("../../../internalHandlers/icrcCacheDataHandlers/allowances/getIcrcAllowanceForContactCacheHandler/getIcrcAllowanceForContactCacheHandler"); const assetRepository_1 = require("../../../repositories/persists/assetRepository/assetRepository"); const contactRepository_1 = require("../../../repositories/persists/contactRepository/contactRepository"); const types_1 = require("../../../types"); const assetManagerConfiguration_1 = require("../../../types/configuration/assetManagerConfiguration"); const dateTimeUtils_1 = require("../../../utils/dateTimeUtils"); const typedi_1 = require("typedi"); let GetListContactHandler = class GetListContactHandler extends common_1.BaseHandler { configuration; contactRepository; assetRepository; getIcrcAllowanceForContactHandler; constructor(logger, configuration, contactRepository, assetRepository, getIcrcAllowanceForContactHandler) { super(logger); this.configuration = configuration; this.contactRepository = contactRepository; this.assetRepository = assetRepository; this.getIcrcAllowanceForContactHandler = getIcrcAllowanceForContactHandler; } validate(form) { return Promise.resolve(); } async process(form) { const contactList = (await this.contactRepository.getContacts()).map((c) => { const contact = { principal: c.principal, name: c.name, hasAllowance: false, assets: c.assets.map((a) => { return { ledgerAddress: a.ledgerAddress, subAccounts: a.subAccounts.map((sa) => { return { name: sa.name, subAccountId: types_1.SubAccountId.parseFromString(sa.subAccountId), }; }) }; }) }; return contact; }); const assetList = await this.assetRepository.getTokensOrDefault(); for (const contact of contactList) { for (const assetContact of contact.assets) { const asset = assetList.find((a) => a.ledgerAddress === assetContact.ledgerAddress); if (!asset) { throw new common_1.ValidationError("asset.contact.not.found", "", "Asset Not Found"); } for (const subAccount of assetContact.subAccounts) { const allowance = await this.getIcrcAllowanceForContactHandler.process({ senderPrincipal: contact.principal, ledgerAddress: assetContact.ledgerAddress, subAccountId: subAccount.subAccountId, loadType: form.loadType }); subAccount.allowance = allowance.amount > BigInt(0) ? { ledgerAddress: allowance.ledgerAddress, subAccount: allowance.subAccountId, sender: allowance.senderPrincipal, amount: allowance.amount, expiration: (0, dateTimeUtils_1.convertBigIntToDateString)(allowance.expiration, this.configuration.defaultDateTimeFormat) } : undefined; } assetContact.symbol = asset.symbol; } contact.hasAllowance = contact.assets .some((asset) => asset.subAccounts .some((subAccount) => subAccount.allowance)); } return { contacts: contactList }; } }; exports.GetListContactHandler = GetListContactHandler; exports.GetListContactHandler = GetListContactHandler = __decorate([ (0, typedi_1.Service)(), __param(0, (0, typedi_1.Inject)("ILogger")), __param(1, (0, typedi_1.Inject)("AssetManagerConfiguration")), __param(2, (0, typedi_1.Inject)("ContactRepository")), __param(3, (0, typedi_1.Inject)("AssetRepository")), __metadata("design:paramtypes", [Object, assetManagerConfiguration_1.AssetManagerConfiguration, contactRepository_1.ContactRepository, assetRepository_1.AssetRepository, getIcrcAllowanceForContactCacheHandler_1.GetIcrcAllowanceForContactCacheHandler]) ], GetListContactHandler);