UNPKG

test-ic-wallet-middleware-icrc

Version:
106 lines (105 loc) 6.33 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.TransferFromAllowanceHandler = void 0; const common_1 = require("@ic-wallet-middleware/common"); const getAllowanceSubAccountBalanceCacheHandler_1 = require("../../../internalHandlers/icrcCacheDataHandlers/allowances/getAllowanceSubAccountBalanceCacheHandler/getAllowanceSubAccountBalanceCacheHandler"); const assetMetaDataCacheHandler_1 = require("../../../internalHandlers/icrcCacheDataHandlers/assets/assetMetaDataCacheHandler/assetMetaDataCacheHandler"); const assetRepository_1 = require("../../../repositories/persists/assetRepository/assetRepository"); const supportedStandardEnum_1 = require("../../../types/wallets/supportedStandardEnum"); const ledgerWrapper_1 = require("../../../wrappers/icrc/ledgerWrapper/ledgerWrapper"); require("reflect-metadata"); const typedi_1 = require("typedi"); let TransferFromAllowanceHandler = class TransferFromAllowanceHandler extends common_1.BaseHandler { identifierService; assetMetaDataHandler; assetRepository; getAllowanceSubAccountBalanceHandler; constructor(logger, identifierService, assetMetaDataHandler, assetRepository, getAllowanceSubAccountBalanceHandler) { super(logger); this.identifierService = identifierService; this.assetMetaDataHandler = assetMetaDataHandler; this.assetRepository = assetRepository; this.getAllowanceSubAccountBalanceHandler = getAllowanceSubAccountBalanceHandler; } validate(form) { if (!form.ledgerAddress) { throw new common_1.ValidationError("transfer.allowance.ledgerAddress.is.required", "ledgerAddress", "Field ledgerAddress is required"); } if (!form.receiverPrincipal) { throw new common_1.ValidationError("transfer.allowance.receiverPrincipal.is.required", "receiverPrincipal", "Field receiverPrincipal is required"); } if (!form.toSubAccountId) { throw new common_1.ValidationError("transfer.allowance.toSubAccountId.is.required", "toSubAccountId", "Field toSubAccountId is required"); } if (!form.senderPrincipal) { throw new common_1.ValidationError("transfer.allowance.senderPrincipal.is.required", "senderPrincipal", "Field senderPrincipal is required"); } if (!form.fromSubAccountId) { throw new common_1.ValidationError("transfer.allowance.fromSubAccountId.is.required", "fromSubAccountId", "Field fromSubAccountId is required"); } if (!form.amount) { throw new common_1.ValidationError("transfer.allowance.amount.is.required", "amount", "Field transferAmount is required"); } return Promise.resolve(); } async process(form) { const isSupportedStandard = await this.assetRepository.checkSupportedStandard(form.ledgerAddress, supportedStandardEnum_1.SupportedStandardEnum.ICRC2); if (!isSupportedStandard) { throw new common_1.ValidationError("asset.not.supported.standard", "", "Asset not supported standard ICRC-2"); } 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 fromSubAccountId = form.fromSubAccountId; const toSubAccountId = form.toSubAccountId; 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"); } const subAccountBalance = await this.getAllowanceSubAccountBalanceHandler.handle({ principal: form.senderPrincipal.toText(), ledgerAddress: form.ledgerAddress, loadType: common_1.LoadType.Full, subAccountId: fromSubAccountId }); if (subAccountBalance.balance - asset.fee < sentAmount) { throw new common_1.ValidationError("balance.less.amount", "transferAmount", "The sent amount should be lees that the balance"); } const ledgerWrapper = ledgerWrapper_1.LedgerWrapper.create(this.identifierService.getAgent(), form.ledgerAddress); await ledgerWrapper.transferFrom({ amount: sentAmount, fromAccountPrincipal: form.senderPrincipal, fromSubAccountId: fromSubAccountId, toSubAccountId: toSubAccountId, toAccountPrincipal: form.receiverPrincipal }); //TODO: make sure that need reload balances ??? return {}; } }; exports.TransferFromAllowanceHandler = TransferFromAllowanceHandler; exports.TransferFromAllowanceHandler = TransferFromAllowanceHandler = __decorate([ (0, typedi_1.Service)(), __param(0, (0, typedi_1.Inject)("ILogger")), __param(3, (0, typedi_1.Inject)("AssetRepository")), __metadata("design:paramtypes", [Object, common_1.IdentifierService, assetMetaDataCacheHandler_1.AssetMetaDataCacheHandler, assetRepository_1.AssetRepository, getAllowanceSubAccountBalanceCacheHandler_1.GetAllowanceSubAccountBalanceCacheHandler]) ], TransferFromAllowanceHandler);