@ic-wallet-kit/hpl
Version:
Ic middleware wallet HPL protocol
123 lines (122 loc) • 6.02 kB
JavaScript
;
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.LoadHplVirtualAccountsHandler = void 0;
const cacheDataHandlers_1 = require("../../../internalHandlers/cacheDataHandlers");
const mintUtils_1 = require("../../../utils/mintUtils");
const common_1 = require("@ic-wallet-kit/common");
require("reflect-metadata");
const typedi_1 = require("typedi");
let LoadHplVirtualAccountsHandler = class LoadHplVirtualAccountsHandler extends common_1.BaseHandler {
identifierService;
hplVirtualAccountCacheDataHandler;
hplVirtualAccountsStateCacheDataHandler;
hplOwnerCacheDataHandler;
hplMintCacheDataHandler;
constructor(logger, identifierService, hplVirtualAccountCacheDataHandler, hplVirtualAccountsStateCacheDataHandler, hplOwnerCacheDataHandler, hplMintCacheDataHandler) {
super(logger);
this.identifierService = identifierService;
this.hplVirtualAccountCacheDataHandler = hplVirtualAccountCacheDataHandler;
this.hplVirtualAccountsStateCacheDataHandler = hplVirtualAccountsStateCacheDataHandler;
this.hplOwnerCacheDataHandler = hplOwnerCacheDataHandler;
this.hplMintCacheDataHandler = hplMintCacheDataHandler;
}
async validate(form) { }
async process(form) {
const hplAssetInfo = {
loadType: form.loadType
};
const virtualAccountResult = await this.hplVirtualAccountCacheDataHandler.process(hplAssetInfo);
let mints = [];
if (virtualAccountResult.virtualAccounts.length > 0) {
mints = await this.getMintPrincipal(virtualAccountResult.virtualAccounts, form.loadType);
}
const ownerCache = await this.hplOwnerCacheDataHandler.process({
principal: this.identifierService.getPrincipal(),
loadType: form.loadType
});
const hplStateCacheDataInfo = {
accountCount: BigInt(0),
ftAssetCount: BigInt(0),
virtualAccountCount: virtualAccountResult.virtualAccountLastId,
remoteAccounts: [],
loadType: form.loadType
};
const stateResult = await this.hplVirtualAccountsStateCacheDataHandler.process(hplStateCacheDataInfo);
const virtualAccounts = this.getHplVirtualAccounts(stateResult, virtualAccountResult.virtualAccounts, mints, ownerCache.ownerId);
const result = {
virtualAccounts: virtualAccounts,
};
return result;
}
async getMintPrincipal(virtualAccountInfo, loadType) {
const auxPrincipal = [];
virtualAccountInfo.map((vt) => {
auxPrincipal.push(vt.virtualAccountInfo.principal);
});
const checkPrincipal = await Promise.all(auxPrincipal
.map(async (principal) => {
let result = {
canisterId: principal,
isMinter: false
};
if ((0, mintUtils_1.getPrincipalGroupsQty)(principal) < 6) {
try {
result = await this.hplMintCacheDataHandler.process({
canisterId: principal,
loadType: loadType
});
}
catch (error) {
this.logger.logError(error);
}
}
return result;
}));
return checkPrincipal.filter((cp) => cp.isMinter);
}
getHplVirtualAccounts(stateData, vtCache, mints, ownerId) {
const auxFullVirtual = [];
stateData.map((va) => {
const vtData = vtCache.find((vt) => vt.virtualAccountId === va.virtualAccountId);
const vtMint = mints.find((vt) => vt.canisterId === vtData?.virtualAccountInfo.principal);
const principalCode = ownerId ? ownerId.toString() : "";
const newCode = (0, common_1.getPxlCode)(principalCode, va.virtualAccountId);
auxFullVirtual.push({
name: "",
virtualAccountId: va.virtualAccountId,
amount: va.accountState.ft,
currencyAmount: "0.00",
expiration: va.time,
accessBy: vtData ? vtData.virtualAccountInfo.principal.toString() : "",
accountId: va.accountId,
assetId: vtData ? vtData.virtualAccountInfo.accountType.ft : BigInt(0),
assetSymbol: "",
code: newCode,
isMint: vtMint ? vtMint.isMinter : false,
});
});
return auxFullVirtual;
}
};
exports.LoadHplVirtualAccountsHandler = LoadHplVirtualAccountsHandler;
exports.LoadHplVirtualAccountsHandler = LoadHplVirtualAccountsHandler = __decorate([
(0, typedi_1.Service)(),
__param(0, (0, typedi_1.Inject)("ILogger")),
__metadata("design:paramtypes", [Object, common_1.IdentifierService,
cacheDataHandlers_1.HplVirtualAccountCacheDataHandler,
cacheDataHandlers_1.HplVirtualAccountsStateCacheDataHandler,
cacheDataHandlers_1.HplOwnerCacheDataHandler,
cacheDataHandlers_1.HplMintCacheDataHandler])
], LoadHplVirtualAccountsHandler);