UNPKG

@ic-wallet-kit/hpl

Version:
109 lines (108 loc) 5.87 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.TransferHandler = void 0; const internalHandlers_1 = require("../../../internalHandlers"); const service_1 = require("../../../service"); const transferProvider_1 = require("../../../utils/transferProvider/transferProvider"); const common_1 = require("@ic-wallet-kit/common"); const hpl_client_1 = require("@research-ag/hpl-client"); require("reflect-metadata"); const rxjs_1 = require("rxjs"); const typedi_1 = require("typedi"); let TransferHandler = class TransferHandler extends common_1.BaseHandler { identifierService; canisterService; hplFtAssetCacheDataHandler; constructor(logger, identifierService, canisterService, hplFtAssetCacheDataHandler) { super(logger); this.identifierService = identifierService; this.canisterService = canisterService; this.hplFtAssetCacheDataHandler = hplFtAssetCacheDataHandler; } validate(form) { if (form.assetId === undefined) { throw new common_1.ValidationError("hpl.transfer.assetId.is.required", (0, common_1.getPropertyName)(form, f => f.assetId), "Field assetId is required"); } if (form.amount === undefined) { throw new common_1.ValidationError("hpl.transfer.amount.is.required", (0, common_1.getPropertyName)(form, f => f.amount), "Field amount is required"); } if (!form.txFrom) { throw new common_1.ValidationError("hpl.transfer.from.is.required", (0, common_1.getPropertyName)(form, f => f.txFrom), "Field from is required"); } if (!form.txTo) { throw new common_1.ValidationError("hpl.transfer.to.is.required", (0, common_1.getPropertyName)(form, f => f.txTo), "Field to is required"); } return Promise.resolve(); } async process(form) { const assetsResult = await this.hplFtAssetCacheDataHandler.handle({ loadType: common_1.LoadType.Cache }); const asset = assetsResult.data?.ftAssets.find((a) => a.assetId === form.assetId); if (!asset) { throw new common_1.ValidationError("asset.not.found", (0, common_1.getPropertyName)(form, f => f.assetId), "Asset Not Found"); } const sentAmount = common_1.AmountProvider.toBigInt(form.amount, asset.ftAssetInfo.decimals); if (!sentAmount) { throw new common_1.ValidationError("transaction.invalid.amount", (0, common_1.getPropertyName)(form, f => f.amount), "Invalid amount"); } let identity = this.identifierService.getIdentity(); const ledgerId = this.canisterService.getLedgerCanisterId(); const hplClient = new hpl_client_1.HPLClient(ledgerId, "ic"); await hplClient.setIdentity(identity); const aggregator = await hplClient.pickAggregator(); if (aggregator) { const txId = await hplClient.simpleTransfer(aggregator, transferProvider_1.TransferAccountReferenceProvider.toTransferAccountReference(form.txFrom), transferProvider_1.TransferAccountReferenceProvider.toTransferAccountReference(form.txTo), form.assetId, sentAmount); let validTx = false; let insufficientFunds = false; let errorMessage = ""; await (0, rxjs_1.lastValueFrom)(hplClient.pollTx(aggregator, txId).pipe((0, rxjs_1.map)((x) => { if (x.status === "processed") { if (x.statusPayload[0].failure) { const ftTransfer = x.statusPayload[0].failure.ftTransfer; errorMessage = JSON.stringify(ftTransfer, hpl_client_1.bigIntReplacer); if (errorMessage.includes("InsufficientFunds")) { insufficientFunds = true; } } else if (x.statusPayload[0].success) { validTx = true; } } }), (0, rxjs_1.catchError)((e) => { errorMessage = e.message; return (0, rxjs_1.of)(null); }))); if (validTx) { return {}; } if (insufficientFunds) { throw new common_1.ValidationError("insufficient.funds", "", "Insufficient funds"); } if (errorMessage) { throw new common_1.ValidationError("simple.transfer.error", "", errorMessage); } } throw new common_1.ValidationError("could.not.pick.aggregator", "", "Could not pick aggregator"); } }; exports.TransferHandler = TransferHandler; exports.TransferHandler = TransferHandler = __decorate([ (0, typedi_1.Service)(), __param(0, (0, typedi_1.Inject)("ILogger")), __metadata("design:paramtypes", [Object, common_1.IdentifierService, service_1.CanisterService, internalHandlers_1.HplFtAssetCacheDataHandler]) ], TransferHandler);