UNPKG

@payburner/keyburner-sidewinder-core

Version:
138 lines 9.33 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OffRampResultTransactionProcessor = void 0; const npm_1 = require("@payburner/keyburner-sidewinder-model/dist/npm"); const TransactionProcessorBase_1 = require("./TransactionProcessorBase"); const CommonErrorCodes_1 = require("../model/CommonErrorCodes"); class OffRampResultTransactionProcessor extends TransactionProcessorBase_1.TransactionProcessorBase { constructor(globalAccountService, tokenService, underlyingTransactionService) { super(globalAccountService, tokenService); this.underlyingTransactionService = underlyingTransactionService; } doProcess(decodedTransaction, items) { const self = this; return new Promise((resolve, reject) => { console.log('Processing off ramp result: ' + decodedTransaction.id); const transferTransaction = decodedTransaction.payload; self.getTokenService().getToken(transferTransaction.environment, transferTransaction.token_symbol) .then((token) => __awaiter(this, void 0, void 0, function* () { self.underlyingTransactionService.getUnderlyingTransaction(transferTransaction.underlying_txn_id) .then((underlyingTransaction) => __awaiter(this, void 0, void 0, function* () { if (underlyingTransaction.internal_address !== token.token_issuer_address) { let withdrawAccount = null; try { withdrawAccount = yield this.getTokenService() .getTokenAccount(transferTransaction.environment, transferTransaction.token_symbol, underlyingTransaction.internal_address); console.log('Got Withdraw Account'); } catch (error) { resolve(CommonErrorCodes_1.CommonErrorCodes.TOKEN_ACCOUNT_NOT_FOUND); } let isWithdrawAccountPermissioned = false; if (token.is_permissioned && withdrawAccount === null) { isWithdrawAccountPermissioned = yield this.getTokenService() .isAddressPermissionedOnToken(transferTransaction.environment, transferTransaction.token_symbol, underlyingTransaction.internal_address); } if (token.frozen) { resolve(CommonErrorCodes_1.CommonErrorCodes.TOKEN_IS_FROZEN); return; } else if (withdrawAccount !== null && withdrawAccount.frozen) { resolve(CommonErrorCodes_1.CommonErrorCodes.TOKEN_ACCOUNT_WITHDRAW_ACCOUNT_IS_FROZEN); return; } else if (withdrawAccount === null && token.is_permissioned && !isWithdrawAccountPermissioned) { resolve(CommonErrorCodes_1.CommonErrorCodes.TOKEN_ACCOUNT_WITHDRAW_ACCOUNT_NO_ACCESS_TO_TOKEN); return; } } console.log('Transaction Direction:' + underlyingTransaction.transaction_direction + ' ' + npm_1.UnderlyingTransactionDirection.WITHDRAW); if (underlyingTransaction.transaction_direction !== npm_1.UnderlyingTransactionDirection.WITHDRAW) { resolve(CommonErrorCodes_1.CommonErrorCodes.UNDERLYING_ATTEMPTING_TO_UPDATE_DEPOSIT_ON_OFFRAMP); return; } console.log('Transaction Status:' + underlyingTransaction.status + ' ' + npm_1.UnderlyingTransactionStatus.PENDING); if (underlyingTransaction.status !== npm_1.UnderlyingTransactionStatus.PENDING) { resolve(CommonErrorCodes_1.CommonErrorCodes.UNDERLYING_STATUS_MUST_BE_PENDING); return; } if (transferTransaction.status === npm_1.UnderlyingTransactionStatus.PENDING) { resolve(CommonErrorCodes_1.CommonErrorCodes.UNDERLYING_STATUS_UPDATE_CANNOT_BE_PENDING); return; } else if (transferTransaction.status === npm_1.UnderlyingTransactionStatus.FAILED) { items.push({ Update: { TableName: 'sidewinder_token_account', Key: { token_account_id: npm_1.AccountUtils.calculateTokenAccountId(transferTransaction.environment, transferTransaction.token_symbol, underlyingTransaction.internal_address), }, UpdateExpression: 'SET available_balance = available_balance + :transfer_amount, locked_balance = locked_balance - :transfer_amount', 'ExpressionAttributeValues': { ':transfer_amount': underlyingTransaction.transfer_amount } } }); } else if (transferTransaction.status === npm_1.UnderlyingTransactionStatus.COMPLETED) { items.push({ Update: { TableName: 'sidewinder_token_account', Key: { token_account_id: npm_1.AccountUtils.calculateTokenAccountId(transferTransaction.environment, transferTransaction.token_symbol, underlyingTransaction.internal_address), }, UpdateExpression: 'SET locked_balance = locked_balance - :transfer_amount, total_balance = total_balance - :transfer_amount', 'ExpressionAttributeValues': { ':transfer_amount': underlyingTransaction.transfer_amount } } }); if (underlyingTransaction.internal_address !== token.token_issuer_address) { items.push({ Update: { TableName: 'sidewinder_token_account', Key: { token_account_id: npm_1.AccountUtils.calculateTokenAccountId(transferTransaction.environment, transferTransaction.token_symbol, token.token_issuer_address), }, UpdateExpression: 'SET available_balance = available_balance + :fee_amount, total_balance = total_balance + :fee_amount', 'ExpressionAttributeValues': { ':fee_amount': underlyingTransaction.transfer_amount * token.offramp_fee_multiplier } } }); } } items.push({ Update: { TableName: 'sidewinder_underlying_transactions', Key: { underlying_txn_id: underlyingTransaction.underlying_txn_id }, "ExpressionAttributeNames": { "#stat": "status" }, UpdateExpression: 'SET #stat = :txn_status, closed_timestamp = :closed_timestamp', 'ExpressionAttributeValues': { ':closed_timestamp': new Date().toISOString(), ':pending_status': npm_1.UnderlyingTransactionStatus.PENDING, ':txn_status': transferTransaction.status }, ConditionExpression: "#stat = :pending_status" } }); resolve({ status: 200 }); })); })).catch((error) => { console.log('ERRR:' + error); console.log('ERRR:' + JSON.stringify(error, null, 2)); resolve(CommonErrorCodes_1.CommonErrorCodes.TOKEN_NOT_FOUND); }); }); } getTransactionType() { return npm_1.TransactionTypes.OffRampResult; } } exports.OffRampResultTransactionProcessor = OffRampResultTransactionProcessor; //# sourceMappingURL=OffRampResultTransactionProcessor.js.map