UNPKG

test-ic-wallet-middleware-icrc

Version:
94 lines (93 loc) 4 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.AssetLocalCache = void 0; const common_1 = require("@ic-wallet-middleware/common"); const typedi_1 = require("typedi"); let AssetLocalCache = class AssetLocalCache { identifierService; logger; storage; constructor(logger, identifierService, storage) { this.identifierService = identifierService; this.logger = logger; this.storage = storage; } getSubAccountById(ledgerAddress, subAccountId) { const asset = this.getAssetById(ledgerAddress); if (asset) { const subAccount = asset.subAccounts.find((a) => a.subAccountId === subAccountId); return subAccount; } return undefined; } getAssetById(ledgerAddress) { const key = this.getKey(ledgerAddress); const value = this.storage.getItem(key); if (value) { try { const model = (0, common_1.jsonParse)(value); return model; } catch (e) { this.logger.logError(e, "Wrong local cache data", [value]); return undefined; } } return undefined; } setSubAccount(ledgerAddress, subAccount) { const asset = this.getAssetById(ledgerAddress); if (asset) { const subAccountCache = asset.subAccounts.find((a) => a.subAccountId === subAccount.subAccountId); if (subAccountCache) { subAccountCache.balance = subAccount.balance; } else { asset.subAccounts.push(subAccount); } this.setAsset(asset); } } setAsset(asset) { const key = this.getKey(asset.ledgerAddress); this.storage.setItem(key, (0, common_1.jsonStringify)(asset)); } removeAsset(ledgerAddress) { const key = this.getKey(ledgerAddress); this.storage.removeItem(key); } removeSubAccount(ledgerAddress, subAccountId) { const asset = this.getAssetById(ledgerAddress); if (!asset) { throw new common_1.ValidationError("asset.not.found", "ledgerAddress", "Asset Not Found"); } const subAccount = asset.subAccounts.find((a) => a.subAccountId === subAccountId.toString()); if (subAccount && subAccount.balance > 0) { throw new common_1.ValidationError("subAccount.balance.not0", "", "Sub-Account balance more that 0"); } asset.subAccounts = asset.subAccounts.filter((a) => a.subAccountId !== subAccountId.toString()); this.setAsset(asset); } getKey(ledgerAddress) { return `${this.identifierService.getPrincipal()}-${ledgerAddress}`; } }; exports.AssetLocalCache = AssetLocalCache; exports.AssetLocalCache = AssetLocalCache = __decorate([ (0, typedi_1.Service)(), __param(0, (0, typedi_1.Inject)("ILogger")), __param(2, (0, typedi_1.Inject)("IStorage")), __metadata("design:paramtypes", [Object, common_1.IdentifierService, Object]) ], AssetLocalCache);