UNPKG

n8n-nodes-dex

Version:

n8n node module for dYdX v4 trading and account access

117 lines • 4.86 kB
#!/usr/bin/env node "use strict"; /** * Command-line utility to test the dYdX module functionality * * Usage: * npx ts-node cli.ts --address=<your_address> --network=<mainnet|testnet> [--subaccount=<number>] * * Example: * npx ts-node cli.ts --address={ADDRESS} --network=mainnet --subaccount=0 */ Object.defineProperty(exports, "__esModule", { value: true }); // Import required modules const client_1 = require("./nodes/Dydx/client"); const account_service_1 = require("./nodes/Dydx/account.service"); const args = process.argv.slice(2).reduce((acc, arg) => { if (arg.startsWith('--')) { const [key, value] = arg.slice(2).split('='); acc[key] = value; } return acc; }, {}); // Default values const address = args.address; const networkName = args.network || 'testnet'; const subaccountId = args.subaccount ? parseInt(args.subaccount, 10) : 0; // Function to validate dYdX address function validateAddress(addr) { if (!addr) return false; return addr.startsWith('dydx1'); } async function main() { try { if (!address) { console.error('āŒ Error: Address is required. Use --address=<your_address>'); process.exit(1); } if (!validateAddress(address)) { console.error(`āŒ Error: Invalid dYdX address format: ${address}`); console.error('Addresses should start with "dydx1".'); process.exit(1); } console.log(`šŸ” Fetching data for address: ${address}`); console.log(`🌐 Network: ${networkName === 'mainnet' ? 'Mainnet (Production)' : 'Testnet (Development)'}`); console.log(`šŸ‘¤ Subaccount: ${subaccountId}`); // Initialize network const network = (0, client_1.getNetwork)(networkName); // Initialize account service const acct = await new account_service_1.AccountService().init(network.indexerConfig, address); console.log('\nšŸ” Checking if account exists...'); const accountExists = await acct.accountExists(); console.log(`Account exists: ${accountExists ? 'āœ… Yes' : 'āŒ No'}`); if (!accountExists) { console.error('āŒ Account not found. Please check your address and network settings.'); process.exit(1); } // Fetch specific subaccount console.log(`\nšŸ”¹ Fetching subaccount #${subaccountId}...`); try { const subaccount = await acct.getSubaccount(subaccountId); console.log('\nSubaccount details:'); console.log(JSON.stringify(subaccount, null, 2)); // Get perpetual positions console.log('\nšŸ“Š Fetching perpetual positions...'); const perpPositions = await acct.getSubaccountPerpetualPositions(subaccountId); if (perpPositions.length > 0) { console.log('\nPerpetual positions:'); console.log(JSON.stringify(perpPositions, null, 2)); } else { console.log('No perpetual positions found.'); } // Get asset positions console.log('\nšŸ’° Fetching asset positions...'); const assetPositions = await acct.getSubaccountAssetPositions(subaccountId); if (assetPositions.length > 0) { console.log('\nAsset positions:'); console.log(JSON.stringify(assetPositions, null, 2)); } else { console.log('No asset positions found.'); } } catch (error) { console.error(`āŒ Error fetching subaccount ${subaccountId}: ${error.message}`); // Try to get all subaccounts instead console.log('\nšŸ“ Trying to fetch all subaccounts...'); const allSubaccounts = await acct.getSubaccounts(); if (allSubaccounts && allSubaccounts.length > 0) { console.log(`Found ${allSubaccounts.length} subaccounts.`); console.log('\nAvailable subaccounts:'); allSubaccounts.forEach((sub) => { console.log(`- Subaccount #${sub.subaccountNumber}`); }); } else { console.log('No subaccounts found.'); } } console.log('\nāœ… Query completed successfully!'); } catch (error) { console.error('\nāŒ Error occurred:', error.message); console.error('\nšŸ“‹ Debug information:'); console.error(`• Network: ${networkName}`); console.error(`• Address: ${address}`); console.error(`• Subaccount: ${subaccountId}`); if (error.stack) { console.error('\nšŸ” Stack trace:'); console.error(error.stack); } process.exit(1); } } main(); //# sourceMappingURL=cli.js.map