n8n-nodes-dex
Version:
n8n node module for dYdX v4 trading and account access
140 lines • 4.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccountService = void 0;
const v4_client_js_1 = require("@dydxprotocol/v4-client-js");
/**
* Service for interacting with dYdX account functionality
*/
class AccountService {
/**
* Initialize the account service with network configuration and wallet address
*/
async init(config, address) {
this.client = new v4_client_js_1.IndexerClient(config);
this.address = address;
return this;
}
/**
* Get all subaccounts for the current address
*/
async getSubaccounts() {
try {
const response = await this.client.account.getSubaccounts(this.address);
console.log('Subaccounts fetched successfully');
return response.subaccounts;
}
catch (error) {
console.error('Error fetching subaccounts:', error.message);
throw error;
}
}
/**
* Get a specific subaccount by number
*/
async getSubaccount(num) {
try {
const response = await this.client.account.getSubaccount(this.address, num);
console.log(`Subaccount ${num} fetched successfully`);
return response.subaccount;
}
catch (error) {
console.error(`Error fetching subaccount ${num}:`, error.message);
throw error;
}
}
/**
* Get asset positions for a specific subaccount
*/
async getSubaccountAssetPositions(num) {
try {
const response = await this.client.account.getSubaccountAssetPositions(this.address, num);
console.log(`Asset positions for subaccount ${num} fetched successfully`);
return response.positions;
}
catch (error) {
console.error(`Error fetching asset positions for subaccount ${num}:`, error.message);
throw error;
}
}
/**
* Get perpetual positions for a specific subaccount
*/
async getSubaccountPerpetualPositions(num) {
try {
const response = await this.client.account.getSubaccountPerpetualPositions(this.address, num);
console.log(`Perpetual positions for subaccount ${num} fetched successfully`);
return response.positions;
}
catch (error) {
console.error(`Error fetching perpetual positions for subaccount ${num}:`, error.message);
throw error;
}
}
/**
* Get transfers for a specific subaccount
*/
async getSubaccountTransfers(subaccountNumber) {
try {
const response = await this.client.account.getSubaccountTransfers(this.address, subaccountNumber);
return response.transfers || [];
}
catch (error) {
console.error(`Error fetching transfers for subaccount ${subaccountNumber}:`, error.message);
throw error;
}
}
/**
* Get orders for a specific subaccount
*/
async getSubaccountOrders(subaccountNumber) {
try {
const response = await this.client.account.getSubaccountOrders(this.address, subaccountNumber);
return response;
}
catch (error) {
console.error(`Error fetching orders for subaccount ${subaccountNumber}:`, error.message);
throw error;
}
}
/**
* Get fills for a specific subaccount
*/
async getSubaccountFills(subaccountNumber) {
try {
const response = await this.client.account.getSubaccountFills(this.address, subaccountNumber);
return response.fills || [];
}
catch (error) {
console.error(`Error fetching fills for subaccount ${subaccountNumber}:`, error.message);
throw error;
}
}
/**
* Get historical PnL for a specific subaccount
*/
async getSubaccountHistoricalPNLs(subaccountNumber) {
try {
const response = await this.client.account.getSubaccountHistoricalPNLs(this.address, subaccountNumber);
return response.historicalPnl || [];
}
catch (error) {
console.error(`Error fetching historical PNL for subaccount ${subaccountNumber}:`, error.message);
throw error;
}
}
/**
* Check if account exists
*/
async accountExists() {
try {
const subaccounts = await this.getSubaccounts();
return subaccounts.length > 0;
}
catch (error) {
console.error('Error checking if account exists:', error.message);
return false;
}
}
}
exports.AccountService = AccountService;
//# sourceMappingURL=account.service.js.map