@payburner/keyburner-sidewinder-core
Version:
Core library for Keyburner Sidewinder
109 lines • 6.53 kB
JavaScript
;
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.OffRampRequestTransactionProcessor = void 0;
const npm_1 = require("@payburner/keyburner-sidewinder-model/dist/npm");
const TransactionProcessorBase_1 = require("./TransactionProcessorBase");
const CommonErrorCodes_1 = require("../model/CommonErrorCodes");
class OffRampRequestTransactionProcessor extends TransactionProcessorBase_1.TransactionProcessorBase {
constructor(globalAccountService, tokenService) {
super(globalAccountService, tokenService);
}
doProcess(decodedTransaction, items) {
const self = this;
return new Promise((resolve, reject) => {
console.log('Processing OffRamp Request: ' + decodedTransaction.id);
const transferTransaction = decodedTransaction.payload;
console.log('Getting Token:' + JSON.stringify(transferTransaction, null, 2));
self.getTokenService().getToken(transferTransaction.environment, transferTransaction.token_symbol).then((token) => __awaiter(this, void 0, void 0, function* () {
let withdrawAccount = null;
try {
withdrawAccount = yield this.getTokenService()
.getTokenAccount(transferTransaction.environment, transferTransaction.token_symbol, transferTransaction.withdraw_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) {
if (token.token_issuer_address === transferTransaction.withdraw_address) {
isWithdrawAccountPermissioned = true;
}
else {
isWithdrawAccountPermissioned = yield this.getTokenService()
.isAddressPermissionedOnToken(transferTransaction.environment, transferTransaction.token_symbol, transferTransaction.withdraw_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;
}
if ((withdrawAccount.available_balance) < (transferTransaction.transfer_amount)) {
resolve(CommonErrorCodes_1.CommonErrorCodes.INSUFFICIENT_BALANCE);
return;
}
items.push({
Put: {
TableName: 'sidewinder_underlying_transactions',
Item: {
underlying_txn_id: transferTransaction.underlying_txn_id,
status: npm_1.UnderlyingTransactionStatus.PENDING,
transaction_type: 'Payment',
transaction_direction: npm_1.UnderlyingTransactionDirection.WITHDRAW,
created_timestamp: new Date().toISOString(),
underlying_amount: transferTransaction.transfer_amount / token.underlying_currency_multiplier,
transfer_amount: transferTransaction.transfer_amount,
transaction_id: decodedTransaction.id,
token_symbol: token.token_symbol,
environment: token.environment,
token_issuer_address: token.token_issuer_address,
underlying_destination_address: transferTransaction.underlying_destination_address,
underlying_source_address: token.underlying_address,
internal_address: transferTransaction.withdraw_address
},
ConditionExpression: "attribute_not_exists(underlying_txn_id)"
}
});
items.push({
Update: {
TableName: 'sidewinder_token_account',
Key: {
token_account_id: npm_1.AccountUtils.calculateTokenAccountId(transferTransaction.environment, transferTransaction.token_symbol, transferTransaction.withdraw_address),
},
UpdateExpression: 'SET available_balance = available_balance - :transfer_amount, locked_balance = locked_balance + :transfer_amount',
'ExpressionAttributeValues': {
':transfer_amount': transferTransaction.transfer_amount
}
}
});
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.OffRampRequest;
}
}
exports.OffRampRequestTransactionProcessor = OffRampRequestTransactionProcessor;
//# sourceMappingURL=OffRampRequestTransactionProcessor.js.map