UNPKG

n8n-nodes-dex

Version:

n8n node module for dYdX v4 trading and account access

190 lines 7.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Dydx = void 0; const n8n_workflow_1 = require("n8n-workflow"); const client_1 = require("./client"); const trading_service_1 = require("./trading.service"); const account_service_1 = require("./account.service"); class Dydx { constructor() { this.description = { displayName: 'dYdX', name: 'dydx', group: ['transform'], version: 1, description: 'Interact with dYdX v4 API', defaults: { name: 'dYdX', }, inputs: ['main'], outputs: ['main'], credentials: [{ name: 'dydxApi', required: true }], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'Get Positions', value: 'positions' }, { name: 'List Subaccounts', value: 'listSubaccounts' }, { name: 'Smart Position', value: 'smartPosition' }, { name: 'Transfer Between Subaccounts', value: 'transfer' }, ], default: 'listSubaccounts', }, { displayName: 'From Subaccount', name: 'fromId', type: 'number', displayOptions: { show: { operation: ['transfer'], }, }, default: 0, description: 'Source subaccount ID', }, { displayName: 'To Subaccount', name: 'toId', type: 'number', displayOptions: { show: { operation: ['transfer'], }, }, default: 1, description: 'Destination subaccount ID', }, { displayName: 'Amount', name: 'amount', type: 'number', displayOptions: { show: { operation: ['transfer'], }, }, default: 100000000, description: 'Amount to transfer', }, // Parameters for Smart Position operation { displayName: 'Market', name: 'market', type: 'string', displayOptions: { show: { operation: ['smartPosition'], }, }, default: '', description: 'Market symbol (e.g., BTC-USD)', required: true, }, { displayName: 'Target Position Size', name: 'size', type: 'number', displayOptions: { show: { operation: ['smartPosition'], }, }, default: 0, description: 'Target size of the position (positive for long, negative for short)', required: true, }, { displayName: 'Subaccount ID', name: 'accountId', type: 'number', displayOptions: { show: { operation: ['smartPosition'], }, }, default: 0, description: 'Subaccount ID to use for the position', }, ], }; } async execute() { const items = this.getInputData(); const returnData = []; const credentials = await this.getCredentials('dydxApi'); const network = (0, client_1.getNetwork)(credentials.network); const trader = new trading_service_1.TradingService(); await trader.init(network, credentials.address); const acct = await new account_service_1.AccountService().init(network.indexerConfig, credentials.address); for (let i = 0; i < items.length; i++) { const operation = this.getNodeParameter('operation', i); try { if (operation === 'listSubaccounts') { const subaccounts = await acct.getSubaccounts(); returnData.push({ json: { subaccounts } }); } else if (operation === 'positions') { const positions = await trader.getPositions(); returnData.push({ json: positions }); } else if (operation === 'transfer') { const params = { fromId: this.getNodeParameter('fromId', i), toId: this.getNodeParameter('toId', i), amount: this.getNodeParameter('amount', i), }; const tx = await trader.transferBetweenSubaccounts(credentials.mnemonic, params); returnData.push({ json: { success: true, tx } }); } else if (operation === 'smartPosition') { const params = { market: this.getNodeParameter('market', i), size: this.getNodeParameter('size', i), }; const accountId = this.getNodeParameter('accountId', i); try { const result = await trader.smartPosition(params, credentials.mnemonic, accountId); returnData.push({ json: { success: true, message: 'Smart position initiated', market: params.market, targetSize: params.size, currentSize: result, timestamp: new Date().toISOString(), } }); } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { success: false, error: error.message, market: params.market, targetSize: params.size } }); } else { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Failed to execute smart position: ${error.message}`, { itemIndex: i }); } } } } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error.message } }); continue; } throw error; } } return [returnData]; } } exports.Dydx = Dydx; //# sourceMappingURL=Dydx.node.js.map