@copytrade/broker-shoonya
Version:
Shoonya (Finvasia) broker plugin for @copytrade/unified-broker
124 lines • 3.35 kB
JavaScript
;
/**
* Shoonya Broker Helper Functions
* Utility functions specific to Shoonya broker integration
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformOrderRequest = transformOrderRequest;
exports.formatSymbol = formatSymbol;
exports.mapProductType = mapProductType;
exports.mapOrderType = mapOrderType;
exports.mapValidity = mapValidity;
exports.parseOrderStatus = parseOrderStatus;
exports.validateCredentials = validateCredentials;
exports.generateTOTP = generateTOTP;
exports.formatErrorMessage = formatErrorMessage;
/**
* Transform unified order request to Shoonya format
*/
function transformOrderRequest(orderRequest, accountId) {
return {
uid: accountId,
actid: accountId,
exch: orderRequest.exchange,
tsym: formatSymbol(orderRequest.symbol, orderRequest.exchange),
qty: orderRequest.quantity.toString(),
prc: orderRequest.price?.toString() || '0',
prd: mapProductType(orderRequest.productType),
trantype: orderRequest.action,
prctyp: mapOrderType(orderRequest.orderType),
ret: mapValidity(orderRequest.validity),
ordersource: 'API',
remarks: orderRequest.remarks || ''
};
}
/**
* Format symbol for Shoonya API
*/
function formatSymbol(symbol, exchange) {
if (exchange === 'NSE') {
return `${symbol}-EQ`;
}
else if (exchange === 'BSE') {
return symbol;
}
return symbol;
}
/**
* Map unified product type to Shoonya product type
*/
function mapProductType(productType) {
const mapping = {
'CNC': 'C',
'MIS': 'I',
'NRML': 'M',
'BO': 'B'
};
return mapping[productType] || 'C';
}
/**
* Map unified order type to Shoonya order type
*/
function mapOrderType(orderType) {
const mapping = {
'MARKET': 'MKT',
'LIMIT': 'LMT',
'SL-LIMIT': 'SL-LMT',
'SL-MARKET': 'SL-MKT'
};
return mapping[orderType] || 'LMT';
}
/**
* Map unified validity to Shoonya validity
*/
function mapValidity(validity) {
const mapping = {
'DAY': 'DAY',
'IOC': 'IOC',
'GTD': 'GTD'
};
return mapping[validity] || 'DAY';
}
/**
* Parse Shoonya order status to unified format
*/
function parseOrderStatus(shoonyaStatus) {
const mapping = {
'PENDING': 'PENDING',
'OPEN': 'PENDING',
'COMPLETE': 'EXECUTED',
'REJECTED': 'REJECTED',
'CANCELLED': 'CANCELLED'
};
return mapping[shoonyaStatus] || 'PENDING';
}
/**
* Validate Shoonya credentials
*/
function validateCredentials(credentials) {
const required = ['userId', 'password', 'vendorCode', 'apiKey', 'imei'];
return required.every(field => credentials[field] && credentials[field].trim() !== '');
}
/**
* Generate TOTP if totpKey is provided
*/
function generateTOTP(totpKey) {
if (!totpKey)
return undefined;
// This would integrate with a TOTP library
// For now, return undefined to use manual OTP
return undefined;
}
/**
* Format error message from Shoonya response
*/
function formatErrorMessage(response) {
if (response.emsg) {
return response.emsg;
}
if (response.stat === 'Not_Ok') {
return 'Operation failed';
}
return 'Unknown error occurred';
}
//# sourceMappingURL=helpers.js.map