@copytrade/broker-fyers
Version:
Fyers broker plugin for @copytrade/unified-broker
127 lines • 3.48 kB
JavaScript
;
/**
* Fyers Broker Helper Functions
* Utility functions specific to Fyers broker integration
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformOrderRequest = transformOrderRequest;
exports.formatSymbol = formatSymbol;
exports.mapOrderType = mapOrderType;
exports.mapOrderSide = mapOrderSide;
exports.mapProductType = mapProductType;
exports.mapValidity = mapValidity;
exports.parseOrderStatus = parseOrderStatus;
exports.validateCredentials = validateCredentials;
exports.generateAuthUrl = generateAuthUrl;
exports.formatErrorMessage = formatErrorMessage;
/**
* Transform unified order request to Fyers format
*/
function transformOrderRequest(orderRequest) {
return {
symbol: formatSymbol(orderRequest.symbol, orderRequest.exchange),
qty: orderRequest.quantity,
type: mapOrderType(orderRequest.orderType),
side: mapOrderSide(orderRequest.action),
productType: mapProductType(orderRequest.productType),
limitPrice: orderRequest.price || 0,
stopPrice: orderRequest.stopPrice || 0,
validity: mapValidity(orderRequest.validity),
disclosedQty: 0,
offlineOrder: false
};
}
/**
* Format symbol for Fyers API
*/
function formatSymbol(symbol, exchange) {
return `${exchange}:${symbol}`;
}
/**
* Map unified order type to Fyers order type
*/
function mapOrderType(orderType) {
const mapping = {
'MARKET': 2,
'LIMIT': 1,
'SL-LIMIT': 3,
'SL-MARKET': 4
};
return mapping[orderType] || 1;
}
/**
* Map unified order action to Fyers side
*/
function mapOrderSide(action) {
return action === 'BUY' ? 1 : -1;
}
/**
* Map unified product type to Fyers product type
*/
function mapProductType(productType) {
const mapping = {
'CNC': 'CNC',
'MIS': 'INTRADAY',
'NRML': 'MARGIN',
'BO': 'BO'
};
return mapping[productType] || 'CNC';
}
/**
* Map unified validity to Fyers validity
*/
function mapValidity(validity) {
const mapping = {
'DAY': 'DAY',
'IOC': 'IOC',
'GTD': 'GTD'
};
return mapping[validity] || 'DAY';
}
/**
* Parse Fyers order status to unified format
*/
function parseOrderStatus(fyersStatus) {
const mapping = {
1: 'PENDING',
2: 'EXECUTED',
3: 'CANCELLED',
4: 'REJECTED',
5: 'PENDING'
};
return mapping[fyersStatus] || 'PENDING';
}
/**
* Validate Fyers credentials
*/
function validateCredentials(credentials) {
const required = ['appId', 'secretKey', 'redirectUri'];
return required.every(field => credentials[field] && credentials[field].trim() !== '');
}
/**
* Generate Fyers auth URL - Fixed to use actual login page
*/
function generateAuthUrl(appId, redirectUri) {
// Use the correct Fyers login URL that doesn't immediately redirect
const baseUrl = 'https://api-t1.fyers.in/api/v3/generate-authcode';
const params = new URLSearchParams({
client_id: appId,
redirect_uri: redirectUri,
response_type: 'code',
state: 'sample_state'
});
return `${baseUrl}?${params.toString()}`;
}
/**
* Format error message from Fyers response
*/
function formatErrorMessage(response) {
if (response.message) {
return response.message;
}
if (response.s === 'error') {
return 'Operation failed';
}
return 'Unknown error occurred';
}
//# sourceMappingURL=helpers.js.map