test-ic-wallet-middleware-icrc
Version:
Ic middleware wallet ICRC protocol
103 lines (102 loc) • 5.79 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.UpdateAllowanceHandler = void 0;
const common_1 = require("@ic-wallet-middleware/common");
const assetMetaDataCacheHandler_1 = require("../../../internalHandlers/icrcCacheDataHandlers/assets/assetMetaDataCacheHandler/assetMetaDataCacheHandler");
const subAccountBalanceHandler_1 = require("../../../internalHandlers/icrcCacheDataHandlers/assets/subAccountBalanceHandler/subAccountBalanceHandler");
const allowanceMapper_1 = require("../../../maps/allowanceMapper");
const repositories_1 = require("../../../repositories");
const types_1 = require("../../../types");
const dateTimeUtils_1 = require("../../../utils/dateTimeUtils");
const wrappers_1 = require("../../../wrappers");
const typedi_1 = require("typedi");
let UpdateAllowanceHandler = class UpdateAllowanceHandler extends common_1.BaseHandler {
identifierService;
assetMetaDataHandler;
allowanceLocalCache;
subAccountBalanceHandler;
configuration;
constructor(logger, identifierService, assetMetaDataHandler, allowanceLocalCache, subAccountBalanceHandler, configuration) {
super(logger);
this.identifierService = identifierService;
this.assetMetaDataHandler = assetMetaDataHandler;
this.allowanceLocalCache = allowanceLocalCache;
this.subAccountBalanceHandler = subAccountBalanceHandler;
this.configuration = configuration;
}
validate(form) {
if (!form.ledgerAddress) {
throw new common_1.ValidationError("update.allowance.ledgerAddress.is.required", "ledgerAddress", "Field ledgerAddress is required");
}
if (!form.spenderPrincipal) {
throw new common_1.ValidationError("update.allowance.spender.is.required", "spender", "Field spender is required");
}
if (!form.subAccountId) {
throw new common_1.ValidationError("update.allowance.subAccountId.is.required", "subAccountId", "Field subAccountId is required");
}
if (!form.amount) {
throw new common_1.ValidationError("update.allowance.amount.is.required", "amount", "Field amount is required");
}
return Promise.resolve();
}
async process(form) {
let asset = await this.assetMetaDataHandler.handle({
ledgerAddress: form.ledgerAddress,
loadType: common_1.LoadType.Quick
});
if (!asset) {
throw new common_1.ValidationError("asset.not.found", (0, common_1.getPropertyName)(form, f => f.ledgerAddress), "Asset Not Found");
}
const subAccountBalance = await this.subAccountBalanceHandler.handle({
ledgerAddress: form.ledgerAddress,
loadType: common_1.LoadType.Full,
subAccountId: form.subAccountId
});
if (subAccountBalance.balance < asset.fee) {
throw new common_1.ValidationError("balance.less.fee", "", "Balance should be more that ledger fee");
}
const sentAmount = common_1.AmountProvider.toBigInt(form.amount, asset.decimals);
if (!sentAmount) {
throw new common_1.ValidationError("transfer.allowance.invalid.amount", (0, common_1.getPropertyName)(form, f => f.amount), "Invalid amount");
}
if (sentAmount < asset.fee) {
throw new common_1.ValidationError("amount.less.fee", "", "Amount should be more that ledger fee");
}
const expiration = (0, dateTimeUtils_1.convertDateStringToBigInt)(form.expiration, this.configuration.defaultDateTimeFormat);
const cache = (0, allowanceMapper_1.allowanceFormToCache)(form, sentAmount, expiration);
const model = (0, allowanceMapper_1.allowanceCacheModelToDataModel)(cache);
await wrappers_1.LedgerWrapper.approveAllowance(model, this.identifierService.getAgent());
this.allowanceLocalCache.updateOrAddAllowance(cache);
return {
ledgerAddress: cache.ledgerAddress,
subAccountId: types_1.SubAccountId.parseFromString(cache.subAccountId),
spenderPrincipal: cache.spenderPrincipal,
spenderSubId: types_1.SubAccountId.parseFromString(cache.spenderSubId),
amount: cache.amount,
expiration: form.expiration
};
}
};
exports.UpdateAllowanceHandler = UpdateAllowanceHandler;
exports.UpdateAllowanceHandler = UpdateAllowanceHandler = __decorate([
(0, typedi_1.Service)(),
__param(0, (0, typedi_1.Inject)("ILogger")),
__param(5, (0, typedi_1.Inject)("AssetManagerConfiguration")),
__metadata("design:paramtypes", [Object, common_1.IdentifierService,
assetMetaDataCacheHandler_1.AssetMetaDataCacheHandler,
repositories_1.AllowanceLocalCache,
subAccountBalanceHandler_1.SubAccountBalanceHandler,
types_1.AssetManagerConfiguration])
], UpdateAllowanceHandler);