UNPKG

@ic-wallet-kit/hpl

Version:
93 lines (92 loc) 4.39 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.AddHplAccountInternalHandler = void 0; const hplWrappers_1 = require("../../../hplWrappers"); const service_1 = require("../../../service"); const common_1 = require("@ic-wallet-kit/common"); require("reflect-metadata"); const typedi_1 = require("typedi"); let AddHplAccountInternalHandler = class AddHplAccountInternalHandler extends common_1.BaseHandler { identifierService; canisterService; hplDataCacheRepository; constructor(logger, identifierService, canisterService, hplDataCacheRepository) { super(logger); this.identifierService = identifierService; this.canisterService = canisterService; this.hplDataCacheRepository = hplDataCacheRepository; } validate(form) { if (form.assetId === undefined) { throw new common_1.ValidationError("adding.hpl.account.internal.assetId.is.required", (0, common_1.getPropertyName)(form, f => f.assetId), "Field assetId is required"); } if (!form.accountName) { throw new common_1.ValidationError("adding.hpl.account.internal.accountName.is.required", (0, common_1.getPropertyName)(form, f => f.accountName), "Field accountName is required"); } return Promise.resolve(); } async process(form) { const ingressActorWrapper = hplWrappers_1.IngressActorWrapper.create(this.identifierService.getAgent(), this.canisterService.getLedgerCanisterId(), this.logger); const accountId = await ingressActorWrapper.openAccount(form.assetId); const accountInfo = await ingressActorWrapper.getAccountInfo(accountId); const accountType = accountInfo.map((ai) => { return { ft: ai[1].ft }; })[0]; 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.accounts.accountLastId = hplData.accounts.accountLastId + BigInt(1); hplData.accounts.accounts.push({ accountId: accountId, accountType: accountType }); this.hplDataCacheRepository.setHplData(this.canisterService.getLedgerCanisterId(), hplData); const result = { accountId: accountId, ft: accountType.ft, amount: BigInt(0), currencyAmount: "0.0", transactionFee: "0", name: form.accountName, virtualAccounts: [] }; return result; } }; exports.AddHplAccountInternalHandler = AddHplAccountInternalHandler; exports.AddHplAccountInternalHandler = AddHplAccountInternalHandler = __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]) ], AddHplAccountInternalHandler);