UNPKG

@payburner/keyburner-sidewinder-core

Version:
185 lines 11.1 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.OnRampTransactionProcessor = void 0; const npm_1 = require("@payburner/keyburner-sidewinder-model/dist/npm"); const TransactionProcessorBase_1 = require("./TransactionProcessorBase"); const CommonErrorCodes_1 = require("../model/CommonErrorCodes"); class OnRampTransactionProcessor extends TransactionProcessorBase_1.TransactionProcessorBase { constructor(globalAccountService, tokenService) { super(globalAccountService, tokenService); } doProcess(decodedTransaction, items) { const self = this; return new Promise((resolve, reject) => { console.log('Processing transfer: ' + decodedTransaction.id); const transferTransaction = decodedTransaction.payload; self.getTokenService().getToken(transferTransaction.environment, transferTransaction.token_symbol).then((token) => __awaiter(this, void 0, void 0, function* () { let receiverAccount = null; try { receiverAccount = yield this.getTokenService() .getTokenAccount(transferTransaction.environment, transferTransaction.token_symbol, transferTransaction.deposit_address); console.log('Got Deposit Account'); } catch (error) { console.log('Did not get Receiver Account'); } let isReceiverPermissioned = false; if (token.is_permissioned && receiverAccount === null) { if (token.token_issuer_address === transferTransaction.deposit_address) { isReceiverPermissioned = true; } else { isReceiverPermissioned = yield this.getTokenService() .isAddressPermissionedOnToken(transferTransaction.environment, transferTransaction.token_symbol, transferTransaction.deposit_address); } } if (token.frozen) { resolve(CommonErrorCodes_1.CommonErrorCodes.TOKEN_IS_FROZEN); return; } else if (receiverAccount !== null && receiverAccount.frozen) { resolve(CommonErrorCodes_1.CommonErrorCodes.TOKEN_ACCOUNT_RECEIVER_ACCOUNT_IS_FROZEN); return; } else if (receiverAccount === null && token.is_permissioned && !isReceiverPermissioned) { resolve(CommonErrorCodes_1.CommonErrorCodes.TOKEN_ACCOUNT_RECEIVER_ACCOUNT_NO_ACCESS_TO_TOKEN); return; } if (receiverAccount !== null && receiverAccount.account_owner_address === token.token_issuer_address) { const rawAmount = token.underlying_currency_multiplier * transferTransaction.underlying_deposit_amount; let depositAmount = rawAmount; items.push({ Update: { TableName: 'sidewinder_token_account', Key: { token_account_id: npm_1.AccountUtils.calculateTokenAccountId(transferTransaction.environment, transferTransaction.token_symbol, transferTransaction.deposit_address), }, UpdateExpression: 'SET available_balance = available_balance + :deposit_amount, total_balance = total_balance ' + ' + :deposit_amount', 'ExpressionAttributeValues': { ':deposit_amount': depositAmount } } }); } else { const currentTotalBalance = receiverAccount !== null ? receiverAccount.total_balance : 0; console.log('Current Balance:' + currentTotalBalance); const rawAmount = token.underlying_currency_multiplier * transferTransaction.underlying_deposit_amount; console.log('Raw Amount:' + rawAmount + ', Deposit Amount:' + transferTransaction.underlying_deposit_amount); const onRampFee = rawAmount * token.onramp_fee_multiplier; console.log('On Ramp Fee:' + onRampFee); let depositAmount = rawAmount - onRampFee; console.log('Deposit Post Fee:' + depositAmount); let overAmount = 0; if (typeof token.maximum_balance !== 'undefined') { overAmount = (depositAmount + currentTotalBalance) - token.maximum_balance; if (overAmount > 0) { depositAmount = depositAmount - overAmount; } else { overAmount = 0; } console.log('OverAmount:' + overAmount); console.log('Deposit Post Over:' + depositAmount); } console.log('Over check:' + (currentTotalBalance + depositAmount) + ' ' + token.maximum_balance); if (receiverAccount === null) { receiverAccount = { token_account_id: npm_1.AccountUtils.calculateTokenAccountId(transferTransaction.environment, transferTransaction.token_symbol, transferTransaction.deposit_address), token_symbol: token.token_symbol, account_owner_address: transferTransaction.deposit_address, environment: token.environment, sequence: 0, available_balance: depositAmount, total_balance: depositAmount, locked_balance: 0, over_balance: overAmount, frozen: false }; items.push({ Put: { TableName: 'sidewinder_token_account', Item: receiverAccount, ConditionExpression: "attribute_not_exists(token_account_id)" } }); } else { console.log('Balance is under max'); items.push({ Update: { TableName: 'sidewinder_token_account', Key: { token_account_id: npm_1.AccountUtils.calculateTokenAccountId(transferTransaction.environment, transferTransaction.token_symbol, transferTransaction.deposit_address), }, UpdateExpression: 'SET available_balance = available_balance + :deposit_amount, total_balance = total_balance ' + ' + :deposit_amount, over_balance = over_balance + :over_amount', 'ExpressionAttributeValues': { ':deposit_amount': depositAmount, ':over_amount': overAmount } } }); } 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 + :transaction_fee, total_balance = total_balance + :transaction_fee', 'ExpressionAttributeValues': { ':transaction_fee': onRampFee } } }); } items.push({ Put: { TableName: 'sidewinder_underlying_transactions', Key: { underlying_txn_id: transferTransaction.underlying_txn_id, }, Item: { underlying_txn_id: transferTransaction.underlying_txn_id, status: npm_1.UnderlyingTransactionStatus.COMPLETED, transaction_type: 'Payment', transaction_action: npm_1.UnderlyingTransactionDirection.DEPOSIT, created_timestamp: new Date().toISOString(), underlying_amount: transferTransaction.underlying_deposit_amount, transfer_amount: token.underlying_currency_multiplier * transferTransaction.underlying_deposit_amount, transaction_id: decodedTransaction.id, token_symbol: token.token_symbol, environment: token.environment, token_issuer_address: token.token_issuer_address, underlying_destination_address: token.underlying_address, underlying_source_address: transferTransaction.underlying_source_address, internal_address: transferTransaction.deposit_address }, ConditionExpression: "attribute_not_exists(underlying_txn_id)" } }); 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.OnRampFromUnderlying; } } exports.OnRampTransactionProcessor = OnRampTransactionProcessor; //# sourceMappingURL=OnRampTransactionProcessor.js.map