@ic-wallet-kit/hpl
Version:
Ic middleware wallet HPL protocol
148 lines (147 loc) • 7.26 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.LoadHplAccountsHandler = 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 LoadHplAccountsHandler = class LoadHplAccountsHandler extends common_1.BaseHandler {
identifierService;
hplAccountCacheDataHandler;
hplVirtualAccountCacheDataHandler;
hplAccountsStateCacheDataHandler;
hplVirtualAccountsStateCacheDataHandler;
hplOwnerCacheDataHandler;
hplMintCacheDataHandler;
constructor(logger, identifierService, hplAccountCacheDataHandler, hplVirtualAccountCacheDataHandler, hplAccountsStateCacheDataHandler, hplVirtualAccountsStateCacheDataHandler, hplOwnerCacheDataHandler, hplMintCacheDataHandler) {
super(logger);
this.identifierService = identifierService;
this.hplAccountCacheDataHandler = hplAccountCacheDataHandler;
this.hplVirtualAccountCacheDataHandler = hplVirtualAccountCacheDataHandler;
this.hplAccountsStateCacheDataHandler = hplAccountsStateCacheDataHandler;
this.hplVirtualAccountsStateCacheDataHandler = hplVirtualAccountsStateCacheDataHandler;
this.hplOwnerCacheDataHandler = hplOwnerCacheDataHandler;
this.hplMintCacheDataHandler = hplMintCacheDataHandler;
}
async validate(form) { }
async process(form) {
const hplAssetInfo = {
loadType: form.loadType
};
const [accountResult, virtualAccountResult] = await Promise.all([
this.hplAccountCacheDataHandler.process(hplAssetInfo),
this.hplVirtualAccountCacheDataHandler.process(hplAssetInfo),
]);
let mints = [];
if (virtualAccountResult.virtualAccounts.length > 0) {
mints = await this.getMintPrinc(virtualAccountResult.virtualAccounts, form.loadType);
}
const ownerCache = await this.hplOwnerCacheDataHandler.process({
principal: this.identifierService.getPrincipal(),
loadType: form.loadType
});
const hplStateCacheDataInfo = {
accountCount: accountResult.accountLastId,
ftAssetCount: BigInt(0),
virtualAccountCount: virtualAccountResult.virtualAccountLastId,
remoteAccounts: [],
loadType: form.loadType
};
const virtualAccountsStateResult = await this.hplVirtualAccountsStateCacheDataHandler.process(hplStateCacheDataInfo);
const accountStateCacheDataHandlerResult = await this.hplAccountsStateCacheDataHandler.process(hplStateCacheDataInfo);
const virtualAccounts = this.getHplVirtualAccounts(virtualAccountsStateResult, virtualAccountResult.virtualAccounts, mints, ownerCache.ownerId);
const accounts = [];
accountStateCacheDataHandlerResult.map((sa) => {
const subData = accountResult.accounts.find((sub) => sub.accountId === sa.accountId);
const auxVirtuals = virtualAccounts.filter((va) => va.accountId == sa.accountId);
accounts.push({
name: "",
accountId: sa.accountId,
amount: sa.accountState.ft,
currencyAmount: "0.00",
transactionFee: "0",
ft: subData ? subData.accountType.ft : BigInt(0),
virtualAccounts: auxVirtuals,
});
});
const result = {
accounts: accounts,
};
return result;
}
async getMintPrinc(virtualAccountInfo, loadType) {
const auxPrincipal = [];
virtualAccountInfo.map((vt) => {
auxPrincipal.push(vt.virtualAccountInfo.principal);
});
const checkPrinc = 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 checkPrinc
.filter((cp) => cp.isMinter);
}
getHplVirtualAccounts(stateData, vtCache, mints, ownerId) {
const auxFullVirtuals = [];
stateData.map((va) => {
const vtData = vtCache.find((vt) => vt.virtualAccountId === va.virtualAccountId);
const vtMint = mints.find((vt) => vt.canisterId === vtData?.virtualAccountInfo.principal);
const prinCode = ownerId ? ownerId.toString() : "";
const newCode = (0, common_1.getPxlCode)(prinCode, va.virtualAccountId);
auxFullVirtuals.push({
name: "",
virtualAccountId: va.virtualAccountId,
amount: va.accountState.ft,
currencyAmount: "0.00",
expiration: va.time,
accessBy: vtData ? vtData.virtualAccountInfo.principal : "",
accountId: va.accountId,
assetId: vtData ? vtData.virtualAccountInfo.accountType.ft : BigInt(0),
assetSymbol: "",
code: newCode,
isMint: vtMint ? vtMint.isMinter : false,
});
});
return auxFullVirtuals;
}
};
exports.LoadHplAccountsHandler = LoadHplAccountsHandler;
exports.LoadHplAccountsHandler = LoadHplAccountsHandler = __decorate([
(0, typedi_1.Service)(),
__param(0, (0, typedi_1.Inject)("ILogger")),
__metadata("design:paramtypes", [Object, common_1.IdentifierService,
cacheDataHandlers_1.HplAccountCacheDataHandler,
cacheDataHandlers_1.HplVirtualAccountCacheDataHandler,
cacheDataHandlers_1.HplAccountsStateCacheDataHandler,
cacheDataHandlers_1.HplVirtualAccountsStateCacheDataHandler,
cacheDataHandlers_1.HplOwnerCacheDataHandler,
cacheDataHandlers_1.HplMintCacheDataHandler])
], LoadHplAccountsHandler);