UNPKG

@ic-wallet-kit/hpl

Version:
129 lines (128 loc) 6.55 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.AddHplVirtualAccountInternalHandler = void 0; const hplWrappers_1 = require("../../../hplWrappers"); const cacheDataHandlers_1 = require("../../../internalHandlers/cacheDataHandlers"); const service_1 = require("../../../service"); const common_1 = require("@ic-wallet-kit/common"); require("reflect-metadata"); const typedi_1 = require("typedi"); let AddHplVirtualAccountInternalHandler = class AddHplVirtualAccountInternalHandler extends common_1.BaseHandler { identifierService; canisterService; hplOwnerCacheDataHandler; hplMintCacheDataHandler; hplDataCacheRepository; constructor(logger, identifierService, canisterService, hplDataCacheRepository, hplOwnerCacheDataHandler, hplMintCacheDataHandler) { super(logger); this.identifierService = identifierService; this.canisterService = canisterService; this.hplOwnerCacheDataHandler = hplOwnerCacheDataHandler; this.hplMintCacheDataHandler = hplMintCacheDataHandler; this.hplDataCacheRepository = hplDataCacheRepository; } validate(form) { if (form.assetId === undefined) { throw new common_1.ValidationError("adding.hpl.virtual.account.internal.assetId.is.required", (0, common_1.getPropertyName)(form, f => f.assetId), "Field assetId is required"); } if (form.accountId === undefined) { throw new common_1.ValidationError("adding.hpl.virtual.account.internal.accountId.is.required", (0, common_1.getPropertyName)(form, f => f.accountId), "Field accountId is required"); } if (!form.accessByPrincipal) { throw new common_1.ValidationError("adding.hpl.virtual.account.internal.accessByPrincipal.is.required", (0, common_1.getPropertyName)(form, f => f.accessByPrincipal), "Field accessByPrincipal is required"); } if (form.amount === undefined) { throw new common_1.ValidationError("adding.hpl.virtual.account.internal.amount.is.required", (0, common_1.getPropertyName)(form, f => f.amount), "Field amount is required"); } return Promise.resolve(); } async process(form) { const ingressActorWrapper = hplWrappers_1.IngressActorWrapper.create(this.identifierService.getAgent(), this.canisterService.getLedgerCanisterId(), this.logger); const virtualAccountId = await ingressActorWrapper.openVirtualAccount(form.assetId, form.accountId, form.accessByPrincipal, form.amount, form.expiration); let hplData = this.hplDataCacheRepository.getHplDataByCanisterId(this.canisterService.getLedgerCanisterId()); if (!hplData) { hplData = { accounts: { accountLastId: BigInt(0), accounts: [] }, virtualAccounts: { virtualAccountLastId: BigInt(0), virtualAccounts: [] }, ftAssets: { ftAssetLastId: BigInt(0), ftAssets: [] }, remotes: [] }; } hplData.virtualAccounts.virtualAccountLastId = hplData.virtualAccounts.virtualAccountLastId + BigInt(1); hplData.virtualAccounts.virtualAccounts.push({ virtualAccountId: virtualAccountId, virtualAccountInfo: { accountType: { ft: form.assetId }, principal: form.accessByPrincipal.toString() } }); this.hplDataCacheRepository.setHplData(this.canisterService.getLedgerCanisterId(), hplData); const mintResult = await this.hplMintCacheDataHandler.handle({ canisterId: form.accessByPrincipal.toString(), loadType: common_1.LoadType.Full }); if (!mintResult.isSuccess) { throw mintResult.errors; } const mint = mintResult.data ?? { canisterId: form.accessByPrincipal.toString(), isMinter: false }; const ownerDataResult = await this.hplOwnerCacheDataHandler.handle({ principal: this.identifierService.getPrincipal(), loadType: common_1.LoadType.Cache }); if (!ownerDataResult.isSuccess) { throw ownerDataResult.errors; } const ownerData = ownerDataResult.data ?? { ownerId: undefined }; const result = { virtualAccountId: virtualAccountId, accountId: form.accountId, accessBy: form.accessByPrincipal.toString(), amount: form.amount, currencyAmount: "0.0", code: (0, common_1.getPxlCode)(ownerData.ownerId ? ownerData.ownerId.toString() : "0", virtualAccountId), isMint: mint.isMinter, name: form.virtualAccountName, expiration: form.expiration, assetId: form.assetId, assetSymbol: "" }; return result; } }; exports.AddHplVirtualAccountInternalHandler = AddHplVirtualAccountInternalHandler; exports.AddHplVirtualAccountInternalHandler = AddHplVirtualAccountInternalHandler = __decorate([ (0, typedi_1.Service)(), __param(0, (0, typedi_1.Inject)("ILogger")), __param(3, (0, typedi_1.Inject)("IHplDataCacheRepository")), __metadata("design:paramtypes", [Object, common_1.IdentifierService, service_1.CanisterService, Object, cacheDataHandlers_1.HplOwnerCacheDataHandler, cacheDataHandlers_1.HplMintCacheDataHandler]) ], AddHplVirtualAccountInternalHandler);