test-ic-wallet-middleware-icrc
Version:
Ic middleware wallet ICRC protocol
105 lines (104 loc) • 5.89 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.GetListAllowanceHandler = void 0;
const common_1 = require("@ic-wallet-middleware/common");
const getIcrcAllowanceCacheHandler_1 = require("../../../internalHandlers/icrcCacheDataHandlers/allowances/getIcrcAllowanceCacheHandler/getIcrcAllowanceCacheHandler");
const assetMetaDataCacheHandler_1 = require("../../../internalHandlers/icrcCacheDataHandlers/assets/assetMetaDataCacheHandler/assetMetaDataCacheHandler");
const allowanceRepository_1 = require("../../../repositories/persists/allowanceRepository/allowanceRepository");
const assetRepository_1 = require("../../../repositories/persists/assetRepository/assetRepository");
const types_1 = require("../../../types");
const assetManagerConfiguration_1 = require("../../../types/configuration/assetManagerConfiguration");
const dateTimeUtils_1 = require("../../../utils/dateTimeUtils");
require("reflect-metadata");
const typedi_1 = require("typedi");
let GetListAllowanceHandler = class GetListAllowanceHandler extends common_1.BaseHandler {
allowanceRepository;
getIcrcAllowanceHandler;
assetMetaDataHandler;
configuration;
assetRepository;
constructor(logger, configuration, allowanceRepository, assetRepository, getIcrcAllowanceHandler, assetMetaDataHandler) {
super(logger);
this.allowanceRepository = allowanceRepository;
this.getIcrcAllowanceHandler = getIcrcAllowanceHandler;
this.assetMetaDataHandler = assetMetaDataHandler;
this.configuration = configuration;
this.assetRepository = assetRepository;
}
validate(form) {
if (!form.ledgerAddress) {
throw new common_1.ValidationError("get.allowance.ledgerAddress.is.required", "ledgerAddress", "Field ledgerAddress is required");
}
return Promise.resolve();
}
async process(form) {
const result = {
allowances: [],
};
const asset = await this.assetRepository.getAssetOrDefault(form.ledgerAddress);
if (!asset) {
throw new common_1.ValidationError("get.asset.allowances.not.found", "ledgerAddress", "Asset Not Found");
}
const storageAllowances = await this.allowanceRepository.getAssetAllowances(form.ledgerAddress);
const handlers = [];
storageAllowances.forEach((storageAllowance) => {
const info = {
spenderPrincipal: storageAllowance.spenderPrincipal,
spenderSubId: types_1.SubAccountId.parseFromString(storageAllowance.spenderSubId),
ledgerAddress: storageAllowance.ledgerAddress,
subAccountId: types_1.SubAccountId.parseFromString(storageAllowance.subAccountId),
loadType: form.loadType
};
handlers.push(this.getIcrcAllowanceHandler.handle(info));
});
// TODO: make sure that if something failed -> handler works well
const handlersResult = await Promise.all(handlers);
let assetMetaData = await this.assetMetaDataHandler.handle({
ledgerAddress: form.ledgerAddress,
loadType: common_1.LoadType.Cache
});
storageAllowances.forEach((item) => {
const icrcAllowance = handlersResult.find((h) => h.spenderPrincipal == item.spenderPrincipal
&& h.ledgerAddress == item.ledgerAddress
&& h.subAccountId == item.subAccountId
&& h.spenderSubId == item.spenderSubId);
if (icrcAllowance) {
const allowance = {
spenderPrincipal: icrcAllowance.spenderPrincipal,
spenderSubId: types_1.SubAccountId.parseFromString(icrcAllowance.spenderSubId),
ledgerAddress: icrcAllowance.ledgerAddress,
subAccountId: types_1.SubAccountId.parseFromString(icrcAllowance.subAccountId),
amount: icrcAllowance.amount,
decimal: assetMetaData.decimals,
expiration: (0, dateTimeUtils_1.convertBigIntToDateString)(icrcAllowance.expiration, this.configuration.defaultDateTimeFormat)
};
result.allowances.push(allowance);
}
});
return result;
}
};
exports.GetListAllowanceHandler = GetListAllowanceHandler;
exports.GetListAllowanceHandler = GetListAllowanceHandler = __decorate([
(0, typedi_1.Service)(),
__param(0, (0, typedi_1.Inject)("ILogger")),
__param(1, (0, typedi_1.Inject)("AssetManagerConfiguration")),
__param(3, (0, typedi_1.Inject)("AssetRepository")),
__metadata("design:paramtypes", [Object, assetManagerConfiguration_1.AssetManagerConfiguration,
allowanceRepository_1.AllowanceRepository,
assetRepository_1.AssetRepository,
getIcrcAllowanceCacheHandler_1.GetIcrcAllowanceCacheHandler,
assetMetaDataCacheHandler_1.AssetMetaDataCacheHandler])
], GetListAllowanceHandler);